package org.simantics.sysdyn.ui.editor;\r
\r
+import org.eclipse.ui.PartInitException;\r
+import org.eclipse.ui.PlatformUI;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.common.request.ReadRequest;\r
import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.request.Read;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.modeling.ComponentUtils;\r
import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.ui.Activator;\r
import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.structural2.StructuralVariables;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.workbench.ResourceEditorInput2;\r
+import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
\r
-\r
-public class OpenDiagramFromConfigurationAdapter extends org.simantics.modeling.ui.diagramEditor.OpenDiagramFromConfigurationAdapter {\r
+public class OpenDiagramFromConfigurationAdapter extends AbstractResourceEditorAdapter {\r
\r
private static final String EDITOR_ID = "org.simantics.sysdyn.ui.diagramViewer";\r
\r
- public OpenDiagramFromConfigurationAdapter() {\r
- super();\r
- }\r
+ public OpenDiagramFromConfigurationAdapter() {\r
+ super("Diagram Editor", Activator.COMPOSITE_ICON);\r
+ }\r
\r
- @Override\r
- protected String getEditorId() {\r
- return EDITOR_ID;\r
- }\r
+ protected String getEditorId() {\r
+ return EDITOR_ID;\r
+ }\r
\r
- @Override\r
- public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {\r
+ @Override\r
+ public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(g);\r
if(g.isInheritedFrom(r, sr.ModuleSymbol)) {\r
ModelingResources mr = ModelingResources.getInstance(g);\r
}\r
}\r
return ComponentUtils.compositeHasDiagram(g, r) /*|| ComponentUtils.componentHasDiagram(g, r)*/;\r
- }\r
-\r
- @Override\r
- public void openEditor(final Resource r) throws Exception {\r
- Resource configuration = null;\r
- try {\r
- configuration = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
-\r
- @Override\r
- public Resource perform(ReadGraph graph) throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- if(graph.isInheritedFrom(r, SysdynResource.getInstance(graph).ModuleSymbol)) {\r
- ModelingResources mr = ModelingResources.getInstance(graph);\r
- StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
- Resource componentType = graph.getSingleObject(r, mr.SymbolToComponentType);\r
- Resource configuration = graph.getSingleObject(componentType, sr2.IsDefinedBy);\r
- return configuration;\r
- } else { \r
- Resource represents = graph.getPossibleObject(r, l0.Represents);\r
- if(represents != null && graph.isInstanceOf(represents, SysdynResource.getInstance(graph).Configuration)){\r
- return represents;\r
- }\r
- else {\r
- return null;\r
- }\r
+ }\r
+\r
+ @Override\r
+ public void openEditor(final Resource r) throws Exception {\r
+\r
+ SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+\r
+ @Override\r
+ public void run(ReadGraph g) throws DatabaseException {\r
+ Resource cr = r;\r
+ Layer0 l0 = Layer0.getInstance(g);\r
+ if(g.isInheritedFrom(r, SysdynResource.getInstance(g).ModuleSymbol)) {\r
+ ModelingResources mr = ModelingResources.getInstance(g);\r
+ StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
+ Resource componentType = g.getSingleObject(r, mr.SymbolToComponentType);\r
+ Resource configuration = g.getSingleObject(componentType, sr2.IsDefinedBy);\r
+ cr = configuration;\r
+ } else { \r
+ Resource represents = g.getPossibleObject(r, l0.Represents);\r
+ if(represents != null && g.isInstanceOf(represents, SysdynResource.getInstance(g).Configuration)){\r
+ cr = represents;\r
+ } else {\r
+ cr = r;\r
}\r
}\r
- });\r
- } catch (DatabaseException e1) {\r
- e1.printStackTrace();\r
- }\r
- if (configuration != null)\r
- super.openEditor(configuration);\r
- else\r
- super.openEditor(r);\r
- }\r
\r
+ \r
+ final Resource diagram = ComponentUtils.getPossibleCompositeDiagram(g, r);\r
+ if(diagram == null) return;\r
+ final ResourceArray compositePath = StructuralVariables.getCompositeArray(g, r);\r
+ final ResourceArray variablePath = compositePath.removeFromBeginning(1);\r
+\r
+ Resource model = StructuralVariables.getModel(g, compositePath.head());\r
+ if(model == null) return;\r
+ final String modelURI = g.getURI(model);\r
+\r
+ final String rvi = StructuralVariables.getRVI(g, variablePath);\r
+ if(rvi == null) return;\r
+\r
+ PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ try {\r
+ String editorId = getEditorId();\r
+// System.out.println("Activating diagram: model=" + modelURI + " rvi='" + rvi + "'");\r
+ WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, diagram, modelURI, rvi));\r
+ } catch (PartInitException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ });\r
+\r
+ }\r
+\r
+ });\r
+\r
+ }\r
}\r