Community
Participate
Working Groups
ToolUtilities.findCommonAncestor should return the containing edit part when one of the edit parts passed to it as an argument contains the other edit part. Instead, an ArrayIndexOutOfBounds exception is thrown. Here is the code. See comment in the code: public static EditPart findCommonAncestor(EditPart ll, EditPart rr) { if (ll == rr) return ll; ArrayList leftAncestors = new ArrayList(); ArrayList rightAncestors = new ArrayList(); EditPart l = ll; EditPart r = rr; while (l != null) { leftAncestors.add(l); l = l.getParent(); } while (r != null) { rightAncestors.add(r); r = r.getParent(); } int il = leftAncestors.size() - 1; int ir = rightAncestors.size() - 1; /***** if ((il == 0 || ir == 0) && leftAncestors.get(il) == rightAncestors.get(ir)) should return leftAncestors.get(il) instead the loop continues and an aioob exception is thrown *********/ while (leftAncestors.get(il) == rightAncestors.get(ir)) { il--; ir--; } il++; return (EditPart)leftAncestors.get(il); }
This utility method has only ever been tested when the left and right editparts are "leaves" in the editpart tree, as occurs in our text example. Would you care to submit a patch?
I don't have my workspace set up to create patches, but here is the new version of the method that I am using in my code. I hope you can use this: public static EditPart findCommonAncestor(EditPart ll, EditPart rr) { if (ll == rr) return ll; ArrayList leftAncestors = new ArrayList(); ArrayList rightAncestors = new ArrayList(); EditPart l = ll; EditPart r = rr; while (l != null) { leftAncestors.add(l); l = l.getParent(); } while (r != null) { rightAncestors.add(r); r = r.getParent(); } int il = leftAncestors.size() - 1; int ir = rightAncestors.size() - 1; while (leftAncestors.get(il) == rightAncestors.get(ir)) { il--; ir--; // fix bug 130042 // one of the parts contains the other part if ( (il == 0 || ir == 0) && leftAncestors.get(il) == rightAncestors.get(ir)) { return leftAncestors.get(il); } } il++; return (EditPart)leftAncestors.get(il); }
My choice: do { if (leftAncestors.get(il) != rightAncestors.get(ir)) break; il--; ir--; } while (il >= 0 && ir >= 0); return (EditPart) leftAncestors.get(il + 1); }
Looks good to me.
Create JUnit test to verify failure and for happy path in org.eclipse.gef.test.ToolUtilitiesTest. Fixed for next integration build.