1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.editor.participant;
\r
14 import java.awt.geom.AffineTransform;
\r
15 import java.awt.geom.Point2D;
\r
16 import java.util.Set;
\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.Hints;
\r
25 import org.simantics.g2d.canvas.SGDesignation;
\r
26 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
\r
27 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
\r
28 import org.simantics.g2d.diagram.DiagramHints;
\r
29 import org.simantics.g2d.diagram.DiagramMutator;
\r
30 import org.simantics.g2d.diagram.DiagramUtils;
\r
31 import org.simantics.g2d.diagram.IDiagram;
\r
32 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
\r
33 import org.simantics.g2d.diagram.participant.ElementPainter;
\r
34 import org.simantics.g2d.diagram.participant.Selection;
\r
35 import org.simantics.g2d.element.ElementClass;
\r
36 import org.simantics.g2d.element.ElementUtils;
\r
37 import org.simantics.g2d.element.IElement;
\r
38 import org.simantics.g2d.participant.MouseUtil;
\r
39 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
\r
40 import org.simantics.scenegraph.g2d.G2DParentNode;
\r
41 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
\r
42 import org.simantics.scenegraph.g2d.events.KeyEvent;
\r
43 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
\r
44 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;
\r
45 import org.simantics.scenegraph.g2d.events.MouseEvent;
\r
46 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
\r
47 import org.simantics.scenegraph.g2d.nodes.ShapeNode;
\r
48 import org.simantics.sysdyn.SysdynResource;
\r
49 import org.simantics.sysdyn.ui.elements.AuxiliaryFactory;
\r
50 import org.simantics.sysdyn.ui.elements.CloudFactory;
\r
51 import org.simantics.sysdyn.ui.elements.InputFactory;
\r
52 import org.simantics.sysdyn.ui.elements.LoopFactory;
\r
53 import org.simantics.sysdyn.ui.elements.ShadowFactory;
\r
54 import org.simantics.sysdyn.ui.elements.StockFactory;
\r
55 import org.simantics.sysdyn.ui.elements.SysdynElementHints;
\r
56 import org.simantics.sysdyn.ui.elements.ValveFactory;
\r
57 import org.simantics.ui.SimanticsUI;
\r
58 import org.simantics.utils.datastructures.Callback;
\r
59 import org.simantics.utils.ui.ExceptionUtils;
\r
61 public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipant {
\r
63 private GraphToDiagramSynchronizer synchronizer;
\r
65 private VariableInformation variableInformation;
\r
68 MouseUtil mouseUtil;
\r
71 Selection selection;
\r
74 ElementPainter diagramPainter;
\r
77 G2DParentNode parent;
\r
79 private boolean createVar;
\r
80 private IDiagram createVarDiagram;
\r
82 @SGInit(designation = SGDesignation.CANVAS)
\r
83 public void init(G2DParentNode parent) {
\r
84 this.parent = parent;
\r
87 public void removeSG() {
\r
96 node = variableInformation.node;
\r
99 MouseInfo mi = mouseUtil.getMouseInfo(0);
\r
103 Point2D newPos = mi.canvasPosition;
\r
104 double x = newPos.getX();
\r
105 double y = newPos.getY();
\r
107 AffineTransform origAt = node.getTransform();
\r
108 double oldX = origAt.getTranslateX();
\r
109 double oldY = origAt.getTranslateY();
\r
110 AffineTransform move = new AffineTransform();
\r
111 move.setToTranslation(x - oldX, y - oldY);
\r
112 AffineTransform at2 = new AffineTransform(origAt);
\r
113 at2.preConcatenate(move);
\r
114 node.setTransform(at2);
\r
118 public CreateVariablesShortcutParticipant(GraphToDiagramSynchronizer synchronizer) {
\r
119 this.synchronizer = synchronizer;
\r
122 @EventHandler(priority = -10)
\r
123 public boolean handleKeyboardEvent(KeyEvent ke) {
\r
125 // ignore this event if we are not in the unmodified pointer tool mode
\r
126 if (!Hints.POINTERTOOL.equals(getHint(Hints.KEY_TOOL)) || getHint(SysdynElementHints.SYSDYN_KEY_TOOL) != null) {
\r
130 KeyPressedEvent kpe;
\r
131 if (ke instanceof KeyPressedEvent) {
\r
133 kpe = (KeyPressedEvent) ke;
\r
135 if (!kpe.isShiftDown() || isEditing())
\r
138 if (kpe.keyCode == java.awt.event.KeyEvent.VK_A) {
\r
139 variableInformation = new VariableInformation(
\r
140 java.awt.event.KeyEvent.VK_A,
\r
141 SysdynResource.URIs.AuxiliarySymbol,
\r
142 (ShapeNode)AuxiliaryFactory.AUX_STATIC_IMAGE.init(parent)
\r
144 } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_S) {
\r
145 variableInformation = new VariableInformation(
\r
146 java.awt.event.KeyEvent.VK_S,
\r
147 SysdynResource.URIs.StockSymbol,
\r
148 (ShapeNode)StockFactory.STOCK_IMAGE.init(parent)
\r
150 } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_C) {
\r
151 variableInformation = new VariableInformation(
\r
152 java.awt.event.KeyEvent.VK_C,
\r
153 SysdynResource.URIs.CloudSymbol,
\r
154 (ShapeNode)CloudFactory.CLOUD_IMAGE.init(parent)
\r
156 } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_V) {
\r
157 variableInformation = new VariableInformation(
\r
158 java.awt.event.KeyEvent.VK_V,
\r
159 SysdynResource.URIs.ValveSymbol,
\r
160 (ShapeNode)ValveFactory.VALVE_STATIC_IMAGE.init(parent)
\r
162 } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_I) {
\r
163 variableInformation = new VariableInformation(
\r
164 java.awt.event.KeyEvent.VK_I,
\r
165 SysdynResource.URIs.InputSymbol,
\r
166 (ShapeNode)InputFactory.INPUT_IMAGE.init(parent)
\r
168 } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_G) {
\r
169 variableInformation = new VariableInformation(
\r
170 java.awt.event.KeyEvent.VK_G,
\r
171 SysdynResource.URIs.ShadowSymbol,
\r
172 (ShapeNode)ShadowFactory.GHOST_IMAGE.init(parent)
\r
174 } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_L) {
\r
175 variableInformation = new VariableInformation(
\r
176 java.awt.event.KeyEvent.VK_L,
\r
177 SysdynResource.URIs.LoopSymbol,
\r
178 (ShapeNode)LoopFactory.LOOP_STATIC_IMAGE.init(parent)
\r
182 if (variableInformation != null) {
\r
188 KeyReleasedEvent kre;
\r
189 if (ke instanceof KeyReleasedEvent) {
\r
190 kre = (KeyReleasedEvent) ke;
\r
192 if (variableInformation != null
\r
193 && (kre.keyCode == variableInformation.shortcutKey || kre.keyCode == java.awt.event.KeyEvent.VK_SHIFT)) {
\r
194 if (node != null) {
\r
195 // If there is a variable to be created, do it when a key is released.
\r
198 createVariableOnDiagram(createVarDiagram);
\r
200 variableInformation = null;
\r
211 @EventHandler(priority = -10)
\r
212 public boolean handleMouse(MouseMovedEvent e) {
\r
214 if (variableInformation != null ) {
\r
217 if (node != null) {
\r
225 @EventHandler(priority = 100)
\r
226 public boolean handleMouseEvent(MouseEvent me) {
\r
229 MouseEvent.MouseClickEvent mce;
\r
230 if (me instanceof MouseEvent.MouseClickEvent) {
\r
231 mce = (MouseEvent.MouseClickEvent) me;
\r
236 // only create a variable if we are in the unmodified pointer tool mode
\r
237 if (!Hints.POINTERTOOL.equals(getHint(Hints.KEY_TOOL)) || getHint(SysdynElementHints.SYSDYN_KEY_TOOL) != null) {
\r
243 mce.button == MouseEvent.LEFT_BUTTON &&
\r
244 variableInformation != null &&
\r
245 mce.stateMask == MouseEvent.SHIFT_MASK
\r
251 final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
\r
255 // Need to create a new variable, save the diagram to do this later.
\r
257 createVarDiagram = d;
\r
263 private void createVariableOnDiagram(IDiagram d) {
\r
264 DiagramUtils.mutateDiagram(d, new Callback<DiagramMutator>() {
\r
266 public void run(DiagramMutator m) {
\r
274 .resource(variableInformation.symbolURI));
\r
275 ElementClass ec = SimanticsUI.getSession().syncRequest(
\r
276 DiagramRequests.getElementClass(r, diagram));
\r
278 IElement element = m.newElement(ec);
\r
280 // MouseUtil mutil = new MouseUtil();
\r
281 MouseInfo minfo = mouseUtil.getMouseInfo(0);
\r
283 //at least when using breakpoints this is possible
\r
287 Point2D p = minfo.canvasPosition;
\r
288 //FIXME - Arto element doesn't know its size at first. Hopefully temp fix.
\r
289 p.setLocation(p.getX()-5.46, p.getY()+1);
\r
291 ElementUtils.setPos(element, p);
\r
293 } catch (DatabaseException e) {
\r
294 ExceptionUtils.logAndShowError(e);
\r
300 synchronizer.getCanvasContext().getContentContext().setDirty();
\r
304 private class VariableInformation {
\r
305 public String symbolURI;
\r
306 public ShapeNode node;
\r
307 public int shortcutKey;
\r
309 public VariableInformation(int shortcutKey, String symbolURI, ShapeNode node) {
\r
310 this.symbolURI = symbolURI;
\r
312 this.shortcutKey = shortcutKey;
\r
316 private boolean isEditing() {
\r
317 int selectionId = 0;
\r
318 Set<IElement> ss = selection.getSelection(selectionId);
\r
319 if (ss.isEmpty()) {
\r
322 for (IElement e : ss) {
\r
323 for(Object o : e.getHints().values()) {
\r
324 if (o instanceof TextNode) {
\r
325 TextNode tn = (TextNode) o;
\r
326 if(tn.isEditMode())
\r