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