From: Hannu Niemistö Date: Tue, 21 Mar 2017 08:15:45 +0000 (+0200) Subject: (refs #7102) Fixed comparator NodeEventHandler.TreePreOrderComparator X-Git-Tag: v1.28.0~45^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=a00b9cf3edf556c32bc6ab74c74e19ae76975dc0;p=simantics%2Fplatform.git (refs #7102) Fixed comparator NodeEventHandler.TreePreOrderComparator Change-Id: Ib7db825832affc47306e351d28935752a8a1c9d6 --- 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..ec0a4fe5f 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 @@ -75,8 +75,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 +94,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 +120,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 + } } };