]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
9816a2424d4d877a839fd597a405f940e6d296e6
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 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.ISelectionUtils;\r
34 import org.simantics.utils.ui.action.PriorityAction;\r
35 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
36 \r
37 public class ShowInstantiatedModuleAction implements IDoubleClickAction {\r
38 \r
39     private static final String EDITOR_ID = "org.simantics.sysdyn.ui.diagramViewer";\r
40     \r
41         @Override\r
42         public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {\r
43                 \r
44         ReadGraph g = e.getGraph();\r
45         \r
46         if(e.getResource() instanceof IStructuredSelection) {\r
47                         Set<BrowserSelection> bss = ISelectionUtils.filterSetSelection(e.getResource(), BrowserSelection.class);\r
48                         \r
49                         if(!bss.isEmpty()) {\r
50                                 Layer0 l0 = Layer0.getInstance(g);\r
51                                 for(BrowserSelection bs : bss) {\r
52                                         Resource configuration = (Resource)bs.getAdapter(Resource.class);\r
53                                         Resource instanceOf = g.getSingleObject(configuration, l0.InstanceOf);\r
54                                         \r
55                                         SysdynResource sr = SysdynResource.getInstance(g);\r
56                                         StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
57                                         if(g.isInheritedFrom(instanceOf, sr.Module)) {\r
58                                                 configuration = g.getSingleObject(instanceOf, sr2.IsDefinedBy);\r
59                                                 Variable variable = (Variable) bs.getAdapter(Variable.class);\r
60                                                 String rvi = Variables.getRVI(g, variable).substring(1);\r
61                                                 Resource model = Variables.getModel(g, variable);\r
62                                                 addShowModuleAction(e, getResourceEditorInput(g, model, configuration, rvi));\r
63                                         } \r
64                                 }\r
65                         } else {\r
66                                 ModuleNode moduleNode = ISelectionUtils.filterSingleSelection(e.getResource(), ModuleNode.class);\r
67                                 if(moduleNode != null) {\r
68                                         Layer0 l0 = Layer0.getInstance(g);\r
69                                         StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
70                                         Resource model;\r
71                                         String rvi;\r
72                                         if(moduleNode.getVariable() != null) {\r
73                                                 model = Variables.getModel(g, moduleNode.getVariable());\r
74                                                 Variable var = moduleNode.getVariable();\r
75                                                 rvi = Variables.getRVI(g, var).substring(1);\r
76                                         } else {\r
77                                                 rvi = (String)g.getRelatedValue(moduleNode.data, Layer0.getInstance(g).HasName);\r
78                                                 model = g.getSingleObject(moduleNode.data, l0.PartOf);\r
79                                         }\r
80                                         \r
81                                 Resource instanceOf = g.getSingleObject(moduleNode.data, l0.InstanceOf);\r
82                                         Resource conf = g.getSingleObject(instanceOf, sr2.IsDefinedBy);\r
83                                     \r
84                                         addShowModuleAction(e, getResourceEditorInput(g, model, conf, rvi));\r
85                         }\r
86                         }\r
87         } \r
88         }\r
89         \r
90         private ResourceEditorInput2 getResourceEditorInput(ReadGraph g, Resource model, Resource configuration, String rvi) throws DatabaseException {\r
91                 Layer0 l0 = Layer0.getInstance(g);\r
92                 SysdynResource sr = SysdynResource.getInstance(g);\r
93                 \r
94                 String modelURI;\r
95                 if(model != null) {\r
96                         modelURI = g.getURI(model);\r
97                 } else {\r
98                         Resource parent = configuration;\r
99                         do {\r
100                                 parent = g.getPossibleObject(parent, l0.PartOf);\r
101                         } while (parent != null && !g.isInstanceOf(parent, sr.SysdynModel));\r
102                         modelURI = g.getURI(parent);\r
103                         rvi = null;\r
104                 }\r
105                 \r
106                 Resource diagram = g.getSingleObject(configuration, ModelingResources.getInstance(g).CompositeToDiagram);\r
107                 return new SysdynEditorInput(EDITOR_ID, diagram, modelURI, rvi == null ? "" : "/" + rvi);\r
108         }\r
109         \r
110         private void addShowModuleAction(DoubleClickEvent e, final ResourceEditorInput2 editorInput) {\r
111         e.add(new PriorityAction(PriorityAction.HIGH) {\r
112             @Override\r
113             public void run() {\r
114                 try {\r
115                                         WorkbenchUtils.openEditor(EDITOR_ID, editorInput);\r
116                                 } catch (PartInitException e) {\r
117                                         e.printStackTrace();\r
118                                 }\r
119             }\r
120         });\r
121                 e.consume();\r
122         }\r
123 \r
124 }\r