1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.editor;
\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
33 public class OpenDiagramFromConfigurationAdapter extends AbstractResourceEditorAdapter {
\r
35 private static final String EDITOR_ID = "org.simantics.sysdyn.ui.diagramViewer";
\r
37 public OpenDiagramFromConfigurationAdapter() {
\r
38 super("Diagram Editor", Activator.COMPOSITE_ICON);
\r
41 protected String getEditorId() {
\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
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
61 return ComponentUtils.compositeHasDiagram(g, r) /*|| ComponentUtils.componentHasDiagram(g, r)*/;
\r
65 public void openEditor(final Resource r) throws Exception {
\r
67 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
70 public void run(ReadGraph g) throws DatabaseException {
\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
80 Resource represents = g.getPossibleObject(r, L0X.Represents);
\r
81 if(represents != null && g.isInstanceOf(represents, SysdynResource.getInstance(g).Configuration)){
\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
94 Resource model = StructuralVariables.getModel(g, compositePath.head());
\r
95 if(model == null) return;
\r
96 final String modelURI = g.getURI(model);
\r
98 final String rvi = StructuralVariables.getRVI(g, variablePath);
\r
99 if(rvi == null) return;
\r
101 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
\r
103 public void run() {
\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