[531919] Fix tool update not working in specific conditions

Fix go to marker illegal state exception cannot activate read/write
transaction

Fix tests not working locally on windows because of a shell not
activated

Fix update tool command visible on command stack and undoable making
many test to fail.

Bug: 531919
Change-Id: I671c85e9953240b94c9c26b2804e80dc6fa6a693
Signed-off-by: Pierre Guilet <pierre.guilet@obeo.fr>
diff --git a/plugins/org.eclipse.sirius.diagram.ui/plugin.properties b/plugins/org.eclipse.sirius.diagram.ui/plugin.properties
index 4aa406f..dd0a240 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/plugin.properties
+++ b/plugins/org.eclipse.sirius.diagram.ui/plugin.properties
@@ -838,6 +838,7 @@
 DDiagramEditorImpl_noSessionMsg = Error while getting the session.
 DDiagramEditorImpl_refreshJobInterruptedMsg = Refresh job got interrupted
 DDiagramEditorImpl_refreshJobLabel = Refresh
+DDiagramEditorImpl_updateToolFailure=The tools for the diagram could not be computed. No VSM tools will be available in the palette.
 DEdgeCreateCommand_executionErrorMsg = Invalid arguments in create link command
 DEdgeLabelItemProvider_label = label
 DNodeContainerViewNodeContainerCompartment2EditPart_title=ViewNodeContainerCompartment
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/providers/AbstractModelMarkerNavigationProvider.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/providers/AbstractModelMarkerNavigationProvider.java
index 406360d..79f7db8 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/providers/AbstractModelMarkerNavigationProvider.java
+++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/providers/AbstractModelMarkerNavigationProvider.java
@@ -76,30 +76,9 @@
             }
         }
         
