]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.diagramEditor.handlers;\r
13 \r
14 import java.util.Set;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.common.ResourceArray;\r
18 import org.simantics.db.common.request.ReadRequest;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.db.management.ISessionContext;\r
21 import org.simantics.diagram.stubs.DiagramResource;\r
22 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
23 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
24 import org.simantics.g2d.diagram.participant.Selection;\r
25 import org.simantics.g2d.element.ElementHints;\r
26 import org.simantics.g2d.element.IElement;\r
27 import org.simantics.modeling.ModelingResources;\r
28 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
29 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
30 import org.simantics.structural.stubs.StructuralResource2;\r
31 \r
32 /**\r
33  * StructuralBrowsingHandler supports visual browsing into subcomponents through\r
34  * mouse events.\r
35  * \r
36  * @author Tuukka Lehtonen\r
37  */\r
38 public class HeadlessStructuralBrowsingHandler extends AbstractCanvasParticipant {\r
39 \r
40     @Dependency\r
41     Selection          selection;\r
42     ISessionContext    sessionContext;\r
43     ResourceArray      parentStructuralPath;\r
44     IDiagramUpdateSupport diagramProvider;\r
45     \r
46     public static interface IDiagramUpdateSupport {\r
47         public void updateDiagram(ResourceArray structuralPath);\r
48     }\r
49     \r
50     public HeadlessStructuralBrowsingHandler(IDiagramUpdateSupport diagramProvider, ISessionContext sessionContext, ResourceArray parentStructuralPath) {\r
51         assert diagramProvider != null;\r
52 \r
53         this.diagramProvider = diagramProvider;\r
54         this.sessionContext = sessionContext;\r
55         this.parentStructuralPath = parentStructuralPath;\r
56     }\r
57 \r
58     @EventHandler(priority = 0)\r
59     public boolean handleDoubleClick(MouseDoubleClickedEvent me) {\r
60         if (sessionContext == null)\r
61             return false;\r
62 \r
63         Set<IElement> sel = selection.getSelection(0);\r
64 \r
65         if (sel.size() == 1) {\r
66             IElement e = sel.iterator().next();\r
67             Object data = e.getHint(ElementHints.KEY_OBJECT);\r
68             if (data instanceof Resource) {\r
69                 final Resource element = (Resource) data;\r
70                 sessionContext.getSession().asyncRequest(new ReadRequest() {\r
71                     @Override\r
72                     public void run(ReadGraph graph) throws DatabaseException {\r
73                         ModelingResources mr = ModelingResources.getInstance(graph);\r
74                         DiagramResource dr = DiagramResource.getInstance(graph);\r
75                         StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
76 \r
77                         if (graph.isInstanceOf(element, dr.Flag)) {\r
78                             /* FIXME: implement headless version\r
79                             ContextMap parameters = new ContextMap();\r
80                             parameters.put(ModelingOperationConstants.WORKBENCH_WINDOW, site.getWorkbenchWindow());\r
81                             parameters.put(ModelingOperationConstants.WORKBENCH_PART, site.getPart());\r
82                             parameters.put(IOperation.SUBJECT, element);\r
83                             new NavigateToTarget().exec(graph.getSession(), parameters);\r
84                             */\r
85                             return;\r
86                         }\r
87 \r
88                         final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);\r
89                         if (component == null)\r
90                             return;\r
91 \r
92                         Resource type = graph.getSingleType(component, sr.Component);\r
93                         if (type == null)\r
94                             return;\r
95 \r
96                         Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);\r
97                         if (definedBy == null)\r
98                             return;\r
99 \r
100                         final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);\r
101                         if (diagram == null)\r
102                             return;\r
103 \r
104                         diagramProvider.updateDiagram(parentStructuralPath.prepended(diagram, component));\r
105                     }\r
106                 });\r
107             }\r
108 \r
109             return true;\r
110         }\r
111 \r
112         return false;\r
113     }\r
114 \r
115 }\r