]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/HeadlessStructuralBrowsingHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / HeadlessStructuralBrowsingHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.handlers;
13
14 import java.util.Set;
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;
31
32 /**
33  * StructuralBrowsingHandler supports visual browsing into subcomponents through
34  * mouse events.
35  * 
36  * @author Tuukka Lehtonen
37  */
38 public class HeadlessStructuralBrowsingHandler extends AbstractCanvasParticipant {
39
40     @Dependency
41     Selection          selection;
42     ISessionContext    sessionContext;
43     ResourceArray      parentStructuralPath;
44     IDiagramUpdateSupport diagramProvider;
45     
46     public static interface IDiagramUpdateSupport {
47         public void updateDiagram(ResourceArray structuralPath);
48     }
49     
50     public HeadlessStructuralBrowsingHandler(IDiagramUpdateSupport diagramProvider, ISessionContext sessionContext, ResourceArray parentStructuralPath) {
51         assert diagramProvider != null;
52
53         this.diagramProvider = diagramProvider;
54         this.sessionContext = sessionContext;
55         this.parentStructuralPath = parentStructuralPath;
56     }
57
58     @EventHandler(priority = 0)
59     public boolean handleDoubleClick(MouseDoubleClickedEvent me) {
60         if (sessionContext == null)
61             return false;
62
63         Set<IElement> sel = selection.getSelection(0);
64
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() {
71                     @Override
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);
76
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);
84                             */
85                             return;
86                         }
87
88                         final Resource component = graph.getPossibleObject(element, mr.ElementToComponent);
89                         if (component == null)
90                             return;
91
92                         Resource type = graph.getSingleType(component, sr.Component);
93                         if (type == null)
94                             return;
95
96                         Resource definedBy = graph.getPossibleObject(type, sr.IsDefinedBy);
97                         if (definedBy == null)
98                             return;
99
100                         final Resource diagram = graph.getPossibleObject(definedBy, mr.CompositeToDiagram);
101                         if (diagram == null)
102                             return;
103
104                         diagramProvider.updateDiagram(parentStructuralPath.prepended(diagram, component));
105                     }
106                 });
107             }
108
109             return true;
110         }
111
112         return false;
113     }
114
115 }