Repository: workcraft/workcraft
Issue: Incorrect enabled/disabled menus on changing the work

To reproduce, open two STG works A and B. In work A  activate Simulation tool and in work B activate Selection tool.

Initially in work A the edit, transformation and verification tools are disabled. However if one switches to B and than back to A - some of the tools become enabled.

---

This issue was imported from Launchpad bug.
- date created: 2015-11-11T09:48:01Z
- owner: danilovesky
- assignee: danilovesky
- the launchpad url was https://bugs.launchpad.net/bugs/1515156


Changed files:
WorkcraftCore/src/org/workcraft/gui/ToolboxPanel.java
WorkcraftCore/src/org/workcraft/gui/graph/tools/SimulationTool.java
WorkcraftCore/src/org/workcraft/gui/graph/tools/GraphEditorTool.java
StgPlugin/src/org/workcraft/plugins/stg/tools/EncodingConflictAnalyserTool.java
WorkcraftCore/src/org/workcraft/gui/MainWindow.java
WorkcraftCore/src/org/workcraft/gui/graph/tools/ConnectionTool.java
WorkcraftCore/src/org/workcraft/gui/graph/tools/AbstractTool.java
DtdPlugin/src/org/workcraft/plugins/dtd/tools/DtdSelectionTool.java
WorkcraftCore/src/org/workcraft/gui/graph/tools/SelectionTool.java
DfsPlugin/src/org/workcraft/plugins/dfs/tools/CycleAnalyserTool.java
CircuitPlugin/src/org/workcraft/plugins/circuit/tools/InitialisationAnalyserTool.java
PolicyPlugin/src/org/workcraft/plugins/policy/tools/TransitionBundlerTool.java

