]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromIssue.java
Open diagram from issue
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / OpenDiagramFromIssue.java
index f2de884bcb504143787ca689e75ecd97961d611e..7fd13bbdddcbb023573651eee62b30bb9eddaa14 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.diagramEditor;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.common.utils.ListUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.diagram.content.ConnectionUtil;\r
-import org.simantics.diagram.flag.FlagUtil;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.issues.ontology.IssueResource;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.modeling.ui.Activator;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;\r
-import org.simantics.utils.datastructures.Pair;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class OpenDiagramFromIssue extends AbstractResourceEditorAdapter {\r
-\r
-    private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor";\r
-\r
-    public OpenDiagramFromIssue() {\r
-        super("Open Diagram Containing Referenced Element", Activator.COMPOSITE_ICON);\r
-    }\r
-\r
-    protected String getEditorId() {\r
-        return EDITOR_ID;\r
-    }\r
-\r
-    @Override\r
-    public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {\r
-        return findElement(g, r) != null;\r
-    }\r
-\r
-    @Override\r
-    public void openEditor(final Resource r) throws Exception {\r
-        SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
-            @Override\r
-            public void run(ReadGraph g) throws DatabaseException {\r
-               \r
-               Layer0 L0 = Layer0.getInstance(g);\r
-               ModelingResources MOD = ModelingResources.getInstance(g);\r
-               \r
-               Resource element = findElement(g, r);\r
-               if(element == null) return;\r
-               \r
-               System.err.println("element " + g.getURI(element));\r
-               \r
-               Resource diagram = g.getSingleObject(element, L0.PartOf);\r
-               Resource composite = g.getSingleObject(diagram, MOD.DiagramToComposite);\r
-                               \r
-                OpenDiagramFromConfigurationAdapter.openEditor(g, composite, getEditorId(), Collections.<Object>singleton(element));\r
-                \r
-            }\r
-        });\r
-    }\r
-\r
-    private static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException {\r
-\r
-       Layer0 L0 = Layer0.getInstance(graph);\r
-       ModelingResources MOD = ModelingResources.getInstance(graph);\r
-\r
-       Resource diagram = graph.getPossibleObject(element, L0.PartOf);\r
-       if (diagram == null) return null;\r
-\r
-       return graph.getPossibleObject(diagram, MOD.DiagramToComposite);\r
-       \r
-    }\r
-    \r
-    private static Resource findElement(ReadGraph graph, Resource issue) throws DatabaseException {\r
-       \r
-       IssueResource ISSUE = IssueResource.getInstance(graph);\r
-       DiagramResource DIA = DiagramResource.getInstance(graph);\r
-       \r
-       if(!graph.isInstanceOf(issue, ISSUE.Issue)) return null;\r
-       \r
-       Resource list = graph.getSingleObject(issue, ISSUE.Issue_HasContexts);\r
-       for(Resource context : ListUtils.toList(graph, list)) {\r
-               if(graph.isInstanceOf(context, DIA.Element) && !graph.isInstanceOf(context, DIA.Diagram)) {\r
-                       return context;\r
-               }\r
-               \r
-       }\r
-       \r
-       return null;\r
-       \r
-    }\r
-\r
-    public static Collection<Object> findElementObjects(ReadGraph g, Resource module) throws DatabaseException {\r
-        DiagramResource DIA = DiagramResource.getInstance(g);\r
-        ModelingResources MOD = ModelingResources.getInstance(g);\r
-        final Collection<Object> selectedObjects = new ArrayList<Object>();\r
-        for (Resource element : g.getObjects(module, MOD.ComponentToElement)) {\r
-            if (g.isInstanceOf(element, DIA.Flag) && FlagUtil.isExternal(g, element)) {\r
-                // Use external flag primarily if one exists in the correspondences\r
-                selectedObjects.clear();\r
-                selectedObjects.add(element);\r
-                break;\r
-            } else if (g.isInstanceOf(element, DIA.RouteGraphConnection)) {\r
-                selectedObjects.add(element);\r
-            } else if (g.isInstanceOf(element, DIA.Connection)) {\r
-                // Okay, we need to find a part of the connection\r
-                ConnectionUtil cu = new ConnectionUtil(g);\r
-                cu.gatherConnectionParts(element, selectedObjects);\r
-            } else {\r
-                selectedObjects.add(element);\r
-            }\r
-        }\r
-        for(Resource element : g.getObjects(module, MOD.HasParentComponent_Inverse)) {\r
-               selectedObjects.add(element);\r
-        }\r
-        return selectedObjects;\r
-    }    \r
-    \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.diagramEditor;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.diagram.content.ConnectionUtil;
+import org.simantics.diagram.flag.FlagUtil;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.issues.ontology.IssueResource;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.modeling.ui.Activator;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.ui.selection.AnyResource;
+import org.simantics.ui.selection.AnyVariable;
+import org.simantics.ui.selection.WorkbenchSelectionContentType;
+import org.simantics.ui.selection.WorkbenchSelectionElement;
+import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
+import org.simantics.utils.datastructures.Pair;
+import org.simantics.utils.ui.ISelectionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class OpenDiagramFromIssue extends AbstractResourceEditorAdapter {
+
+    private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor";
+
+    public OpenDiagramFromIssue() {
+        super("Open Diagram Containing Referenced Component", Activator.COMPOSITE_ICON);
+    }
+
+    protected String getEditorId() {
+        return EDITOR_ID;
+    }
+
+    @Override
+    public boolean canHandle(ReadGraph g, Object input) throws DatabaseException {
+        return extractContext(g, input) != null;
+    }
+
+    @Override
+    public void openEditor(Object input) throws Exception {
+        Simantics.getSession().asyncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph g) throws DatabaseException {
+                Pair<Resource, Collection<Object>> data = extractContext(g, input);
+                if (data != null)
+                    OpenDiagramFromConfigurationAdapter.openEditor(g, data.first, getEditorId(), data.second);
+            }
+        });
+    }
+
+    private static Pair<Resource, Collection<Object>> extractContext(ReadGraph graph, Object input) throws DatabaseException {
+        Pair<Variable, Resource> p = extractInput(graph, input);
+        Pair<Resource, Collection<Object>> result = p.first != null ? findConfigurationAndObjects(graph, p.first) : null;
+        if (result == null && p.second != null)
+            result = findConfigurationAndObjects(graph, p.second);
+        return result;
+    }
+
+    protected static <T> T extractContent(ReadGraph graph, Object input, WorkbenchSelectionContentType<T> contentType, Class<T> contentClass) throws DatabaseException {
+        if (contentClass.isInstance(input))
+            return contentClass.cast(input);
+        if (input instanceof WorkbenchSelectionElement) {
+            WorkbenchSelectionElement single = (WorkbenchSelectionElement) input;
+            if (single != null)
+                return single.getContent(contentType);
+        }
+        return ISelectionUtils.filterSingleSelection(input, contentClass);
+    }
+
+    protected static Pair<Variable, Resource> extractInput(ReadGraph graph, Object input) throws DatabaseException {
+        return Pair.make(
+                extractContent(graph, input, new AnyVariable(graph), Variable.class),
+                extractContent(graph, input, new AnyResource(graph), Resource.class));
+    }
+
+    protected static Pair<Resource, Collection<Object>> findConfigurationAndObjects(ReadGraph graph, Resource issue) throws DatabaseException {
+        IssueResource ISSUE = IssueResource.getInstance(graph);
+        if (!graph.isInstanceOf(issue, ISSUE.Issue))
+            return null;
+        List<Resource> contexts = graph.getRelatedValue2(issue, ISSUE.Issue_contexts);
+        return contexts != null ? findConfigurationAndObjects(graph, contexts) : null;
+    }
+
+    protected static Pair<Resource, Collection<Object>> findConfigurationAndObjects(ReadGraph graph, Variable v) throws DatabaseException {
+        List<Resource> contexts = v.getPossiblePropertyValue(graph, IssueResource.getInstance(graph).Issue_contexts);
+        return contexts != null ? findConfigurationAndObjects(graph, contexts) : null;
+    }
+
+    protected static Pair<Resource, Collection<Object>> findConfigurationAndObjects(ReadGraph graph, List<Resource> contexts) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+        ModelingResources MOD = ModelingResources.getInstance(graph);
+
+        for(Resource context : contexts) {
+            Set<Resource> types = graph.getTypes(context);
+            if (types.contains(DIA.Element)) {
+                Resource config = findConfigurationForElement(graph, context);
+                if (config != null) {
+                    Collection<Object> elements = Collections.<Object>singleton(context);
+                    return Pair.make(config, elements);
+                }
+            } else if (types.contains(STR.Component) && !types.contains(STR.Composite)) {
+                Resource config = graph.getPossibleObject(context, L0.PartOf);
+                if (config != null) {
+                    return Pair.make(config, findElementObjects(graph, context));
+                }
+            } else if (types.contains(STR.Connection)) {
+                Resource element = graph.getPossibleObject(context, MOD.ConnectionToDiagramConnection);
+                if (element != null) {
+                    Resource config = findConfigurationForElement(graph, element);
+                    if (config != null) {
+                        return Pair.make(config, Collections.<Object>singleton(element));
+                    }
+                }
+            }
+        }
+
+        return null;
+    }
+
+    protected static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        ModelingResources MOD = ModelingResources.getInstance(graph);
+        Resource diagram = graph.getPossibleObject(element, L0.PartOf);
+        if (diagram == null) return null;
+        return graph.getPossibleObject(diagram, MOD.DiagramToComposite);
+    }
+
+    protected static Collection<Object> findElementObjects(ReadGraph g, Resource component) throws DatabaseException {
+        Collection<Object> result = findElementObjects(g, component, "");
+        ModelingResources MOD = ModelingResources.getInstance(g);
+        for (Resource element : g.getObjects(component, MOD.HasParentComponent_Inverse))
+            result.add(element);
+        return result;
+    }
+    
+    public static Collection<Object> findElementObjects(ReadGraph g, Resource component, String rviFromComponent) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(g);
+        DiagramResource DIA = DiagramResource.getInstance(g);
+        ModelingResources MOD = ModelingResources.getInstance(g);
+        final Collection<Object> selectedObjects = new ArrayList<>(4);
+        if (rviFromComponent.isEmpty()) {
+            // The selected objects are configuration objects
+            for (Resource element : g.getObjects(component, MOD.ComponentToElement)) {
+                if (g.isInstanceOf(element, DIA.Flag) && FlagUtil.isExternal(g, element)) {
+                    // Use external flag primarily if one exists in the correspondences
+                    selectedObjects.clear();
+                    selectedObjects.add(element);
+                    break;
+                } else if (g.isInstanceOf(element, DIA.RouteGraphConnection)) {
+                    selectedObjects.add(element);
+                } else if (g.isInstanceOf(element, DIA.Connection)) {
+                    // Okay, we need to find a part of the connection
+                    ConnectionUtil cu = new ConnectionUtil(g);
+                    cu.gatherConnectionParts(element, selectedObjects);
+                } else {
+                    selectedObjects.add(element);
+                }
+            }
+        } else {
+            // The selected objects are generated components
+            for (Resource refElement : g.getObjects(component, MOD.HasParentComponent_Inverse)) {
+                Resource relation = g.getPossibleObject(refElement, MOD.HasReferenceRelation);
+                if (relation != null) {
+                    String suffix = g.getPossibleRelatedValue(relation, L0.HasName, Bindings.STRING);
+                    if (suffix != null) {
+                        if (rviFromComponent.equals(suffix)) {
+                            selectedObjects.add(refElement);
+                        }
+                    }
+                }
+            }
+        }
+        return selectedObjects;
+    }
+    
+}