Merge R4_7_Maintenance for Java9


Change-Id: I93feda21eb29db31eee086d317c17706aca3fdd0
diff --git a/org.eclipse.debug.ui/.settings/.api_filters b/org.eclipse.debug.ui/.settings/.api_filters
index ff20708..b6cbd92 100644
--- a/org.eclipse.debug.ui/.settings/.api_filters
+++ b/org.eclipse.debug.ui/.settings/.api_filters
@@ -1,66 +1,80 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>

-<component id="org.eclipse.debug.ui" version="2">

-    <resource path="ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationViewer.java" type="org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationViewer">

-        <filter comment="Known illegal extension" id="571473929">

-            <message_arguments>

-                <message_argument value="TreeViewer"/>

-                <message_argument value="LaunchConfigurationViewer"/>

-            </message_arguments>

-        </filter>

-    </resource>

-    <resource path="ui/org/eclipse/debug/internal/ui/preferences/LaunchPerspectivePreferencePage.java" type="org.eclipse.debug.internal.ui.preferences.LaunchPerspectivePreferencePage$PerspectivesTreeViewer">

-        <filter comment="Known illegal extension" id="571473929">

-            <message_arguments>

-                <message_argument value="TreeViewer"/>

-                <message_argument value="PerspectivesTreeViewer"/>

-            </message_arguments>

-        </filter>

-    </resource>

-    <resource path="ui/org/eclipse/debug/internal/ui/sourcelookup/SourceContainerViewer.java" type="org.eclipse.debug.internal.ui.sourcelookup.SourceContainerViewer">

-        <filter comment="Known illegal extension" id="571473929">

-            <message_arguments>

-                <message_argument value="TreeViewer"/>

-                <message_argument value="SourceContainerViewer"/>

-            </message_arguments>

-        </filter>

-    </resource>

-    <resource path="ui/org/eclipse/debug/internal/ui/sourcelookup/browsers/ProjectSourceContainerDialog.java" type="org.eclipse.debug.internal.ui.sourcelookup.browsers.ProjectSourceContainerDialog">

-        <filter comment="Known illegal extension" id="571473929">

-            <message_arguments>

-                <message_argument value="ListSelectionDialog"/>

-                <message_argument value="ProjectSourceContainerDialog"/>

-            </message_arguments>

-        </filter>

-    </resource>

-    <resource path="ui/org/eclipse/debug/internal/ui/viewers/model/InternalTreeModelViewer.java" type="org.eclipse.debug.internal.ui.viewers.model.InternalTreeModelViewer">

-        <filter comment="Known illegal extension for the async viewer support" id="571473929">

-            <message_arguments>

-                <message_argument value="TreeViewer"/>

-                <message_argument value="InternalTreeModelViewer"/>

-            </message_arguments>

-        </filter>

-    </resource>

-    <resource path="ui/org/eclipse/debug/internal/ui/views/breakpoints/BreakpointsViewer.java" type="org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsViewer">

-        <filter comment="Known illegal extension" id="571473929">

-            <message_arguments>

-                <message_argument value="CheckboxTreeViewer"/>

-                <message_argument value="BreakpointsViewer"/>

-            </message_arguments>

-        </filter>

-    </resource>

-    <resource path="ui/org/eclipse/debug/ui/actions/AbstractLaunchHistoryAction.java" type="org.eclipse.debug.ui.actions.AbstractLaunchHistoryAction">

-        <filter comment="Known leaked internal types" id="576725006">

-            <message_arguments>

-                <message_argument value="ILaunchHistoryChangedListener"/>

-                <message_argument value="AbstractLaunchHistoryAction"/>

-            </message_arguments>

-        </filter>

-        <filter comment="Known leaked internal types" id="643842064">

-            <message_arguments>

-                <message_argument value="LaunchHistory"/>

-                <message_argument value="AbstractLaunchHistoryAction"/>