-        // If the refresh at opening is activated, we do not run the gotoMarker
-        // in runExclusive since the refresh can modify the model. See bug
-        // 509970.
-        if (DialectUIManager.INSTANCE.isRefreshActivatedOnRepresentationOpening()) {
-            AbstractModelMarkerNavigationProvider.super.gotoMarker(editor, marker);
-        } else if (editingDomain != null) {
-            if (editingDomain != null) {
-    
-                // Perform the "goto" in a read operation.
-                try {
-                    editingDomain.runExclusive(new Runnable() {
-
-                        @Override
-                        public void run() {
-                            AbstractModelMarkerNavigationProvider.super.gotoMarker(editor, marker);
-                        }
-                    });
-
-                } catch (InterruptedException e) {
-                    Trace.catching(MslUIPlugin.getDefault(), MslUIDebugOptions.EXCEPTIONS_CATCHING, getClass(), "gotoMarker", e); //$NON-NLS-1$
-                    Log.error(MslUIPlugin.getDefault(), MslUIStatusCodes.IGNORED_EXCEPTION_WARNING, e.getLocalizedMessage(), e);
-                }
-            }
-        }
+        // We do not run the gotoMarker
+        // in runExclusive since tool computation modifies the model.
+        AbstractModelMarkerNavigationProvider.super.gotoMarker(editor, marker);
     }
     
     /**
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorImpl.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorImpl.java
index 9b75441..5e7e3b5 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorImpl.java
+++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorImpl.java
@@ -14,6 +14,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -43,7 +44,9 @@
 import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.emf.edit.domain.EditingDomain;
 import org.eclipse.emf.edit.provider.IDisposable;
+import org.eclipse.emf.transaction.RollbackException;
 import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.transaction.impl.TransactionImpl;
 import org.eclipse.emf.workspace.IWorkspaceCommandStack;
 import org.eclipse.gef.DefaultEditDomain;
 import org.eclipse.gef.EditPart;
@@ -759,10 +762,20 @@
             toolManagement.addToolChangeListener(paletteToolChangeListener);
 
             if (getSession() != null) {
-                UpdateToolRecordingCommand updateToolRecordingCommand = new UpdateToolRecordingCommand(getSession().getTransactionalEditingDomain(), (DDiagram) getDiagram().getElement(), true);
-                session.getTransactionalEditingDomain().getCommandStack().execute(updateToolRecordingCommand);
+                // We don't use a command stack because we don't want the tool computation to be undone.
+                TransactionImpl t = new TransactionImpl(session.getTransactionalEditingDomain(), false, Collections.EMPTY_MAP);
+                try {
+                    t.start();
+                    UpdateToolRecordingCommand updateToolRecordingCommand = new UpdateToolRecordingCommand(session.getTransactionalEditingDomain(), (DDiagram) getDiagram().getElement(), true);
+                    if (updateToolRecordingCommand.canExecute()) {
+                        updateToolRecordingCommand.execute();
+                    }
+                    t.commit();
+                    toolManagement.notifyToolChange();
+                } catch (RollbackException | InterruptedException e) {
+                    DiagramPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, DiagramPlugin.ID, Messages.DDiagramEditorImpl_updateToolFailure, e));
+                }
             }
-            toolManagement.notifyToolChange();
         }
         // Initialize drag'n drop listener from palette
         paletteTransferDropTargetListener = new SiriusPaletteToolDropTargetListener(getGraphicalViewer());
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorSessionListenerDelegate.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorSessionListenerDelegate.java
index 24e01b4..a4f1eef 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorSessionListenerDelegate.java
+++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorSessionListenerDelegate.java
@@ -11,10 +11,15 @@
 package org.eclipse.sirius.diagram.ui.tools.internal.editor;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.emf.edit.provider.ComposedImage;
+import org.eclipse.emf.transaction.RollbackException;
 import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.transaction.impl.TransactionImpl;
 import org.eclipse.gmf.runtime.notation.Diagram;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.sirius.business.api.session.SessionListener;
@@ -24,6 +29,7 @@
 import org.eclipse.sirius.diagram.tools.api.management.ToolManagement;
 import org.eclipse.sirius.diagram.tools.internal.management.UpdateToolRecordingCommand;
 import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin;
+import org.eclipse.sirius.diagram.ui.provider.Messages;
 import org.eclipse.sirius.diagram.ui.tools.api.graphical.edit.palette.PaletteManager;
 import org.eclipse.sirius.diagram.ui.tools.api.image.DiagramImagesPath;
 import org.eclipse.sirius.ecore.extender.business.api.permission.IPermissionAuthority;
@@ -225,16 +231,28 @@
     private void reloadPalette(PaletteManager paletteManager, Diagram gmfDiagram, boolean clean) {
         TransactionalEditingDomain editingDomain = dDiagramEditorImpl.getEditingDomain();
         if (editingDomain != null) {
-            UpdateToolRecordingCommand updateToolRecordingCommand = new UpdateToolRecordingCommand(editingDomain, (DDiagram) gmfDiagram.getElement(), true);
-            editingDomain.getCommandStack().execute(updateToolRecordingCommand);
-            ToolManagement toolManagement = DiagramPlugin.getDefault().getToolManagement(gmfDiagram);
-            if (toolManagement != null) {
-                if (clean) {
-                    toolManagement.notifyToolChangeAfterVSMReload();
-                } else {
-                    toolManagement.notifyToolChange();
+
+            // We don't use a command stack because we don't want the tool computation to be undone.
+            TransactionImpl t = new TransactionImpl(editingDomain, false, Collections.EMPTY_MAP);
+            try {
+                t.start();
+                UpdateToolRecordingCommand updateToolRecordingCommand = new UpdateToolRecordingCommand(editingDomain, (DDiagram) gmfDiagram.getElement(), true);
+                if (updateToolRecordingCommand.canExecute()) {
+                    updateToolRecordingCommand.execute();
                 }
+                t.commit();
+                ToolManagement toolManagement = DiagramPlugin.getDefault().getToolManagement(gmfDiagram);
+                if (toolManagement != null) {
+                    if (clean) {
+                        toolManagement.notifyToolChangeAfterVSMReload();
+                    } else {
+                        toolManagement.notifyToolChange();
+                    }
+                }
+            } catch (RollbackException | InterruptedException e) {
+                DiagramPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, DiagramPlugin.ID, Messages.DDiagramEditorImpl_updateToolFailure, e));
             }
+
         }
     }
 
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src/org/eclipse/sirius/diagram/ui/provider/Messages.java b/plugins/org.eclipse.sirius.diagram.ui/src/org/eclipse/sirius/diagram/ui/provider/Messages.java
index d743f81..ae86a82 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/src/org/eclipse/sirius/diagram/ui/provider/Messages.java
+++ b/plugins/org.eclipse.sirius.diagram.ui/src/org/eclipse/sirius/diagram/ui/provider/Messages.java
@@ -1555,6 +1555,9 @@
 
     @TranslatableMessage
     public static String UndoRedoCapableEMFCommandFactory_insertHorizontalBlankSpaceNotImplemented;
+
+    @TranslatableMessage
+    public static String DDiagramEditorImpl_updateToolFailure;
     // CHECKSTYLE:ON
 
     private Messages() {
diff --git a/plugins/org.eclipse.sirius.diagram/src/org/eclipse/sirius/diagram/tools/internal/management/UpdateToolRecordingCommand.java b/plugins/org.eclipse.sirius.diagram/src/org/eclipse/sirius/diagram/tools/internal/management/UpdateToolRecordingCommand.java
index 694c89f..8f50981 100644
--- a/plugins/org.eclipse.sirius.diagram/src/org/eclipse/sirius/diagram/tools/internal/management/UpdateToolRecordingCommand.java
+++ b/plugins/org.eclipse.sirius.diagram/src/org/eclipse/sirius/diagram/tools/internal/management/UpdateToolRecordingCommand.java
@@ -10,7 +10,6 @@
  *******************************************************************************/
 package org.eclipse.sirius.diagram.tools.internal.management;
 
