Bug 487554 - Add "Terminate and Relaunch" to menu and toolbar

Change-Id: I37cc8c369e34ad860070d50a88964185b4d1721b
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPreferenceInitializer.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPreferenceInitializer.java
index 771fd79..1ddf8b4 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPreferenceInitializer.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPreferenceInitializer.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *  Copyright (c) 2004, 2014 IBM Corporation and others.
+ *  Copyright (c) 2004, 2016 IBM Corporation and others.
  *  All rights reserved. This program and the accompanying materials
  *  are made available under the terms of the Eclipse Public License v1.0
  *  which accompanies this distribution, and is available at
@@ -63,6 +63,8 @@
 		prefs.setDefault(IInternalDebugUIConstants.PREF_LAUNCH_PARENT_PROJECT, false);
 		prefs.setDefault(IInternalDebugUIConstants.PREF_LAUNCH_LAST_IF_NOT_LAUNCHABLE, true);
 		
+		prefs.setDefault(IInternalDebugUIConstants.PREF_TERMINATE_AND_RELAUNCH_LAUNCH_ACTION, false);
+
 		//View Management preference page
 		prefs.setDefault(IDebugUIConstants.PREF_MANAGE_VIEW_PERSPECTIVES, IDebugUIConstants.PREF_MANAGE_VIEW_PERSPECTIVES_DEFAULT);
 		prefs.setDefault(IInternalDebugUIConstants.PREF_TRACK_VIEWS, true);
@@ -201,8 +203,8 @@
 		
 			if (oldValue != null && !oldValue.equals(newValue))
 				store.firePropertyChangeEvent(key, oldValue, newValue);
+			}
 		}
-	}
 
 	public static void setThemeBasedPreferences(final IPreferenceStore store, final boolean fireEvent) {
 		Display display= PlatformUI.getWorkbench().getDisplay();
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/IInternalDebugUIConstants.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/IInternalDebugUIConstants.java
index df92d26..34ca105 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/IInternalDebugUIConstants.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/IInternalDebugUIConstants.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -435,5 +435,15 @@
      * 
      * @since 3.4
      */
-    public static final String THEME_CONSOLE_COLOR_STD_ERR= "org.eclipse.debug.ui.console.stream.err"; //$NON-NLS-1$        
+	public static final String THEME_CONSOLE_COLOR_STD_ERR = "org.eclipse.debug.ui.console.stream.err"; //$NON-NLS-1$
+
+
+	/**
+	 * Boolean preference indicating if launch action should launch or terminate
+	 * and relaunch by default
+	 * 
+	 * @since 3.11
+	 */
+	public static final String PREF_TERMINATE_AND_RELAUNCH_LAUNCH_ACTION = IDebugUIConstants.PLUGIN_ID + ".RelaunchAndTerminateLaunchAction"; //$NON-NLS-1$
+
 }
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java
index 9acd358..d28da94 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *  Copyright (c) 2000, 2012 IBM Corporation and others.
+ *  Copyright (c) 2000, 2016 IBM Corporation and others.
  *  All rights reserved. This program and the accompanying materials
  *  are made available under the terms of the Eclipse Public License v1.0
  *  which accompanies this distribution, and is available at
@@ -85,6 +85,7 @@
 	public static String LaunchingPreferencePage_4;
 
 	public static String LaunchingPreferencePage_41;
+	public static String LaunchingPreferencePage_42;
 	public static String LaunchingPreferencePage_5;
 	public static String LaunchingPreferencePage_6;
 	public static String LaunchingPreferencePage_7;
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties
index 8c219dc..2e822e2 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties
@@ -1,5 +1,5 @@
 ###############################################################################
-# Copyright (c) 2000, 2014 IBM Corporation and others.
+# Copyright (c) 2000, 2016 IBM Corporation and others.
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Eclipse Public License v1.0
 # which accompanies this distribution, and is available at
@@ -84,6 +84,7 @@
 LaunchingPreferencePage_40=Launch Operation
 LaunchingPreferencePage_41=La&unch the previously launched application
 LaunchingPreferencePage_confirm_0=Prompt for confirmation when removin&g a configuration from the launch history
+LaunchingPreferencePage_42=Termin&ate and Relaunch while launching (Press 'Shift' to toggle during launch)
 
 ProcessPropertyPage_Command_Line__1=Co&mmand Line:
 ProcessPropertyPage_0=Run-&at time:
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/LaunchingPreferencePage.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/LaunchingPreferencePage.java
index 731dbd0..25e1631 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/LaunchingPreferencePage.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/LaunchingPreferencePage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2004, 2015 IBM Corporation and others.
+ * Copyright (c) 2004, 2016 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -48,6 +48,7 @@
 	private Button fUseOldLaunching;
 	private Button fLaunchLastIfNotLaunchable;
 	private Button fCheckParent;
