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.actions.NavigateToTarget;
36 import org.simantics.modeling.ui.Activator;
37 import org.simantics.structural.stubs.StructuralResource2;
38 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
39 import org.simantics.utils.datastructures.Pair;
40 import org.simantics.utils.threads.ThreadUtils;
43 * @author Tuukka Lehtonen
45 public class OpenDiagramFromConfigurationAdapter extends AbstractResourceEditorAdapter {
47 private static final String EDITOR_ID = "org.simantics.modeling.ui.diagramEditor"; //$NON-NLS-1$
49 public OpenDiagramFromConfigurationAdapter() {
50 super(Messages.OpenDiagramFromConfigurationAdapter_DiagramEditor, Activator.COMPOSITE_ICON);
53 protected String getEditorId() {
58 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
59 Resource diagram = getDiagram(g, r, Collections.emptySet());
63 // Check if the diagram is locked, because of the locked component type
64 return !isLocked(g, diagram);
66 // return ComponentUtils.compositeHasDiagram(g, r) /*|| ComponentUtils.componentHasDiagram(g, r)*/;
69 public static boolean isLocked(ReadGraph g, Resource diagram) throws DatabaseException {
70 StructuralResource2 STR = StructuralResource2.getInstance(g);
71 Resource componentType = g.syncRequest(new PossibleTypedParent(diagram, STR.ComponentType));
72 if(componentType == null)
73 return false; // Not part of a component type
75 return g.hasStatement(componentType, STR.ComponentType_Locked);
79 public void openEditor(final Resource r) throws Exception {
80 Simantics.getSession().asyncRequest(new ReadRequest() {
82 public void run(ReadGraph g) throws DatabaseException {
83 openEditor(g, r, getEditorId());
90 * @param configurationComposite
92 * @throws DatabaseException
94 public static boolean openEditor(ReadGraph g, Resource configurationComposite, final String editorId) throws DatabaseException {
95 return openEditor(g, configurationComposite, editorId, Collections.emptySet());
98 protected static Resource getDiagram(ReadGraph graph, Resource r, final Collection<Object> selectedObjects) throws DatabaseException {
100 Layer0 L0 = Layer0.getInstance(graph);
101 DiagramResource DIA = DiagramResource.getInstance(graph);
103 if(graph.isInstanceOf(r, DIA.Diagram)) return r;
105 Resource diagram = ComponentUtils.getPossibleCompositeDiagram(graph, r);
106 if(diagram != null) return diagram;
108 // TODO: what if the selected objects are from different diagrams?
109 if (selectedObjects.size() > 0) {
110 Set<Resource> diagrams = new HashSet<>();
111 for (Object o : selectedObjects) {
112 if (o instanceof Resource) {
113 Resource res = (Resource)o;
114 if (graph.isInstanceOf(res, DIA.Element)) {
115 diagrams.add(graph.getPossibleObject(res, L0.PartOf));
119 if (diagrams.size() == 1) {
120 return diagrams.iterator().next();
127 public static Pair<Resource, RVI> getModelAndRVI(ReadGraph graph, Resource configurationComposite) throws DatabaseException {
128 Variable compositeVariable = Variables.getPossibleVariable(graph, configurationComposite);
129 if (compositeVariable == null)
132 Layer0 L0 = Layer0.getInstance(graph);
133 StructuralResource2 STR = StructuralResource2.getInstance(graph);
134 boolean isComponentType = graph.hasStatement(configurationComposite, STR.Defines);
135 Resource rviContext = graph.syncRequest(new PossibleTypedParent(configurationComposite, L0.RVIContext));
137 if (isComponentType || rviContext == null) {
138 Resource indexRoot = graph.sync(new PossibleIndexRoot(configurationComposite));
139 if (indexRoot == null)
142 return Pair.make(indexRoot, null);
145 final Resource model = Variables.getPossibleModel(graph, compositeVariable);
148 final RVI rvi = compositeVariable.getPossibleRVI(graph);
150 return Pair.make(model, rvi);
155 * @param configurationComposite
157 * @param selectedObjects
158 * @throws DatabaseException
160 public static boolean openEditor(ReadGraph g, Resource r, String editorId, Collection<Object> selectedObjects) throws DatabaseException {
161 Resource diagram = getDiagram(g, r, selectedObjects);
162 Resource configurationComposite = diagram != null ? ComponentUtils.getPossibleDiagramComposite(g, diagram) : null;
163 Pair<Resource, RVI> modelAndRVI = configurationComposite != null ? getModelAndRVI(g, configurationComposite) : null;
164 //System.out.println("modelAndRVI: " + modelAndRVI);
165 if (modelAndRVI == null)
167 scheduleOpenEditor(editorId, diagram, modelAndRVI.first, modelAndRVI.second, selectedObjects);
173 * @param configurationComposite
175 * @param selectedObjects
176 * @throws DatabaseException
178 public static boolean openEditor(ReadGraph g, Resource r, String editorId, Collection<Object> selectedObjects, Resource model, RVI rvi) throws DatabaseException {
179 Resource diagram = getDiagram(g, r, selectedObjects);
182 scheduleOpenEditor(editorId, diagram, model, rvi, selectedObjects);
188 * @param configurationComposite
190 * @param selectedObjects
191 * @throws DatabaseException
193 private static void scheduleOpenEditor(String editorId, Resource diagram, Resource model, RVI rvi, Collection<Object> selectedObjects) throws DatabaseException {
194 Runnable editorActivator = NavigateToTarget.editorActivator(editorId, diagram, model, rvi, part -> {
195 if (selectedObjects.isEmpty())
197 ICanvasContext openedCanvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
198 assert openedCanvas != null;
199 // CanvasContext-wide denial of initial zoom-to-fit on diagram open.
200 openedCanvas.getDefaultHintContext().setHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT, Boolean.FALSE);
201 ThreadUtils.asyncExec(openedCanvas.getThreadAccess(),
202 NavigateToTarget.elementSelectorZoomer(openedCanvas, selectedObjects, false));
204 PlatformUI.getWorkbench().getDisplay().asyncExec(editorActivator);