]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/StructuralBrowsingHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / StructuralBrowsingHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2014 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  *     Semantum Oy - support SHIFT+left double click browsing outside of model configuration\r
12  *******************************************************************************/\r
13 package org.simantics.modeling.ui.diagramEditor.handlers;\r
14 \r
15 import java.util.Set;\r
16 \r
17 import org.eclipse.core.runtime.IStatus;\r
18 import org.eclipse.core.runtime.Status;\r
19 import org.eclipse.ui.IWorkbenchPartSite;\r
20 import org.eclipse.ui.PartInitException;\r
21 import org.eclipse.ui.PlatformUI;\r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.common.request.PossibleIndexRoot;\r
25 import org.simantics.db.common.request.UniqueRead;\r
26 import org.simantics.db.exception.DatabaseException;\r
27 import org.simantics.db.layer0.variable.RVI;\r
28 import org.simantics.db.layer0.variable.Variable;\r
29 import org.simantics.db.layer0.variable.Variables;\r
30 import org.simantics.db.management.ISessionContext;\r
31 import org.simantics.diagram.flag.FlagUtil;\r
32 import org.simantics.diagram.stubs.DiagramResource;\r
33 import org.simantics.diagram.ui.DiagramModelHints;\r
34 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
35 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
36 import org.simantics.g2d.diagram.participant.Selection;\r
37 import org.simantics.g2d.element.ElementHints;\r
38 import org.simantics.g2d.element.IElement;\r
39 import org.simantics.layer0.utils.operations.IOperation;\r
40 import org.simantics.modeling.ModelingOperationConstants;\r
41 import org.simantics.modeling.ModelingResources;\r
42 import org.simantics.modeling.actions.NavigateToTarget;\r
43 import org.simantics.modeling.ui.Activator;\r
44 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
45 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
46 import org.simantics.structural.stubs.StructuralResource2;\r
47 import org.simantics.ui.workbench.IResourceEditorInput2;\r
48 import org.simantics.ui.workbench.ResourceEditorInput2;\r
49 import org.simantics.utils.datastructures.persistent.ContextMap;\r
50 import org.simantics.utils.ui.ErrorLogger;\r
51 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
52 \r
53 /**\r
54  * StructuralBrowsingHandler supports visual browsing into subcomponents through\r
55  * mouse events.\r
56  * \r
57  * @author Tuukka Lehtonen\r
58  */\r
59 public class StructuralBrowsingHandler extends AbstractDiagramParticipant {\r
60 \r
61     @Dependency\r
62     Selection             selection;\r
63     IWorkbenchPartSite    site;\r
64     ISessionContext       sessionContext;\r
65     IResourceEditorInput2 input;\r
66 \r
67     public StructuralBrowsingHandler(IWorkbenchPartSite site, ISessionContext sessionContext, IResourceEditorInput2 input) {\r
68         assert site != null;\r
69 \r
70         this.site = site;\r
71         this.sessionContext = sessionContext;\r
72         this.input = input;\r
73     }\r
74 \r
75     @EventHandler(priority = 0)\r
76     public boolean handleDoubleClick(MouseDoubleClickedEvent me) {\r
77         if (sessionContext == null || diagram == null)\r
78             return false;\r
79         \r
80         Set<IElement> sel = selection.getSelection(0);\r
81         if (sel.size() == 1) {\r
82             IElement e = sel.iterator().next();\r
83             Object data = e.getHint(ElementHints.KEY_OBJECT);\r
84             final Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
85             if (data instanceof Resource) {\r
86                 final Resource element = (Resource) data;\r
87                 final int stateMask = me.stateMask;\r
88                 try {\r
89                     return sessionContext.getSession().syncRequest(new UniqueRead<Boolean>() {\r
90                         @Override\r
91                         public Boolean perform(ReadGraph graph) throws DatabaseException {\r
92                             return browse(graph, element, runtime, stateMask);\r
93                         }\r
94                     });\r
95                 } catch (DatabaseException e1) {\r
96                     ErrorLogger.defaultLogError(e1);\r
97                 }\r
98             }\r
99         }\r
100 \r
101         return false;\r
102     }\r
103     \r
104     private boolean browse(ReadGraph graph, Resource element, Resource runtimeDiagram, int stateMask) throws DatabaseException {\r
105         DiagramResource DIA = DiagramResource.getInstance(graph);\r
106         ModelingResources mr = ModelingResources.getInstance(graph);\r
107         DiagramResource dr = DiagramResource.getInstance(graph);\r
108         StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
109 \r
110         if (graph.isInstanceOf(element, dr.Flag)) {\r
111             ContextMap parameters = new ContextMap();\r
112             parameters.put(ModelingOperationConstants.WORKBENCH_WINDOW, site.getWorkbenchWindow());\r
113             parameters.put(ModelingOperationConstants.WORKBENCH_PART, site.getPart());\r
114             parameters.put(IOperation.SUBJECT, element);\r
115             new NavigateToTarget().exec(graph.getSession(), parameters);\r
116             // Return true only if the flag is joined somewhere and navigation is possible.\r
117             return FlagUtil.isJoined(graph, element);\r
118         }\r
119 \r
120         if((stateMask & MouseDoubleClickedEvent.SHIFT_MASK) == 0)\r
121             return false;\r
122 \r
123         if(runtimeDiagram == null || !graph.isInstanceOf(runtimeDiagram, DIA.RuntimeDiagram))\r
124             return false;\r
125 \r
126         final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);\r
127         if (component == null)\r
128             return false;\r
129 \r
130         Resource type = graph.getPossibleType(component, sr.Component);\r
131         if (type == null)\r
132             return false;\r
133         if(graph.hasStatement(type, sr.ComponentType_Locked))\r
134             return false;\r
135 \r
136         Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);\r
137         if (definedBy == null)\r
138             return false;\r
139 \r
140         final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);\r
141         if (diagram == null)\r
142             return false;\r
143 \r
144         Resource model = input.getModel(graph);\r
145         RVI subprocessRVI = null;\r
146 \r
147         String rvis = (String) graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasRVI);\r
148         if (rvis != null) {\r
149             RVI rvi = RVI.fromResourceFormat(graph, rvis);\r
150             Variable base = Variables.getConfigurationContext(graph, model);\r
151             Variable current = rvi.resolve(graph, base);\r
152             Variable subprocess = current.browse(graph, component);\r
153             subprocessRVI = subprocess.getRVI(graph);\r
154         } else {\r
155             // Not in a model configuration context.\r
156             // Probably in a component type configuration.\r
157             // Use the index root of the diagram to be opened as model[0]\r
158             // and leave subprocessRVI[0] as null.\r
159             model = graph.syncRequest(new PossibleIndexRoot(definedBy));\r
160         }\r
161 \r
162         PlatformUI.getWorkbench().getDisplay().asyncExec(navigateToSubstructure(diagram, model, subprocessRVI));\r
163         return true;\r
164     }\r
165 \r
166     private Runnable navigateToSubstructure(final Resource diagram, final Resource model, final RVI subprocessRVI) {\r
167         return new Runnable() {\r
168             @Override\r
169             public void run() {\r
170                 try {\r
171                     WorkbenchUtils.openEditor(site.getId(), new ResourceEditorInput2(site.getId(), diagram, model, subprocessRVI));\r
172                 } catch (PartInitException e) {\r
173                     Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to open editor, see exception for details.", e));\r
174                 }\r
175             }\r
176         };\r
177     }\r
178 \r
179 }\r