1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2012 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.actions;
\r
14 import java.util.Set;
\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
39 * Open an editor for a module instance.
\r
40 * @author Teemu Lempinen
\r
43 public class ShowInstantiatedModuleAction implements IDoubleClickAction {
\r
45 private static final String EDITOR_ID = "org.simantics.sysdyn.ui.diagramViewer";
\r
48 public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
\r
50 ReadGraph g = e.getGraph();
\r
51 Layer0 l0 = Layer0.getInstance(g);
\r
52 StructuralResource2 sr2 = StructuralResource2.getInstance(g);
\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
63 rvi = rvi.substring(1);
\r
64 Resource model = Variables.getModel(g, variable);
\r
65 addShowModuleAction(e, getResourceEditorInput(g, model, configuration, rvi));
\r
70 if(e.getResource() instanceof IStructuredSelection) {
\r
71 Set<BrowserSelection> bss = ISelectionUtils.filterSetSelection(e.getResource(), BrowserSelection.class);
\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
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
89 ModuleNode moduleNode = ISelectionUtils.filterSingleSelection(e.getResource(), ModuleNode.class);
\r
90 if(moduleNode != null) {
\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
98 rvi = (String)g.getRelatedValue(moduleNode.data, Layer0.getInstance(g).HasName);
\r
99 model = g.getSingleObject(moduleNode.data, l0.PartOf);
\r
102 Resource instanceOf = g.getSingleObject(moduleNode.data, l0.InstanceOf);
\r
103 Resource conf = g.getSingleObject(instanceOf, sr2.IsDefinedBy);
\r
105 addShowModuleAction(e, getResourceEditorInput(g, model, conf, rvi));
\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
116 if(model != null) {
\r
117 modelURI = g.getURI(model);
\r
119 Resource parent = configuration;
\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
127 Resource diagram = g.getSingleObject(configuration, ModelingResources.getInstance(g).CompositeToDiagram);
\r
128 return new SysdynEditorInput(EDITOR_ID, diagram, modelURI, rvi == null ? "" : "/" + rvi);
\r
131 private void addShowModuleAction(DoubleClickEvent e, final ResourceEditorInput2 editorInput) {
\r
132 e.add(new PriorityAction(PriorityAction.HIGH) {
\r
134 public void run() {
\r
136 WorkbenchUtils.openEditor(EDITOR_ID, editorInput);
\r
137 } catch (PartInitException e) {
\r
138 e.printStackTrace();
\r