}
private Iterator sortedChildren(final NodeItem n) {
double base = 0;
// update base angle for node ordering
NodeItem p = (NodeItem)n.getParent();
if ( p != null ) {
base = normalize(Math.atan2(p.getY()-n.getY(), p.getX()-n.getX()));
}
int cc = n.getChildCount();
if ( cc == 0 ) return null;
NodeItem c = (NodeItem)n.getFirstChild();
// TODO: this is hacky and will break when filtering
// how to know that a branch is newly expanded?
// is there an alternative property we should check?
if ( !c.isStartVisible() ) {
// use natural ordering for previously invisible nodes
return n.children();
}
double angle[] = new double[cc];
final int idx[] = new int[cc];
for ( int i=0; i<cc; ++i, c=(NodeItem)c.getNextSibling() ) {
idx[i] = i;
angle[i] = normalize(-base +
Math.atan2(c.getY()-n.getY(), c.getX()-n.getX()));
}
ArrayLib.sort(angle, idx);
// return iterator over sorted children
return new Iterator() {