/** * Returns the path from the child, where the path indicates the child is * placed in the whole tree. * @param child the node we are interested in * @since 6.0.0 */ public int[] getPath(TreeNode<E> child) { final TreeNode<E> root = getRoot(); List<Integer> p = new ArrayList<Integer>(); while (root != child) { TreeNode<E> parent = child.getParent(); if (parent != null) { for (int i = 0, j = parent.getChildCount(); i < j; i++) { if (parent.getChildAt(i) == child) { p.add(0, i); break; } } child = parent; } else break; // ZK-838 } final Integer[] objs = p.toArray(new Integer[p.size()]); final int[] path = new int[objs.length]; for (int i = 0; i < objs.length; i++) path[i] = objs[i].intValue(); return path; }
@SuppressWarnings("unchecked") public Object clone() { final DefaultTreeModel clone = (DefaultTreeModel) super.clone(); final TreeNode cloneRoot = (TreeNode) getRoot().clone(); cloneRoot.setModel(this); clone.setRootDirectly(cloneRoot); return clone; } }
/** Sorts the data. * * <p>Notice: it invokes {@link #beforeSort} and {@link #afterSort} * to save and restore the selection and open states. * If you prefer not to preserve objects and prefer to save the paths, * you can override {@link #beforeSort} to do nothing but returning null. * If you prefer to clear the selection, you can override {@link #beforeSort} * to clear {@link #_selection} and return null. * * @param cmpr the comparator. * @param ascending whether to sort in the ascending order. * It is ignored since this implementation uses cmprt to compare. */ public void sort(Comparator<TreeNode<E>> cmpr, final boolean ascending) { _sorting = cmpr; _sortDir = ascending; TreeNode<E> root = getRoot(); if (root != null) { final Object ctx = beforeSort(); sort0(root, cmpr); afterSort(ctx); //before firing event fireEvent(TreeDataEvent.STRUCTURE_CHANGED, null, 0, 0); } }