1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2014 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 * Semantum Oy - support SHIFT+left double click browsing outside of model configuration
\r
12 *******************************************************************************/
\r
13 package org.simantics.modeling.ui.diagramEditor.handlers;
\r
15 import java.util.Set;
\r
17 import org.eclipse.core.runtime.IStatus;
\r
18 import org.eclipse.core.runtime.Status;
\r
19 import org.eclipse.ui.IWorkbenchPartSite;
\r
20 import org.eclipse.ui.PartInitException;
\r
21 import org.eclipse.ui.PlatformUI;
\r
22 import org.simantics.db.ReadGraph;
\r
23 import org.simantics.db.Resource;
\r
24 import org.simantics.db.common.request.PossibleIndexRoot;
\r
25 import org.simantics.db.common.request.UniqueRead;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.db.layer0.variable.RVI;
\r
28 import org.simantics.db.layer0.variable.Variable;
\r
29 import org.simantics.db.layer0.variable.Variables;
\r
30 import org.simantics.db.management.ISessionContext;
\r
31 import org.simantics.diagram.flag.FlagUtil;
\r
32 import org.simantics.diagram.stubs.DiagramResource;
\r
33 import org.simantics.diagram.ui.DiagramModelHints;
\r
34 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
\r
35 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
\r
36 import org.simantics.g2d.diagram.participant.Selection;
\r
37 import org.simantics.g2d.element.ElementHints;
\r
38 import org.simantics.g2d.element.IElement;
\r
39 import org.simantics.layer0.utils.operations.IOperation;
\r
40 import org.simantics.modeling.ModelingOperationConstants;
\r
41 import org.simantics.modeling.ModelingResources;
\r
42 import org.simantics.modeling.actions.NavigateToTarget;
\r
43 import org.simantics.modeling.ui.Activator;
\r
44 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
\r
45 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
\r
46 import org.simantics.structural.stubs.StructuralResource2;
\r
47 import org.simantics.ui.workbench.IResourceEditorInput2;
\r
48 import org.simantics.ui.workbench.ResourceEditorInput2;
\r
49 import org.simantics.utils.datastructures.persistent.ContextMap;
\r
50 import org.simantics.utils.ui.ErrorLogger;
\r
51 import org.simantics.utils.ui.workbench.WorkbenchUtils;
\r
54 * StructuralBrowsingHandler supports visual browsing into subcomponents through
\r
57 * @author Tuukka Lehtonen
\r
59 public class StructuralBrowsingHandler extends AbstractDiagramParticipant {
\r
62 Selection selection;
\r
63 IWorkbenchPartSite site;
\r
64 ISessionContext sessionContext;
\r
65 IResourceEditorInput2 input;
\r
67 public StructuralBrowsingHandler(IWorkbenchPartSite site, ISessionContext sessionContext, IResourceEditorInput2 input) {
\r
68 assert site != null;
\r
71 this.sessionContext = sessionContext;
\r
75 @EventHandler(priority = 0)
\r
76 public boolean handleDoubleClick(MouseDoubleClickedEvent me) {
\r
77 if (sessionContext == null || diagram == null)
\r
80 Set<IElement> sel = selection.getSelection(0);
\r
81 if (sel.size() == 1) {
\r
82 IElement e = sel.iterator().next();
\r
83 Object data = e.getHint(ElementHints.KEY_OBJECT);
\r
84 final Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
\r
85 if (data instanceof Resource) {
\r
86 final Resource element = (Resource) data;
\r
87 final int stateMask = me.stateMask;
\r
89 return sessionContext.getSession().syncRequest(new UniqueRead<Boolean>() {
\r
91 public Boolean perform(ReadGraph graph) throws DatabaseException {
\r
92 return browse(graph, element, runtime, stateMask);
\r
95 } catch (DatabaseException e1) {
\r
96 ErrorLogger.defaultLogError(e1);
\r
104 private boolean browse(ReadGraph graph, Resource element, Resource runtimeDiagram, int stateMask) throws DatabaseException {
\r
105 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
106 ModelingResources mr = ModelingResources.getInstance(graph);
\r
107 DiagramResource dr = DiagramResource.getInstance(graph);
\r
108 StructuralResource2 sr = StructuralResource2.getInstance(graph);
\r
110 if (graph.isInstanceOf(element, dr.Flag)) {
\r
111 ContextMap parameters = new ContextMap();
\r
112 parameters.put(ModelingOperationConstants.WORKBENCH_WINDOW, site.getWorkbenchWindow());
\r
113 parameters.put(ModelingOperationConstants.WORKBENCH_PART, site.getPart());
\r
114 parameters.put(IOperation.SUBJECT, element);
\r
115 new NavigateToTarget().exec(graph.getSession(), parameters);
\r
116 // Return true only if the flag is joined somewhere and navigation is possible.
\r
117 return FlagUtil.isJoined(graph, element);
\r
120 if((stateMask & MouseDoubleClickedEvent.SHIFT_MASK) == 0)
\r
123 if(runtimeDiagram == null || !graph.isInstanceOf(runtimeDiagram, DIA.RuntimeDiagram))
\r
126 final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);
\r
127 if (component == null)
\r
130 Resource type = graph.getPossibleType(component, sr.Component);
\r
133 if(graph.hasStatement(type, sr.ComponentType_Locked))
\r
136 Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);
\r
137 if (definedBy == null)
\r
140 final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);
\r
141 if (diagram == null)
\r
144 Resource model = input.getModel(graph);
\r
145 RVI subprocessRVI = null;
\r
147 String rvis = (String) graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasRVI);
\r
148 if (rvis != null) {
\r
149 RVI rvi = RVI.fromResourceFormat(graph, rvis);
\r
150 Variable base = Variables.getConfigurationContext(graph, model);
\r
151 Variable current = rvi.resolve(graph, base);
\r
152 Variable subprocess = current.browse(graph, component);
\r
153 subprocessRVI = subprocess.getRVI(graph);
\r
155 // Not in a model configuration context.
\r
156 // Probably in a component type configuration.
\r
157 // Use the index root of the diagram to be opened as model[0]
\r
158 // and leave subprocessRVI[0] as null.
\r
159 model = graph.syncRequest(new PossibleIndexRoot(definedBy));
\r
162 PlatformUI.getWorkbench().getDisplay().asyncExec(navigateToSubstructure(diagram, model, subprocessRVI));
\r
166 private Runnable navigateToSubstructure(final Resource diagram, final Resource model, final RVI subprocessRVI) {
\r
167 return new Runnable() {
\r
169 public void run() {
\r
171 WorkbenchUtils.openEditor(site.getId(), new ResourceEditorInput2(site.getId(), diagram, model, subprocessRVI));
\r
172 } catch (PartInitException e) {
\r
173 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to open editor, see exception for details.", e));
\r