diff --git a/CircuitPlugin/src/org/workcraft/plugins/circuit/tools/InitialisationAnalyserTool.java b/CircuitPlugin/src/org/workcraft/plugins/circuit/tools/InitialisationAnalyserTool.java
index 87f5e48c6..e7a9d29e9 100644
--- a/CircuitPlugin/src/org/workcraft/plugins/circuit/tools/InitialisationAnalyserTool.java
+++ b/CircuitPlugin/src/org/workcraft/plugins/circuit/tools/InitialisationAnalyserTool.java
@@ -59,12 +59,19 @@ public class InitialisationAnalyserTool extends AbstractTool {
 
     @Override
     public void activated(final GraphEditor editor) {
-        editor.getWorkspaceEntry().setCanModify(false);
         Circuit circuit = (Circuit) editor.getModel().getMathModel();
         updateState(circuit);
         super.activated(editor);
     }
 
+    @Override
+    public void setup(final GraphEditor editor) {
+        super.setup(editor);
+        editor.getWorkspaceEntry().setCanModify(false);
+        editor.getWorkspaceEntry().setCanSelect(false);
+        editor.getWorkspaceEntry().setCanCopy(false);
+    }
+
     @Override
     public void deactivated(final GraphEditor editor) {
         initHighSet = null;
diff --git a/DfsPlugin/src/org/workcraft/plugins/dfs/tools/CycleAnalyserTool.java b/DfsPlugin/src/org/workcraft/plugins/dfs/tools/CycleAnalyserTool.java
index 608903d53..8909edce9 100644
--- a/DfsPlugin/src/org/workcraft/plugins/dfs/tools/CycleAnalyserTool.java
+++ b/DfsPlugin/src/org/workcraft/plugins/dfs/tools/CycleAnalyserTool.java
@@ -179,7 +179,14 @@ public class CycleAnalyserTool extends AbstractTool {
             cycleCountLabel.setText("Cycle count (out of " + cycles.size() + "):");
         }
         super.activated(editor);
+    }
+
+    @Override
+    public void setup(final GraphEditor editor) {
+        super.setup(editor);
         editor.getWorkspaceEntry().setCanModify(false);
+        editor.getWorkspaceEntry().setCanSelect(false);
+        editor.getWorkspaceEntry().setCanCopy(false);
     }
 
     @Override
diff --git a/DtdPlugin/src/org/workcraft/plugins/dtd/tools/DtdSelectionTool.java b/DtdPlugin/src/org/workcraft/plugins/dtd/tools/DtdSelectionTool.java
index 44d24a5d8..0b2810c5d 100644
--- a/DtdPlugin/src/org/workcraft/plugins/dtd/tools/DtdSelectionTool.java
+++ b/DtdPlugin/src/org/workcraft/plugins/dtd/tools/DtdSelectionTool.java
@@ -22,8 +22,8 @@ public class DtdSelectionTool extends SelectionTool {
     }
 
     @Override
-    public void activated(GraphEditor editor) {
-        super.activated(editor);
+    public void setup(final GraphEditor editor) {
+        super.setup(editor);
         editor.getWorkspaceEntry().setCanCopy(false);
     }
 
diff --git a/PolicyPlugin/src/org/workcraft/plugins/policy/tools/TransitionBundlerTool.java b/PolicyPlugin/src/org/workcraft/plugins/policy/tools/TransitionBundlerTool.java
index d50db3bfa..55e21ba62 100644
--- a/PolicyPlugin/src/org/workcraft/plugins/policy/tools/TransitionBundlerTool.java
+++ b/PolicyPlugin/src/org/workcraft/plugins/policy/tools/TransitionBundlerTool.java
@@ -1,6 +1,5 @@
 package org.workcraft.plugins.policy.tools;
 
-import org.workcraft.Framework;
 import org.workcraft.TransformationTool;
 import org.workcraft.plugins.policy.PolicyNet;
 import org.workcraft.plugins.policy.VisualPolicyNet;
@@ -25,8 +24,6 @@ public class TransitionBundlerTool extends TransformationTool {
 
     @Override
     public void run(WorkspaceEntry we) {
-        final Framework framework = Framework.getInstance();
-        framework.getMainWindow().getCurrentEditor().getToolBox().selectDefaultTool();
         we.saveMemento();
 
         final VisualPolicyNet visualModel = (VisualPolicyNet) we.getModelEntry().getVisualModel();
diff --git a/StgPlugin/src/org/workcraft/plugins/stg/tools/EncodingConflictAnalyserTool.java b/StgPlugin/src/org/workcraft/plugins/stg/tools/EncodingConflictAnalyserTool.java
index 10532fb31..99aee8af4 100644
--- a/StgPlugin/src/org/workcraft/plugins/stg/tools/EncodingConflictAnalyserTool.java
+++ b/StgPlugin/src/org/workcraft/plugins/stg/tools/EncodingConflictAnalyserTool.java
@@ -181,10 +181,16 @@ public class EncodingConflictAnalyserTool extends AbstractTool {
 
     @Override
     public void activated(final GraphEditor editor) {
-        editor.getWorkspaceEntry().setCanModify(true);
         stg = (VisualStg) editor.getModel();
         super.activated(editor);
+    }
+
+    @Override
+    public void setup(final GraphEditor editor) {
+        super.setup(editor);
         editor.getWorkspaceEntry().setCanModify(false);
+        editor.getWorkspaceEntry().setCanSelect(false);
+        editor.getWorkspaceEntry().setCanCopy(false);
     }
 
     @Override
diff --git a/WorkcraftCore/src/org/workcraft/gui/MainWindow.java b/WorkcraftCore/src/org/workcraft/gui/MainWindow.java
index e4fdd36a0..b5a20c3c2 100644
--- a/WorkcraftCore/src/org/workcraft/gui/MainWindow.java
+++ b/WorkcraftCore/src/org/workcraft/gui/MainWindow.java
@@ -87,6 +87,7 @@ import org.workcraft.gui.actions.Action;
 import org.workcraft.gui.actions.ScriptedActionListener;
 import org.workcraft.gui.graph.GraphEditorPanel;
 import org.workcraft.gui.graph.tools.GraphEditor;
+import org.workcraft.gui.graph.tools.GraphEditorTool;
 import org.workcraft.gui.propertyeditor.SettingsEditorDialog;
 import org.workcraft.gui.tasks.TaskFailureNotifier;
 import org.workcraft.gui.tasks.TaskManagerWindow;
@@ -844,9 +845,15 @@ public class MainWindow extends JFrame {
         if (editorInFocus != sender) {
             editorInFocus = sender;
 
-            toolControlsWindow.setContent(sender.getToolBox());
-            editorToolsWindow.setContent(sender.getToolBox().getControlPanel());
-            mainMenu.setMenuForWorkspaceEntry(editorInFocus.getWorkspaceEntry());
+            WorkspaceEntry we = editorInFocus.getWorkspaceEntry();
+            mainMenu.setMenuForWorkspaceEntry(we);
+
+            ToolboxPanel toolBox = sender.getToolBox();
+            toolControlsWindow.setContent(toolBox);
+            editorToolsWindow.setContent(toolBox.getControlPanel());
+
+            GraphEditorTool selectedTool = toolBox.getTool();
+            selectedTool.setup(editorInFocus);
             sender.updatePropertyView();
 
             final Framework framework = Framework.getInstance();
diff --git a/WorkcraftCore/src/org/workcraft/gui/ToolboxPanel.java b/WorkcraftCore/src/org/workcraft/gui/ToolboxPanel.java
index 769e3c189..53ccdb49b 100644
--- a/WorkcraftCore/src/org/workcraft/gui/ToolboxPanel.java
+++ b/WorkcraftCore/src/org/workcraft/gui/ToolboxPanel.java
@@ -97,6 +97,18 @@ public class ToolboxPanel extends JPanel implements ToolProvider, GraphEditorKey
 
     private final GraphEditorPanel editor;
 
+    public ToolboxPanel(GraphEditorPanel editor) {
+        this.editor = editor;
+        this.setFocusable(false);
+
+        selectionTool = new SelectionTool();
+        labelTool = new CommentGeneratorTool();
+        connectionTool = new ConnectionTool();
+        selectedTool = null;
+
+        setToolsForModel(editor.getModel());
+    }
+
     public void addTool(final GraphEditorTool tool, boolean selected) {
         tools.add(tool);
         int hotKeyCode = tool.getHotKeyCode();
@@ -197,15 +209,12 @@ public class ToolboxPanel extends JPanel implements ToolProvider, GraphEditorKey
 
         controlPanel.setTool(selectedTool, editor);
         setToolButtonSelected(selectedTool, true);
+        selectedTool.setup(editor);
         selectedTool.activated(editor);
         editor.updatePropertyView();
         editor.repaint();
     }
 
-    public void selectDefaultTool() {
-        selectTool(selectedTool);
-    }
-
     public void addCommonTools() {
         addTool(selectionTool, true);
         addTool(labelTool, false);
@@ -250,18 +259,6 @@ public class ToolboxPanel extends JPanel implements ToolProvider, GraphEditorKey
         this.repaint();
     }
 
-    public ToolboxPanel(GraphEditorPanel editor) {
-        this.editor = editor;
-        this.setFocusable(false);
-
-        selectionTool = new SelectionTool();
-        labelTool = new CommentGeneratorTool();
-        connectionTool = new ConnectionTool();
-        selectedTool = null;
-
-        setToolsForModel(editor.getModel());
-    }
-
     public GraphEditorTool getTool() {
         return selectedTool;
     }
diff --git a/WorkcraftCore/src/org/workcraft/gui/graph/tools/AbstractTool.java b/WorkcraftCore/src/org/workcraft/gui/graph/tools/AbstractTool.java
index 79ecba56c..a653bc847 100644
--- a/WorkcraftCore/src/org/workcraft/gui/graph/tools/AbstractTool.java
+++ b/WorkcraftCore/src/org/workcraft/gui/graph/tools/AbstractTool.java
@@ -48,6 +48,13 @@ public abstract class AbstractTool implements GraphEditorTool {
     public void reactivated(final GraphEditor editor) {
     }
 
+    @Override
+    public void setup(final GraphEditor editor) {
+        editor.getWorkspaceEntry().setCanModify(true);
+        editor.getWorkspaceEntry().setCanSelect(true);
+        editor.getWorkspaceEntry().setCanCopy(true);
+    }
+
     @Override
     public void createInterfacePanel(final GraphEditor editor) {
     }
diff --git a/WorkcraftCore/src/org/workcraft/gui/graph/tools/ConnectionTool.java b/WorkcraftCore/src/org/workcraft/gui/graph/tools/ConnectionTool.java
index c92a81dd3..912ca8a98 100644
--- a/WorkcraftCore/src/org/workcraft/gui/graph/tools/ConnectionTool.java
+++ b/WorkcraftCore/src/org/workcraft/gui/graph/tools/ConnectionTool.java
@@ -106,8 +106,7 @@ public class ConnectionTool extends AbstractTool {
         warningMessage = null;
         mouseLeftFirstNode = false;
         editor.getModel().selectNone();
-        editor.getWorkspaceEntry().setCanSelect(false);
-        editor.getWorkspaceEntry().setCanModify(true);
+        setup(editor);
     }
 
     protected void updateState(GraphEditor editor) {
@@ -153,6 +152,14 @@ public class ConnectionTool extends AbstractTool {
         templateNode = null;
     }
 
+    @Override
+    public void setup(final GraphEditor editor) {
+        super.setup(editor);
+        editor.getWorkspaceEntry().setCanModify(firstNode == null);
+        editor.getWorkspaceEntry().setCanSelect(false);
+        editor.getWorkspaceEntry().setCanCopy(false);
+    }
+
     @Override
     public void drawInUserSpace(GraphEditor editor, Graphics2D g) {
         if ((firstNode != null) && (currentPoint != null)) {
diff --git a/WorkcraftCore/src/org/workcraft/gui/graph/tools/GraphEditorTool.java b/WorkcraftCore/src/org/workcraft/gui/graph/tools/GraphEditorTool.java
index a009251c9..e676fda2a 100644
--- a/WorkcraftCore/src/org/workcraft/gui/graph/tools/GraphEditorTool.java
+++ b/WorkcraftCore/src/org/workcraft/gui/graph/tools/GraphEditorTool.java
@@ -30,6 +30,7 @@ public interface GraphEditorTool extends GraphEditorKeyListener, GraphEditorMous
     void activated(final GraphEditor editor);
     void deactivated(final GraphEditor editor);
     void reactivated(final GraphEditor editor);
+    void setup(final GraphEditor editor);
 
     void drawInUserSpace(final GraphEditor editor, Graphics2D g);
     void drawInScreenSpace(final GraphEditor editor, Graphics2D g);
diff --git a/WorkcraftCore/src/org/workcraft/gui/graph/tools/SelectionTool.java b/WorkcraftCore/src/org/workcraft/gui/graph/tools/SelectionTool.java
index ae482772b..9ce8a5ccb 100644
--- a/WorkcraftCore/src/org/workcraft/gui/graph/tools/SelectionTool.java
+++ b/WorkcraftCore/src/org/workcraft/gui/graph/tools/SelectionTool.java
@@ -281,8 +281,6 @@ public class SelectionTool extends AbstractTool {
     @Override
     public void activated(final GraphEditor editor) {
         super.activated(editor);
-        editor.getWorkspaceEntry().setCanModify(true);
-        editor.getWorkspaceEntry().setCanSelect(true);
         currentNode = null;
     }
 
diff --git a/WorkcraftCore/src/org/workcraft/gui/graph/tools/SimulationTool.java b/WorkcraftCore/src/org/workcraft/gui/graph/tools/SimulationTool.java
index 0ea9b08f3..972a6a1d9 100644
--- a/WorkcraftCore/src/org/workcraft/gui/graph/tools/SimulationTool.java
+++ b/WorkcraftCore/src/org/workcraft/gui/graph/tools/SimulationTool.java
@@ -387,7 +387,6 @@ public abstract class SimulationTool extends AbstractTool implements ClipboardOw
     @Override
     public void activated(final GraphEditor editor) {
         editor.getWorkspaceEntry().captureMemento();
-        editor.getWorkspaceEntry().setCanModify(false);
         generateUnderlyingModel(editor.getModel());
         initialState = readModelState();
         super.activated(editor);
@@ -409,6 +408,14 @@ public abstract class SimulationTool extends AbstractTool implements ClipboardOw
         underlyingModel = null;
     }
 
+    @Override
+    public void setup(final GraphEditor editor) {
+        super.setup(editor);
+        editor.getWorkspaceEntry().setCanModify(false);
+        editor.getWorkspaceEntry().setCanSelect(false);
+        editor.getWorkspaceEntry().setCanCopy(false);
+    }
+
     public void generateUnderlyingModel(VisualModel model) {
         setUnderlyingModel(model);
     }