--- /dev/null
+package org.simantics.sysdyn.ui.editor.participant;\r
+\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Point2D;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.Queries;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;\r
+import org.simantics.diagram.handler.DiagramSelection;\r
+import org.simantics.diagram.query.DiagramRequests;\r
+import org.simantics.g2d.canvas.SGDesignation;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.DiagramMutator;\r
+import org.simantics.g2d.diagram.DiagramUtils;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
+import org.simantics.g2d.diagram.participant.ElementPainter;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.event.EventHandlerReflection.EventHandler;\r
+import org.simantics.g2d.event.KeyEvent;\r
+import org.simantics.g2d.event.KeyEvent.KeyPressedEvent;\r
+import org.simantics.g2d.event.KeyEvent.KeyReleasedEvent;\r
+import org.simantics.g2d.event.MouseEvent;\r
+import org.simantics.g2d.event.MouseEvent.MouseMovedEvent;\r
+import org.simantics.g2d.participant.MouseUtil;\r
+import org.simantics.g2d.participant.MouseUtil.MouseClickEvent;\r
+import org.simantics.g2d.participant.MouseUtil.MouseInfo;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
+import org.simantics.sysdyn.ui.elements2.AuxiliaryFactory;\r
+import org.simantics.sysdyn.ui.elements2.CloudFactory;\r
+import org.simantics.sysdyn.ui.elements2.InputFactory;\r
+import org.simantics.sysdyn.ui.elements2.StockFactory;\r
+import org.simantics.sysdyn.ui.elements2.ValveFactory;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.datastructures.Callback;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+\r
+public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipant {\r
+\r
+ public static final Key KEY_DIAGRAM_SELECTION = new KeyOf(\r
+ DiagramSelection.class);\r
+\r
+ private GraphToDiagramSynchronizer synchronizer;\r
+\r
+ private VariableInformation variableInformation;\r
+\r
+ @Dependency\r
+ MouseUtil mouseUtil;\r
+\r
+ @Dependency\r
+ Selection selection;\r
+\r
+ @Dependency\r
+ ElementPainter diagramPainter;\r
+\r
+ ShapeNode node;\r
+ G2DParentNode parent;\r
+\r
+ @SGInit(designation = SGDesignation.CANVAS)\r
+ public void init(G2DParentNode parent) {\r
+ this.parent = parent;\r
+ }\r
+\r
+ public void removeSG() {\r
+ node.remove();\r
+ node = null;\r
+ setDirty();\r
+ }\r
+\r
+ void updateSG() {\r
+\r
+ if (node == null) {\r
+ node = variableInformation.node;\r
+ }\r
+\r
+ MouseInfo mi = mouseUtil.getMouseInfo(0);\r
+ if (mi == null)\r
+ return;\r
+\r
+ Point2D newPos = mi.canvasPosition;\r
+ double x = newPos.getX();\r
+ double y = newPos.getY();\r
+\r
+ AffineTransform origAt = node.getTransform();\r
+ double oldX = origAt.getTranslateX();\r
+ double oldY = origAt.getTranslateY();\r
+ AffineTransform move = new AffineTransform();\r
+ move.setToTranslation(x - oldX, y - oldY);\r
+ AffineTransform at2 = new AffineTransform(origAt);\r
+ at2.preConcatenate(move);\r
+ node.setTransform(at2);\r
+ setDirty();\r
+ }\r
+\r
+ public CreateVariablesShortcutParticipant(GraphToDiagramSynchronizer synchronizer) {\r
+ this.synchronizer = synchronizer;\r
+ }\r
+\r
+ @EventHandler(priority = -10)\r
+ public boolean handleKeyboardEvent(KeyEvent ke) {\r
+\r
+ KeyPressedEvent kpe;\r
+ if (ke instanceof KeyPressedEvent) {\r
+ kpe = (KeyPressedEvent) ke;\r
+ \r
+ if (kpe.keyCode == java.awt.event.KeyEvent.VK_A) {\r
+ variableInformation = new VariableInformation(\r
+ java.awt.event.KeyEvent.VK_A,\r
+ "http://www.simantics.org/Sysdyn-1.0/AuxiliarySymbol",\r
+ (ShapeNode)AuxiliaryFactory.AUX_STATIC_IMAGE.init(parent)\r
+ );\r
+ } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_S) {\r
+ variableInformation = new VariableInformation(\r
+ java.awt.event.KeyEvent.VK_S,\r
+ "http://www.simantics.org/Sysdyn-1.0/StockSymbol",\r
+ (ShapeNode)StockFactory.STOCK_IMAGE.init(parent)\r
+ );\r
+ } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_C) {\r
+ variableInformation = new VariableInformation(\r
+ java.awt.event.KeyEvent.VK_C,\r
+ "http://www.simantics.org/Sysdyn-1.0/CloudSymbol",\r
+ (ShapeNode)CloudFactory.CLOUD_IMAGE.init(parent)\r
+ );\r
+ } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_V) {\r
+ variableInformation = new VariableInformation(\r
+ java.awt.event.KeyEvent.VK_V,\r
+ "http://www.simantics.org/Sysdyn-1.0/ValveSymbol",\r
+ (ShapeNode)ValveFactory.VALVE_STATIC_IMAGE.init(parent)\r
+ );\r
+ } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_I) {\r
+ variableInformation = new VariableInformation(\r
+ java.awt.event.KeyEvent.VK_I,\r
+ "http://www.simantics.org/Sysdyn-1.0/InputSymbol",\r
+ (ShapeNode)InputFactory.INPUT_IMAGE.init(parent)\r
+ );\r
+ }\r
+\r
+ if (variableInformation != null) {\r
+ updateSG();\r
+ return true;\r
+ }\r
+ }\r
+\r
+ KeyReleasedEvent kre;\r
+ if (ke instanceof KeyReleasedEvent) {\r
+ kre = (KeyReleasedEvent) ke;\r
+\r
+ if (variableInformation != null && kre.keyCode == variableInformation.shortcutKey) {\r
+ if (node != null) {\r
+ variableInformation = null;\r
+ removeSG();\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+\r
+ return false;\r
+\r
+ }\r
+\r
+ @EventHandler(priority = -10)\r
+ public boolean handleMouse(MouseMovedEvent e) {\r
+\r
+ if (variableInformation != null ) {\r
+ updateSG();\r
+ } else {\r
+ if (node != null) {\r
+ removeSG();\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+\r
+ @EventHandler(priority = 100)\r
+ public boolean handleMouseEvent(MouseEvent me) {\r
+\r
+\r
+ MouseClickEvent mce;\r
+ if (me instanceof MouseClickEvent) {\r
+ mce = (MouseClickEvent) me;\r
+ } else {\r
+ return false;\r
+ }\r
+\r
+ if (!\r
+ (\r
+ mce.button == MouseEvent.LEFT_BUTTON && \r
+ variableInformation != null && \r
+ (mce.stateMask == 0 || mce.stateMask == MouseEvent.SHIFT_MASK)\r
+ )) \r
+ {\r
+ return false;\r
+ }\r
+\r
+\r
+ final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);\r
+ if (d == null)\r
+ return false;\r
+\r
+ DiagramUtils.mutateDiagram(d, new Callback<DiagramMutator>() {\r
+ @Override\r
+ public void run(DiagramMutator m) {\r
+\r
+ Resource r;\r
+ try {\r
+ r = SimanticsUI\r
+ .getSession()\r
+ .syncRequest(\r
+ Queries\r
+ .resource(variableInformation.symbolURI));\r
+ ElementClass ec = SimanticsUI.getSession().syncRequest(\r
+ DiagramRequests.getElementClass(r, diagram));\r
+\r
+ IElement element = m.newElement(ec);\r
+\r
+ // MouseUtil mutil = new MouseUtil();\r
+ MouseInfo minfo = mouseUtil.getMouseInfo(0);\r
+\r
+ //at least when using breakpoints this is possible\r
+ if(minfo == null) \r
+ return;\r
+\r
+ Point2D p = minfo.canvasPosition;\r
+ //FIXME - Arto element doesn't know its size at first. Hopefully temp fix.\r
+ p.setLocation(p.getX()-5.46, p.getY()+1);\r
+\r
+ ElementUtils.setPos(element, p);\r
+\r
+ } catch (DatabaseException e) {\r
+ ExceptionUtils.logAndShowError(e);\r
+ }\r
+\r
+ }\r
+ });\r
+\r
+ synchronizer.getCanvasContext().getContentContext().setDirty();\r
+\r
+ return true;\r
+ }\r
+\r
+ private class VariableInformation {\r
+ public String symbolURI;\r
+ public ShapeNode node;\r
+ public int shortcutKey;\r
+\r
+ public VariableInformation(int shortcutKey, String symbolURI, ShapeNode node) {\r
+ this.symbolURI = symbolURI;\r
+ this.node = node;\r
+ this.shortcutKey = shortcutKey;\r
+ }\r
+ }\r
+\r
+}\r