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