-                <message_argument value="getLaunchHistory()"/>

-            </message_arguments>

-        </filter>

-    </resource>

-</component>

+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<component id="org.eclipse.debug.ui" version="2">
+    <resource path="ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationViewer.java" type="org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationViewer">
+        <filter comment="Known illegal extension" id="571473929">
+            <message_arguments>
+                <message_argument value="TreeViewer"/>
+                <message_argument value="LaunchConfigurationViewer"/>
+            </message_arguments>
+        </filter>
+    </resource>
+    <resource path="ui/org/eclipse/debug/internal/ui/preferences/LaunchPerspectivePreferencePage.java" type="org.eclipse.debug.internal.ui.preferences.LaunchPerspectivePreferencePage$PerspectivesTreeViewer">
+        <filter comment="Known illegal extension" id="571473929">
+            <message_arguments>
+                <message_argument value="TreeViewer"/>
+                <message_argument value="PerspectivesTreeViewer"/>
+            </message_arguments>
+        </filter>
+    </resource>
+    <resource path="ui/org/eclipse/debug/internal/ui/sourcelookup/SourceContainerViewer.java" type="org.eclipse.debug.internal.ui.sourcelookup.SourceContainerViewer">
+        <filter comment="Known illegal extension" id="571473929">
+            <message_arguments>
+                <message_argument value="TreeViewer"/>
+                <message_argument value="SourceContainerViewer"/>
+            </message_arguments>
+        </filter>
+    </resource>
+    <resource path="ui/org/eclipse/debug/internal/ui/sourcelookup/browsers/ProjectSourceContainerDialog.java" type="org.eclipse.debug.internal.ui.sourcelookup.browsers.ProjectSourceContainerDialog">
+        <filter comment="Known illegal extension" id="571473929">
+            <message_arguments>
+                <message_argument value="ListSelectionDialog"/>
+                <message_argument value="ProjectSourceContainerDialog"/>
+            </message_arguments>
+        </filter>
+    </resource>
+    <resource path="ui/org/eclipse/debug/internal/ui/viewers/model/InternalTreeModelViewer.java" type="org.eclipse.debug.internal.ui.viewers.model.InternalTreeModelViewer">
+        <filter comment="Known illegal extension for the async viewer support" id="571473929">
+            <message_arguments>
+                <message_argument value="TreeViewer"/>
+                <message_argument value="InternalTreeModelViewer"/>
+            </message_arguments>
+        </filter>
+    </resource>
+    <resource path="ui/org/eclipse/debug/internal/ui/views/breakpoints/BreakpointsViewer.java" type="org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsViewer">
+        <filter comment="Known illegal extension" id="571473929">
+            <message_arguments>
+                <message_argument value="CheckboxTreeViewer"/>
+                <message_argument value="BreakpointsViewer"/>
+            </message_arguments>
+        </filter>
+    </resource>
+    <resource path="ui/org/eclipse/debug/ui/ILaunchConfigurationTab.java" type="org.eclipse.debug.ui.ILaunchConfigurationTab">
+        <filter id="404000815">
+            <message_arguments>
+                <message_argument value="org.eclipse.debug.ui.ILaunchConfigurationTab"/>
+                <message_argument value="OkToLeaveTab()"/>
+            </message_arguments>
+        </filter>
+        <filter id="404000815">
+            <message_arguments>
+                <message_argument value="org.eclipse.debug.ui.ILaunchConfigurationTab"/>
+                <message_argument value="postApply()"/>
+            </message_arguments>
+        </filter>
+    </resource>
+    <resource path="ui/org/eclipse/debug/ui/actions/AbstractLaunchHistoryAction.java" type="org.eclipse.debug.ui.actions.AbstractLaunchHistoryAction">
+        <filter comment="Known leaked internal types" id="576725006">
+            <message_arguments>
+                <message_argument value="ILaunchHistoryChangedListener"/>
+                <message_argument value="AbstractLaunchHistoryAction"/>
+            </message_arguments>
+        </filter>
+        <filter comment="Known leaked internal types" id="643842064">
+            <message_arguments>
+                <message_argument value="LaunchHistory"/>
+                <message_argument value="AbstractLaunchHistoryAction"/>
+                <message_argument value="getLaunchHistory()"/>
+            </message_arguments>
+        </filter>
+    </resource>
+</component>
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationTabGroupViewer.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationTabGroupViewer.java
index f6d8098..04a53eb 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationTabGroupViewer.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationTabGroupViewer.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2017 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
@@ -68,8 +68,11 @@
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Link;
+import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.IWorkbenchPreferenceConstants;
@@ -100,6 +103,11 @@
 	private ILaunchConfigurationWorkingCopy fWorkingCopy;
 
 	/**
+	 * The old configuration
+	 */
+	private boolean fRefreshTabs = false;
+
+	/**
 	 * This view's control, which contains a composite area of controls
 	 */
 	private Composite fViewerControl;
