Bug 540010: [Debug] Add LaunchConfigPresets.apply (extract from
LaunchConfigTabWithPresets)

Change-Id: I4e694c8d79a112d818452dc026a53e3b0a3b2419
diff --git a/ecommons/org.eclipse.statet.ecommons.debug.ui/src/org/eclipse/statet/ecommons/debug/ui/config/LaunchConfigPresets.java b/ecommons/org.eclipse.statet.ecommons.debug.ui/src/org/eclipse/statet/ecommons/debug/ui/config/LaunchConfigPresets.java
index 0d28d2c..80a2944 100644
--- a/ecommons/org.eclipse.statet.ecommons.debug.ui/src/org/eclipse/statet/ecommons/debug/ui/config/LaunchConfigPresets.java
+++ b/ecommons/org.eclipse.statet.ecommons.debug.ui/src/org/eclipse/statet/ecommons/debug/ui/config/LaunchConfigPresets.java
@@ -14,8 +14,12 @@
 
 package org.eclipse.statet.ecommons.debug.ui.config;
 
+import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullAssert;
+
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.debug.core.DebugPlugin;
@@ -25,8 +29,11 @@
 
 import org.eclipse.statet.jcommons.collections.ImCollections;
 import org.eclipse.statet.jcommons.collections.ImList;
+import org.eclipse.statet.jcommons.lang.NonNullByDefault;
+import org.eclipse.statet.jcommons.lang.Nullable;
 
 
+@NonNullByDefault
 public class LaunchConfigPresets {
 	
 	
@@ -42,7 +49,48 @@
 	
 	static String getName(final ILaunchConfiguration preset) {
 		try {
-			return preset.getAttribute(NAME_ATTR_NAME, (String) null);
+			return nonNullAssert(preset.getAttribute(NAME_ATTR_NAME, (String) null));
+		}
+		catch (final CoreException e) {
+			throw new RuntimeException(e);
+		}
+	}
+	
+	static void apply(final Map<String, Object> preset,
+			final ILaunchConfigurationWorkingCopy config) {
+		for (final Entry<String, Object> entry : preset.entrySet()) {
+			final String name= entry.getKey();
+			if (LaunchConfigPresets.isInternalArgument(name)) {
+				continue;
+			}
+			final Object value= entry.getValue();
+			if (value instanceof String) {
+				if (value.equals(LaunchConfigPresets.UNDEFINED_VALUE)) {
+					config.removeAttribute(name);
+				}
+				else {
+					config.setAttribute(name, (String) value);
+				}
+			}
+			else if (value instanceof Integer) {
+				config.setAttribute(name, value);
+			}
+			else if (value instanceof Boolean) {
+				config.setAttribute(name, value);
+			}
+			else if (value instanceof List) {
+				config.setAttribute(name, (List) value);
+			}
+			else if (value instanceof Map) {
+				config.setAttribute(name, (Map) value);
+			}
+		}
+	}
+	
+	public static void apply(final ILaunchConfiguration preset,
+			final ILaunchConfigurationWorkingCopy config) {
+		try {
+			apply(preset.getAttributes(), config);
 		}
 		catch (final CoreException e) {
 			throw new RuntimeException(e);
@@ -56,7 +104,8 @@
 	
 	
 	public LaunchConfigPresets(final String typeId) {
-		this.type= DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(typeId);
+		this.type= nonNullAssert(DebugPlugin.getDefault().getLaunchManager()
+				.getLaunchConfigurationType(typeId) );
 	}
 	
 	
@@ -65,18 +114,27 @@
 			throw new NullPointerException("name"); //$NON-NLS-1$
 		}
 		try {
-			final ILaunchConfigurationWorkingCopy config= this.type.newInstance(null, "template").getWorkingCopy(); //$NON-NLS-1$
-			config.setAttribute(NAME_ATTR_NAME, name);
+			final ILaunchConfigurationWorkingCopy preset= this.type.newInstance(null, "template").getWorkingCopy(); //$NON-NLS-1$
+			preset.setAttribute(NAME_ATTR_NAME, name);
 			
-			this.presets.add(config);
+			this.presets.add(preset);
 			
-			return config;
+			return preset;
 		}
 		catch (final CoreException e) {
 			throw new RuntimeException(e);
 		}
 	}
 	
+	public @Nullable ILaunchConfiguration get(final String name) {
+		for (final ILaunchConfiguration config : this.presets) {
+			if (name.equals(getName(config))) {
+				return config;
+			}
+		}
+		return null;
+	}
+	
 	public ImList<ILaunchConfiguration> toList() {
 		return ImCollections.toList(this.presets);
 	}
diff --git a/ecommons/org.eclipse.statet.ecommons.debug.ui/src/org/eclipse/statet/ecommons/debug/ui/config/LaunchConfigTabWithPresets.java b/ecommons/org.eclipse.statet.ecommons.debug.ui/src/org/eclipse/statet/ecommons/debug/ui/config/LaunchConfigTabWithPresets.java
index d812778..27e0183 100644
--- a/ecommons/org.eclipse.statet.ecommons.debug.ui/src/org/eclipse/statet/ecommons/debug/ui/config/LaunchConfigTabWithPresets.java
+++ b/ecommons/org.eclipse.statet.ecommons.debug.ui/src/org/eclipse/statet/ecommons/debug/ui/config/LaunchConfigTabWithPresets.java
@@ -16,7 +16,6 @@
 
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -135,33 +134,7 @@
 			try {
 				configuration.removeAttribute(getValidationErrorAttr());
 				
-				for (final Entry<String, Object> entry : this.presetToLoad.entrySet()) {
-					final String name= entry.getKey();
-					if (LaunchConfigPresets.isInternalArgument(name)) {
-						continue;
-					}
-					final Object value= entry.getValue();
-					if (value instanceof String) {
-						if (value.equals(LaunchConfigPresets.UNDEFINED_VALUE)) {
-							configuration.removeAttribute(name);
-						}
-						else {
-							configuration.setAttribute(name, (String) value);
-						}
-					}
-					else if (value instanceof Integer) {
-						configuration.setAttribute(name, (Integer) value);
-					}
-					else if (value instanceof Boolean) {
-						configuration.setAttribute(name, (Boolean) value);
-					}
-					else if (value instanceof List) {
-						configuration.setAttribute(name, (List) value);
-					}
-					else if (value instanceof Map) {
-						configuration.setAttribute(name, (Map) value);
-					}
-				}
+				LaunchConfigPresets.apply(this.presetToLoad, configuration);
 				this.presetLoaded= configuration;
 				return;
 			}