1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
14 import java.util.Collection;
15 import java.util.Collections;
17 import org.eclipse.ui.PlatformUI;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.request.PossibleIndexRoot;
21 import org.simantics.db.common.request.PossibleTypedParent;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.variable.RVI;
25 import org.simantics.db.layer0.variable.Variable;
26 import org.simantics.db.layer0.variable.Variables;
27 import org.simantics.diagram.stubs.DiagramResource;
28 import org.simantics.g2d.canvas.ICanvasContext;
29 import org.simantics.g2d.diagram.DiagramHints;
30 import org.simantics.layer0.Layer0;
31 import org.simantics.modeling.ComponentUtils;
32 import org.simantics.modeling.actions.NavigateToTarget;
33 import org.simantics.modeling.ui.Activator;
34 import org.simantics.structural.stubs.StructuralResource2;
35 import org.simantics.ui.SimanticsUI;
36 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
37 import org.simantics.utils.datastructures.Pair;
38 import org.simantics.utils.threads.ThreadUtils;
41 * @author Tuukka Lehtonen
43 public class OpenDiagramFromConfigurationAdapter extends AbstractResourceEditorAdapter {
45 private static final String EDITOR_ID = "org.simantics.modeling.ui.diagramEditor";
47 public OpenDiagramFromConfigurationAdapter() {
48 super("Diagram Editor", Activator.COMPOSITE_ICON);
51 protected String getEditorId() {
56 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
57 Resource diagram = getDiagram(g, r, Collections.emptySet());
61 // Check if the diagram is locked, because of the locked component type
62 return !isLocked(g, diagram);
64 // return ComponentUtils.compositeHasDiagram(g, r) /*|| ComponentUtils.componentHasDiagram(g, r)*/;
67 public static boolean isLocked(ReadGraph g, Resource diagram) throws DatabaseException {
68 StructuralResource2 STR = StructuralResource2.getInstance(g);
69 Resource componentType = g.syncRequest(new PossibleTypedParent(diagram, STR.ComponentType));
70 if(componentType == null)
71 return false; // Not part of a component type
73 return g.hasStatement(componentType, STR.ComponentType_Locked);
77 public void openEditor(final Resource r) throws Exception {
78 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
80 public void run(ReadGraph g) throws DatabaseException {
81 openEditor(g, r, getEditorId());
88 * @param configurationComposite
90 * @throws DatabaseException
92 public static boolean openEditor(ReadGraph g, Resource configurationComposite, final String editorId) throws DatabaseException {
93 return openEditor(g, configurationComposite, editorId, Collections.emptySet());
96 protected static Resource getDiagram(ReadGraph graph, Resource r, final Collection<Object> selectedObjects) throws DatabaseException {
98 Layer0 L0 = Layer0.getInstance(graph);
99 DiagramResource DIA = DiagramResource.getInstance(graph);
101 if(graph.isInstanceOf(r, DIA.Diagram)) return r;
103 Resource diagram = ComponentUtils.getPossibleCompositeDiagram(graph, r);
104 if(diagram != null) return diagram;
106 if(selectedObjects.size() == 1) {
107 Object o = selectedObjects.iterator().next();
108 if(o instanceof Resource) {
109 Resource res = (Resource)o;
110 if(graph.isInstanceOf(res, DIA.Element)) {
111 return graph.getPossibleObject(res, L0.PartOf);
120 public static Pair<Resource, RVI> getModelAndRVI(ReadGraph graph, Resource configurationComposite) throws DatabaseException {
121 Variable compositeVariable = Variables.getPossibleVariable(graph, configurationComposite);
122 if (compositeVariable == null)
125 Layer0 L0 = Layer0.getInstance(graph);
126 StructuralResource2 STR = StructuralResource2.getInstance(graph);
127 boolean isComponentType = graph.hasStatement(configurationComposite, STR.Defines);
128 Resource rviContext = graph.syncRequest(new PossibleTypedParent(configurationComposite, L0.RVIContext));
130 if (isComponentType || rviContext == null) {
131 Resource indexRoot = graph.sync(new PossibleIndexRoot(configurationComposite));
132 if (indexRoot == null)
135 return Pair.make(indexRoot, null);
138 final Resource model = Variables.getPossibleModel(graph, compositeVariable);
141 final RVI rvi = compositeVariable.getPossibleRVI(graph);
143 return Pair.make(model, rvi);
148 * @param configurationComposite
150 * @param selectedObjects
151 * @throws DatabaseException
153 public static boolean openEditor(ReadGraph g, Resource r, final String editorId, final Collection<Object> selectedObjects) throws DatabaseException {
155 final Resource diagram = getDiagram(g, r, selectedObjects);
159 final Resource configurationComposite = ComponentUtils.getPossibleDiagramComposite(g, diagram);
160 if (configurationComposite == null)
163 Pair<Resource, RVI> modelAndRVI = getModelAndRVI(g, configurationComposite);
164 //System.out.println("modelAndRVI: " + modelAndRVI);
165 if (modelAndRVI == null)
168 final Runnable editorActivator = NavigateToTarget.editorActivator(editorId, diagram, modelAndRVI.first, modelAndRVI.second, part -> {
169 if (selectedObjects.isEmpty())
172 final ICanvasContext openedCanvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
173 assert openedCanvas != null;
174 // CanvasContext-wide denial of initial zoom-to-fit on diagram open.
175 openedCanvas.getDefaultHintContext().setHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT, Boolean.FALSE);
176 //System.out.println("ASDF: " + element);
177 ThreadUtils.asyncExec(openedCanvas.getThreadAccess(),
178 NavigateToTarget.elementSelectorZoomer(openedCanvas, selectedObjects, false));
181 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
184 editorActivator.run();