]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
16b08a62b1bcd58e21839468cd2b63e85a296b81
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.editor.participant;\r
2 /*******************************************************************************\r
3  * Copyright (c) 2010 Association for Decentralized Information Management in\r
4  * Industry THTH ry.\r
5  * All rights reserved. This program and the accompanying materials\r
6  * are made available under the terms of the Eclipse Public License v1.0\r
7  * which accompanies this distribution, and is available at\r
8  * http://www.eclipse.org/legal/epl-v10.html\r
9  *\r
10  * Contributors:\r
11  *     VTT Technical Research Centre of Finland - initial API and implementation\r
12  *******************************************************************************/\r
13 \r
14 import java.awt.Shape;\r
15 import java.awt.geom.Point2D;\r
16 import java.awt.geom.Rectangle2D;\r
17 import java.util.List;\r
18 \r
19 import org.simantics.diagram.participant.ConnectTool2;\r
20 import org.simantics.g2d.canvas.Hints;\r
21 import org.simantics.g2d.canvas.IToolMode;\r
22 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
23 import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;\r
24 import org.simantics.g2d.connection.IConnectionAdvisor;\r
25 import org.simantics.g2d.diagram.DiagramHints;\r
26 import org.simantics.g2d.diagram.handler.PickContext;\r
27 import org.simantics.g2d.diagram.participant.Selection;\r
28 import org.simantics.g2d.diagram.participant.TerminalPainter;\r
29 import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil;\r
30 import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo;\r
31 import org.simantics.g2d.element.ElementClasses;\r
32 import org.simantics.g2d.element.IElement;\r
33 import org.simantics.g2d.element.IElementClassProvider;\r
34 import org.simantics.g2d.participant.KeyUtil;\r
35 import org.simantics.g2d.participant.MouseUtil;\r
36 import org.simantics.g2d.participant.TransformUtil;\r
37 import org.simantics.g2d.utils.GeometryUtils;\r
38 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
39 import org.simantics.scenegraph.g2d.events.MouseEvent;\r
40 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
41 import org.simantics.sysdyn.ui.editor.participant.SysdynElementClassProviders.ISysdynElementClassProvider;\r
42 import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;\r
43 import org.simantics.sysdyn.ui.editor.routing.FlowRouter;\r
44 import org.simantics.sysdyn.ui.elements.AuxiliaryFactory;\r
45 import org.simantics.sysdyn.ui.elements.CloudFactory;\r
46 import org.simantics.sysdyn.ui.elements.InputFactory;\r
47 import org.simantics.sysdyn.ui.elements.ModuleFactory;\r
48 import org.simantics.sysdyn.ui.elements.connections.ConnectionClasses;\r
49 \r
50 /**\r
51  * Pointer tool does the following operations with mouse:\r
52  *   - Selections\r
53  *   - Scale\r
54  *   - Rotate\r
55  *   - Translate\r
56  *   - Draws connections\r
57  *\r
58  *\r
59  * Pointer tool is active only when KEY_TOOLMODE is PointerToolMode\r
60  *\r
61  * TODO Pick rectangle not a point\r
62  *\r
63  * @author Toni Kalajainen\r
64  */\r
65 public class PointerInteractor extends org.simantics.diagram.participant.PointerInteractor2 {\r
66 \r
67         @Dependency Selection selection;\r
68         @Dependency KeyUtil keys;\r
69         @Dependency TransformUtil util;\r
70         @Dependency PickContext pickContext;\r
71         @Dependency MouseUtil mice;\r
72         @Reference TerminalPainter terminalPainter;\r
73 \r
74         public PointerInteractor(boolean clickSelect, boolean boxSelect, boolean dragElement, boolean dndDragElement, boolean connect, boolean doubleClickEdit, IElementClassProvider newConnectionClassProvider) {\r
75                 super(clickSelect, boxSelect, dragElement, dndDragElement, connect, doubleClickEdit, newConnectionClassProvider);\r
76         }\r
77 \r
78         @EventHandler(priority = TOOL_PRIORITY)\r
79         public boolean handlePress(MouseButtonPressedEvent me) {\r
80                 if (!connects()) return false;\r
81                 \r
82                 IToolMode mode = getHint(Hints.KEY_TOOL);\r
83 \r
84                 if (mode != Hints.POINTERTOOL && mode != Hints.CONNECTTOOL) return false;\r
85                 assertDependencies();\r
86                 Point2D         curCanvasPos    = util.controlToCanvas(me.controlPosition, null);\r
87 \r
88                 // Pick Terminal\r
89                 TerminalInfo ti = pickTerminal(me.controlPosition);\r
90 \r
91                 if((me.stateMask & MouseEvent.ALT_MASK) == 0) return false;\r
92 \r
93                 if(elementClassProvider != null) {\r
94                         ConnectTool2 bsi = null;\r
95                         if (ti != null) {\r
96 \r
97                                 IElement terminalElement = ti.e;\r
98 \r
99                                 if( me.button == MouseEvent.LEFT_BUTTON) {\r
100                                         if(terminalElement.getElementClass().getId().equals(CloudFactory.class.getSimpleName())) return false;\r
101                                         diagram.setHint(DiagramHints.ROUTE_ALGORITHM, new DependencyRouter());\r
102                                         diagram.setHint(DiagramHints.KEY_USE_CONNECTION_FLAGS, false);\r
103                                         ISysdynElementClassProvider secp = (ISysdynElementClassProvider)elementClassProvider;\r
104                                         secp.put(ElementClasses.CONNECTION, elementClassProvider.get(ConnectionClasses.DEPENDENCY));\r
105 \r
106                                 } else if (me.button == MouseEvent.RIGHT_BUTTON) {\r
107                                         String id  = terminalElement.getElementClass().getId();\r
108                                         if(id.equals(AuxiliaryFactory.class.getSimpleName())\r
109                                                         || id.equals(InputFactory.class.getSimpleName())\r
110                                                         || id.equals(ModuleFactory.class.getSimpleName())) return false;\r
111                                         diagram.setHint(DiagramHints.ROUTE_ALGORITHM, new FlowRouter(false));\r
112                                         diagram.setHint(DiagramHints.KEY_USE_CONNECTION_FLAGS, true);\r
113                                         ISysdynElementClassProvider secp = (ISysdynElementClassProvider)elementClassProvider;\r
114                                         secp.put(ElementClasses.CONNECTION, elementClassProvider.get(ConnectionClasses.FLOW));\r
115 \r
116                                 }\r
117 \r
118                                 IConnectionAdvisor advisor = diagram.getHint(DiagramHints.CONNECTION_ADVISOR);\r
119                                 if (advisor == null || (advisor != null && advisor.canBeginConnection(null, ti.e, ti.t))) {\r
120                                         bsi = new SysdynConnectTool(ti, me.mouseId, curCanvasPos);\r
121                                 }\r
122                         }  \r
123                         else if (me.button == MouseEvent.RIGHT_BUTTON) {\r
124                                  // Start connection out of thin air, without a terminal.\r
125                                  diagram.setHint(DiagramHints.ROUTE_ALGORITHM, new FlowRouter(false));\r
126                                  diagram.setHint(DiagramHints.KEY_USE_CONNECTION_FLAGS, true);\r
127                                  ISysdynElementClassProvider secp = (ISysdynElementClassProvider)elementClassProvider;\r
128                                  secp.put(ElementClasses.CONNECTION, elementClassProvider.get(ConnectionClasses.FLOW));\r
129                                  bsi = new SysdynConnectTool(null, me.mouseId, curCanvasPos);\r
130                         }\r
131 \r
132                         if (bsi != null) {\r
133                                 getContext().add(bsi);\r
134                                 return true;\r
135                         }\r
136 \r
137                 }\r
138                 return false;\r
139         }\r
140 \r
141         public List<TerminalInfo> pickTerminals(Point2D controlPos)\r
142         {\r
143                 Rectangle2D     controlPickRect     = new Rectangle2D.Double(controlPos.getX()-PointerInteractor.PICK_DIST, controlPos.getY()-PointerInteractor.PICK_DIST, PointerInteractor.PICK_DIST*2+1, PointerInteractor.PICK_DIST*2+1);\r
144                 Shape           canvasPickRect      = GeometryUtils.transformShape(controlPickRect, util.getInverseTransform());\r
145                 List<TerminalInfo> ti = TerminalUtil.pickTerminals(diagram, canvasPickRect, false, true);\r
146                 return ti;\r
147         }\r
148 }\r