Bug 532464 - Opening a new window destroys/blocks tab dragging in first
window

Fixed search order for the control search. The active shell should be
checked first, otherwise the findControl() could find "visible" widgets
from shells which are overlapped with the active one.

Reduced visibility of findControl() methods to avoid cases clients
search for controls in a wrong order. The
org.eclipse.e4.ui.workbench.addons.dndaddon package is not API, so
should be OK to do so.

Change-Id: Ia7e4f48f76cc043a6e905e892745ae13e141c18b
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/bundles/org.eclipse.e4.ui.workbench.addons.swt/src/org/eclipse/e4/ui/workbench/addons/dndaddon/DragAndDropUtil.java b/bundles/org.eclipse.e4.ui.workbench.addons.swt/src/org/eclipse/e4/ui/workbench/addons/dndaddon/DragAndDropUtil.java
index 6df4b0e..e2b66b2 100644
--- a/bundles/org.eclipse.e4.ui.workbench.addons.swt/src/org/eclipse/e4/ui/workbench/addons/dndaddon/DragAndDropUtil.java
+++ b/bundles/org.eclipse.e4.ui.workbench.addons.swt/src/org/eclipse/e4/ui/workbench/addons/dndaddon/DragAndDropUtil.java
@@ -55,11 +55,35 @@
 	 */
 	public static Control findControl(Display displayToSearch, Point locationToFind) {
 		Shell[] shells = displayToSearch.getShells();
-
+		fixShellOrder(displayToSearch, shells);
 		return findControl(shells, locationToFind);
 	}
 
 	/**
+	 * Finds the active shell and moves it to the end of the given array, so that
+	 * findControl() will find the controls from the active shell first.
+	 */
+	private static void fixShellOrder(Display display, Shell[] shells) {
+		if (shells.length <= 1) {
+			return;
+		}
+		Shell activeShell = display.getActiveShell();
+		int lastIndex = shells.length - 1;
+		if (activeShell == null || shells[lastIndex] == activeShell) {
+			return;
+		}
+		// Find the index of the active shell and exchange last one with active
+		for (int i = 0; i < shells.length; i++) {
+			if (shells[i] == activeShell) {
+				Shell toMove = shells[lastIndex];
+				shells[i] = toMove;
+				shells[lastIndex] = activeShell;
+				break;
+			}
+		}
+	}
+
+	/**
 	 * Searches the given list of controls for a control containing the given
 	 * point. If the array contains any composites, those composites will be
 	 * recursively searched to find the most specific child that contains the
@@ -73,7 +97,7 @@
 	 * @return the most specific Control that overlaps the given point, or null
 	 *         if none
 	 */
-	public static Control findControl(Control[] toSearch, Point locationToFind) {
+	private static Control findControl(Control[] toSearch, Point locationToFind) {
 		for (int idx = toSearch.length - 1; idx >= 0; idx--) {
 			Control next = toSearch[idx];
 
@@ -111,7 +135,7 @@
 	 *            location (in display coordinates)
 	 * @return the control at the given location
 	 */
-	public static Control findControl(Composite toSearch, Point locationToFind) {
+	private static Control findControl(Composite toSearch, Point locationToFind) {
 		Control[] children = toSearch.getChildren();
 
 		return findControl(children, locationToFind);