/** Sent to completely rebuild the visible tree. All nodes are collapsed. */ private void rebuild(boolean clearSelection) { Object rootUO; treePathMapping.clear(); if (treeModel != null && (rootUO = treeModel.getRoot()) != null) { root = createNodeForValue(rootUO, 0); root.path = new TreePath(rootUO); addMapping(root); if (isRootVisible()) { rowCount = 1; root.row = 0; } else { rowCount = 0; root.row = -1; } root.expand(); } else { root = null; rowCount = 0; } if (clearSelection && treeSelectionModel != null) { treeSelectionModel.clearSelection(); } this.visibleNodesChanged(); }
/** * Invoked after the tree has drastically changed structure from a given node down. If the path * returned by e.getPath() is of length one and the first element does not identify the current * root node the first element should become the new root of the tree. * * <p>e.path() holds the path to the node. * * <p>e.childIndices() returns null. */ public void treeStructureChanged(TreeModelEvent e) { if (e != null) { TreePath changedPath = SwingUtilities2.getTreePath(e, getModel()); FHTreeStateNode changedNode = getNodeForPath(changedPath, false, false); // Check if root has changed, either to a null root, or // to an entirely new root. if (changedNode == root || (changedNode == null && ((changedPath == null && treeModel != null && treeModel.getRoot() == null) || (changedPath != null && changedPath.getPathCount() <= 1)))) { rebuild(true); } else if (changedNode != null) { boolean wasExpanded, wasVisible; FHTreeStateNode parent = (FHTreeStateNode) changedNode.getParent(); wasExpanded = changedNode.isExpanded(); wasVisible = changedNode.isVisible(); int index = parent.getIndex(changedNode); changedNode.collapse(false); parent.remove(index); if (wasVisible && wasExpanded) { int row = changedNode.getRow(); parent.resetChildrenRowsFrom(row, index, changedNode.getChildIndex()); changedNode = getNodeForPath(changedPath, false, true); changedNode.expand(); } if (treeSelectionModel != null && wasVisible && wasExpanded) treeSelectionModel.resetRowSelection(); if (wasVisible) this.visibleNodesChanged(); } } }
/** * Ensures that all the path components in path are expanded, accept for the last component which * will only be expanded if expandLast is true. Returns true if succesful in finding the path. */ private boolean ensurePathIsExpanded(TreePath aPath, boolean expandLast) { if (aPath != null) { // Make sure the last entry isn't a leaf. if (treeModel.isLeaf(aPath.getLastPathComponent())) { aPath = aPath.getParentPath(); expandLast = true; } if (aPath != null) { FHTreeStateNode lastNode = getNodeForPath(aPath, false, true); if (lastNode != null) { lastNode.makeVisible(); if (expandLast) lastNode.expand(); return true; } } } return false; }