1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 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 * Semantum Oy - support SHIFT+left double click browsing outside of model configuration
12 *******************************************************************************/
13 package org.simantics.modeling.ui.diagramEditor.handlers.e4;
15 import java.util.HashMap;
19 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20 import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
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.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
44 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
45 import org.simantics.structural.stubs.StructuralResource2;
46 import org.simantics.ui.workbench.IResourceEditorInput2;
47 import org.simantics.ui.workbench.e4.E4ResourceEditorConstants;
48 import org.simantics.ui.workbench.e4.E4WorkbenchUtils;
49 import org.simantics.utils.datastructures.persistent.ContextMap;
50 import org.simantics.utils.ui.ErrorLogger;
53 * StructuralBrowsingHandler supports visual browsing into subcomponents through
56 * @author Tuukka Lehtonen
58 public class StructuralBrowsingHandler extends AbstractDiagramParticipant {
63 ISessionContext sessionContext;
64 IResourceEditorInput2 input;
66 public StructuralBrowsingHandler(ISessionContext sessionContext, MPart part, IResourceEditorInput2 input) {
67 this.sessionContext = sessionContext;
72 @EventHandler(priority = 0)
73 public boolean handleDoubleClick(MouseDoubleClickedEvent me) {
74 if (sessionContext == null || diagram == null)
77 Set<IElement> sel = selection.getSelection(0);
78 if (sel.size() == 1) {
79 IElement e = sel.iterator().next();
80 Object data = e.getHint(ElementHints.KEY_OBJECT);
81 final Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
82 if (data instanceof Resource) {
83 final Resource element = (Resource) data;
84 final int stateMask = me.stateMask;
86 return sessionContext.getSession().syncRequest(new UniqueRead<Boolean>() {
88 public Boolean perform(ReadGraph graph) throws DatabaseException {
89 return browse(graph, element, runtime, stateMask);
92 } catch (DatabaseException e1) {
93 ErrorLogger.defaultLogError(e1);
101 private boolean browse(ReadGraph graph, Resource element, Resource runtimeDiagram, int stateMask) throws DatabaseException {
102 DiagramResource DIA = DiagramResource.getInstance(graph);
103 ModelingResources mr = ModelingResources.getInstance(graph);
104 DiagramResource dr = DiagramResource.getInstance(graph);
105 StructuralResource2 sr = StructuralResource2.getInstance(graph);
107 if (graph.isInstanceOf(element, dr.Flag)) {
108 ContextMap parameters = new ContextMap();
109 parameters.put(ModelingOperationConstants.WORKBENCH_WINDOW, part.getContext().get(MWindow.class));
110 parameters.put(ModelingOperationConstants.WORKBENCH_PART, part);
111 parameters.put(IOperation.SUBJECT, element);
112 new NavigateToTarget().exec(graph.getSession(), parameters);
113 // Return true only if the flag is joined somewhere and navigation is possible.
114 return FlagUtil.isJoined(graph, element);
117 if((stateMask & MouseDoubleClickedEvent.SHIFT_MASK) == 0)
120 if(runtimeDiagram == null || !graph.isInstanceOf(runtimeDiagram, DIA.RuntimeDiagram))
123 final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);
124 if (component == null)
127 Resource type = graph.getPossibleType(component, sr.Component);
130 if(graph.hasStatement(type, sr.ComponentType_Locked))
133 Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);
134 if (definedBy == null)
137 final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);
141 Resource model = input.getModel(graph);
142 RVI subprocessRVI = null;
144 String rvis = (String) graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasRVI);
146 RVI rvi = RVI.fromResourceFormat(graph, rvis);
147 Variable base = Variables.getConfigurationContext(graph, model);
148 Variable current = rvi.resolve(graph, base);
149 Variable subprocess = current.browse(graph, component);
150 subprocessRVI = subprocess.getRVI(graph);
152 // Not in a model configuration context.
153 // Probably in a component type configuration.
154 // Use the index root of the diagram to be opened as model[0]
155 // and leave subprocessRVI[0] as null.
156 model = graph.syncRequest(new PossibleIndexRoot(definedBy));
159 PlatformUI.getWorkbench().getDisplay().asyncExec(navigateToSubstructure(diagram, model, subprocessRVI));
163 private Runnable navigateToSubstructure(final Resource diagram, final Resource model, final RVI subprocessRVI) {
164 return new Runnable() {
167 String inputId = Long.toString(diagram.getResourceId());
168 Map<String, Object> inputMap = new HashMap<>();
169 inputMap.put(E4ResourceEditorConstants.KEY_RESOURCE, diagram);
170 inputMap.put(E4ResourceEditorConstants.KEY_MODEL, model);
171 inputMap.put(E4ResourceEditorConstants.KEY_RVI, subprocessRVI);
172 E4WorkbenchUtils.openEditor(part.getElementId(), "", "", inputId, inputMap);