]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
d5a155cff3e490b4e70c81ed01e2bde6552ca45a
[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.editor;\r
13 \r
14 import org.eclipse.ui.PartInitException;\r
15 import org.eclipse.ui.PlatformUI;\r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.common.ResourceArray;\r
19 import org.simantics.db.common.request.ReadRequest;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.modeling.ComponentUtils;\r
22 import org.simantics.modeling.ModelingResources;\r
23 import org.simantics.modeling.ui.Activator;\r
24 import org.simantics.operation.Layer0X;\r
25 import org.simantics.structural.stubs.StructuralResource2;\r
26 import org.simantics.structural2.StructuralVariables;\r
27 import org.simantics.sysdyn.SysdynResource;\r
28 import org.simantics.ui.SimanticsUI;\r
29 import org.simantics.ui.workbench.ResourceEditorInput2;\r
30 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;\r
31 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
32 \r
33 public class OpenDiagramFromConfigurationAdapter extends AbstractResourceEditorAdapter {\r
34 \r
35         private static final String EDITOR_ID = "org.simantics.sysdyn.ui.diagramViewer";\r
36 \r
37     public OpenDiagramFromConfigurationAdapter() {\r
38         super("Diagram Editor", Activator.COMPOSITE_ICON);\r
39     }\r
40 \r
41     protected String getEditorId() {\r
42         return EDITOR_ID;\r
43     }\r
44 \r
45     @Override\r
46     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {\r
47                 SysdynResource sr = SysdynResource.getInstance(g);\r
48                 if(g.isInheritedFrom(r, sr.ModuleSymbol)) {\r
49                         ModelingResources mr = ModelingResources.getInstance(g);\r
50                         StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
51                         Resource componentType = g.getSingleObject(r, mr.SymbolToComponentType);\r
52                         r = g.getSingleObject(componentType, sr2.IsDefinedBy);\r
53                 }\r
54                 Layer0X L0X = Layer0X.getInstance(g);\r
55                 Resource represents = g.getPossibleObject(r, L0X.Represents);\r
56                 if(represents != null){\r
57                         if(g.isInstanceOf(represents, sr.Configuration)) {\r
58                                 r = represents;\r
59                         }\r
60                 }\r
61                 return ComponentUtils.compositeHasDiagram(g, r) /*|| ComponentUtils.componentHasDiagram(g, r)*/;\r
62     }\r
63 \r
64     @Override\r
65     public void openEditor(final Resource r) throws Exception {\r
66 \r
67         SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
68 \r
69             @Override\r
70             public void run(ReadGraph g) throws DatabaseException {\r
71                 Resource cr = r;\r
72                                 Layer0X L0X = Layer0X.getInstance(g);\r
73                                 if(g.isInheritedFrom(r, SysdynResource.getInstance(g).ModuleSymbol)) {\r
74                                         ModelingResources mr = ModelingResources.getInstance(g);\r
75                                         StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
76                                         Resource componentType = g.getSingleObject(r, mr.SymbolToComponentType);\r
77                                         Resource configuration = g.getSingleObject(componentType, sr2.IsDefinedBy);\r
78                                         cr = configuration;\r
79                                 } else {     \r
80                                         Resource represents = g.getPossibleObject(r, L0X.Represents);\r
81                                         if(represents != null && g.isInstanceOf(represents, SysdynResource.getInstance(g).Configuration)){\r
82                                                 cr = represents;\r
83                                         } else {\r
84                                                 cr = r;\r
85                                         }\r
86                                 }\r
87 \r
88                 \r
89                 final Resource diagram = ComponentUtils.getPossibleCompositeDiagram(g, cr);\r
90                 if(diagram == null) return;\r
91                 final ResourceArray compositePath = StructuralVariables.getCompositeArray(g, cr);\r
92                 final ResourceArray variablePath = compositePath.removeFromBeginning(1);\r
93 \r
94                 Resource model = StructuralVariables.getModel(g, compositePath.head());\r
95                 if(model == null) return;\r
96                 final String modelURI = g.getURI(model);\r
97 \r
98                 final String rvi = StructuralVariables.getRVI(g, variablePath);\r
99                 if(rvi == null) return;\r
100 \r
101                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {\r
102                     @Override\r
103                     public void run() {\r
104                         try {\r
105                             String editorId = getEditorId();\r
106 //                            System.out.println("Activating diagram: model=" + modelURI + " rvi='" + rvi + "'");\r
107                             WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, diagram, modelURI, rvi));\r
108                         } catch (PartInitException e) {\r
109                             // TODO Auto-generated catch block\r
110                             e.printStackTrace();\r
111                         }\r
112                     }\r
113                 });\r
114 \r
115             }\r
116 \r
117         });\r
118 \r
119     }\r
120 }\r