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;
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;
54 * StructuralBrowsingHandler supports visual browsing into subcomponents through
57 * @author Tuukka Lehtonen
59 public class StructuralBrowsingHandler extends AbstractDiagramParticipant {
63 IWorkbenchPartSite site;
64 ISessionContext sessionContext;
65 IResourceEditorInput2 input;
67 public StructuralBrowsingHandler(IWorkbenchPartSite site, ISessionContext sessionContext, IResourceEditorInput2 input) {
71 this.sessionContext = sessionContext;
75 @EventHandler(priority = 0)
76 public boolean handleDoubleClick(MouseDoubleClickedEvent me) {
77 if (sessionContext == null || diagram == null)
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;
89 return sessionContext.getSession().syncRequest(new UniqueRead<Boolean>() {
91 public Boolean perform(ReadGraph graph) throws DatabaseException {
92 return browse(graph, element, runtime, stateMask);
95 } catch (DatabaseException e1) {
96 ErrorLogger.defaultLogError(e1);
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);
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);
120 if((stateMask & MouseDoubleClickedEvent.SHIFT_MASK) == 0)
123 if(runtimeDiagram == null || !graph.isInstanceOf(runtimeDiagram, DIA.RuntimeDiagram))
126 final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);
127 if (component == null)
130 Resource type = graph.getPossibleType(component, sr.Component);
133 if(graph.hasStatement(type, sr.ComponentType_Locked))
136 Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);
137 if (definedBy == null)
140 final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);
144 Resource model = input.getModel(graph);
145 RVI subprocessRVI = null;
147 String rvis = (String) graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasRVI);
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);
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));
162 PlatformUI.getWorkbench().getDisplay().asyncExec(navigateToSubstructure(diagram, model, subprocessRVI));
166 private Runnable navigateToSubstructure(final Resource diagram, final Resource model, final RVI subprocessRVI) {
167 return new Runnable() {
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));