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