]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Automatically activate rename when creating a new Sysdyn variable either by drag...
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Nov 2013 09:36:25 +0000 (09:36 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Nov 2013 09:36:25 +0000 (09:36 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28243 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ontology/graph.tg
org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph
org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ExportTester.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynCreationInstruction.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/function/ModulesSearchFunction.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelButtonHandler.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/SysdynWorkbenchUtils.java [new file with mode: 0644]

index 69f8f6695b943b4760b0f5d099ab62c8e85cddb9..d606923c15b4afbcbe8d492f504ceceb6247a62a 100644 (file)
Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ
index f23805968a613b346f095fda73ef5ca4667f3ee8..8c5efed853de4e02692078fb9679e5bc0bdc6672 100644 (file)
@@ -163,6 +163,7 @@ SYSDYN.IndependentVariable <T SYSDYN.Variable
     >-- SYSDYN.IndependentVariable.rangeStep --> L0.Double <R L0.HasProperty : L0.FunctionalRelation
     // To be used in transient graphs for showing the correct experiment in equation editor
     >-- SYSDYN.IndependentVariable.activeExpression --> SYSDYN.Expression <R L0.IsWeaklyRelatedTo : L0.FunctionalRelation
+    >-- SYSDYN.IndependentVariable.isUninitialized --> SYSDYN.IndependentVariable <R L0.HasProperty
 
 SYSDYN.Auxiliary <T SYSDYN.IndependentVariable
     @L0.assert SYSDYN.Variable.type "Real"
index 08d0361ede24e7c0ca458d2644a60211b2e7588d..f0b55a2f9859d339cc959157044ce9ec11314d4b 100644 (file)
@@ -429,6 +429,8 @@ public class SysdynResource {
     public final Resource ImportedOntologies;\r
     public final Resource IndependentVariable;\r
     public final Resource IndependentVariable_activeExpression;\r
+    public final Resource IndependentVariable_isUninitialized;\r
+    public final Resource IndependentVariable_isUninitialized_Inverse;\r
     public final Resource IndependentVariable_rangeEnd;\r
     public final Resource IndependentVariable_rangeEnd_Inverse;\r
     public final Resource IndependentVariable_rangeStart;\r
@@ -1173,6 +1175,8 @@ public class SysdynResource {
         public static final String ImportedOntologies = "http://www.simantics.org/Sysdyn-1.1/ImportedOntologies";\r
         public static final String IndependentVariable = "http://www.simantics.org/Sysdyn-1.1/IndependentVariable";\r
         public static final String IndependentVariable_activeExpression = "http://www.simantics.org/Sysdyn-1.1/IndependentVariable/activeExpression";\r
+        public static final String IndependentVariable_isUninitialized = "http://www.simantics.org/Sysdyn-1.1/IndependentVariable/isUninitialized";\r
+        public static final String IndependentVariable_isUninitialized_Inverse = "http://www.simantics.org/Sysdyn-1.1/IndependentVariable/isUninitialized/Inverse";\r
         public static final String IndependentVariable_rangeEnd = "http://www.simantics.org/Sysdyn-1.1/IndependentVariable/rangeEnd";\r
         public static final String IndependentVariable_rangeEnd_Inverse = "http://www.simantics.org/Sysdyn-1.1/IndependentVariable/rangeEnd/Inverse";\r
         public static final String IndependentVariable_rangeStart = "http://www.simantics.org/Sysdyn-1.1/IndependentVariable/rangeStart";\r
@@ -1927,6 +1931,8 @@ public class SysdynResource {
         ImportedOntologies = getResourceOrNull(graph, URIs.ImportedOntologies);\r
         IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable);\r
         IndependentVariable_activeExpression = getResourceOrNull(graph, URIs.IndependentVariable_activeExpression);\r
+        IndependentVariable_isUninitialized = getResourceOrNull(graph, URIs.IndependentVariable_isUninitialized);\r
+        IndependentVariable_isUninitialized_Inverse = getResourceOrNull(graph, URIs.IndependentVariable_isUninitialized_Inverse);\r
         IndependentVariable_rangeEnd = getResourceOrNull(graph, URIs.IndependentVariable_rangeEnd);\r
         IndependentVariable_rangeEnd_Inverse = getResourceOrNull(graph, URIs.IndependentVariable_rangeEnd_Inverse);\r
         IndependentVariable_rangeStart = getResourceOrNull(graph, URIs.IndependentVariable_rangeStart);\r
index 09aafe6d54bae4c32a1c44cd4913b922a8d802bd..df9343b543a3e11c19e67dacb72e6a8a2d8e7910 100644 (file)
@@ -15,10 +15,7 @@ package org.simantics.sysdyn.ui.browser.nodes;
 import java.util.Collection;\r
 \r
 import org.eclipse.core.expressions.PropertyTester;\r
-import org.eclipse.ui.IWorkbench;\r
 import org.eclipse.ui.IWorkbenchPage;\r
-import org.eclipse.ui.IWorkbenchWindow;\r
-import org.eclipse.ui.PlatformUI;\r
 import org.simantics.DatabaseJob;\r
 import org.simantics.browsing.ui.common.node.AbstractNode;\r
 import org.simantics.db.ReadGraph;\r
@@ -28,6 +25,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.PossibleModel;\r
 import org.simantics.db.request.Read;\r
 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;\r
+import org.simantics.sysdyn.ui.utils.SysdynWorkbenchUtils;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
 import org.simantics.utils.ui.AdaptionUtils;\r
@@ -53,25 +51,10 @@ public class ExportTester extends PropertyTester {
                        inputResource = (Resource)a.iterator().next().data;\r
         }\r
         if (inputResource == null) {\r
-               DiagramEditor editor = null;\r
-               IWorkbench workbench = PlatformUI.getWorkbench();\r
-               IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();\r
-               // To ask for the active window doesn't work, so browse through all\r
-               // windows and when an active editor is found, use that\r
-               for (IWorkbenchWindow window : windows) {\r
-                       IWorkbenchPage page = window.getActivePage();\r
-                       if (page != null) {\r
-                               try {\r
-                           editor = (DiagramEditor)page.getActiveEditor();\r
-                           if (editor != null)\r
-                                       // Found one\r
-                                       break;\r
-                               } catch (ClassCastException e) {\r
-                                       continue;\r
-                    }\r
-                       }\r
-               }\r
-            if (editor != null) {\r
+               \r
+               IWorkbenchPage page = SysdynWorkbenchUtils.getActivePageOfEditor();\r
+               DiagramEditor editor = (DiagramEditor)page.getActiveEditor();\r
+               if (editor != null) {\r
                if (editor instanceof DiagramEditor) {\r
                        inputResource = editor.getInputResource();\r
                }\r
index 675d2e903588aceb9067421f2a705e74d11fc363..67721548d5b12464aeed43d9719f3f321452c024 100644 (file)
@@ -66,10 +66,11 @@ public class SysdynCreationInstruction extends CreationInstruction {
             );\r
             g.claim(result, L0X.Represents, result);\r
             \r
-            \r
+            // Add uninitialized tag. The tag is used to start name editing \r
+            SysdynResource SR = SysdynResource.getInstance(g);\r
+            g.claim(result, SR.IndependentVariable_isUninitialized, result);\r
             \r
             // Async reference dialog for shadow variables\r
-            SysdynResource SR = SysdynResource.getInstance(g);\r
             if(SR.Shadow.equals(componentType)) {\r
                 Layer0 L0 = Layer0.getInstance(g);\r
                 Collection<Resource> referrableVariables = g.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, SR.IndependentVariable));\r
index 7866f7d8c3b1e76a226642c8cd5da4bf765297ed..a5a2e5a318f572dc499e46ea804b0955918603d1 100644 (file)
@@ -73,6 +73,9 @@ public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipa
        ShapeNode node;\r
        G2DParentNode parent;\r
 \r
+       private boolean createVar;\r
+       private IDiagram createVarDiagram;\r
+\r
        @SGInit(designation = SGDesignation.CANVAS)\r
        public void init(G2DParentNode parent) {\r
                this.parent = parent;\r
@@ -175,6 +178,11 @@ public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipa
                        if (variableInformation != null\r
                                        && (kre.keyCode == variableInformation.shortcutKey || kre.keyCode == java.awt.event.KeyEvent.VK_SHIFT)) {\r
                                if (node != null) {\r
+                                       // If there is a variable to be created, do it when a key is released.\r
+                                       if (createVar) {\r
+                                               createVar = false;\r
+                                               createVariableOnDiagram(createVarDiagram);\r
+                                       }\r
                                        variableInformation = null;\r
                                        removeSG();\r
                                        return true;\r
@@ -220,12 +228,20 @@ public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipa
                {\r
                        return false;\r
                }\r
-\r
-\r
+               \r
                final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);\r
                if (d == null)\r
                        return false;\r
+               \r
+               // Need to create a new variable, save the diagram to do this later.\r
+               createVar = true;\r
+               createVarDiagram = d;\r
+\r
+               return true;\r
+       }\r
 \r
+\r
+       private void createVariableOnDiagram(IDiagram d) {\r
                DiagramUtils.mutateDiagram(d, new Callback<DiagramMutator>() {\r
                        @Override\r
                        public void run(DiagramMutator m) {\r
@@ -264,7 +280,6 @@ public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipa
 \r
                synchronizer.getCanvasContext().getContentContext().setDirty();\r
 \r
-               return true;\r
        }\r
 \r
        private class VariableInformation {\r
index b623b9d7f2998a52b9f16cafcdca2a13e5a783bb..9ac131dcebcea2eece5905126d5cbb426a221e9a 100644 (file)
@@ -15,15 +15,21 @@ import java.awt.Color;
 import java.awt.Font;\r
 import java.awt.geom.AffineTransform;\r
 \r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.ui.IWorkbenchPage;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.elements.DiagramNodeUtil;\r
 import org.simantics.diagram.elements.ITextListener;\r
 import org.simantics.diagram.elements.TextElementNoBounds;\r
 import org.simantics.diagram.elements.TextNode;\r
+import org.simantics.diagram.participant.SGFocusParticipant;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.chassis.SWTChassis;\r
 import org.simantics.g2d.diagram.DiagramUtils;\r
 import org.simantics.g2d.diagram.IDiagram;\r
 import org.simantics.g2d.element.ElementHints;\r
@@ -33,7 +39,11 @@ import org.simantics.g2d.element.handler.LifeCycle;
 import org.simantics.g2d.utils.Alignment;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.ui.diagramEditor.DiagramEditor;\r
+import org.simantics.modeling.ui.diagramEditor.DiagramViewer;\r
 import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.SysdynWorkbenchUtils;\r
 import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.datastructures.Callback;\r
@@ -177,10 +187,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds implements Lif
        \r
        public static double SCALE = 0.235;\r
 \r
-       @Override\r
-       public void init(final IElement e, G2DParentNode parent) {\r
-               HoverTextNode node = getTextNode(e, parent);\r
-\r
+       protected void initNode(IElement e, HoverTextNode node) {\r
                //Font font = new Font("Tahoma", 0, 12);\r
                Font font = ElementUtils.getTextFont(e);\r
                Color color = ElementUtils.getTextColor(e);\r
@@ -199,6 +206,99 @@ public class HoverTextElementNoBounds extends TextElementNoBounds implements Lif
                if(at != null)\r
                        node.setTransform(at);\r
        }\r
+       \r
+       protected void activateEdit(final HoverTextNode node, final IElement e) {\r
+               final ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(node);\r
+        // FIXME: needed only because eventdelegator registrations are done before adding node to scene graph.\r
+        if (ctx == null)\r
+            return;\r
+        if (!node.isEditMode()) {\r
+               \r
+               // Get the active editor\r
+               IWorkbenchPage page = SysdynWorkbenchUtils.getActivePageOfEditor();\r
+               final DiagramEditor editor = (DiagramEditor)page.getActiveEditor();\r
+            final ICanvasContext editorCtx = (ICanvasContext) editor.getViewer().getAdapter(ICanvasContext.class);\r
+\r
+            editor.getViewer().getComposite().getDisplay().asyncExec(new Runnable() {\r
+                    \r
+                @Override\r
+                public void run() {\r
+                       Control c = editor.getViewer().getComposite().getDisplay().getFocusControl();\r
+                       if (c == null || "BasicSymbols".equals(c.getParent().getToolTipText())) {\r
+                       // If the variable has been drag and dropped, set focus to diagram and then activate edit.\r
+                               \r
+                               editorCtx.add(new SGFocusParticipant((SWTChassis)editor.getViewer().getComposite(), DiagramViewer.DIAGRAMMING_CONTEXT) {\r
+                \r
+                               @Override\r
+                               public void focusGained(java.awt.event.FocusEvent event) {\r
+                                               \r
+                                       // When focus has been gained, acticate edit and destroy the listener.\r
+                                       editor.getViewer().getComposite().getDisplay().asyncExec(new Runnable() {\r
+                                           \r
+                                           @Override\r
+                                           public void run() {\r
+                                                       if (Boolean.TRUE.equals(node.setEditMode(true))) {\r
+                                                               node.activateEdit(0, e, ctx, true);\r
+                                                               node.repaint();\r
+                                                       }\r
+                                           }\r
+                                       });\r
+                                       ctx.remove(this);\r
+                               }\r
+                               \r
+                               @Override\r
+                               public void focusLost(java.awt.event.FocusEvent e) {\r
+                               }\r
+                       });\r
+                       \r
+                               editor.setFocus();\r
+                       } else {\r
+                               // If the variable has been created with shortcut key, just activate the edit.\r
+                        if (Boolean.TRUE.equals(node.setEditMode(true))) {\r
+                               node.activateEdit(0, e, ctx, true);\r
+                                       node.repaint();\r
+                               }\r
+                       }\r
+                }\r
+            });        \r
+        }\r
+       }\r
+       \r
+       @Override\r
+       public void init(final IElement e, G2DParentNode parent) {\r
+               final HoverTextNode node = getTextNode(e, parent);\r
+               initNode(e, node);\r
+\r
+               Object o = e.getHint(ElementHints.KEY_OBJECT);\r
+               if (o instanceof Resource) {\r
+                       final Resource element = (Resource)o;\r
+                       try {\r
+                               SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+       \r
+                                       @Override\r
+                                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                                               SysdynResource SR = SysdynResource.getInstance(graph);\r
+                                               ModelingResources MR = ModelingResources.getInstance(graph);\r
+                                               Resource component = graph.getPossibleObject(element, MR.ElementToComponent);\r
+                                               if (component == null)\r
+                                                       return;\r
+                                               \r
+                                               // See if the resource of the element has just been created. \r
+                                               Resource r = graph.getPossibleObject(component, SR.IndependentVariable_isUninitialized);\r
+                                               if (r == null){\r
+                                                       return;\r
+                                               }\r
+                                               \r
+                                               // If the resource is just been created, activate editing its name. \r
+                                               activateEdit(node, e);\r
+                                   graph.deny(component, SR.IndependentVariable_isUninitialized, r);\r
+                                       }\r
+                               });\r
+                       } catch (DatabaseException e1) {\r
+                               e1.printStackTrace();\r
+                       }\r
+               }\r
+       }\r
 \r
        @Override\r
        public void cleanup(IElement e) {\r
index be323011a6d9743a2d8c0c951d961f95011cdd8c..c469a1b5400a8f4f9ce742de9452fa8d512fedad 100644 (file)
@@ -19,6 +19,7 @@ import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;\r
 \r
 import org.simantics.diagram.elements.DiagramNodeUtil;\r
+import org.simantics.diagram.elements.TextEditActivation;\r
 import org.simantics.diagram.elements.TextNode;\r
 import org.simantics.g2d.canvas.ICanvasContext;\r
 import org.simantics.g2d.element.IElement;\r
@@ -158,4 +159,10 @@ public class HoverTextNode extends TextNode implements ISelectionPainterNode {
         }\r
         return false;\r
     }\r
+\r
+    public TextEditActivation activateEdit(int mouseId, IElement e, ICanvasContext ctx, boolean save) {\r
+       if (save)\r
+               return editActivation = super.activateEdit(mouseId, e, ctx);\r
+       return super.activateEdit(mouseId, e, ctx);\r
+    }\r
 }\r
index 4e565d5c8920d1686ddd45cd8a1b9b249e4d472b..3caae72cec844972cf5635e0347207c191133503 100644 (file)
@@ -18,10 +18,7 @@ import java.util.Map;
 import java.util.Set;\r
 \r
 import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.ui.IWorkbench;\r
 import org.eclipse.ui.IWorkbenchPage;\r
-import org.eclipse.ui.IWorkbenchWindow;\r
-import org.eclipse.ui.PlatformUI;\r
 import org.simantics.databoard.Bindings;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
@@ -40,6 +37,7 @@ import org.simantics.scl.runtime.function.FunctionImpl5;
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.handlers.FindSearchTrim;\r
 import org.simantics.sysdyn.ui.handlers.FindSearchTrim.Scope;\r
+import org.simantics.sysdyn.ui.utils.SysdynWorkbenchUtils;\r
 import org.simantics.workbench.search.NameAndTypeRow;\r
 import org.simantics.workbench.search.NamedResource;\r
 import org.simantics.workbench.search.SearchQuery;\r
@@ -76,25 +74,9 @@ public class ModulesSearchFunction extends FunctionImpl5<IProgressMonitor, ReadG
     \r
     private static boolean resourceFoundInCurrentDiagram(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
 \r
-        DiagramEditor editor = null;\r
-       IWorkbench workbench = PlatformUI.getWorkbench();\r
-       IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();\r
-       // To ask for the active window doesn't work, so browse through all\r
-       // windows and when an active editor is found, use that\r
-       for (IWorkbenchWindow window : windows) {\r
-               IWorkbenchPage page = window.getActivePage();\r
-               if (page != null) {\r
-                       try {\r
-                   editor = (DiagramEditor)page.getActiveEditor();\r
-                   if (editor != null)\r
-                                       // Found one\r
-                                       break;\r
-                       } catch (ClassCastException e) {\r
-                               continue;\r
-                }\r
-               }\r
-       }\r
-        if (editor != null) {\r
+       IWorkbenchPage page = SysdynWorkbenchUtils.getActivePageOfEditor();\r
+       DiagramEditor editor = (DiagramEditor)page.getActiveEditor();\r
+       if (editor != null) {\r
                Resource inputResource = editor.getInputResource();\r
                ModelingResources MOD = ModelingResources.getInstance(graph);\r
                Resource editorResource = graph.getPossibleObject(inputResource, MOD.DiagramToComposite);\r
index bb6388ca940c724323f0aac1ad6baf043b9e3721..a9ee8a3255a97fd425361a7d1cb3782e64b8f6e4 100644 (file)
@@ -15,11 +15,8 @@ import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;\r
 import org.eclipse.jface.viewers.ISelection;\r
 import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.ui.IWorkbench;\r
 import org.eclipse.ui.IWorkbenchPage;\r
 import org.eclipse.ui.IWorkbenchPart;\r
-import org.eclipse.ui.IWorkbenchWindow;\r
-import org.eclipse.ui.PlatformUI;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.browsing.ui.common.node.AbstractNode;\r
 import org.simantics.browsing.ui.platform.PropertyPageView;\r
@@ -29,6 +26,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.PossibleModel;\r
 import org.simantics.db.request.Read;\r
 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;\r
+import org.simantics.sysdyn.ui.utils.SysdynWorkbenchUtils;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
 import org.simantics.utils.ui.AdaptionUtils;\r
@@ -85,25 +83,9 @@ public class ExportModelButtonHandler extends ExportModelHandler {
         \r
         // When the selection doesn't have a resource, use the currently active diagram.\r
         if (inputResource == null) {\r
-               DiagramEditor editor = null;\r
-               IWorkbench workbench = PlatformUI.getWorkbench();\r
-               IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();\r
-               // To ask for the active window doesn't work, so browse through all\r
-               // windows and when an active editor is found, use that\r
-               for (IWorkbenchWindow window : windows) {\r
-                       IWorkbenchPage page = window.getActivePage();\r
-                       if (page != null) {\r
-                               try {\r
-                           editor = (DiagramEditor)page.getActiveEditor();\r
-                           if (editor != null)\r
-                                       // Found one\r
-                                       break;\r
-                               } catch (ClassCastException e) {\r
-                                       continue;\r
-                    }\r
-                       }\r
-               }\r
-            if (editor != null && editor instanceof DiagramEditor) {\r
+               IWorkbenchPage page = SysdynWorkbenchUtils.getActivePageOfEditor();\r
+               DiagramEditor editor = (DiagramEditor)page.getActiveEditor();\r
+               if (editor != null && editor instanceof DiagramEditor) {\r
                inputResource = editor.getInputResource();\r
             } else {\r
                        return null; \r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/SysdynWorkbenchUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/SysdynWorkbenchUtils.java
new file mode 100644 (file)
index 0000000..233a6e4
--- /dev/null
@@ -0,0 +1,38 @@
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.eclipse.ui.IWorkbench;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.IWorkbenchWindow;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.simantics.modeling.ui.diagramEditor.DiagramEditor;\r
+\r
+public class SysdynWorkbenchUtils {\r
+\r
+       /**\r
+        * Get the active page of the diagram editor.\r
+        * \r
+        * @return active page of the diagram editor.\r
+        */\r
+       public static IWorkbenchPage getActivePageOfEditor() {\r
+               DiagramEditor editor = null;\r
+               IWorkbench workbench = PlatformUI.getWorkbench();\r
+               IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();\r
+               IWorkbenchPage page = null;\r
+               // To ask for the active window doesn't work, so browse through all\r
+               // windows and when an active editor is found, use that\r
+               for (IWorkbenchWindow window : windows) {\r
+                       page = window.getActivePage();\r
+                       if (page != null) {\r
+                               try {\r
+                           editor = (DiagramEditor)page.getActiveEditor();\r
+                           if (editor != null)\r
+                                       // Found one\r
+                                       break;\r
+                               } catch (ClassCastException e) {\r
+                                       continue;\r
+                   }\r
+                       }\r
+               }\r
+               return page;\r
+       }\r
+}\r