-/*******************************************************************************\r
- * Copyright (c) 2007, 2014 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- * Semantum Oy - support SHIFT+left double click browsing outside of model configuration\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.diagramEditor.handlers;\r
-\r
-import java.util.Set;\r
-\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.ui.IWorkbenchPartSite;\r
-import org.eclipse.ui.PartInitException;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.PossibleIndexRoot;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.variable.RVI;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.diagram.flag.FlagUtil;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.diagram.ui.DiagramModelHints;\r
-import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
-import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
-import org.simantics.g2d.diagram.participant.Selection;\r
-import org.simantics.g2d.element.ElementHints;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.layer0.utils.operations.IOperation;\r
-import org.simantics.modeling.ModelingOperationConstants;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.modeling.actions.NavigateToTarget;\r
-import org.simantics.modeling.ui.Activator;\r
-import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.ui.workbench.IResourceEditorInput2;\r
-import org.simantics.ui.workbench.ResourceEditorInput2;\r
-import org.simantics.utils.datastructures.persistent.ContextMap;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
-\r
-/**\r
- * StructuralBrowsingHandler supports visual browsing into subcomponents through\r
- * mouse events.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class StructuralBrowsingHandler extends AbstractDiagramParticipant {\r
-\r
- @Dependency\r
- Selection selection;\r
- IWorkbenchPartSite site;\r
- ISessionContext sessionContext;\r
- IResourceEditorInput2 input;\r
-\r
- public StructuralBrowsingHandler(IWorkbenchPartSite site, ISessionContext sessionContext, IResourceEditorInput2 input) {\r
- assert site != null;\r
-\r
- this.site = site;\r
- this.sessionContext = sessionContext;\r
- this.input = input;\r
- }\r
-\r
- @EventHandler(priority = 0)\r
- public boolean handleDoubleClick(MouseDoubleClickedEvent me) {\r
- if (sessionContext == null || diagram == null)\r
- return false;\r
- \r
- Set<IElement> sel = selection.getSelection(0);\r
- if (sel.size() == 1) {\r
- IElement e = sel.iterator().next();\r
- Object data = e.getHint(ElementHints.KEY_OBJECT);\r
- final Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
- if (data instanceof Resource) {\r
- final Resource element = (Resource) data;\r
- final int stateMask = me.stateMask;\r
- try {\r
- return sessionContext.getSession().syncRequest(new UniqueRead<Boolean>() {\r
- @Override\r
- public Boolean perform(ReadGraph graph) throws DatabaseException {\r
- return browse(graph, element, runtime, stateMask);\r
- }\r
- });\r
- } catch (DatabaseException e1) {\r
- ErrorLogger.defaultLogError(e1);\r
- }\r
- }\r
- }\r
-\r
- return false;\r
- }\r
- \r
- private boolean browse(ReadGraph graph, Resource element, Resource runtimeDiagram, int stateMask) throws DatabaseException {\r
- DiagramResource DIA = DiagramResource.getInstance(graph);\r
- ModelingResources mr = ModelingResources.getInstance(graph);\r
- DiagramResource dr = DiagramResource.getInstance(graph);\r
- StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
-\r
- if (graph.isInstanceOf(element, dr.Flag)) {\r
- ContextMap parameters = new ContextMap();\r
- parameters.put(ModelingOperationConstants.WORKBENCH_WINDOW, site.getWorkbenchWindow());\r
- parameters.put(ModelingOperationConstants.WORKBENCH_PART, site.getPart());\r
- parameters.put(IOperation.SUBJECT, element);\r
- new NavigateToTarget().exec(graph.getSession(), parameters);\r
- // Return true only if the flag is joined somewhere and navigation is possible.\r
- return FlagUtil.isJoined(graph, element);\r
- }\r
-\r
- if((stateMask & MouseDoubleClickedEvent.SHIFT_MASK) == 0)\r
- return false;\r
-\r
- if(runtimeDiagram == null || !graph.isInstanceOf(runtimeDiagram, DIA.RuntimeDiagram))\r
- return false;\r
-\r
- final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);\r
- if (component == null)\r
- return false;\r
-\r
- Resource type = graph.getPossibleType(component, sr.Component);\r
- if (type == null)\r
- return false;\r
- if(graph.hasStatement(type, sr.ComponentType_Locked))\r
- return false;\r
-\r
- Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);\r
- if (definedBy == null)\r
- return false;\r
-\r
- final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);\r
- if (diagram == null)\r
- return false;\r
-\r
- Resource model = input.getModel(graph);\r
- RVI subprocessRVI = null;\r
-\r
- String rvis = (String) graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasRVI);\r
- if (rvis != null) {\r
- RVI rvi = RVI.fromResourceFormat(graph, rvis);\r
- Variable base = Variables.getConfigurationContext(graph, model);\r
- Variable current = rvi.resolve(graph, base);\r
- Variable subprocess = current.browse(graph, component);\r
- subprocessRVI = subprocess.getRVI(graph);\r
- } else {\r
- // Not in a model configuration context.\r
- // Probably in a component type configuration.\r
- // Use the index root of the diagram to be opened as model[0]\r
- // and leave subprocessRVI[0] as null.\r
- model = graph.syncRequest(new PossibleIndexRoot(definedBy));\r
- }\r
-\r
- PlatformUI.getWorkbench().getDisplay().asyncExec(navigateToSubstructure(diagram, model, subprocessRVI));\r
- return true;\r
- }\r
-\r
- private Runnable navigateToSubstructure(final Resource diagram, final Resource model, final RVI subprocessRVI) {\r
- return new Runnable() {\r
- @Override\r
- public void run() {\r
- try {\r
- WorkbenchUtils.openEditor(site.getId(), new ResourceEditorInput2(site.getId(), diagram, model, subprocessRVI));\r
- } catch (PartInitException e) {\r
- Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to open editor, see exception for details.", e));\r
- }\r
- }\r
- };\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2014 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ * Semantum Oy - support SHIFT+left double click browsing outside of model configuration
+ *******************************************************************************/
+package org.simantics.modeling.ui.diagramEditor.handlers;
+
+import java.util.Set;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.ui.IWorkbenchPartSite;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.PossibleIndexRoot;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.RVI;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.diagram.flag.FlagUtil;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
+import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
+import org.simantics.g2d.diagram.participant.Selection;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.IElement;
+import org.simantics.layer0.utils.operations.IOperation;
+import org.simantics.modeling.ModelingOperationConstants;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.modeling.actions.NavigateToTarget;
+import org.simantics.modeling.ui.Activator;
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.ui.workbench.IResourceEditorInput2;
+import org.simantics.ui.workbench.ResourceEditorInput2;
+import org.simantics.utils.datastructures.persistent.ContextMap;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+
+/**
+ * StructuralBrowsingHandler supports visual browsing into subcomponents through
+ * mouse events.
+ *
+ * @author Tuukka Lehtonen
+ */
+public class StructuralBrowsingHandler extends AbstractDiagramParticipant {
+
+ @Dependency
+ Selection selection;
+ IWorkbenchPartSite site;
+ ISessionContext sessionContext;
+ IResourceEditorInput2 input;
+
+ public StructuralBrowsingHandler(IWorkbenchPartSite site, ISessionContext sessionContext, IResourceEditorInput2 input) {
+ assert site != null;
+
+ this.site = site;
+ this.sessionContext = sessionContext;
+ this.input = input;
+ }
+
+ @EventHandler(priority = 0)
+ public boolean handleDoubleClick(MouseDoubleClickedEvent me) {
+ if (sessionContext == null || diagram == null)
+ return false;
+
+ Set<IElement> sel = selection.getSelection(0);
+ if (sel.size() == 1) {
+ IElement e = sel.iterator().next();
+ Object data = e.getHint(ElementHints.KEY_OBJECT);
+ final Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
+ if (data instanceof Resource) {
+ final Resource element = (Resource) data;
+ final int stateMask = me.stateMask;
+ try {
+ return sessionContext.getSession().syncRequest(new UniqueRead<Boolean>() {
+ @Override
+ public Boolean perform(ReadGraph graph) throws DatabaseException {
+ return browse(graph, element, runtime, stateMask);
+ }
+ });
+ } catch (DatabaseException e1) {
+ ErrorLogger.defaultLogError(e1);
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private boolean browse(ReadGraph graph, Resource element, Resource runtimeDiagram, int stateMask) throws DatabaseException {
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ ModelingResources mr = ModelingResources.getInstance(graph);
+ DiagramResource dr = DiagramResource.getInstance(graph);
+ StructuralResource2 sr = StructuralResource2.getInstance(graph);
+
+ if (graph.isInstanceOf(element, dr.Flag)) {
+ ContextMap parameters = new ContextMap();
+ parameters.put(ModelingOperationConstants.WORKBENCH_WINDOW, site.getWorkbenchWindow());
+ parameters.put(ModelingOperationConstants.WORKBENCH_PART, site.getPart());
+ parameters.put(IOperation.SUBJECT, element);
+ new NavigateToTarget().exec(graph.getSession(), parameters);
+ // Return true only if the flag is joined somewhere and navigation is possible.
+ return FlagUtil.isJoined(graph, element);
+ }
+
+ if((stateMask & MouseDoubleClickedEvent.SHIFT_MASK) == 0)
+ return false;
+
+ if(runtimeDiagram == null || !graph.isInstanceOf(runtimeDiagram, DIA.RuntimeDiagram))
+ return false;
+
+ final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);
+ if (component == null)
+ return false;
+
+ Resource type = graph.getPossibleType(component, sr.Component);
+ if (type == null)
+ return false;
+ if(graph.hasStatement(type, sr.ComponentType_Locked))
+ return false;
+
+ Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);
+ if (definedBy == null)
+ return false;
+
+ final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);
+ if (diagram == null)
+ return false;
+
+ Resource model = input.getModel(graph);
+ RVI subprocessRVI = null;
+
+ String rvis = (String) graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasRVI);
+ if (rvis != null) {
+ RVI rvi = RVI.fromResourceFormat(graph, rvis);
+ Variable base = Variables.getConfigurationContext(graph, model);
+ Variable current = rvi.resolve(graph, base);
+ Variable subprocess = current.browse(graph, component);
+ subprocessRVI = subprocess.getRVI(graph);
+ } else {
+ // Not in a model configuration context.
+ // Probably in a component type configuration.
+ // Use the index root of the diagram to be opened as model[0]
+ // and leave subprocessRVI[0] as null.
+ model = graph.syncRequest(new PossibleIndexRoot(definedBy));
+ }
+
+ PlatformUI.getWorkbench().getDisplay().asyncExec(navigateToSubstructure(diagram, model, subprocessRVI));
+ return true;
+ }
+
+ private Runnable navigateToSubstructure(final Resource diagram, final Resource model, final RVI subprocessRVI) {
+ return new Runnable() {
+ @Override
+ public void run() {
+ try {
+ WorkbenchUtils.openEditor(site.getId(), new ResourceEditorInput2(site.getId(), diagram, model, subprocessRVI));
+ } catch (PartInitException e) {
+ Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to open editor, see exception for details.", e));
+ }
+ }
+ };
+ }
+
+}