From d403a4c4d5e15b8fc79ecc9f4647f4041b962adc Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 20 Aug 2010 09:58:11 +0000 Subject: [PATCH] Shortcut keys for different types of variables. SHIFT + variable (Auxiliary = A, Stock = S, etc..) works always, A, C and I even without shift git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17411 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/editor/DiagramViewer.java | 4 +- .../CreateVariablesShortcutParticipant.java | 263 ++++++++++++++++++ .../sysdyn/ui/elements2/AuxiliaryFactory.java | 3 +- .../sysdyn/ui/elements2/CloudFactory.java | 4 +- .../sysdyn/ui/elements2/InputFactory.java | 20 +- .../sysdyn/ui/elements2/StockFactory.java | 4 +- .../sysdyn/ui/elements2/ValveFactory.java | 2 +- 7 files changed, 282 insertions(+), 18 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java index fa546979..9245e4ec 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java @@ -34,6 +34,7 @@ import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; import org.simantics.modeling.ui.diagramEditor.handlers.LinkBrowsingHandler; import org.simantics.structural2.modelingRules.IModelingRules; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.editor.participant.CreateVariablesShortcutParticipant; import org.simantics.sysdyn.ui.elements2.connections.ConnectionClasses; import org.simantics.sysdyn.ui.elements2.connections.SysdynConnectionClass; import org.simantics.sysdyn.ui.properties.SysdynPropertyPage; @@ -83,6 +84,7 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr protected void addKeyBindingParticipants(CanvasContext ctx) { ctx.add(new DeleteHandler(getEditorSite().getActionBars().getStatusLineManager())); ctx.add(new CopyPasteHandler(getEditorSite().getActionBars().getStatusLineManager())); + ctx.add(new CreateVariablesShortcutParticipant(synchronizer)); } @Override @@ -131,7 +133,7 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr // Remove double click handler, because it is not working properly // ctx.add(new StructuralBrowsingHandler(getSite(), sessionContext, getResourceInput2())); ctx.add(new LinkBrowsingHandler(getSite(), this, sessionContext)); - + } @Override diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java new file mode 100644 index 00000000..1e775965 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java @@ -0,0 +1,263 @@ +package org.simantics.sysdyn.ui.editor.participant; + +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; + +import org.simantics.db.Resource; +import org.simantics.db.common.request.Queries; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.adapter.GraphToDiagramSynchronizer; +import org.simantics.diagram.handler.DiagramSelection; +import org.simantics.diagram.query.DiagramRequests; +import org.simantics.g2d.canvas.SGDesignation; +import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency; +import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit; +import org.simantics.g2d.diagram.DiagramHints; +import org.simantics.g2d.diagram.DiagramMutator; +import org.simantics.g2d.diagram.DiagramUtils; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant; +import org.simantics.g2d.diagram.participant.ElementPainter; +import org.simantics.g2d.diagram.participant.Selection; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.event.EventHandlerReflection.EventHandler; +import org.simantics.g2d.event.KeyEvent; +import org.simantics.g2d.event.KeyEvent.KeyPressedEvent; +import org.simantics.g2d.event.KeyEvent.KeyReleasedEvent; +import org.simantics.g2d.event.MouseEvent; +import org.simantics.g2d.event.MouseEvent.MouseMovedEvent; +import org.simantics.g2d.participant.MouseUtil; +import org.simantics.g2d.participant.MouseUtil.MouseClickEvent; +import org.simantics.g2d.participant.MouseUtil.MouseInfo; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.nodes.ShapeNode; +import org.simantics.sysdyn.ui.elements2.AuxiliaryFactory; +import org.simantics.sysdyn.ui.elements2.CloudFactory; +import org.simantics.sysdyn.ui.elements2.InputFactory; +import org.simantics.sysdyn.ui.elements2.StockFactory; +import org.simantics.sysdyn.ui.elements2.ValveFactory; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.datastructures.Callback; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; +import org.simantics.utils.ui.ExceptionUtils; + +public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipant { + + public static final Key KEY_DIAGRAM_SELECTION = new KeyOf( + DiagramSelection.class); + + private GraphToDiagramSynchronizer synchronizer; + + private VariableInformation variableInformation; + + @Dependency + MouseUtil mouseUtil; + + @Dependency + Selection selection; + + @Dependency + ElementPainter diagramPainter; + + ShapeNode node; + G2DParentNode parent; + + @SGInit(designation = SGDesignation.CANVAS) + public void init(G2DParentNode parent) { + this.parent = parent; + } + + public void removeSG() { + node.remove(); + node = null; + setDirty(); + } + + void updateSG() { + + if (node == null) { + node = variableInformation.node; + } + + MouseInfo mi = mouseUtil.getMouseInfo(0); + if (mi == null) + return; + + Point2D newPos = mi.canvasPosition; + double x = newPos.getX(); + double y = newPos.getY(); + + AffineTransform origAt = node.getTransform(); + double oldX = origAt.getTranslateX(); + double oldY = origAt.getTranslateY(); + AffineTransform move = new AffineTransform(); + move.setToTranslation(x - oldX, y - oldY); + AffineTransform at2 = new AffineTransform(origAt); + at2.preConcatenate(move); + node.setTransform(at2); + setDirty(); + } + + public CreateVariablesShortcutParticipant(GraphToDiagramSynchronizer synchronizer) { + this.synchronizer = synchronizer; + } + + @EventHandler(priority = -10) + public boolean handleKeyboardEvent(KeyEvent ke) { + + KeyPressedEvent kpe; + if (ke instanceof KeyPressedEvent) { + kpe = (KeyPressedEvent) ke; + + if (kpe.keyCode == java.awt.event.KeyEvent.VK_A) { + variableInformation = new VariableInformation( + java.awt.event.KeyEvent.VK_A, + "http://www.simantics.org/Sysdyn-1.0/AuxiliarySymbol", + (ShapeNode)AuxiliaryFactory.AUX_STATIC_IMAGE.init(parent) + ); + } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_S) { + variableInformation = new VariableInformation( + java.awt.event.KeyEvent.VK_S, + "http://www.simantics.org/Sysdyn-1.0/StockSymbol", + (ShapeNode)StockFactory.STOCK_IMAGE.init(parent) + ); + } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_C) { + variableInformation = new VariableInformation( + java.awt.event.KeyEvent.VK_C, + "http://www.simantics.org/Sysdyn-1.0/CloudSymbol", + (ShapeNode)CloudFactory.CLOUD_IMAGE.init(parent) + ); + } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_V) { + variableInformation = new VariableInformation( + java.awt.event.KeyEvent.VK_V, + "http://www.simantics.org/Sysdyn-1.0/ValveSymbol", + (ShapeNode)ValveFactory.VALVE_STATIC_IMAGE.init(parent) + ); + } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_I) { + variableInformation = new VariableInformation( + java.awt.event.KeyEvent.VK_I, + "http://www.simantics.org/Sysdyn-1.0/InputSymbol", + (ShapeNode)InputFactory.INPUT_IMAGE.init(parent) + ); + } + + if (variableInformation != null) { + updateSG(); + return true; + } + } + + KeyReleasedEvent kre; + if (ke instanceof KeyReleasedEvent) { + kre = (KeyReleasedEvent) ke; + + if (variableInformation != null && kre.keyCode == variableInformation.shortcutKey) { + if (node != null) { + variableInformation = null; + removeSG(); + return true; + } + } + } + + return false; + + } + + @EventHandler(priority = -10) + public boolean handleMouse(MouseMovedEvent e) { + + if (variableInformation != null ) { + updateSG(); + } else { + if (node != null) { + removeSG(); + } + } + return false; + } + + + @EventHandler(priority = 100) + public boolean handleMouseEvent(MouseEvent me) { + + + MouseClickEvent mce; + if (me instanceof MouseClickEvent) { + mce = (MouseClickEvent) me; + } else { + return false; + } + + if (! + ( + mce.button == MouseEvent.LEFT_BUTTON && + variableInformation != null && + (mce.stateMask == 0 || mce.stateMask == MouseEvent.SHIFT_MASK) + )) + { + return false; + } + + + final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM); + if (d == null) + return false; + + DiagramUtils.mutateDiagram(d, new Callback() { + @Override + public void run(DiagramMutator m) { + + Resource r; + try { + r = SimanticsUI + .getSession() + .syncRequest( + Queries + .resource(variableInformation.symbolURI)); + ElementClass ec = SimanticsUI.getSession().syncRequest( + DiagramRequests.getElementClass(r, diagram)); + + IElement element = m.newElement(ec); + + // MouseUtil mutil = new MouseUtil(); + MouseInfo minfo = mouseUtil.getMouseInfo(0); + + //at least when using breakpoints this is possible + if(minfo == null) + return; + + Point2D p = minfo.canvasPosition; + //FIXME - Arto element doesn't know its size at first. Hopefully temp fix. + p.setLocation(p.getX()-5.46, p.getY()+1); + + ElementUtils.setPos(element, p); + + } catch (DatabaseException e) { + ExceptionUtils.logAndShowError(e); + } + + } + }); + + synchronizer.getCanvasContext().getContentContext().setDirty(); + + return true; + } + + private class VariableInformation { + public String symbolURI; + public ShapeNode node; + public int shortcutKey; + + public VariableInformation(int shortcutKey, String symbolURI, ShapeNode node) { + this.symbolURI = symbolURI; + this.node = node; + this.shortcutKey = shortcutKey; + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java index 1620323b..e8f65686 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java @@ -36,8 +36,7 @@ import org.simantics.g2d.image.impl.ShapeImage; */ public class AuxiliaryFactory extends SysdynElementFactory { - // TODO: make the static symbol image a text "AUX" - private static final Image AUX_STATIC_IMAGE = new ShapeImage(new Ellipse2D.Double(-5, -2, 10, 4), null, new BasicStroke(1), true); + public static final Image AUX_STATIC_IMAGE = new ShapeImage(new Ellipse2D.Double(-5, -2, 10, 4), null, new BasicStroke(1), true); @Override protected ElementClass compileElementClass(Resource elementType, Collection terminals) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java index 20da2813..d71c5796 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java @@ -58,7 +58,7 @@ public class CloudFactory extends SysdynElementFactory { public static final double CLOUD_CURVES = 7; private static final BasicStroke STROKE = new BasicStroke(1f); - private static final Image DEFAULT_IMAGE = new ShapeImage(getCloudShape(), null, STROKE, true); + public static final Image CLOUD_IMAGE = new ShapeImage(getCloudShape(), null, STROKE, true); static Shape getCloudShape() { Path2D path = new Path2D.Double(); @@ -91,7 +91,7 @@ public class CloudFactory extends SysdynElementFactory { TextFontImpl.DEFAULT, DefaultTransform.INSTANCE, new StaticObjectAdapter(elementType), - new StaticSymbolImpl(DEFAULT_IMAGE), + new StaticSymbolImpl(CLOUD_IMAGE), StaticSymbolImageInitializer.INSTANCE, CloudSceneGraph.INSTANCE, HoverImpl.INSTANCE, diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java index 43936fd3..1876d3a6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java @@ -46,19 +46,19 @@ import org.simantics.utils.datastructures.hints.IHintContext.Key; public class InputFactory extends SysdynElementFactory { private static final BasicStroke STROKE = new BasicStroke(1f); - private static final Image INPUT_IMAGE = new ShapeImage(getInputShape(), null, STROKE, true); + public static final Image INPUT_IMAGE = new ShapeImage(getInputShape(), null, STROKE, true); static Shape getInputShape() { Path2D path = new Path2D.Double(); - path.moveTo(0, -1); - path.lineTo(5, -1); - path.lineTo(5, -2.5); - path.lineTo(7.5, 0); - path.curveTo(7.5, -2.5, 12.5, -2.5, 12.5, 0); - path.curveTo(12.5, 2.5, 7.5, 2.5, 7.5, 0); - path.lineTo(5, 2.5); - path.lineTo(5, 1); - path.lineTo(0, 1); + path.moveTo(-6, -1); + path.lineTo(-1, -1); + path.lineTo(-1, -2.5); + path.lineTo(1.5, 0); + path.curveTo(1.5, -2.5, 6.5, -2.5, 6.5, 0); + path.curveTo(6.5, 2.5, 1.5, 2.5, 1.5, 0); + path.lineTo(-1, 2.5); + path.lineTo(-1, 1); + path.lineTo(-6, 1); path.closePath(); return path; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java index 44476a87..5e7e48e3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java @@ -35,7 +35,7 @@ import org.simantics.g2d.utils.Alignment; public class StockFactory extends SysdynElementFactory { private static final BasicStroke STROKE = new BasicStroke(1f); - private static final Image DEFAULT_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -2.5, 10, 5), null, STROKE, true); + public static final Image STOCK_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -2.5, 10, 5), null, STROKE, true); @Override protected ElementClass compileElementClass(Resource elementType, Collection terminals) { @@ -47,7 +47,7 @@ public class StockFactory extends SysdynElementFactory { TextFontImpl.DEFAULT, DefaultTransform.INSTANCE, new StaticObjectAdapter(elementType), - new StaticSymbolImpl(DEFAULT_IMAGE), + new StaticSymbolImpl(STOCK_IMAGE), StaticSymbolImageInitializer.INSTANCE, new HoverTextElementHandler(0, 0, Alignment.LEADING, 1f), BoundsOutline.INSTANCE, diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java index 6c9123a8..ffcadcdb 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java @@ -55,7 +55,7 @@ public class ValveFactory extends SysdynElementFactory { public static final double VALVE_SIZE = 2.5; private static final BasicStroke STROKE = new BasicStroke(1f); - private static final Image VALVE_STATIC_IMAGE = new ShapeImage(createShape(VALVE_SIZE, false), null, STROKE, true); + public static final Image VALVE_STATIC_IMAGE = new ShapeImage(createShape(VALVE_SIZE, false), null, STROKE, true); /* (non-Javadoc) * @see org.simantics.sysdyn.ui.elements2.SysdynElementFactory#compileElementClass(org.simantics.db.Resource, java.util.Collection) -- 2.47.1