[463179] Reuse the active window shell when available

Bug: 463179
Change-Id: Ia6588bb626036b8885abb01c30d6a07a50586bbd
Signed-off-by: Cedric Brun <cedric.brun@obeo.fr>
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/commands/emf/PaneBasedSelectionWizardCommand.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/commands/emf/PaneBasedSelectionWizardCommand.java
index a273b31..c1254f3 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/commands/emf/PaneBasedSelectionWizardCommand.java
+++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/commands/emf/PaneBasedSelectionWizardCommand.java
@@ -37,6 +37,7 @@
 import org.eclipse.sirius.viewpoint.description.tool.PaneBasedSelectionWizardDescription;
 import org.eclipse.sirius.viewpoint.description.tool.ToolPackage;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -84,7 +85,15 @@
     public void doExecute() {
         computeInput();
         final Collection<EObject> preSelection = computePreSelection();
-        final Shell shell = new Shell();
+        Shell shell = null;
+        boolean createdShell = false;
+        if (PlatformUI.getWorkbench() != null && PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
+            shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
+        }
+        if (shell == null) {
+            shell = new Shell();
+            createdShell = true;
+        }
         final EObjectPaneBasedSelectionWizard wizard = new EObjectPaneBasedSelectionWizard(this.tool.getWindowTitle(), this.tool.getMessage(), getImage(), this.tool.getChoiceOfValuesMessage(),
                 this.tool.getSelectedValuesMessage(), DiagramUIPlugin.getPlugin().getItemProvidersAdapterFactory());
         wizard.init(input, preSelection);
@@ -116,7 +125,9 @@
                 SiriusLayoutDataManager.INSTANCE.getData((AbstractDNode) containerView);
             }
         }
-        shell.dispose();
+        if (createdShell) {
+            shell.dispose();
+        }
     }
 
     private ImageDescriptor getImage() {
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/commands/emf/SelectionWizardCommand.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/commands/emf/SelectionWizardCommand.java
index f28f04d..f16ae93 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/commands/emf/SelectionWizardCommand.java
+++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/commands/emf/SelectionWizardCommand.java
@@ -35,6 +35,7 @@
 import org.eclipse.sirius.viewpoint.SiriusPlugin;
 import org.eclipse.sirius.viewpoint.description.tool.SelectionWizardDescription;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
 
 /**
  * A command to display a selection wizard.
@@ -79,7 +80,15 @@
     @Override
     public void doExecute() {
         computeInput();
-        final Shell shell = new Shell();
+        Shell shell = null;
+        boolean createdShell = false;
+        if (PlatformUI.getWorkbench() != null && PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
+            shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
+        }
+        if (shell == null) {
+            shell = new Shell();
+            createdShell = true;
+        }
         final EObjectSelectionWizard wizard = new EObjectSelectionWizard(this.tool.getWindowTitle(), this.tool.getMessage(), getImage(), input, DiagramUIPlugin.getPlugin()
                 .getItemProvidersAdapterFactory());
         wizard.setMany(tool.isMultiple());
@@ -89,12 +98,16 @@
             final Collection<EObject> selectedElements = wizard.getSelectedEObjects();
             final org.eclipse.emf.common.command.Command command = factory.buildSelectionWizardCommandFromTool(tool, containerView, selectedElements);
             command.execute();
-            shell.dispose();
+            if (createdShell) {
+                shell.dispose();
+            }
         } else {
             if (containerView instanceof AbstractDNode) {
                 SiriusLayoutDataManager.INSTANCE.getData((AbstractDNode) containerView);
             }
-            shell.dispose();
+            if (createdShell) {
+                shell.dispose();
+            }
             throw new OperationCanceledException("User cancel operation");
         }
     }