@@ -316,6 +324,10 @@
 			@Override
 			public void widgetSelected(SelectionEvent evt) {
 				handleApplyPressed();
+				ILaunchConfigurationTab[] tabs = getTabs();
+				for (ILaunchConfigurationTab tab : tabs) {
+					tab.postApply();
+				}
 			}
 		});
         Dialog.applyDialogFont(parent);
@@ -373,15 +385,53 @@
 			fTabFolder.addSelectionListener(new SelectionAdapter() {
 				@Override
 				public void widgetSelected(SelectionEvent event) {
+					int selectedIndex = fTabFolder.indexOf((CTabItem) event.item);
 					if (!fInitializingTabs) {
-						handleTabSelected();
-						refresh();
+						handleTabChange(selectedIndex);
 					}
+
+					Display display = fTabFolder.getDisplay();
+					display.addListener(SWT.PostEvent, new Listener() {
+						@Override
+						public void handleEvent(Event newEvent) {
+							if (newEvent.detail == SWT.Selection) { //
+								display.removeListener(SWT.PostEvent, this);
+								if (fRefreshTabs) {
+									refreshTabs(true);
+									fTabFolder.setSelection(selectedIndex);
+									refreshStatus();
+								}
+							}
+						}
+					});
+
 				}
 			});
 		}
 	}
 