-import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.emf.transaction.RecordingCommand;
 import org.eclipse.emf.transaction.TransactionalEditingDomain;
 import org.eclipse.sirius.diagram.DDiagram;
@@ -56,8 +55,6 @@
         ToolManagement toolManagement = DiagramPlugin.getPlugin().getToolManagement(diagram);
         if (toolManagement != null) {
             toolManagement.updateTools(updateFilters);
-        } else {
-            throw new OperationCanceledException(Messages.UpdateToolRecordingCommand_cancelExceptionMessage);
         }
     }
 
diff --git a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/api/command/CreationAndDeletionUndoRedoTests.java b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/api/command/CreationAndDeletionUndoRedoTests.java
index d2bbcda..2ea47e7 100644
--- a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/api/command/CreationAndDeletionUndoRedoTests.java
+++ b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/api/command/CreationAndDeletionUndoRedoTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2016 THALES GLOBAL SERVICES.
+ * Copyright (c) 2010, 2018 THALES GLOBAL SERVICES.
  * 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
@@ -35,8 +35,7 @@
 import org.eclipse.ui.PlatformUI;
 
 /**
- * Tests creation and deletion undo command based on entities diagram of ecore
- * modeler.
+ * Tests creation and deletion undo command based on entities diagram of ecore modeler.
  * 
  * @author mchauvin
  */
@@ -171,18 +170,6 @@
         assertTrue(DialectManager.INSTANCE.getAllRepresentations(session).contains(createdRepresentation));
         TestsUtil.synchronizationWithUIThread();
 
-        /* undo tools availability update. */
-        assertTrue(canUndo());
-        assertEquals("Update available tools", getCommandLabel());
-        undo();
-        TestsUtil.synchronizationWithUIThread();
-
-        /* undo tools availability update. */
-        assertTrue(canUndo());
-        assertEquals("Update available tools", getCommandLabel());
-        undo();
-        TestsUtil.synchronizationWithUIThread();
-
         /* undo diagram creation command */
         assertTrue(canUndo());
         assertEquals("Create representation", getCommandLabel());
@@ -198,9 +185,8 @@
 
     private String getArrangeAllCommandLabel() {
         /*
-         * org.eclipse.gmf.runtime.diagram.ui.editpolicies.ContainerEditPolicy.
-         * getArrangeCommand() starting from Eclipse 3.6 return directly a
-         * ArrangeCommand with an empty label
+         * org.eclipse.gmf.runtime.diagram.ui.editpolicies.ContainerEditPolicy. getArrangeCommand() starting from
+         * Eclipse 3.6 return directly a ArrangeCommand with an empty label
          */
         if (isPlatformAtLeastEclipse36())
             return "";
diff --git a/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/AbstractSiriusSwtBotGefTestCase.java b/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/AbstractSiriusSwtBotGefTestCase.java
index ba3c17f..46867e1 100644
--- a/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/AbstractSiriusSwtBotGefTestCase.java
+++ b/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/AbstractSiriusSwtBotGefTestCase.java
@@ -276,6 +276,9 @@
                 } else {
                     shell.setFullScreen(AbstractSiriusSwtBotGefTestCase.fFullScreen);
                 }
+                if (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell() != null) {
+                    PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().forceActive();
+                }
             }
         });