1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 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 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.diagramEditor.handlers;
\r
14 import java.util.Set;
\r
15 import org.simantics.db.Resource;
\r
16 import org.simantics.db.ReadGraph;
\r
17 import org.simantics.db.common.ResourceArray;
\r
18 import org.simantics.db.common.request.ReadRequest;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.db.management.ISessionContext;
\r
21 import org.simantics.diagram.stubs.DiagramResource;
\r
22 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
\r
23 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
\r
24 import org.simantics.g2d.diagram.participant.Selection;
\r
25 import org.simantics.g2d.element.ElementHints;
\r
26 import org.simantics.g2d.element.IElement;
\r
27 import org.simantics.modeling.ModelingResources;
\r
28 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
\r
29 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
\r
30 import org.simantics.structural.stubs.StructuralResource2;
\r
33 * StructuralBrowsingHandler supports visual browsing into subcomponents through
\r
36 * @author Tuukka Lehtonen
\r
38 public class HeadlessStructuralBrowsingHandler extends AbstractCanvasParticipant {
\r
41 Selection selection;
\r
42 ISessionContext sessionContext;
\r
43 ResourceArray parentStructuralPath;
\r
44 IDiagramUpdateSupport diagramProvider;
\r
46 public static interface IDiagramUpdateSupport {
\r
47 public void updateDiagram(ResourceArray structuralPath);
\r
50 public HeadlessStructuralBrowsingHandler(IDiagramUpdateSupport diagramProvider, ISessionContext sessionContext, ResourceArray parentStructuralPath) {
\r
51 assert diagramProvider != null;
\r
53 this.diagramProvider = diagramProvider;
\r
54 this.sessionContext = sessionContext;
\r
55 this.parentStructuralPath = parentStructuralPath;
\r
58 @EventHandler(priority = 0)
\r
59 public boolean handleDoubleClick(MouseDoubleClickedEvent me) {
\r
60 if (sessionContext == null)
\r
63 Set<IElement> sel = selection.getSelection(0);
\r
65 if (sel.size() == 1) {
\r
66 IElement e = sel.iterator().next();
\r
67 Object data = e.getHint(ElementHints.KEY_OBJECT);
\r
68 if (data instanceof Resource) {
\r
69 final Resource element = (Resource) data;
\r
70 sessionContext.getSession().asyncRequest(new ReadRequest() {
\r
72 public void run(ReadGraph graph) throws DatabaseException {
\r
73 ModelingResources mr = ModelingResources.getInstance(graph);
\r
74 DiagramResource dr = DiagramResource.getInstance(graph);
\r
75 StructuralResource2 sr = StructuralResource2.getInstance(graph);
\r
77 if (graph.isInstanceOf(element, dr.Flag)) {
\r
78 /* FIXME: implement headless version
\r
79 ContextMap parameters = new ContextMap();
\r
80 parameters.put(ModelingOperationConstants.WORKBENCH_WINDOW, site.getWorkbenchWindow());
\r
81 parameters.put(ModelingOperationConstants.WORKBENCH_PART, site.getPart());
\r
82 parameters.put(IOperation.SUBJECT, element);
\r
83 new NavigateToTarget().exec(graph.getSession(), parameters);
\r
88 final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);
\r
89 if (component == null)
\r
92 Resource type = graph.getSingleType(component, sr.Component);
\r
96 Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);
\r
97 if (definedBy == null)
\r
100 final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);
\r
101 if (diagram == null)
\r
104 diagramProvider.updateDiagram(parentStructuralPath.prepended(diagram, component));
\r