1 package org.simantics.sysdyn.ui.editor.participant;
\r
3 import java.awt.geom.AffineTransform;
\r
4 import java.awt.geom.Point2D;
\r
5 import java.util.Set;
\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
46 public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipant {
\r
48 private GraphToDiagramSynchronizer synchronizer;
\r
50 private VariableInformation variableInformation;
\r
53 MouseUtil mouseUtil;
\r
56 Selection selection;
\r
59 ElementPainter diagramPainter;
\r
62 G2DParentNode parent;
\r
64 @SGInit(designation = SGDesignation.CANVAS)
\r
65 public void init(G2DParentNode parent) {
\r
66 this.parent = parent;
\r
69 public void removeSG() {
\r
78 node = variableInformation.node;
\r
81 MouseInfo mi = mouseUtil.getMouseInfo(0);
\r
85 Point2D newPos = mi.canvasPosition;
\r
86 double x = newPos.getX();
\r
87 double y = newPos.getY();
\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
100 public CreateVariablesShortcutParticipant(GraphToDiagramSynchronizer synchronizer) {
\r
101 this.synchronizer = synchronizer;
\r
104 @EventHandler(priority = -10)
\r
105 public boolean handleKeyboardEvent(KeyEvent ke) {
\r
107 KeyPressedEvent kpe;
\r
108 if (ke instanceof KeyPressedEvent) {
\r
110 kpe = (KeyPressedEvent) ke;
\r
112 if (kpe.stateMask != java.awt.event.KeyEvent.SHIFT_DOWN_MASK || isEditing())
\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
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
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
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
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
147 if (variableInformation != null) {
\r
153 KeyReleasedEvent kre;
\r
154 if (ke instanceof KeyReleasedEvent) {
\r
155 kre = (KeyReleasedEvent) ke;
\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
171 @EventHandler(priority = -10)
\r
172 public boolean handleMouse(MouseMovedEvent e) {
\r
174 if (variableInformation != null ) {
\r
177 if (node != null) {
\r
185 @EventHandler(priority = 100)
\r
186 public boolean handleMouseEvent(MouseEvent me) {
\r
189 MouseClickEvent mce;
\r
190 if (me instanceof MouseClickEvent) {
\r
191 mce = (MouseClickEvent) me;
\r
198 mce.button == MouseEvent.LEFT_BUTTON &&
\r
199 variableInformation != null &&
\r
200 mce.stateMask == MouseEvent.SHIFT_MASK
\r
207 final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
\r
211 DiagramUtils.mutateDiagram(d, new Callback<DiagramMutator>() {
\r
213 public void run(DiagramMutator m) {
\r
221 .resource(variableInformation.symbolURI));
\r
222 ElementClass ec = SimanticsUI.getSession().syncRequest(
\r
223 DiagramRequests.getElementClass(r, diagram));
\r
225 IElement element = m.newElement(ec);
\r
227 // MouseUtil mutil = new MouseUtil();
\r
228 MouseInfo minfo = mouseUtil.getMouseInfo(0);
\r
230 //at least when using breakpoints this is possible
\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
238 ElementUtils.setPos(element, p);
\r
240 } catch (DatabaseException e) {
\r
241 ExceptionUtils.logAndShowError(e);
\r
247 synchronizer.getCanvasContext().getContentContext().setDirty();
\r
252 private class VariableInformation {
\r
253 public String symbolURI;
\r
254 public ShapeNode node;
\r
255 public int shortcutKey;
\r
257 public VariableInformation(int shortcutKey, String symbolURI, ShapeNode node) {
\r
258 this.symbolURI = symbolURI;
\r
260 this.shortcutKey = shortcutKey;
\r
264 private boolean isEditing() {
\r
265 int selectionId = 0;
\r
266 Set<IElement> ss = selection.getSelection(selectionId);
\r
267 if (ss.isEmpty()) {
\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