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