]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/HeadlessStructuralBrowsingHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / HeadlessStructuralBrowsingHandler.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/HeadlessStructuralBrowsingHandler.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/HeadlessStructuralBrowsingHandler.java
new file mode 100644 (file)
index 0000000..ff17e21
--- /dev/null
@@ -0,0 +1,115 @@
+/*******************************************************************************\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.handlers;\r
+\r
+import java.util.Set;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+/**\r
+ * StructuralBrowsingHandler supports visual browsing into subcomponents through\r
+ * mouse events.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class HeadlessStructuralBrowsingHandler extends AbstractCanvasParticipant {\r
+\r
+    @Dependency\r
+    Selection          selection;\r
+    ISessionContext    sessionContext;\r
+    ResourceArray      parentStructuralPath;\r
+    IDiagramUpdateSupport diagramProvider;\r
+    \r
+    public static interface IDiagramUpdateSupport {\r
+       public void updateDiagram(ResourceArray structuralPath);\r
+    }\r
+    \r
+    public HeadlessStructuralBrowsingHandler(IDiagramUpdateSupport diagramProvider, ISessionContext sessionContext, ResourceArray parentStructuralPath) {\r
+        assert diagramProvider != null;\r
+\r
+        this.diagramProvider = diagramProvider;\r
+        this.sessionContext = sessionContext;\r
+        this.parentStructuralPath = parentStructuralPath;\r
+    }\r
+\r
+    @EventHandler(priority = 0)\r
+    public boolean handleDoubleClick(MouseDoubleClickedEvent me) {\r
+        if (sessionContext == null)\r
+            return false;\r
+\r
+        Set<IElement> sel = selection.getSelection(0);\r
+\r
+        if (sel.size() == 1) {\r
+            IElement e = sel.iterator().next();\r
+            Object data = e.getHint(ElementHints.KEY_OBJECT);\r
+            if (data instanceof Resource) {\r
+                final Resource element = (Resource) data;\r
+                sessionContext.getSession().asyncRequest(new ReadRequest() {\r
+                    @Override\r
+                    public void run(ReadGraph graph) throws DatabaseException {\r
+                        ModelingResources mr = ModelingResources.getInstance(graph);\r
+                        DiagramResource dr = DiagramResource.getInstance(graph);\r
+                        StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
+\r
+                        if (graph.isInstanceOf(element, dr.Flag)) {\r
+                            /* FIXME: implement headless version\r
+                            ContextMap parameters = new ContextMap();\r
+                            parameters.put(ModelingOperationConstants.WORKBENCH_WINDOW, site.getWorkbenchWindow());\r
+                            parameters.put(ModelingOperationConstants.WORKBENCH_PART, site.getPart());\r
+                            parameters.put(IOperation.SUBJECT, element);\r
+                            new NavigateToTarget().exec(graph.getSession(), parameters);\r
+                            */\r
+                            return;\r
+                        }\r
+\r
+                        final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);\r
+                        if (component == null)\r
+                            return;\r
+\r
+                        Resource type = graph.getSingleType(component, sr.Component);\r
+                        if (type == null)\r
+                            return;\r
+\r
+                        Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);\r
+                        if (definedBy == null)\r
+                            return;\r
+\r
+                        final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);\r
+                        if (diagram == null)\r
+                            return;\r
+\r
+                        diagramProvider.updateDiagram(parentStructuralPath.prepended(diagram, component));\r
+                    }\r
+                });\r
+            }\r
+\r
+            return true;\r
+        }\r
+\r
+        return false;\r
+    }\r
+\r
+}\r