]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
7866f7d8c3b1e76a226642c8cd5da4bf765297ed
[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         @SGInit(designation = SGDesignation.CANVAS)\r
77         public void init(G2DParentNode parent) {\r
78                 this.parent = parent;\r
79         }\r
80 \r
81         public void removeSG() {\r
82                 node.remove();\r
83                 node = null;\r
84                 setDirty();\r
85         }\r
86 \r
87         void updateSG() {\r
88 \r
89                 if (node == null) {\r
90                         node = variableInformation.node;\r
91                 }\r
92 \r
93                 MouseInfo mi = mouseUtil.getMouseInfo(0);\r
94                 if (mi == null)\r
95                         return;\r
96 \r
97                 Point2D newPos = mi.canvasPosition;\r
98                 double x = newPos.getX();\r
99                 double y = newPos.getY();\r
100 \r
101                 AffineTransform origAt = node.getTransform();\r
102                 double oldX = origAt.getTranslateX();\r
103                 double oldY = origAt.getTranslateY();\r
104                 AffineTransform move = new AffineTransform();\r
105                 move.setToTranslation(x - oldX, y - oldY);\r
106                 AffineTransform at2 = new AffineTransform(origAt);\r
107                 at2.preConcatenate(move);\r
108                 node.setTransform(at2);\r
109                 setDirty();\r
110         }\r
111 \r
112         public CreateVariablesShortcutParticipant(GraphToDiagramSynchronizer synchronizer) {\r
113                 this.synchronizer = synchronizer;\r
114         }\r
115 \r
116         @EventHandler(priority = -10)\r
117         public boolean handleKeyboardEvent(KeyEvent ke) {\r
118 \r
119                 KeyPressedEvent kpe;\r
120                 if (ke instanceof KeyPressedEvent) {\r
121                         \r
122                         kpe = (KeyPressedEvent) ke;\r
123                         \r
124                         if (!kpe.isShiftDown() || isEditing()) \r
125                                 return false;\r
126                         \r
127                         if (kpe.keyCode == java.awt.event.KeyEvent.VK_A) {\r
128                                 variableInformation = new VariableInformation(\r
129                                 java.awt.event.KeyEvent.VK_A,\r
130                                 SysdynResource.URIs.AuxiliarySymbol,\r
131                                 (ShapeNode)AuxiliaryFactory.AUX_STATIC_IMAGE.init(parent)\r
132                                 );\r
133                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_S) {\r
134                                 variableInformation = new VariableInformation(\r
135                                                 java.awt.event.KeyEvent.VK_S,\r
136                                                 SysdynResource.URIs.StockSymbol,\r
137                                                 (ShapeNode)StockFactory.STOCK_IMAGE.init(parent)\r
138                                                 );\r
139                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_C) {\r
140                                 variableInformation = new VariableInformation(\r
141                                                 java.awt.event.KeyEvent.VK_C,\r
142                                                 SysdynResource.URIs.CloudSymbol,\r
143                                                 (ShapeNode)CloudFactory.CLOUD_IMAGE.init(parent)\r
144                                                 );\r
145                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_V) {\r
146                                 variableInformation = new VariableInformation(\r
147                                                 java.awt.event.KeyEvent.VK_V,\r
148                                                 SysdynResource.URIs.ValveSymbol,\r
149                                                 (ShapeNode)ValveFactory.VALVE_STATIC_IMAGE.init(parent)\r
150                                                 );\r
151                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_I) {\r
152                                 variableInformation = new VariableInformation(\r
153                                                 java.awt.event.KeyEvent.VK_I,\r
154                                                 SysdynResource.URIs.InputSymbol,\r
155                                                 (ShapeNode)InputFactory.INPUT_IMAGE.init(parent)\r
156                                                 );\r
157                         } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_G) {\r
158                 variableInformation = new VariableInformation(\r
159                         java.awt.event.KeyEvent.VK_G,\r
160                         SysdynResource.URIs.ShadowSymbol,\r
161                         (ShapeNode)ShadowFactory.GHOST_IMAGE.init(parent)\r
162                         );\r
163             }\r
164 \r
165                         if (variableInformation != null) {\r
166                                 updateSG();\r
167                                 return true;\r
168                         }\r
169                 }\r
170 \r
171                 KeyReleasedEvent kre;\r
172                 if (ke instanceof KeyReleasedEvent) {\r
173                         kre = (KeyReleasedEvent) ke;\r
174                         \r
175                         if (variableInformation != null\r
176                                         && (kre.keyCode == variableInformation.shortcutKey || kre.keyCode == java.awt.event.KeyEvent.VK_SHIFT)) {\r
177                                 if (node != null) {\r
178                                         variableInformation = null;\r
179                                         removeSG();\r
180                                         return true;\r
181                                 }\r
182                         }\r
183                 }\r
184 \r
185                 return false;\r
186 \r
187         }\r
188 \r
189         @EventHandler(priority = -10)\r
190         public boolean handleMouse(MouseMovedEvent e) {\r
191 \r
192                 if (variableInformation != null ) {\r
193                         updateSG();\r
194                 } else {\r
195                         if (node != null) {\r
196                                 removeSG();\r
197                         }\r
198                 }\r
199                 return false;\r
200         }\r
201 \r
202 \r
203         @EventHandler(priority = 100)\r
204         public boolean handleMouseEvent(MouseEvent me) {\r
205 \r
206 \r
207                 MouseEvent.MouseClickEvent mce;\r
208                 if (me instanceof MouseEvent.MouseClickEvent) {\r
209                         mce = (MouseEvent.MouseClickEvent) me;\r
210                 } else {\r
211                         return false;\r
212                 }\r
213 \r
214                 if (!\r
215                                 (\r
216                                                 mce.button == MouseEvent.LEFT_BUTTON && \r
217                                                 variableInformation != null && \r
218                                                 mce.stateMask ==  MouseEvent.SHIFT_MASK\r
219                                 )) \r
220                 {\r
221                         return false;\r
222                 }\r
223 \r
224 \r
225                 final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);\r
226                 if (d == null)\r
227                         return false;\r
228 \r
229                 DiagramUtils.mutateDiagram(d, new Callback<DiagramMutator>() {\r
230                         @Override\r
231                         public void run(DiagramMutator m) {\r
232 \r
233                                 Resource r;\r
234                                 try {\r
235                                         r = SimanticsUI\r
236                                         .getSession()\r
237                                         .syncRequest(\r
238                                                         Queries\r
239                                                         .resource(variableInformation.symbolURI));\r
240                                         ElementClass ec = SimanticsUI.getSession().syncRequest(\r
241                                                         DiagramRequests.getElementClass(r, diagram));\r
242 \r
243                                         IElement element = m.newElement(ec);\r
244 \r
245                                         // MouseUtil mutil = new MouseUtil();\r
246                                         MouseInfo minfo = mouseUtil.getMouseInfo(0);\r
247 \r
248                                         //at least when using breakpoints this is possible\r
249                                         if(minfo == null) \r
250                                                 return;\r
251 \r
252                                         Point2D p = minfo.canvasPosition;\r
253                                         //FIXME - Arto element doesn't know its size at first. Hopefully temp fix.\r
254                                         p.setLocation(p.getX()-5.46, p.getY()+1);\r
255 \r
256                                         ElementUtils.setPos(element, p);\r
257 \r
258                                 } catch (DatabaseException e) {\r
259                                         ExceptionUtils.logAndShowError(e);\r
260                                 }\r
261 \r
262                         }\r
263                 });\r
264 \r
265                 synchronizer.getCanvasContext().getContentContext().setDirty();\r
266 \r
267                 return true;\r
268         }\r
269 \r
270         private class VariableInformation {\r
271                 public String symbolURI;\r
272                 public ShapeNode node;\r
273                 public int shortcutKey;\r
274 \r
275                 public VariableInformation(int shortcutKey, String symbolURI, ShapeNode node) {\r
276                         this.symbolURI = symbolURI;\r
277                         this.node = node;\r
278                         this.shortcutKey = shortcutKey;\r
279                 }\r
280         }\r
281         \r
282         private boolean isEditing() {\r
283         int selectionId = 0;\r
284         Set<IElement> ss = selection.getSelection(selectionId);\r
285         if (ss.isEmpty()) {\r
286             return false;\r
287         }\r
288         for (IElement e : ss) {\r
289                 for(Object o : e.getHints().values()) {\r
290                         if (o instanceof TextNode) {\r
291                                 TextNode tn = (TextNode) o;\r
292                                 if(tn.isEditMode())\r
293                                         return true;\r
294                         }\r
295                 }\r
296         }\r
297                 return false;\r
298         }\r
299 \r
300 }\r