+	private Button fTerminatAndRelaunch;
 	
 	/**
 	 * The default constructor
@@ -172,7 +173,9 @@
 		fCheckParent = SWTFactory.createRadioButton(space, DebugPreferencesMessages.LaunchingPreferencePage_39);
 		fLaunchLastIfNotLaunchable = SWTFactory.createRadioButton(space, DebugPreferencesMessages.LaunchingPreferencePage_41);
 		
-		//initialize the buttons
+		fTerminatAndRelaunch = SWTFactory.createCheckButton(group, DebugPreferencesMessages.LaunchingPreferencePage_42, null, false, 1);
+
+		// initialize the buttons
 		boolean value = getPreferenceStore().getBoolean(IInternalDebugUIConstants.PREF_USE_CONTEXTUAL_LAUNCH);
 		fUseOldLaunching.setSelection(!value);
 		fUseContextLaunching.setSelection(value);
@@ -181,6 +184,9 @@
 		fCheckParent.setEnabled(value);
 		fLaunchLastIfNotLaunchable.setSelection(!enable);
 		fLaunchLastIfNotLaunchable.setEnabled(value);
+
+		value = getPreferenceStore().getBoolean(IInternalDebugUIConstants.PREF_TERMINATE_AND_RELAUNCH_LAUNCH_ACTION);
+		fTerminatAndRelaunch.setSelection(value);
 	}
 	
 	/* (non-Javadoc)
@@ -202,6 +208,8 @@
 		fCheckParent.setEnabled(value);
 		fLaunchLastIfNotLaunchable.setSelection(!parent);
 		fLaunchLastIfNotLaunchable.setEnabled(value);
+		value = getPreferenceStore().getBoolean(IInternalDebugUIConstants.PREF_TERMINATE_AND_RELAUNCH_LAUNCH_ACTION);
+		fTerminatAndRelaunch.setSelection(value);
 		super.performDefaults();
 	}
 	
@@ -213,6 +221,7 @@
 		getPreferenceStore().setValue(IInternalDebugUIConstants.PREF_USE_CONTEXTUAL_LAUNCH, fUseContextLaunching.getSelection());
 		getPreferenceStore().setValue(IInternalDebugUIConstants.PREF_LAUNCH_PARENT_PROJECT, fCheckParent.getSelection());
 		getPreferenceStore().setValue(IInternalDebugUIConstants.PREF_LAUNCH_LAST_IF_NOT_LAUNCHABLE, fLaunchLastIfNotLaunchable.getSelection());
+		getPreferenceStore().setValue(IInternalDebugUIConstants.PREF_TERMINATE_AND_RELAUNCH_LAUNCH_ACTION, fTerminatAndRelaunch.getSelection());
 		return super.performOk();
 	}
 
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/LaunchAction.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/LaunchAction.java
index ccbfff5..4321268 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/LaunchAction.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/LaunchAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -15,8 +15,12 @@
 import java.util.Set;
 
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.debug.core.DebugException;
+import org.eclipse.debug.core.DebugPlugin;
+import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.debug.core.ILaunchConfigurationType;
+import org.eclipse.debug.core.ILaunchManager;
 import org.eclipse.debug.internal.ui.DebugUIPlugin;
 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
@@ -79,7 +83,22 @@
 	public void run() {
 		DebugUITools.launch(fConfiguration, fMode);
 	}
-	
+
+	private void terminateIfPreferred(boolean isShift) {
+		if (DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IInternalDebugUIConstants.PREF_TERMINATE_AND_RELAUNCH_LAUNCH_ACTION) != isShift) {
+			ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
+			ILaunch[] launches = launchManager.getLaunches();
+			for (ILaunch iLaunch : launches) {
+				if (fConfiguration.contentsEqual(iLaunch.getLaunchConfiguration())) {
+					try {
+						iLaunch.terminate();
+					} catch (DebugException e) {
+						e.printStackTrace();
+					}
+				}
+			}
+		}
+	}
 	/**
 	 * If the user has control-clicked the launch history item, open the launch
 	 * configuration dialog on the launch configuration, rather than running it.
@@ -122,6 +141,7 @@
 			}
 		} 
 		else {
+			terminateIfPreferred(((event.stateMask & SWT.SHIFT) > 0) ? true : false);
 			run();
 		}
 	}