]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
4e2c6c87fb3996627330296ceaf2dc7edf537447
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * 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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.editor.participant;\r
13 \r
14 import java.awt.geom.AffineTransform;\r
15 import java.awt.geom.Point2D;\r
16 import java.util.Set;\r
17 \r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.common.request.Queries;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;\r
22 import org.simantics.diagram.elements.TextNode;\r
23 import org.simantics.diagram.query.DiagramRequests;\r
24 import org.simantics.g2d.canvas.Hints;\r
25 import org.simantics.g2d.canvas.SGDesignation;\r
26 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
27 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
28 import org.simantics.g2d.diagram.DiagramHints;\r
29 import org.simantics.g2d.diagram.DiagramMutator;\r
30 import org.simantics.g2d.diagram.DiagramUtils;\r
31 import org.simantics.g2d.diagram.IDiagram;\r
32 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
33 import org.simantics.g2d.diagram.participant.ElementPainter;\r
34 import org.simantics.g2d.diagram.participant.Selection;\r
35 import org.simantics.g2d.element.ElementClass;\r
36 import org.simantics.g2d.element.ElementUtils;\r
37 import org.simantics.g2d.element.IElement;\r
38 import org.simantics.g2d.participant.MouseUtil;\r
39 import org.simantics.g2d.participant.MouseUtil.MouseInfo;\r
40 import org.simantics.scenegraph.g2d.G2DParentNode;\r
41 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
42 import org.simantics.scenegraph.g2d.events.KeyEvent;\r
43 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;\r
44 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;\r
45 import org.simantics.scenegraph.g2d.events.MouseEvent;\r
46 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;\r
47 import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
48 import org.simantics.sysdyn.SysdynResource;\r
49 import org.simantics.sysdyn.ui.elements.AuxiliaryFactory;\r
50 import org.simantics.sysdyn.ui.elements.CloudFactory;\r
51 import org.simantics.sysdyn.ui.elements.InputFactory;\r
52 import org.simantics.sysdyn.ui.elements.LoopFactory;\r
53 import org.simantics.sysdyn.ui.elements.ShadowFactory;\r
54 import org.simantics.sysdyn.ui.elements.StockFactory;\r
55 import org.simantics.sysdyn.ui.elements.SysdynElementHints;\r
56 import org.simantics.sysdyn.ui.elements.ValveFactory;\r
57 import org.simantics.ui.SimanticsUI;\r
58 import org.simantics.utils.datastructures.Callback;\r
59 import org.simantics.utils.ui.ExceptionUtils;\r
60 \r
61 public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipant {\r
62 \r
63         private GraphToDiagramSynchronizer synchronizer;\r
64 \r
65         private VariableInformation variableInformation;\r
66 \r
67         @Dependency\r
68         MouseUtil mouseUtil;\r
69 \r
70         @Dependency\r
71         Selection selection;\r
72 \r
73         @Dependency\r
74         ElementPainter diagramPainter;\r
75 \r
76         ShapeNode node;\r
77         G2DParentNode parent;\r
78 \r
79         private boolean createVar;\r
80         private IDiagram createVarDiagram;\r
81 \r
82         @SGInit(designation = SGDesignation.CANVAS)\r
83         public void init(G2DParentNode parent) {\r
84                 this.parent = parent;\r
85         }\r
86 \r
87         public void removeSG() {\r
88                 node.remove();\r
89                 node = null;\r
90                 setDirty();\r
91         }\r
92 \r
93         void updateSG() {\r
94 \r
95                 if (node == null) {\r
96                         node = variableInformation.node;\r
97                 }\r
98 \r
99                 MouseInfo mi = mouseUtil.getMouseInfo(0);\r
100                 if (mi == null)\r
101                         return;\r
102 \r
103                 Point2D newPos = mi.canvasPosition;\r
104                 double x = newPos.getX();\r
105                 double y = newPos.getY();\r
106 \r
107                 AffineTransform origAt = node.getTransform();\r
108                 double oldX = origAt.getTranslateX();\r
109                 double oldY = origAt.getTranslateY();\r
110                 AffineTransform move = new AffineTransform();\r
111                 move.setToTranslation(x - oldX, y - oldY);\r
112                 AffineTransform at2 = new AffineTransform(origAt);\r
113                 at2.preConcatenate(move);\r
114                 node.setTransform(at2);\r
115                 setDirty();\r
116         }\r
117 \r
118         public CreateVariablesShortcutParticipant(GraphToDiagramSynchronizer synchronizer) {\r
119                 this.synchronizer = synchronizer;\r
120         }\r
121 \r
122         @EventHandler(priority = -10)\r
123         public boolean handleKeyboardEvent(KeyEvent ke) {\r
124                 \r
125                 // ignore this event if we are not in the unmodified pointer tool mode\r
126                 if (!Hints.POINTERTOOL.equals(getHint(Hints.KEY_TOOL)) || getHint(SysdynElementHints.SYSDYN_KEY_TOOL) != null) {\r
127                         return false;\r
128                 }\r
129 \r
130                 KeyPressedEvent kpe;\r
131                 if (ke instanceof KeyPressedEvent) {\r
132                         \r
133                         kpe = (KeyPressedEvent) ke;\r
134                         \r
135                         if (!kpe.isShiftDown() || isEditing()) \r
136                                 return false;\r
137                         \r
138                         if (kpe.keyCode == java.awt.event.KeyEvent.VK_A) {\r
139                                 variableInformation = new VariableInformation(\r
140                                 java.awt.event.KeyEvent.VK_A,\r
141                                 SysdynResource.URIs.AuxiliarySymbol,\r
142                                 (ShapeNode)AuxiliaryFactory.AUX_STATIC_IMAGE.init(parent)\r
143                                 );\r
144                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_S) {\r
145                                 variableInformation = new VariableInformation(\r
146                                                 java.awt.event.KeyEvent.VK_S,\r
147                                                 SysdynResource.URIs.StockSymbol,\r
148                                                 (ShapeNode)StockFactory.STOCK_IMAGE.init(parent)\r
149                                                 );\r
150                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_C) {\r
151                                 variableInformation = new VariableInformation(\r
152                                                 java.awt.event.KeyEvent.VK_C,\r
153                                                 SysdynResource.URIs.CloudSymbol,\r
154                                                 (ShapeNode)CloudFactory.CLOUD_IMAGE.init(parent)\r
155                                                 );\r
156                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_V) {\r
157                                 variableInformation = new VariableInformation(\r
158                                                 java.awt.event.KeyEvent.VK_V,\r
159                                                 SysdynResource.URIs.ValveSymbol,\r
160                                                 (ShapeNode)ValveFactory.VALVE_STATIC_IMAGE.init(parent)\r
161                                                 );\r
162                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_I) {\r
163                                 variableInformation = new VariableInformation(\r
164                                                 java.awt.event.KeyEvent.VK_I,\r
165                                                 SysdynResource.URIs.InputSymbol,\r
166                                                 (ShapeNode)InputFactory.INPUT_IMAGE.init(parent)\r
167                                                 );\r
168                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_G) {\r
169                 variableInformation = new VariableInformation(\r
170                         java.awt.event.KeyEvent.VK_G,\r
171                         SysdynResource.URIs.ShadowSymbol,\r
172                         (ShapeNode)ShadowFactory.GHOST_IMAGE.init(parent)\r
173                         );\r
174                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_L) {\r
175                 variableInformation = new VariableInformation(\r
176                         java.awt.event.KeyEvent.VK_L,\r
177                         SysdynResource.URIs.LoopSymbol,\r
178                         (ShapeNode)LoopFactory.LOOP_STATIC_IMAGE.init(parent)\r
179                         );\r
180             }\r
181 \r
182                         if (variableInformation != null) {\r
183                                 updateSG();\r
184                                 return true;\r
185                         }\r
186                 }\r
187 \r
188                 KeyReleasedEvent kre;\r
189                 if (ke instanceof KeyReleasedEvent) {\r
190                         kre = (KeyReleasedEvent) ke;\r
191                         \r
192                         if (variableInformation != null\r
193                                         && (kre.keyCode == variableInformation.shortcutKey || kre.keyCode == java.awt.event.KeyEvent.VK_SHIFT)) {\r
194                                 if (node != null) {\r
195                                         // If there is a variable to be created, do it when a key is released.\r
196                                         if (createVar) {\r
197                                                 createVar = false;\r
198                                                 createVariableOnDiagram(createVarDiagram);\r
199                                         }\r
200                                         variableInformation = null;\r
201                                         removeSG();\r
202                                         return true;\r
203                                 }\r
204                         }\r
205                 }\r
206 \r
207                 return false;\r
208 \r
209         }\r
210 \r
211         @EventHandler(priority = -10)\r
212         public boolean handleMouse(MouseMovedEvent e) {\r
213 \r
214                 if (variableInformation != null ) {\r
215                         updateSG();\r
216                 } else {\r
217                         if (node != null) {\r
218                                 removeSG();\r
219                         }\r
220                 }\r
221                 return false;\r
222         }\r
223 \r
224 \r
225         @EventHandler(priority = 100)\r
226         public boolean handleMouseEvent(MouseEvent me) {\r
227 \r
228 \r
229                 MouseEvent.MouseClickEvent mce;\r
230                 if (me instanceof MouseEvent.MouseClickEvent) {\r
231                         mce = (MouseEvent.MouseClickEvent) me;\r
232                 } else {\r
233                         return false;\r
234                 }\r
235                 \r
236                 // only create a variable if we are in the unmodified pointer tool mode\r
237                 if (!Hints.POINTERTOOL.equals(getHint(Hints.KEY_TOOL)) || getHint(SysdynElementHints.SYSDYN_KEY_TOOL) != null) {\r
238                         return false;\r
239                 }\r
240 \r
241                 if (!\r
242                                 (\r
243                                                 mce.button == MouseEvent.LEFT_BUTTON && \r
244                                                 variableInformation != null && \r
245                                                 mce.stateMask ==  MouseEvent.SHIFT_MASK\r
246                                 )) \r
247                 {\r
248                         return false;\r
249                 }\r
250                 \r
251                 final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);\r
252                 if (d == null)\r
253                         return false;\r
254                 \r
255                 // Need to create a new variable, save the diagram to do this later.\r
256                 createVar = true;\r
257                 createVarDiagram = d;\r
258 \r
259                 return true;\r
260         }\r
261 \r
262 \r
263         private void createVariableOnDiagram(IDiagram d) {\r
264                 DiagramUtils.mutateDiagram(d, new Callback<DiagramMutator>() {\r
265                         @Override\r
266                         public void run(DiagramMutator m) {\r
267 \r
268                                 Resource r;\r
269                                 try {\r
270                                         r = SimanticsUI\r
271                                         .getSession()\r
272                                         .syncRequest(\r
273                                                         Queries\r
274                                                         .resource(variableInformation.symbolURI));\r
275                                         ElementClass ec = SimanticsUI.getSession().syncRequest(\r
276                                                         DiagramRequests.getElementClass(r, diagram));\r
277 \r
278                                         IElement element = m.newElement(ec);\r
279 \r
280                                         // MouseUtil mutil = new MouseUtil();\r
281                                         MouseInfo minfo = mouseUtil.getMouseInfo(0);\r
282 \r
283                                         //at least when using breakpoints this is possible\r
284                                         if(minfo == null) \r
285                                                 return;\r
286 \r
287                                         Point2D p = minfo.canvasPosition;\r
288                                         //FIXME - Arto element doesn't know its size at first. Hopefully temp fix.\r
289                                         p.setLocation(p.getX()-5.46, p.getY()+1);\r
290 \r
291                                         ElementUtils.setPos(element, p);\r
292 \r
293                                 } catch (DatabaseException e) {\r
294                                         ExceptionUtils.logAndShowError(e);\r
295                                 }\r
296 \r
297                         }\r
298                 });\r
299 \r
300                 synchronizer.getCanvasContext().getContentContext().setDirty();\r
301 \r
302         }\r
303 \r
304         private class VariableInformation {\r
305                 public String symbolURI;\r
306                 public ShapeNode node;\r
307                 public int shortcutKey;\r
308 \r
309                 public VariableInformation(int shortcutKey, String symbolURI, ShapeNode node) {\r
310                         this.symbolURI = symbolURI;\r
311                         this.node = node;\r
312                         this.shortcutKey = shortcutKey;\r
313                 }\r
314         }\r
315         \r
316         private boolean isEditing() {\r
317         int selectionId = 0;\r
318         Set<IElement> ss = selection.getSelection(selectionId);\r
319         if (ss.isEmpty()) {\r
320             return false;\r
321         }\r
322         for (IElement e : ss) {\r
323                 for(Object o : e.getHints().values()) {\r
324                         if (o instanceof TextNode) {\r
325                                 TextNode tn = (TextNode) o;\r
326                                 if(tn.isEditMode())\r
327                                         return true;\r
328                         }\r
329                 }\r
330         }\r
331                 return false;\r
332         }\r
333 \r
334 }\r