]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
fixed the automatic editor closing when browsing deep hierarchical model structures simantics-1.4
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Jul 2011 10:57:40 +0000 (10:57 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Jul 2011 10:57:40 +0000 (10:57 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches/simantics-1.4@21473 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ShowInstantiatedModuleAction.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynEditorInput.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java

index 83c993de775ffa1373c6e3291c831b1dcfef80af..9816a2424d4d877a839fd597a405f940e6d296e6 100644 (file)
@@ -26,6 +26,7 @@ import org.simantics.structural.stubs.StructuralResource2;
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.browser.BrowserSelection;\r
 import org.simantics.sysdyn.ui.browser.nodes.ModuleNode;\r
+import org.simantics.sysdyn.ui.editor.SysdynEditorInput;\r
 import org.simantics.ui.DoubleClickEvent;\r
 import org.simantics.ui.IDoubleClickAction;\r
 import org.simantics.ui.workbench.ResourceEditorInput2;\r
@@ -103,7 +104,7 @@ public class ShowInstantiatedModuleAction implements IDoubleClickAction {
                }\r
                \r
                Resource diagram = g.getSingleObject(configuration, ModelingResources.getInstance(g).CompositeToDiagram);\r
-               return new ResourceEditorInput2(EDITOR_ID, diagram, modelURI, rvi == null ? "" : "/" + rvi);\r
+               return new SysdynEditorInput(EDITOR_ID, diagram, modelURI, rvi == null ? "" : "/" + rvi);\r
        }\r
        \r
        private void addShowModuleAction(DoubleClickEvent e, final ResourceEditorInput2 editorInput) {\r
index b0b181cf4c18c0cc3e203fc7f40ace123308616d..8e412512e205a66594abbfc255f62d58c4f61bfa 100644 (file)
@@ -65,6 +65,8 @@ import org.simantics.sysdyn.ui.elements2.connections.SysdynConnectionClass;
 import org.simantics.sysdyn.ui.properties.SysdynPropertyPage;\r
 import org.simantics.utils.datastructures.hints.HintContext;\r
 import org.simantics.utils.datastructures.hints.IHintContext;\r
+import org.simantics.utils.threads.AWTThread;\r
+import org.simantics.utils.threads.ThreadUtils;\r
 \r
 /**\r
  * @author Tuukka Lehtonen\r
@@ -79,13 +81,24 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr
        public void createPartControl(Composite parent) {\r
                super.createPartControl(parent);\r
                \r
-               ElementPainter ep = canvasContext.getSingleItem(ElementPainter.class);\r
-               for(IElement e : sourceDiagram.getElements()) {\r
-                       if(e.getElementClass().getId().contains("Connection")){\r
-                               ep.update(e);\r
+               \r
+               ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { \r
+\r
+                       @Override\r
+                       public void run() {\r
+                               if (isDisposed())  {\r
+                                       return; \r
+                               } else {\r
+                                       ElementPainter ep = canvasContext.getSingleItem(ElementPainter.class);\r
+                                       for(IElement e : sourceDiagram.getElements()) {\r
+                                               if(e.getElementClass().getId().contains("Connection")){\r
+                                                       ep.update(e);\r
+                                               }\r
+                                       }\r
+                                       scheduleZoomToFit();\r
+                               }\r
                        }\r
-               }\r
-               scheduleZoomToFit();\r
+               });\r
        } \r
 \r
        @Override\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynEditorInput.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynEditorInput.java
new file mode 100644 (file)
index 0000000..7789c2a
--- /dev/null
@@ -0,0 +1,416 @@
+package org.simantics.sysdyn.ui.editor;\r
+\r
+import org.eclipse.core.runtime.IAdaptable;\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.ui.IMemento;\r
+import org.eclipse.ui.IPersistableElement;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.AdaptionException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ResourceNotFoundException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.db.service.LifecycleSupport;\r
+import org.simantics.db.service.SerialisationSupport;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.icons.ImageDescriptorProvider;\r
+import org.simantics.ui.workbench.ResourceEditorInput2;\r
+import org.simantics.ui.workbench.ResourceEditorInputFactory2;\r
+import org.simantics.ui.workbench.TitleRequest;\r
+import org.simantics.ui.workbench.ToolTipRequest;\r
+import org.simantics.utils.ObjectUtils;\r
+import org.simantics.utils.datastructures.cache.ProvisionException;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.workbench.StringMemento;\r
+\r
+/**\r
+ * TEMPORARY fix to support sysdyn\r
+ * \r
+ * @author tlteemu\r
+ *\r
+ */\r
+public class SysdynEditorInput extends ResourceEditorInput2 {\r
+    \r
+    private final static boolean      DEBUG_UPDATE    = false;\r
+\r
+    private static final String       NO_NAME         =  "(no name)";\r
+\r
+    private final String              editorID;\r
+\r
+    private final String              modelURI;\r
+\r
+    private final String              rvi;\r
+\r
+    private String                    randomAccessResourceId;\r
+\r
+    private transient Resource        resource;\r
+\r
+    /**\r
+     * Gotten from the editor that needs to initialize this input. Currently\r
+     * needed for two things: {@link #exists()} and {@link #saveState(IMemento)}.\r
+     */\r
+    private transient Session         session;\r
+\r
+    private transient boolean         exists;\r
+\r
+    private transient String          name;\r
+\r
+    private transient String          tooltip;\r
+\r
+    private transient ImageDescriptor imageDesc;\r
+\r
+    /** Persistent memento for external data */\r
+    private final StringMemento       persistentStore = new StringMemento();\r
+\r
+    /**\r
+     * @param editorID\r
+     * @param r\r
+     */\r
+    public SysdynEditorInput(String editorID, Resource r, String modelURI, String rvi) {\r
+        super(editorID, r, modelURI, rvi);\r
+        if (editorID == null)\r
+            throw new IllegalArgumentException("null editor id");\r
+        if (r == null)\r
+            throw new IllegalArgumentException("null resource");\r
+\r
+        this.editorID = editorID;\r
+        this.randomAccessResourceId = "";\r
+        this.resource = r;\r
+        this.modelURI = modelURI;\r
+        this.rvi = rvi;\r
+        this.session = SimanticsUI.getSession();\r
+\r
+        ensureRandomAccessId();\r
+        setNonExistant();\r
+    }\r
+\r
+    /**\r
+     * @param editorID\r
+     * @param randomAccessResourceId\r
+     */\r
+    public SysdynEditorInput(String editorID, String randomAccessResourceId, String modelURI, String rvi) {\r
+        super(editorID, randomAccessResourceId, modelURI, rvi);\r
+        if (editorID == null)\r
+            throw new IllegalArgumentException("null editor id");\r
+        if (randomAccessResourceId == null)\r
+            throw new IllegalArgumentException("null resource id");\r
+\r
+        this.editorID = editorID;\r
+        this.randomAccessResourceId = randomAccessResourceId;\r
+        this.resource = null;\r
+        this.modelURI = modelURI;\r
+        this.rvi = rvi;\r
+        this.session = SimanticsUI.getSession();\r
+\r
+        setNonExistant();\r
+    }\r
+\r
+    void ensureRandomAccessId() {\r
+        if (resource == null)\r
+            throw new IllegalStateException("resource is null, input is disposed");\r
+        // Make sure that the resource has a random access id\r
+        try {\r
+            SerialisationSupport support = session.getService(SerialisationSupport.class);\r
+            randomAccessResourceId = String.valueOf(support.getRandomAccessId(resource));\r
+        } catch (DatabaseException e) {\r
+            throw new RuntimeException(e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void init(IAdaptable adapter) throws DatabaseException {\r
+        if (resource == null && randomAccessResourceId != null) {\r
+            getSession().syncRequest(new ReadRequest() {\r
+                @Override\r
+                public void run(ReadGraph g) throws DatabaseException {\r
+                    try {\r
+                        long id = Long.parseLong(randomAccessResourceId);\r
+                        resource = g.getService(SerialisationSupport.class).getResource(id);\r
+                        update(g);\r
+                    } catch (NumberFormatException e) {\r
+                        setNonExistant();\r
+                    } catch (DatabaseException e) {\r
+                        setNonExistant();\r
+                    }\r
+                }\r
+            });\r
+        } else {\r
+            if (resource != null) {\r
+                updateCaches(getSession(), true);\r
+            }\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void dispose() {\r
+        //System.out.println("dispose resource editor input: " + name);\r
+        // NOTE: this has to be done since Eclipse will cache these IEditorInput\r
+        // instances within EditorHistoryItem's that are stored in an EditorHistory\r
+        // instance. They are held by strong reference which means that the session\r
+        // cannot be collected if it is not nulled here.\r
+        session = null;\r
+        resource = null;\r
+    }\r
+\r
+    /**\r
+     * @return a graph instance if it exists and has not yet been disposed,\r
+     *         <code>null</code> otherwise\r
+     */\r
+    public Session getSession() {\r
+        // TODO: also throw an exception if the session is disposed\r
+        if (session == null)\r
+            throw new IllegalStateException("session is disposed");\r
+        return session;\r
+    }\r
+\r
+    @Override\r
+    public boolean exists() {\r
+        return exists;\r
+    }\r
+\r
+    @Override\r
+    public boolean exists(ReadGraph graph) throws DatabaseException {\r
+        try {\r
+            assertExists(graph);\r
+            return true;\r
+        } catch (ResourceNotFoundException e) {\r
+        } catch (Nonexistant e) {\r
+        }\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    public Resource getResource() {\r
+        return resource;\r
+    }\r
+\r
+    @Override\r
+    @Deprecated\r
+    public ResourceArray getResourceArray() {\r
+        return new ResourceArray(resource);\r
+    }\r
+\r
+    @Override\r
+    public String getModelURI() {\r
+        return modelURI;\r
+    }\r
+\r
+    @Override\r
+    public String getRVI() {\r
+        return rvi;\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see org.eclipse.ui.IEditorInput#getImageDescriptor()\r
+     */\r
+    @Override\r
+    public ImageDescriptor getImageDescriptor() {\r
+        return imageDesc;\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see org.eclipse.ui.IEditorInput#getName()\r
+     */\r
+    @Override\r
+    public String getName() {\r
+        return name;\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see org.eclipse.ui.IEditorInput#getToolTipText()\r
+     */\r
+    @Override\r
+    public String getToolTipText() {\r
+        return tooltip;\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see org.eclipse.ui.IEditorInput#getPersistable()\r
+     */\r
+    @Override\r
+    public IPersistableElement getPersistable() {\r
+        // Don't allow persistability when it's not possible.\r
+        if (!isPersistable())\r
+            return null;\r
+        return this;\r
+    }\r
+\r
+    protected boolean isPersistable() {\r
+        if (session == null)\r
+            return false;\r
+        LifecycleSupport lc = session.peekService(LifecycleSupport.class);\r
+        if (lc == null)\r
+            return false;\r
+        if (lc.isClosed())\r
+            return false;\r
+        return true;\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see org.eclipse.ui.IPersistableElement#getFactoryId()\r
+     */\r
+    @Override\r
+    public String getFactoryId() {\r
+        return ResourceEditorInputFactory2.getFactoryId();\r
+    }\r
+\r
+    /**\r
+     * Saves the state of the given resource editor input into the given memento.\r
+     *\r
+     * @param memento the storage area for element state\r
+     * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)\r
+     */\r
+    @Override\r
+    public void saveState(IMemento memento) {\r
+//        List<String> ids = randomAccessResourceId;\r
+        if (randomAccessResourceId == null) {\r
+            // Must create a new random access ID.\r
+            ensureRandomAccessId();\r
+        }\r
+        IMemento child = memento.createChild(ResourceEditorInputFactory2.TAG_RESOURCE_ID);\r
+        child.putTextData(randomAccessResourceId);\r
+        memento.putString(ResourceEditorInputFactory2.TAG_EDITOR_ID, editorID);\r
+        memento.putString(ResourceEditorInputFactory2.TAG_MODEL_URI, modelURI);\r
+        memento.putString(ResourceEditorInputFactory2.TAG_RVI, rvi);\r
+        memento.putString(ResourceEditorInputFactory2.TAG_EXTERNAL_MEMENTO_ID, persistentStore.toString());\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)\r
+     */\r
+    @SuppressWarnings("rawtypes")\r
+    @Override\r
+    public Object getAdapter(Class adapter) {\r
+        //System.out.println("[ResourceEditorInput] getAdapter: " + adapter.getName());\r
+        return null;\r
+    }\r
+\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        final int prime = 31;\r
+        int result = 1;\r
+        result = prime * result + editorID.hashCode();\r
+        result = prime * result + ObjectUtils.hashCode(modelURI);\r
+        result = prime * result + ObjectUtils.hashCode(rvi);\r
+        result = prime * result + ObjectUtils.hashCode(randomAccessResourceId);\r
+        return result;\r
+    }\r
+\r
+    private void updateCaches(RequestProcessor processor, boolean sync) throws DatabaseException {\r
+        ReadRequest req = new ReadRequest() {\r
+            @Override\r
+            public void run(ReadGraph g) throws DatabaseException {\r
+                update(g);\r
+            }\r
+        };\r
+        if (sync) {\r
+            processor.syncRequest(req);\r
+        } else {\r
+            processor.asyncRequest(req);\r
+        }\r
+    }\r
+\r
+    static class Nonexistant extends DatabaseException {\r
+        private static final long serialVersionUID = -7964385375237203651L;\r
+\r
+        @Override\r
+        public synchronized Throwable fillInStackTrace() {\r
+            return this;\r
+        }\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see org.simantics.ui.workbench.IResourceEditorInput#update(org.simantics.db.Graph)\r
+     */\r
+    @Override\r
+    public void update(ReadGraph g) throws DatabaseException {\r
+        Resource r = getResource();\r
+        if (r == null)\r
+            return;\r
+\r
+        if (DEBUG_UPDATE)\r
+            System.out.println("update(" + this + ")");\r
+\r
+        try {\r
+            assertExists(g);\r
+\r
+            name = g.syncRequest(new TitleRequest(editorID, this));\r
+            if (name == null)\r
+                name = NO_NAME;\r
+\r
+            tooltip = g.syncRequest(new ToolTipRequest(editorID, this));\r
+            if (tooltip == null)\r
+                tooltip = NO_NAME;\r
+\r
+            try {\r
+                ImageDescriptorProvider idp = g.adapt(r, ImageDescriptorProvider.class);\r
+                imageDesc = idp.get();\r
+            } catch (AdaptionException e) {\r
+                imageDesc = ImageDescriptor.getMissingImageDescriptor();\r
+            } catch (ProvisionException e) {\r
+                imageDesc = ImageDescriptor.getMissingImageDescriptor();\r
+                ErrorLogger.defaultLogError(e);\r
+            }\r
+\r
+            if (DEBUG_UPDATE)\r
+                System.out.println("update(" + this + ") finished");\r
+        } catch (DatabaseException e) {\r
+            if (DEBUG_UPDATE)\r
+                e.printStackTrace();\r
+            setNonExistant();\r
+        }\r
+    }\r
+\r
+    private void assertExists(ReadGraph g) throws DatabaseException {\r
+        Resource r = getResource();\r
+        if (r == null)\r
+            throw new Nonexistant();\r
+\r
+        // 1. Check resource existence\r
+        boolean exists = g.hasStatement(r);\r
+        if (!exists)\r
+            throw new Nonexistant();\r
+\r
+        Layer0 L0 = Layer0.getInstance(g);\r
+        // 2. Validate modelURI\r
+        if (getModelURI() != null) {\r
+            // 3. Validate RVI\r
+            Resource model = g.getResource(modelURI);\r
+            Resource baseRealization = g.getPossibleObject(model, L0.HasBaseRealization);\r
+            Variable modelVariable = Variables.getVariable(g, g.getURI(baseRealization));\r
+            modelVariable.browse(g, getRVI());\r
+        }\r
+\r
+        // Touch the diagram title calculation within this existence\r
+        // checking request.\r
+        g.syncRequest(new TitleRequest(editorID, this));\r
+    }\r
+\r
+    private void setNonExistant() {\r
+        if (DEBUG_UPDATE)\r
+            System.out.println("setNonExistant(" + this + " @ " + System.identityHashCode(this) + ")");\r
+\r
+        exists = false;\r
+        tooltip = name = NO_NAME;\r
+        imageDesc = ImageDescriptor.getMissingImageDescriptor();\r
+    }\r
+\r
+    public IMemento getPersistentStore() {\r
+        return persistentStore;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return getClass().getSimpleName() + " [name=" + getName() + ", resource=" + resource + "]";\r
+    }\r
+    \r
+\r
+\r
+}\r
index 6c289d6638c4145cbdc42088787a82cebf4f7ce6..27220788ae743ff9ad99c88d2541888af58ac603 100644 (file)
@@ -32,9 +32,9 @@ import org.simantics.modeling.ModelingResources;
 import org.simantics.structural.stubs.StructuralResource2;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.editor.DiagramViewer;\r
+import org.simantics.sysdyn.ui.editor.SysdynEditorInput;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-import org.simantics.ui.workbench.ResourceEditorInput2;\r
 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
 \r
 \r
@@ -90,59 +90,12 @@ public class ShowModuleHandler extends AbstractHandler {
                                                public void run() {\r
                                                        try {\r
                                                                String editorId = EDITOR_ID;\r
-                                                               WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, diagram, modelURI, finalRvi));\r
+                                                               WorkbenchUtils.openEditor(editorId, new SysdynEditorInput(editorId, diagram, modelURI, finalRvi));\r
                                                        } catch (PartInitException e) {\r
                                                                e.printStackTrace();\r
                                                        }\r
                                                }\r
                                        });\r
-                                       \r
-                                       /*\r
-                                       \r
-                                       \r
-                                       final Resource diagram = ComponentUtils.getPossibleCompositeDiagram(graph, configuration);\r
-                                       \r
-                                       if(diagram == null) return;\r
-                                       \r
-                                       String finalRvi = uri + "/" + graph.getPossibleRelatedValue(component, l0.HasName, Bindings.STRING);\r
-                                       \r
-                                       // This is ugly but it has to be done like this because WorkModel example has too PartOfs\r
-                                       Resource rootLibrary = graph.getPossibleObject(model, l0.PartOf);\r
-                                       while(!graph.getObjects(rootLibrary, l0.PartOf).isEmpty()) {\r
-                                               for(Resource possibleLibrary : graph.getObjects(rootLibrary, l0.PartOf)){\r
-                                                       if(graph.isInstanceOf(possibleLibrary, l0.Library)) {\r
-                                                               rootLibrary = possibleLibrary;\r
-                                                               break;\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                                       Variable rootVariable = graph.adapt(rootLibrary, Variable.class);\r
-                                       \r
-                                       String moduleUri = modelURI + "/BaseRealization" + finalRvi;\r
-                                       Variable var = rootVariable.browsePossible(graph, moduleUri.substring(6));\r
-                                       if(var == null) {\r
-                                               finalRvi = "";\r
-                                       }\r
-                                       final String rvi = finalRvi;\r
-                                       \r
-                                       PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {\r
-\r
-                                               @Override\r
-                                               public void run() {\r
-                                                       //                        for (Triple<Resource, String, String> in : ins) {\r
-                                                       try {\r
-                                                               String editorId = EDITOR_ID;\r
-//                                                             System.out.println("Activating diagram: model=" + modelURI + " rvi='" + rvi + "'");\r
-                                                               System.out.println("SHOW MODULE: " + modelURI + ", " + rvi);\r
-                                                               WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, diagram, modelURI, rvi));\r
-                                                       } catch (PartInitException e) {\r
-                                                               // TODO Auto-generated catch block\r
-                                                               e.printStackTrace();\r
-                                                       }\r
-                                                       //                        }\r
-                                               }\r
-                                       });\r
-                                       */\r
                                \r
                                }\r
                                \r