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;
16 import java.util.HashSet;
19 import org.eclipse.ui.PlatformUI;
20 import org.simantics.Simantics;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.common.request.PossibleIndexRoot;
24 import org.simantics.db.common.request.PossibleTypedParent;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.variable.RVI;
28 import org.simantics.db.layer0.variable.Variable;
29 import org.simantics.db.layer0.variable.Variables;
30 import org.simantics.diagram.stubs.DiagramResource;
31 import org.simantics.g2d.canvas.ICanvasContext;
32 import org.simantics.g2d.diagram.DiagramHints;
33 import org.simantics.layer0.Layer0;
34 import org.simantics.modeling.ComponentUtils;
35 import org.simantics.modeling.ModelingResources;
36 import org.simantics.modeling.actions.NavigateToTarget;
37 import org.simantics.modeling.ui.Activator;
38 import org.simantics.structural.stubs.StructuralResource2;
39 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
40 import org.simantics.utils.datastructures.Pair;
41 import org.simantics.utils.threads.ThreadUtils;
44 * @author Tuukka Lehtonen
46 public class OpenDiagramFromConfigurationAdapter extends AbstractResourceEditorAdapter {
48 private static final String EDITOR_ID = "org.simantics.modeling.ui.diagramEditor"; //$NON-NLS-1$
50 public OpenDiagramFromConfigurationAdapter() {
51 super(Messages.OpenDiagramFromConfigurationAdapter_DiagramEditor, Activator.COMPOSITE_ICON);
54 protected String getEditorId(ReadGraph g, Resource composite) throws DatabaseException {
55 ModelingResources MOD = ModelingResources.getInstance(g);
56 String preferredEditorId = g.getPossibleRelatedValue(composite, MOD.PreferredDiagramEditorID);
57 if(preferredEditorId != null)
58 return preferredEditorId;
64 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
65 Resource diagram = getDiagram(g, r, Collections.emptySet());
69 // Check if the diagram is locked, because of the locked component type
70 return !isLocked(g, diagram);
72 // return ComponentUtils.compositeHasDiagram(g, r) /*|| ComponentUtils.componentHasDiagram(g, r)*/;
75 public static boolean isLocked(ReadGraph g, Resource diagram) throws DatabaseException {
76 StructuralResource2 STR = StructuralResource2.getInstance(g);
77 Resource componentType = g.syncRequest(new PossibleTypedParent(diagram, STR.ComponentType));
78 if(componentType == null)
79 return false; // Not part of a component type
81 return g.hasStatement(componentType, STR.ComponentType_Locked);
85 public void openEditor(final Resource r) throws Exception {
86 Simantics.getSession().asyncRequest(new ReadRequest() {
88 public void run(ReadGraph g) throws DatabaseException {
89 openEditor(g, r, getEditorId(g, r));
96 * @param configurationComposite
98 * @throws DatabaseException
100 public static boolean openEditor(ReadGraph g, Resource configurationComposite, final String editorId) throws DatabaseException {
101 return openEditor(g, configurationComposite, editorId, Collections.emptySet());
104 protected static Resource getDiagram(ReadGraph graph, Resource r, final Collection<Object> selectedObjects) throws DatabaseException {
106 Layer0 L0 = Layer0.getInstance(graph);
107 DiagramResource DIA = DiagramResource.getInstance(graph);
109 if(graph.isInstanceOf(r, DIA.Diagram)) return r;
111 Resource diagram = ComponentUtils.getPossibleCompositeDiagram(graph, r);
112 if(diagram != null) return diagram;
114 // TODO: what if the selected objects are from different diagrams?
115 if (selectedObjects.size() > 0) {
116 Set<Resource> diagrams = new HashSet<>();
117 for (Object o : selectedObjects) {
118 if (o instanceof Resource) {
119 Resource res = (Resource)o;
120 if (graph.isInstanceOf(res, DIA.Element)) {
121 diagrams.add(graph.getPossibleObject(res, L0.PartOf));
125 if (diagrams.size() == 1) {
126 return diagrams.iterator().next();
133 public static Pair<Resource, RVI> getModelAndRVI(ReadGraph graph, Resource configurationComposite) throws DatabaseException {
134 Variable compositeVariable = Variables.getPossibleVariable(graph, configurationComposite);
135 if (compositeVariable == null)
138 Layer0 L0 = Layer0.getInstance(graph);
139 StructuralResource2 STR = StructuralResource2.getInstance(graph);
140 boolean isComponentType = graph.hasStatement(configurationComposite, STR.Defines);
141 Resource rviContext = graph.syncRequest(new PossibleTypedParent(configurationComposite, L0.RVIContext));
143 if (isComponentType || rviContext == null) {
144 Resource indexRoot = graph.sync(new PossibleIndexRoot(configurationComposite));
145 if (indexRoot == null)
148 return Pair.make(indexRoot, null);
151 final Resource model = Variables.getPossibleModel(graph, compositeVariable);
154 final RVI rvi = compositeVariable.getPossibleRVI(graph);
156 return Pair.make(model, rvi);
161 * @param configurationComposite
163 * @param selectedObjects
164 * @throws DatabaseException
166 public static boolean openEditor(ReadGraph g, Resource r, String editorId, Collection<Object> selectedObjects) throws DatabaseException {
167 Resource diagram = getDiagram(g, r, selectedObjects);
168 Resource configurationComposite = diagram != null ? ComponentUtils.getPossibleDiagramComposite(g, diagram) : null;
169 Pair<Resource, RVI> modelAndRVI = configurationComposite != null ? getModelAndRVI(g, configurationComposite) : null;
170 //System.out.println("modelAndRVI: " + modelAndRVI);
171 if (modelAndRVI == null)
173 scheduleOpenEditor(editorId, diagram, modelAndRVI.first, modelAndRVI.second, selectedObjects);
179 * @param configurationComposite
181 * @param selectedObjects
182 * @throws DatabaseException
184 public static boolean openEditor(ReadGraph g, Resource r, String editorId, Collection<Object> selectedObjects, Resource model, RVI rvi) throws DatabaseException {
185 Resource diagram = getDiagram(g, r, selectedObjects);
188 scheduleOpenEditor(editorId, diagram, model, rvi, selectedObjects);
194 * @param configurationComposite
196 * @param selectedObjects
197 * @throws DatabaseException
199 private static void scheduleOpenEditor(String editorId, Resource diagram, Resource model, RVI rvi, Collection<Object> selectedObjects) throws DatabaseException {
200 Runnable editorActivator = NavigateToTarget.editorActivator(editorId, diagram, model, rvi, part -> {
201 if (selectedObjects.isEmpty())
203 ICanvasContext openedCanvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
204 assert openedCanvas != null;
205 // CanvasContext-wide denial of initial zoom-to-fit on diagram open.
206 openedCanvas.getDefaultHintContext().setHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT, Boolean.FALSE);
207 ThreadUtils.asyncExec(openedCanvas.getThreadAccess(),
208 NavigateToTarget.elementSelectorZoomer(openedCanvas, selectedObjects, false));
210 PlatformUI.getWorkbench().getDisplay().asyncExec(editorActivator);