]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
4447c94076d3d98268de110d2240b8c544a6cdde
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.actions;\r
13 \r
14 import java.util.Set;\r
15 \r
16 import org.eclipse.jface.viewers.IStructuredSelection;\r
17 import org.eclipse.ui.PartInitException;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.db.layer0.variable.Variable;\r
22 import org.simantics.db.layer0.variable.Variables;\r
23 import org.simantics.layer0.Layer0;\r
24 import org.simantics.modeling.ModelingResources;\r
25 import org.simantics.structural.stubs.StructuralResource2;\r
26 import org.simantics.sysdyn.SysdynResource;\r
27 import org.simantics.sysdyn.ui.browser.BrowserSelection;\r
28 import org.simantics.sysdyn.ui.browser.nodes.ModuleNode;\r
29 import org.simantics.sysdyn.ui.editor.SysdynEditorInput;\r
30 import org.simantics.ui.DoubleClickEvent;\r
31 import org.simantics.ui.IDoubleClickAction;\r
32 import org.simantics.ui.workbench.ResourceEditorInput2;\r
33 import org.simantics.utils.ui.AdaptionUtils;\r
34 import org.simantics.utils.ui.ISelectionUtils;\r
35 import org.simantics.utils.ui.action.PriorityAction;\r
36 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
37 \r
38 /**\r
39  * Open an editor for a module instance. \r
40  * @author Teemu Lempinen\r
41  *\r
42  */\r
43 public class ShowInstantiatedModuleAction implements IDoubleClickAction {\r
44 \r
45     private static final String EDITOR_ID = "org.simantics.sysdyn.ui.diagramViewer";\r
46     \r
47         @Override\r
48         public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {\r
49                 \r
50         ReadGraph g = e.getGraph();\r
51         Layer0 l0 = Layer0.getInstance(g);\r
52         StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
53 \r
54         // Open instance for a module instance, if the resource is adaptable to variable\r
55         Variable variable = AdaptionUtils.adaptToSingle(e.getResource(), Variable.class);\r
56         if(variable != null) {\r
57             Resource represents = variable.getRepresents(g);\r
58             Resource instanceOf = g.getPossibleObject(represents, l0.InstanceOf);\r
59             Resource configuration = g.getPossibleObject(instanceOf, sr2.IsDefinedBy);\r
60             if(configuration != null) {\r
61                 String rvi = Variables.getRVI(g, variable);\r
62                 if(!rvi.isEmpty())\r
63                     rvi = rvi.substring(1);\r
64                 Resource model = Variables.getModel(g, variable);\r
65                 addShowModuleAction(e, getResourceEditorInput(g, model, configuration, rvi));\r
66                 return;\r
67             }\r
68         }\r
69         \r
70         if(e.getResource() instanceof IStructuredSelection) {\r
71                         Set<BrowserSelection> bss = ISelectionUtils.filterSetSelection(e.getResource(), BrowserSelection.class);\r
72                         \r
73                         if(!bss.isEmpty()) {\r
74                          // Find varaible from BrowserSelection. (Will be replaced with the Variable adaption above)\r
75                                 for(BrowserSelection bs : bss) {\r
76                                         Resource configuration = (Resource)bs.getAdapter(Resource.class);\r
77                                         Resource instanceOf = g.getSingleObject(configuration, l0.InstanceOf);\r
78                                         \r
79                                         SysdynResource sr = SysdynResource.getInstance(g);\r
80                                         if(g.isInheritedFrom(instanceOf, sr.Module)) {\r
81                                                 configuration = g.getSingleObject(instanceOf, sr2.IsDefinedBy);\r
82                                                 variable = (Variable) bs.getAdapter(Variable.class);\r
83                                                 String rvi = Variables.getRVI(g, variable).substring(1);\r
84                                                 Resource model = Variables.getModel(g, variable);\r
85                                                 addShowModuleAction(e, getResourceEditorInput(g, model, configuration, rvi));\r
86                                         } \r
87                                 }\r
88                         } else {\r
89                                 ModuleNode moduleNode = ISelectionUtils.filterSingleSelection(e.getResource(), ModuleNode.class);\r
90                                 if(moduleNode != null) {\r
91                                         Resource model;\r
92                                         String rvi;\r
93                                         if(moduleNode.getVariable() != null) {\r
94                                                 model = Variables.getModel(g, moduleNode.getVariable());\r
95                                                 Variable var = moduleNode.getVariable();\r
96                                                 rvi = Variables.getRVI(g, var).substring(1);\r
97                                         } else {\r
98                                                 rvi = (String)g.getRelatedValue(moduleNode.data, Layer0.getInstance(g).HasName);\r
99                                                 model = g.getSingleObject(moduleNode.data, l0.PartOf);\r
100                                         }\r
101                                         \r
102                                 Resource instanceOf = g.getSingleObject(moduleNode.data, l0.InstanceOf);\r
103                                         Resource conf = g.getSingleObject(instanceOf, sr2.IsDefinedBy);\r
104                                     \r
105                                         addShowModuleAction(e, getResourceEditorInput(g, model, conf, rvi));\r
106                         }\r
107                         }\r
108         } \r
109         }\r
110         \r
111         private ResourceEditorInput2 getResourceEditorInput(ReadGraph g, Resource model, Resource configuration, String rvi) throws DatabaseException {\r
112                 Layer0 l0 = Layer0.getInstance(g);\r
113                 SysdynResource sr = SysdynResource.getInstance(g);\r
114                 \r
115                 String modelURI;\r
116                 if(model != null) {\r
117                         modelURI = g.getURI(model);\r
118                 } else {\r
119                         Resource parent = configuration;\r
120                         do {\r
121                                 parent = g.getPossibleObject(parent, l0.PartOf);\r
122                         } while (parent != null && !g.isInstanceOf(parent, sr.SysdynModel));\r
123                         modelURI = g.getURI(parent);\r
124                         rvi = null;\r
125                 }\r
126                 \r
127                 Resource diagram = g.getSingleObject(configuration, ModelingResources.getInstance(g).CompositeToDiagram);\r
128                 return new SysdynEditorInput(EDITOR_ID, diagram, modelURI, rvi == null ? "" : "/" + rvi);\r
129         }\r
130         \r
131         private void addShowModuleAction(DoubleClickEvent e, final ResourceEditorInput2 editorInput) {\r
132         e.add(new PriorityAction(PriorityAction.HIGH) {\r
133             @Override\r
134             public void run() {\r
135                 try {\r
136                                         WorkbenchUtils.openEditor(EDITOR_ID, editorInput);\r
137                                 } catch (PartInitException e) {\r
138                                         e.printStackTrace();\r
139                                 }\r
140             }\r
141         });\r
142                 e.consume();\r
143         }\r
144 \r
145 }\r