X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scenegraph%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2Fg2d%2Fevents%2FNodeEventHandler.java;h=8d3ba3a94482e3405382ea4817faa90c0c107441;hb=07bb01bc6390b0d22242edda1a1af9ce4760f5ec;hp=598dab9fefe266a62b12cfaf3946fcbc6907a8c7;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/NodeEventHandler.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/NodeEventHandler.java index 598dab9fe..8d3ba3a94 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/NodeEventHandler.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/NodeEventHandler.java @@ -11,17 +11,6 @@ *******************************************************************************/ package org.simantics.scenegraph.g2d.events; -import java.awt.Component; -import java.awt.dnd.DnDConstants; -import java.awt.dnd.DragGestureEvent; -import java.awt.dnd.DragGestureListener; -import java.awt.dnd.DragSource; -import java.awt.dnd.DragSourceDragEvent; -import java.awt.dnd.DragSourceDropEvent; -import java.awt.dnd.DragSourceEvent; -import java.awt.dnd.DragSourceListener; -import java.awt.event.InputEvent; -import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -33,8 +22,9 @@ import org.simantics.scenegraph.g2d.G2DSceneGraph; import org.simantics.scenegraph.g2d.IG2DNode; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDragBegin; -import org.simantics.scenegraph.g2d.events.adapter.AWTMouseEventAdapter; import org.simantics.scenegraph.g2d.events.command.CommandEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Delivers events (mouse, key, focus, command, time) to scene graph nodes that @@ -44,6 +34,8 @@ import org.simantics.scenegraph.g2d.events.command.CommandEvent; */ public class NodeEventHandler implements IEventHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(NodeEventHandler.class); + private static final boolean DEBUG_EVENTS = false; private static final boolean DEBUG_HANDLER_SORT = false; @@ -75,8 +67,8 @@ public class NodeEventHandler implements IEventHandler { void getTreePath(INode node, ArrayList result) { result.clear(); - for (INode parent = node.getParent(); parent != null; parent = parent.getParent()) - result.add(parent); + for (; node != null; node = node.getParent()) + result.add(node); } void notSameGraph(INode o1, INode o2) { @@ -94,19 +86,15 @@ public class NodeEventHandler implements IEventHandler { ArrayList path1 = tmp.path1; ArrayList path2 = tmp.path2; - // Get path to root node for both nodes - INode o1 = (INode) e1; - INode o2 = (INode) e2; - getTreePath(o1, path1); - getTreePath(o2, path2); + try { + // Get path to root node for both nodes + getTreePath((INode) e1, path1); + getTreePath((INode) e2, path2); - // Sanity checks: nodes part of same scene graph - INode root1 = path1.isEmpty() ? o1 : path1.get(path1.size() - 1); - INode root2 = path2.isEmpty() ? o2 : path2.get(path2.size() - 1); - if (root1 != root2) - notSameGraph(o1, o2); + // Sanity checks: nodes part of same scene graph + if (path1.get(path1.size() - 1) != path2.get(path2.size() - 1)) + notSameGraph((INode)e1, (INode)e2); - try { // Find first non-matching nodes in the paths starting from the root node int i1 = path1.size() - 1; int i2 = path2.size() - 1; @@ -124,27 +112,31 @@ public class NodeEventHandler implements IEventHandler { if (i2 < 0) return Order.ASCENDING == order ? 1 : -1; - INode n1 = path1.get(i1); - INode n2 = path2.get(i2); - IG2DNode g1 = n1 instanceof IG2DNode ? (IG2DNode) n1 : null; - IG2DNode g2 = n2 instanceof IG2DNode ? (IG2DNode) n2 : null; - if (g1 != null && g2 != null) { - int z1 = g1.getZIndex(); - int z2 = g2.getZIndex(); - int c = compare(z1, z2); - return order == Order.ASCENDING ? c : -c; - } - // Can't sort non-IG2DNodes. - return 0; + return compare(path1.get(i1), path2.get(i2)); } finally { // Don't hold on to objects unnecessarily path1.clear(); path2.clear(); } } - - private int compare(int v1, int v2) { - return v1 < v2 ? -1 : (v1 > v2 ? 1 : 0); + + private int compare(INode n1, INode n2) { + if(n1 instanceof IG2DNode) { + if(n2 instanceof IG2DNode) { + int z1 = ((IG2DNode)n1).getZIndex(); + int z2 = ((IG2DNode)n2).getZIndex(); + int c = Integer.compare(z1, z2); + return order == Order.ASCENDING ? c : -c; + } + else + return -1; // sort IG2DNodes before non-IG2DNodes + } + else { + if(n2 instanceof IG2DNode) + return 1; + else + return 0; // all non-IG2DNodes are equal in comparison + } } }; @@ -197,12 +189,6 @@ public class NodeEventHandler implements IEventHandler { */ protected G2DSceneGraph sg; - /** - * For proper initiation of native DnD operations within this AWT-based - * scenegraph system. - */ - protected DragSource ds = new DragSource(); - public NodeEventHandler(G2DSceneGraph sg) { this.sg = sg; } @@ -221,60 +207,6 @@ public class NodeEventHandler implements IEventHandler { return sort; } - public void setRootPane(Component rootPane) { - final DragSourceListener dsl = new DragSourceListener() { - @Override - public void dropActionChanged(DragSourceDragEvent dsde) { - } - @Override - public void dragOver(DragSourceDragEvent dsde) { - } - @Override - public void dragExit(DragSourceEvent dse) { - } - @Override - public void dragEnter(DragSourceDragEvent dsde) { - } - @Override - public void dragDropEnd(DragSourceDropEvent dsde) { - } - }; - DragGestureListener dgl = new DragGestureListener() { - @Override - public void dragGestureRecognized(DragGestureEvent dge) { - InputEvent ie = dge.getTriggerEvent(); - if (ie instanceof java.awt.event.MouseEvent) { - java.awt.event.MouseEvent e = (java.awt.event.MouseEvent) ie; - Point2D controlPos = AWTMouseEventAdapter.getControlPosition(e); - MouseDragBegin event = new MouseDragBegin(NodeEventHandler.this, - e.getWhen(), 0, - AWTMouseEventAdapter.getButtonStatus(e), - AWTMouseEventAdapter.getStateMask(e), - AWTMouseEventAdapter.getMouseButton(e), - // TODO: fix canvas position if necessary - new Point2D.Double(), - controlPos, - controlPos, - AWTMouseEventAdapter.getScreenPosition(e)); - - // Send MouseDragBegin to the scenegraph and see - // if anyone sets event.transferable to start DnD. - handleMouseDragBeginEvent(event, EventTypes.MouseDragBegin); - if (event.transferable != null) { - ds.startDrag(dge, null, event.transferable, dsl); - if (DEBUG_EVENTS) - debug("dragGestureRecognized: startDrag " + event.transferable); - } - } - } - }; - ds.createDefaultDragGestureRecognizer( - rootPane, - DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK, - dgl); - ds.addDragSourceListener(dsl); - } - public boolean mousePressed(MouseButtonPressedEvent event) { G2DFocusManager.INSTANCE.clearFocus(); try {