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