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.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.ISelectionUtils;
\r
34 import org.simantics.utils.ui.action.PriorityAction;
\r
35 import org.simantics.utils.ui.workbench.WorkbenchUtils;
\r
37 public class ShowInstantiatedModuleAction implements IDoubleClickAction {
\r
39 private static final String EDITOR_ID = "org.simantics.sysdyn.ui.diagramViewer";
\r
42 public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
\r
44 ReadGraph g = e.getGraph();
\r
46 if(e.getResource() instanceof IStructuredSelection) {
\r
47 Set<BrowserSelection> bss = ISelectionUtils.filterSetSelection(e.getResource(), BrowserSelection.class);
\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
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
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
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
77 rvi = (String)g.getRelatedValue(moduleNode.data, Layer0.getInstance(g).HasName);
\r
78 model = g.getSingleObject(moduleNode.data, l0.PartOf);
\r
81 Resource instanceOf = g.getSingleObject(moduleNode.data, l0.InstanceOf);
\r
82 Resource conf = g.getSingleObject(instanceOf, sr2.IsDefinedBy);
\r
84 addShowModuleAction(e, getResourceEditorInput(g, model, conf, rvi));
\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
96 modelURI = g.getURI(model);
\r
98 Resource parent = configuration;
\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
106 Resource diagram = g.getSingleObject(configuration, ModelingResources.getInstance(g).CompositeToDiagram);
\r
107 return new SysdynEditorInput(EDITOR_ID, diagram, modelURI, rvi == null ? "" : "/" + rvi);
\r
110 private void addShowModuleAction(DoubleClickEvent e, final ResourceEditorInput2 editorInput) {
\r
111 e.add(new PriorityAction(PriorityAction.HIGH) {
\r
113 public void run() {
\r
115 WorkbenchUtils.openEditor(EDITOR_ID, editorInput);
\r
116 } catch (PartInitException e) {
\r
117 e.printStackTrace();
\r