]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromConfigurationAdapter.java
8c052561e2340ed051d5989836742d0684083e0f
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / OpenDiagramFromConfigurationAdapter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
13
14 import java.util.Collection;
15 import java.util.Collections;
16
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;
39
40 /**
41  * @author Tuukka Lehtonen
42  */
43 public class OpenDiagramFromConfigurationAdapter extends AbstractResourceEditorAdapter {
44
45     private static final String EDITOR_ID = "org.simantics.modeling.ui.diagramEditor"; //$NON-NLS-1$
46
47     public OpenDiagramFromConfigurationAdapter() {
48         super(Messages.OpenDiagramFromConfigurationAdapter_DiagramEditor, Activator.COMPOSITE_ICON);
49     }
50
51     protected String getEditorId() {
52         return EDITOR_ID;
53     }
54
55     @Override
56     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
57         Resource diagram = getDiagram(g, r, Collections.emptySet());
58         if(diagram == null)
59             return false;
60         
61         // Check if the diagram is locked, because of the locked component type
62         return !isLocked(g, diagram);
63         
64 //        return ComponentUtils.compositeHasDiagram(g, r) /*|| ComponentUtils.componentHasDiagram(g, r)*/;
65     }
66     
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
72         
73         return g.hasStatement(componentType, STR.ComponentType_Locked);
74     }
75
76     @Override
77     public void openEditor(final Resource r) throws Exception {
78         Simantics.getSession().asyncRequest(new ReadRequest() {
79             @Override
80             public void run(ReadGraph g) throws DatabaseException {
81                 openEditor(g, r, getEditorId());
82             }
83         });
84     }
85
86     /**
87      * @param g
88      * @param configurationComposite
89      * @param editorId
90      * @throws DatabaseException
91      */
92     public static boolean openEditor(ReadGraph g, Resource configurationComposite, final String editorId) throws DatabaseException {
93         return openEditor(g, configurationComposite, editorId, Collections.emptySet());
94     }
95
96     protected static Resource getDiagram(ReadGraph graph, Resource r, final Collection<Object> selectedObjects) throws DatabaseException {
97
98         Layer0 L0 = Layer0.getInstance(graph);
99         DiagramResource DIA = DiagramResource.getInstance(graph);
100         
101         if(graph.isInstanceOf(r, DIA.Diagram)) return r;
102         
103         Resource diagram = ComponentUtils.getPossibleCompositeDiagram(graph, r);
104         if(diagram != null) return diagram;
105         
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);
112                         }
113                 }
114         }
115         
116         return null;
117
118     }
119
120     public static Pair<Resource, RVI> getModelAndRVI(ReadGraph graph, Resource configurationComposite) throws DatabaseException {
121         Variable compositeVariable = Variables.getPossibleVariable(graph, configurationComposite);
122         if (compositeVariable == null)
123             return null;
124
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));
129
130         if (isComponentType || rviContext == null) {
131             Resource indexRoot = graph.sync(new PossibleIndexRoot(configurationComposite));
132             if (indexRoot == null)
133                 return null;
134
135             return Pair.make(indexRoot, null);
136         }
137
138         final Resource model = Variables.getPossibleModel(graph, compositeVariable);
139         if (model == null)
140             return null;
141         final RVI rvi = compositeVariable.getPossibleRVI(graph);
142
143         return Pair.make(model, rvi);
144     }
145
146     /**
147      * @param g
148      * @param configurationComposite
149      * @param editorId
150      * @param selectedObjects
151      * @throws DatabaseException
152      */
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)
159             return false;
160         scheduleOpenEditor(editorId, diagram, modelAndRVI.first, modelAndRVI.second, selectedObjects);
161         return true;
162     }
163
164     /**
165      * @param g
166      * @param configurationComposite
167      * @param editorId
168      * @param selectedObjects
169      * @throws DatabaseException
170      */
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);
173         if (diagram == null)
174             return false;
175         scheduleOpenEditor(editorId, diagram, model, rvi, selectedObjects);
176         return true;
177     }
178
179     /**
180      * @param g
181      * @param configurationComposite
182      * @param editorId
183      * @param selectedObjects
184      * @throws DatabaseException
185      */
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())
189                 return;
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));
196         });
197         PlatformUI.getWorkbench().getDisplay().asyncExec(editorActivator);
198     }
199
200 }