1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.sysdyn.ui.editor.participant;
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Point2D;
17 import java.util.function.Consumer;
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;
61 public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipant {
63 private GraphToDiagramSynchronizer synchronizer;
65 private VariableInformation variableInformation;
74 ElementPainter diagramPainter;
79 private boolean createVar;
80 private IDiagram createVarDiagram;
82 @SGInit(designation = SGDesignation.CANVAS)
83 public void init(G2DParentNode parent) {
87 public void removeSG() {
96 node = variableInformation.node;
99 MouseInfo mi = mouseUtil.getMouseInfo(0);
103 Point2D newPos = mi.canvasPosition;
104 double x = newPos.getX();
105 double y = newPos.getY();
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);
118 public CreateVariablesShortcutParticipant(GraphToDiagramSynchronizer synchronizer) {
119 this.synchronizer = synchronizer;
122 @EventHandler(priority = -10)
123 public boolean handleKeyboardEvent(KeyEvent ke) {
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) {
131 if (ke instanceof KeyPressedEvent) {
133 kpe = (KeyPressedEvent) ke;
135 if (!kpe.isShiftDown() || isEditing())
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)
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)
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)
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)
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)
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)
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)
182 if (variableInformation != null) {
188 KeyReleasedEvent kre;
189 if (ke instanceof KeyReleasedEvent) {
190 kre = (KeyReleasedEvent) ke;
192 if (variableInformation != null
193 && (kre.keyCode == variableInformation.shortcutKey || kre.keyCode == java.awt.event.KeyEvent.VK_SHIFT)) {
195 // If there is a variable to be created, do it when a key is released.
198 createVariableOnDiagram(createVarDiagram);
200 variableInformation = null;
211 @EventHandler(priority = -10)
212 public boolean handleMouse(MouseMovedEvent e) {
214 if (variableInformation != null ) {
225 @EventHandler(priority = 100)
226 public boolean handleMouseEvent(MouseEvent me) {
229 MouseEvent.MouseClickEvent mce;
230 if (me instanceof MouseEvent.MouseClickEvent) {
231 mce = (MouseEvent.MouseClickEvent) me;
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) {
243 mce.button == MouseEvent.LEFT_BUTTON &&
244 variableInformation != null &&
245 mce.stateMask == MouseEvent.SHIFT_MASK
251 final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
255 // Need to create a new variable, save the diagram to do this later.
257 createVarDiagram = d;
263 private void createVariableOnDiagram(IDiagram d) {
264 DiagramUtils.mutateDiagram(d, new Consumer<DiagramMutator>() {
266 public void accept(DiagramMutator m) {
274 .resource(variableInformation.symbolURI));
275 ElementClass ec = Simantics.getSession().syncRequest(
276 DiagramRequests.getElementClass(r, diagram));
278 IElement element = m.newElement(ec);
280 // MouseUtil mutil = new MouseUtil();
281 MouseInfo minfo = mouseUtil.getMouseInfo(0);
283 //at least when using breakpoints this is possible
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);
291 ElementUtils.setPos(element, p);
293 } catch (DatabaseException e) {
294 ExceptionUtils.logAndShowError(e);
300 synchronizer.getCanvasContext().getContentContext().setDirty();
304 private class VariableInformation {
305 public String symbolURI;
306 public ShapeNode node;
307 public int shortcutKey;
309 public VariableInformation(int shortcutKey, String symbolURI, ShapeNode node) {
310 this.symbolURI = symbolURI;
312 this.shortcutKey = shortcutKey;
316 private boolean isEditing() {
318 Set<IElement> ss = selection.getSelection(selectionId);
322 for (IElement e : ss) {
323 for(Object o : e.getHints().values()) {
324 if (o instanceof TextNode) {
325 TextNode tn = (TextNode) o;