From 986f656cd3175ebbd84e5105847e808cce9f8709 Mon Sep 17 00:00:00 2001 From: jplaine Date: Fri, 10 Dec 2010 11:00:04 +0000 Subject: [PATCH] SceneGraph event handling changed to listener based, fixes #1942 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19116 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/elements2/GraphPropertyNode.java | 53 +++----- .../elements2/connections/DependencyNode.java | 114 ++++++++++++------ 2 files changed, 92 insertions(+), 75 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/GraphPropertyNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/GraphPropertyNode.java index 4f604273..62ace38c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/GraphPropertyNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/GraphPropertyNode.java @@ -11,8 +11,8 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.elements2; -import java.awt.AWTEvent; -import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; import java.awt.geom.Rectangle2D; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -20,9 +20,9 @@ import java.lang.reflect.Field; import java.util.HashMap; import org.simantics.scenegraph.g2d.G2DNode; -import org.simantics.scenegraph.g2d.events.SGMouseEvent; +import org.simantics.scenegraph.utils.NodeUtil; -abstract public class GraphPropertyNode extends G2DNode { +abstract public class GraphPropertyNode extends G2DNode implements MouseListener, MouseMotionListener { private static final long serialVersionUID = 245761992671850588L; @@ -30,6 +30,20 @@ abstract public class GraphPropertyNode extends G2DNode { private PropertyChangeListener fieldListener = null; + @Override + public void init() { + super.init(); + NodeUtil.getEventDelegator(this).addMouseListener(this); + NodeUtil.getEventDelegator(this).addMouseMotionListener(this); + } + + @Override + public void cleanup() { + NodeUtil.getEventDelegator(this).removeMouseListener(this); + NodeUtil.getEventDelegator(this).removeMouseMotionListener(this); + super.cleanup(); + } + protected GraphPropertyNode() { for(Field f : getClass().getFields()) { // System.out.println("register field '" + f.getName() + "'"); @@ -72,37 +86,6 @@ abstract public class GraphPropertyNode extends G2DNode { public boolean hitTest(double x, double y, double tolerance) { return false; } - - @Override - public void handleEvent(AWTEvent event) { - if(event instanceof SGMouseEvent) { - SGMouseEvent e = (SGMouseEvent)event; - boolean hit = hitTest(e.getDoubleX(), e.getDoubleY(), 3.0); - if(e.getID() == MouseEvent.MOUSE_DRAGGED) { - mouseDragged((SGMouseEvent)event, pressHit, hit); - } - if(e.getID() == MouseEvent.MOUSE_PRESSED) { - pressHit = hit; - mouseReleased((SGMouseEvent)event, hit); - } - if(e.getID() == MouseEvent.MOUSE_RELEASED) - mouseReleased((SGMouseEvent)event, hit); - if(e.getID() == MouseEvent.MOUSE_MOVED) - mouseMoved((SGMouseEvent)event, hit); - } - } - - public void mouseDragged(SGMouseEvent event, boolean pressHit, boolean currentHit) { - - } - - public void mouseReleased(SGMouseEvent event, boolean hit) { - - } - - public void mouseMoved(SGMouseEvent event, boolean hit) { - - } @Override public Rectangle2D getBoundsInLocal() { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java index 988b0001..1577b1bc 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java @@ -11,7 +11,6 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.elements2.connections; -import java.awt.AWTEvent; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; @@ -19,17 +18,19 @@ import java.awt.RenderingHints; import java.awt.Shape; import java.awt.Stroke; import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import org.simantics.scenegraph.ISelectionPainterNode; import org.simantics.scenegraph.g2d.G2DNode; -import org.simantics.scenegraph.g2d.events.SGMouseEvent; import org.simantics.scenegraph.utils.NodeUtil; import org.simantics.utils.datastructures.Pair; -public class DependencyNode extends G2DNode implements ISelectionPainterNode { +public class DependencyNode extends G2DNode implements ISelectionPainterNode, MouseListener, MouseMotionListener { private static final long serialVersionUID = 1294351381209071074L; @@ -44,6 +45,20 @@ public class DependencyNode extends G2DNode implements ISelectionPainterNode { transient public boolean hover = false; private transient PropertyChangeListener fieldListener = null; + + @Override + public void init() { + super.init(); + NodeUtil.getEventDelegator(this).addMouseListener(this); + NodeUtil.getEventDelegator(this).addMouseMotionListener(this); + } + + @Override + public void cleanup() { + NodeUtil.getEventDelegator(this).removeMouseListener(this); + NodeUtil.getEventDelegator(this).removeMouseMotionListener(this); + super.cleanup(); + } public void setFieldListener(PropertyChangeListener listener) { this.fieldListener = listener; @@ -146,57 +161,76 @@ public class DependencyNode extends G2DNode implements ISelectionPainterNode { boolean pressHit = false; - @Override - public void handleEvent(AWTEvent event) { - if(event instanceof SGMouseEvent) { - SGMouseEvent e = (SGMouseEvent)event; - boolean hit = hitTest(e.getDoubleX(), e.getDoubleY(), 3.0); - if(e.getID() == MouseEvent.MOUSE_DRAGGED) { - mouseDragged((SGMouseEvent)event, pressHit, hit); - } - if(e.getID() == MouseEvent.MOUSE_PRESSED) { - pressHit = hit; - mouseReleased((SGMouseEvent)event, hit); - } - if(e.getID() == MouseEvent.MOUSE_RELEASED) - mouseReleased((SGMouseEvent)event, hit); - if(e.getID() == MouseEvent.MOUSE_MOVED) - mouseMoved((SGMouseEvent)event, hit); + private boolean hitTest(MouseEvent event, double tolerance) { + Point2D mp = NodeUtil.sgEventLocations.get(event); + if(mp != null) { + if(beginBounds == null || endBounds == null) return false; + + return Arcs.hitTest(beginBounds, endBounds, angle, mp.getX(), mp.getY(), tolerance); + } else { + return false; } } - public boolean hitTest(double x, double y, double tolerance) { - if(beginBounds == null || endBounds == null) return false; + @Override + public Rectangle2D getBoundsInLocal() { + return null; + } - return Arcs.hitTest(beginBounds, endBounds, angle, x, y, tolerance); + @Override + public void mouseDragged(MouseEvent e) { + Point2D mp = NodeUtil.sgEventLocations.get(e); + if(pressHit && mp != null) { + setAngle(Arcs.angleOfArc( + beginBounds.getCenterX(), beginBounds.getCenterY(), + mp.getX(), mp.getY(), + endBounds.getCenterX(), endBounds.getCenterY())); + } } - - public void mouseDragged(SGMouseEvent event, boolean pressHit, boolean currentHit) { - - if(!pressHit) return; - - setAngle(Arcs.angleOfArc( - beginBounds.getCenterX(), beginBounds.getCenterY(), - event.getDoubleX(), event.getDoubleY(), - endBounds.getCenterX(), endBounds.getCenterY())); + + @Override + public void mouseMoved(MouseEvent e) { + boolean hit = hitTest(e, 3.0); + if(hit != hover) { + hover = hit; + repaint(); + } + } + + @Override + public void mouseClicked(MouseEvent e) { + // TODO Auto-generated method stub + } - public void mouseReleased(SGMouseEvent event, boolean hit) { + @Override + public void mousePressed(MouseEvent e) { + boolean hit = hitTest(e, 3.0); + pressHit = hit; if(hit) { commitProperty("angle", angle); } + } - public void mouseMoved(SGMouseEvent event, boolean hit) { - if(hit != hover) { - hover = hit; - repaint(); + @Override + public void mouseReleased(MouseEvent e) { + boolean hit = hitTest(e, 3.0); + if(hit) { + commitProperty("angle", angle); } - } + } @Override - public Rectangle2D getBoundsInLocal() { - return null; - } + public void mouseEntered(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseExited(MouseEvent e) { + // TODO Auto-generated method stub + + } } -- 2.47.1