/** * Updates the explorer for new perspectives / orderings. * * <p>{@inheritDoc} */ public void itemStateChanged(ItemEvent e) { if (e.getSource() instanceof PerspectiveComboBox) { rules = ((ExplorerPerspective) e.getItem()).getList(); } else { // it is the combo for "order" order = (Comparator) e.getItem(); } structureChanged(); // TODO: temporary - let tree expand implicitly - tfm tree.expandPath(tree.getPathForRow(1)); }
/** * Sorts the child nodes of node using the current ordering. * * <p>Note: UserObject is only available from descendants of DefaultMutableTreeNode, so any other * children couldn't be sorted. Thus these are currently forbidden. But currently no such node is * ever inserted into the tree. * * @param node the node whose children to sort * @return the UserObjects of the children, in the same order as the children. * @throws IllegalArgumentException if node has a child that is not a (descendant of) * DefaultMutableTreeNode. */ private List<Object> reorderChildren(ExplorerTreeNode node) { List<Object> childUserObjects = new ArrayList<Object>(); List<ExplorerTreeNode> reordered = new ArrayList<ExplorerTreeNode>(); // Enumerate the current children of node to find out which now sorts // in different order, since these must be moved Enumeration enChld = node.children(); Object lastObj = null; while (enChld.hasMoreElements()) { Object child = enChld.nextElement(); if (child instanceof ExplorerTreeNode) { Object obj = ((ExplorerTreeNode) child).getUserObject(); if (lastObj != null && order.compare(lastObj, obj) > 0) { /* * If a node to be moved is currently selected, * move its predecessors instead so don't lose selection. * This fixes issue 3249. * NOTE: this does not deal with the case where * multiple nodes are selected and they are out * of order with respect to each other, but I * don't think more than one node is ever reordered * at a time - tfm */ if (!tree.isPathSelected(new TreePath(getPathToRoot((ExplorerTreeNode) child)))) { reordered.add((ExplorerTreeNode) child); } else { ExplorerTreeNode prev = (ExplorerTreeNode) ((ExplorerTreeNode) child).getPreviousSibling(); while (prev != null && (order.compare(prev.getUserObject(), obj) >= 0)) { reordered.add(prev); childUserObjects.remove(childUserObjects.size() - 1); prev = (ExplorerTreeNode) prev.getPreviousSibling(); } childUserObjects.add(obj); lastObj = obj; } } else { childUserObjects.add(obj); lastObj = obj; } } else { throw new IllegalArgumentException("Incomprehencible child node " + child.toString()); } } for (ExplorerTreeNode child : reordered) { // Avoid our deinitialization here // The node will be added back to the tree again super.removeNodeFromParent(child); } // For each reordered node, find it's new position among the current // children and move it there for (ExplorerTreeNode child : reordered) { Object obj = child.getUserObject(); int ip = Collections.binarySearch(childUserObjects, obj, order); if (ip < 0) { ip = -(ip + 1); } // Avoid our initialization here super.insertNodeInto(child, node, ip); childUserObjects.add(ip, obj); } return childUserObjects; }