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.Simantics;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.request.PossibleIndexRoot;
22 import org.simantics.db.common.request.PossibleTypedParent;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.variable.RVI;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.db.layer0.variable.Variables;
28 import org.simantics.diagram.stubs.DiagramResource;
29 import org.simantics.g2d.canvas.ICanvasContext;
30 import org.simantics.g2d.diagram.DiagramHints;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.modeling.ComponentUtils;
33 import org.simantics.modeling.actions.NavigateToTarget;
34 import org.simantics.modeling.ui.Activator;
35 import org.simantics.structural.stubs.StructuralResource2;
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"; //$NON-NLS-1$
47 public OpenDiagramFromConfigurationAdapter() {
48 super(Messages.OpenDiagramFromConfigurationAdapter_DiagramEditor, 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 Simantics.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, String editorId, Collection<Object> selectedObjects) throws DatabaseException {
154 Resource diagram = getDiagram(g, r, selectedObjects);
155 Resource configurationComposite = diagram != null ? ComponentUtils.getPossibleDiagramComposite(g, diagram) : null;
156 Pair<Resource, RVI> modelAndRVI = configurationComposite != null ? getModelAndRVI(g, configurationComposite) : null;
157 //System.out.println("modelAndRVI: " + modelAndRVI);
158 if (modelAndRVI == null)
160 scheduleOpenEditor(editorId, diagram, modelAndRVI.first, modelAndRVI.second, selectedObjects);
166 * @param configurationComposite
168 * @param selectedObjects
169 * @throws DatabaseException
171 public static boolean openEditor(ReadGraph g, Resource r, String editorId, Collection<Object> selectedObjects, Resource model, RVI rvi) throws DatabaseException {
172 Resource diagram = getDiagram(g, r, selectedObjects);
175 scheduleOpenEditor(editorId, diagram, model, rvi, selectedObjects);
181 * @param configurationComposite
183 * @param selectedObjects
184 * @throws DatabaseException
186 private static void scheduleOpenEditor(String editorId, Resource diagram, Resource model, RVI rvi, Collection<Object> selectedObjects) throws DatabaseException {
187 Runnable editorActivator = NavigateToTarget.editorActivator(editorId, diagram, model, rvi, part -> {
188 if (selectedObjects.isEmpty())
190 ICanvasContext openedCanvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
191 assert openedCanvas != null;
192 // CanvasContext-wide denial of initial zoom-to-fit on diagram open.
193 openedCanvas.getDefaultHintContext().setHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT, Boolean.FALSE);
194 ThreadUtils.asyncExec(openedCanvas.getThreadAccess(),
195 NavigateToTarget.elementSelectorZoomer(openedCanvas, selectedObjects, false));
197 PlatformUI.getWorkbench().getDisplay().asyncExec(editorActivator);