]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/StructuralBrowsingHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / StructuralBrowsingHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2014 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  *     Semantum Oy - support SHIFT+left double click browsing outside of model configuration
12  *******************************************************************************/
13 package org.simantics.modeling.ui.diagramEditor.handlers;
14
15 import java.util.Set;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.ui.IWorkbenchPartSite;
20 import org.eclipse.ui.PartInitException;
21 import org.eclipse.ui.PlatformUI;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.request.PossibleIndexRoot;
25 import org.simantics.db.common.request.UniqueRead;
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.db.management.ISessionContext;
31 import org.simantics.diagram.flag.FlagUtil;
32 import org.simantics.diagram.stubs.DiagramResource;
33 import org.simantics.diagram.ui.DiagramModelHints;
34 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
35 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
36 import org.simantics.g2d.diagram.participant.Selection;
37 import org.simantics.g2d.element.ElementHints;
38 import org.simantics.g2d.element.IElement;
39 import org.simantics.layer0.utils.operations.IOperation;
40 import org.simantics.modeling.ModelingOperationConstants;
41 import org.simantics.modeling.ModelingResources;
42 import org.simantics.modeling.actions.NavigateToTarget;
43 import org.simantics.modeling.ui.Activator;
44 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
45 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
46 import org.simantics.structural.stubs.StructuralResource2;
47 import org.simantics.ui.workbench.IResourceEditorInput2;
48 import org.simantics.ui.workbench.ResourceEditorInput2;
49 import org.simantics.utils.datastructures.persistent.ContextMap;
50 import org.simantics.utils.ui.ErrorLogger;
51 import org.simantics.utils.ui.workbench.WorkbenchUtils;
52
53 /**
54  * StructuralBrowsingHandler supports visual browsing into subcomponents through
55  * mouse events.
56  * 
57  * @author Tuukka Lehtonen
58  */
59 public class StructuralBrowsingHandler extends AbstractDiagramParticipant {
60
61     @Dependency
62     Selection             selection;
63     IWorkbenchPartSite    site;
64     ISessionContext       sessionContext;
65     IResourceEditorInput2 input;
66
67     public StructuralBrowsingHandler(IWorkbenchPartSite site, ISessionContext sessionContext, IResourceEditorInput2 input) {
68         assert site != null;
69
70         this.site = site;
71         this.sessionContext = sessionContext;
72         this.input = input;
73     }
74
75     @EventHandler(priority = 0)
76     public boolean handleDoubleClick(MouseDoubleClickedEvent me) {
77         if (sessionContext == null || diagram == null)
78             return false;
79         
80         Set<IElement> sel = selection.getSelection(0);
81         if (sel.size() == 1) {
82             IElement e = sel.iterator().next();
83             Object data = e.getHint(ElementHints.KEY_OBJECT);
84             final Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
85             if (data instanceof Resource) {
86                 final Resource element = (Resource) data;
87                 final int stateMask = me.stateMask;
88                 try {
89                     return sessionContext.getSession().syncRequest(new UniqueRead<Boolean>() {
90                         @Override
91                         public Boolean perform(ReadGraph graph) throws DatabaseException {
92                             return browse(graph, element, runtime, stateMask);
93                         }
94                     });
95                 } catch (DatabaseException e1) {
96                     ErrorLogger.defaultLogError(e1);
97                 }
98             }
99         }
100
101         return false;
102     }
103     
104     private boolean browse(ReadGraph graph, Resource element, Resource runtimeDiagram, int stateMask) throws DatabaseException {
105         DiagramResource DIA = DiagramResource.getInstance(graph);
106         ModelingResources mr = ModelingResources.getInstance(graph);
107         DiagramResource dr = DiagramResource.getInstance(graph);
108         StructuralResource2 sr = StructuralResource2.getInstance(graph);
109
110         if (graph.isInstanceOf(element, dr.Flag)) {
111             ContextMap parameters = new ContextMap();
112             parameters.put(ModelingOperationConstants.WORKBENCH_WINDOW, site.getWorkbenchWindow());
113             parameters.put(ModelingOperationConstants.WORKBENCH_PART, site.getPart());
114             parameters.put(IOperation.SUBJECT, element);
115             new NavigateToTarget().exec(graph.getSession(), parameters);
116             // Return true only if the flag is joined somewhere and navigation is possible.
117             return FlagUtil.isJoined(graph, element);
118         }
119
120         if((stateMask & MouseDoubleClickedEvent.SHIFT_MASK) == 0)
121             return false;
122
123         if(runtimeDiagram == null || !graph.isInstanceOf(runtimeDiagram, DIA.RuntimeDiagram))
124             return false;
125
126         final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);
127         if (component == null)
128             return false;
129
130         Resource type = graph.getPossibleType(component, sr.Component);
131         if (type == null)
132             return false;
133         if(graph.hasStatement(type, sr.ComponentType_Locked))
134             return false;
135
136         Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);
137         if (definedBy == null)
138             return false;
139
140         final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);
141         if (diagram == null)
142             return false;
143
144         Resource model = input.getModel(graph);
145         RVI subprocessRVI = null;
146
147         String rvis = (String) graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasRVI);
148         if (rvis != null) {
149             RVI rvi = RVI.fromResourceFormat(graph, rvis);
150             Variable base = Variables.getConfigurationContext(graph, model);
151             Variable current = rvi.resolve(graph, base);
152             Variable subprocess = current.browse(graph, component);
153             subprocessRVI = subprocess.getRVI(graph);
154         } else {
155             // Not in a model configuration context.
156             // Probably in a component type configuration.
157             // Use the index root of the diagram to be opened as model[0]
158             // and leave subprocessRVI[0] as null.
159             model = graph.syncRequest(new PossibleIndexRoot(definedBy));
160         }
161
162         PlatformUI.getWorkbench().getDisplay().asyncExec(navigateToSubstructure(diagram, model, subprocessRVI));
163         return true;
164     }
165
166     private Runnable navigateToSubstructure(final Resource diagram, final Resource model, final RVI subprocessRVI) {
167         return new Runnable() {
168             @Override
169             public void run() {
170                 try {
171                     WorkbenchUtils.openEditor(site.getId(), new ResourceEditorInput2(site.getId(), diagram, model, subprocessRVI));
172                 } catch (PartInitException e) {
173                     Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to open editor, see exception for details.", e));
174                 }
175             }
176         };
177     }
178
179 }