+	private void handleTabChange(int newPageIndex) {
+		if (fCurrentTabIndex == newPageIndex) {
+			return;
+		}
+		if (isDirty() && !fInitializingTabs) {
+			ILaunchConfigurationTab[] tabs = getTabs();
+			if (fCurrentTabIndex != -1) {
+				ILaunchConfigurationTab tab = tabs[fCurrentTabIndex];
+				fTabFolder.setSelection(fCurrentTabIndex);
+				if (tab.OkToLeaveTab()) {
+					fTabFolder.setSelection(newPageIndex);
+					handleTabSelected();
+					refresh();
+				}
+			}
+		} else {
+			handleTabSelected();
+			refresh();
+		}
+
+	}
+
 	/**
 	 * Returns the apply button
 	 * @return the 'Apply' button
@@ -616,6 +666,42 @@
 			}
 		}
 	}
+	/*
+	 * Refresh tabs for same input
+	 */
+
+	public void refreshTabs(boolean refresh) {
+		if (DebugUIPlugin.getStandardDisplay().getThread().equals(Thread.currentThread())) {
+			refreshTabs0(refresh);
+		} else {
+			DebugUIPlugin.getStandardDisplay().syncExec(new Runnable() {
+				@Override
+				public void run() {
+					refreshTabs0(refresh);
+				}
+			});
+		}
+	}
+
+	/**
+	 * The input has changed to the given object, possibly <code>null</code>.
+	 *
+	 * @param input the new input, possibly <code>null</code>
+	 */
+	protected void refreshTabs0(boolean refreshTabs) {
+		final boolean refresh = refreshTabs;
+		Runnable r = new Runnable() {
+			@Override
+			public void run() {
+				fViewform.setRedraw(false);
+				fRefreshTabs = !refresh;
+				displayInstanceTabs(refresh);
+				refreshStatus();
+				fViewform.setRedraw(true);
+			}
+		};
+		BusyIndicator.showWhile(getShell().getDisplay(), r);
+	}
 
 	/**
 	 * The input has changed to the given object, possibly <code>null</code>.
@@ -631,10 +717,14 @@
 					fViewform.setRedraw(false);
 					if (finput instanceof ILaunchConfiguration) {
 						ILaunchConfiguration configuration = (ILaunchConfiguration)finput;
-						boolean refreshtabs = !delegatesEqual(fWorkingCopy, configuration);
+						boolean refreshTabs = true;
+						if (fWorkingCopy != null && fWorkingCopy.getOriginal().equals(configuration.getWorkingCopy().getOriginal())) {
+							refreshTabs = false;
+						}
 						fOriginal = configuration;
 						fWorkingCopy = configuration.getWorkingCopy();
-						displayInstanceTabs(refreshtabs);
+						// Need to refresh all the time as tabs might have changed
+						displayInstanceTabs(refreshTabs);
 					} else if (finput instanceof ILaunchConfigurationType) {
 						fDescription = getDescription((ILaunchConfigurationType)finput);
 						setNoInput();
@@ -1383,7 +1473,7 @@
 	 *
 	 * @return the saved launch configuration or <code>null</code> if not saved
 	 */
-	protected ILaunchConfiguration handleApplyPressed() {
+	public ILaunchConfiguration handleApplyPressed() {
 		if(fOriginal != null && fOriginal.isReadOnly()) {
 			IStatus status = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] {fOriginal.getFile()}, fViewerControl.getShell());
 			if(!status.isOK()) {
@@ -1484,10 +1574,12 @@
 	 * @param index the index of the tab to display
 	 */
 	public void setActiveTab(int index) {
-		ILaunchConfigurationTab[] tabs = getTabs();
-		if (index >= 0 && index < tabs.length) {
-			fTabFolder.setSelection(index);
-			handleTabSelected();
+		if (!fTabFolder.isDisposed()) {
+			ILaunchConfigurationTab[] tabs = getTabs();
+			if (index >= 0 && index < tabs.length) {
+				fTabFolder.setSelection(index);
+				handleTabSelected();
+			}
 		}
 	}
 
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationsDialog.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationsDialog.java
index 4ab262b..22d5bef 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationsDialog.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationsDialog.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *  Copyright (c) 2000, 2016 IBM Corporation and others.
+ *  Copyright (c) 2000, 2017 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
@@ -931,7 +931,7 @@
 	 *
 	 * @return LaunchConfigurationTabGroupViewer
 	 */
-	protected LaunchConfigurationTabGroupViewer getTabViewer() {
+	public LaunchConfigurationTabGroupViewer getTabViewer() {
 		return fTabViewer;
 	}
 
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/ILaunchConfigurationTab.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/ILaunchConfigurationTab.java
index 3a8551d..158530f 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/ILaunchConfigurationTab.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/ILaunchConfigurationTab.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2017 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
@@ -235,5 +235,23 @@
 	 * @since 3.0
 	 */
 	public void deactivated(ILaunchConfigurationWorkingCopy workingCopy);
+
+	/**
+	 * Checks if it is OK to leave the Tab.
+	 *
+	 * @return <code>true</code> if OK to leave the tab or <code>false</code>
+	 * @since 3.12
+	 */
+	public default boolean OkToLeaveTab() {
+		return true;
+	}
+
+	/**
+	 * Perform the changes after apply.
+	 *
+	 * @since 3.12
+	 */
+	public default void postApply() {
+	}
 }