home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.tree.DefaultMutableTreeNode;
- import com.sun.java.swing.tree.MutableTreeNode;
- import com.sun.java.swing.tree.TreeModel;
- import com.sun.java.swing.tree.TreePath;
- import com.sun.java.swing.tree.TreeSelectionModel;
- import java.awt.Dimension;
- import java.awt.Rectangle;
- import java.util.Enumeration;
- import java.util.Vector;
-
- public class VisibleTreeNode extends DefaultMutableTreeNode {
- protected AbstractTreeUI treeUI;
- protected Dimension preferredSize;
- protected int yOrigin;
- protected boolean expanded;
- protected boolean hasBeenExpanded;
- protected boolean isValid;
- public static final Dimension EMPTY_SIZE = new Dimension(0, 0);
-
- public VisibleTreeNode(AbstractTreeUI treeUI, Object value, int index) {
- super(value);
- this.treeUI = treeUI;
- this.isValid = true;
- this.updatePreferredSize(index);
- }
-
- public Enumeration children() {
- return !this.isExpanded() ? DefaultMutableTreeNode.EMPTY_ENUMERATION : super.children();
- }
-
- public void collapse() {
- this.collapse(true);
- }
-
- protected void collapse(boolean adjustTree) {
- if (this.isExpanded()) {
- Vector selectedPaths = null;
- Enumeration cursor = ((DefaultMutableTreeNode)this).preorderEnumeration();
- cursor.nextElement();
- int rowsDeleted = 0;
- boolean isFixed = this.treeUI.isFixedRowHeight();
- int lastYEnd;
- if (isFixed) {
- lastYEnd = 0;
- } else {
- lastYEnd = this.getPreferredSize().height + this.getYOrigin();
- }
-
- int startYEnd = lastYEnd;
- int myRow = this.getRow();
- Vector visibleNodes = this.getVisibleNodes();
- TreeSelectionModel treeSelectionModel = this.treeUI.getSelectionModel();
- if (!isFixed) {
- while(cursor.hasMoreElements()) {
- VisibleTreeNode node = (VisibleTreeNode)cursor.nextElement();
- if (visibleNodes.contains(node)) {
- ++rowsDeleted;
- if (treeSelectionModel != null && treeSelectionModel.isRowSelected(rowsDeleted + myRow)) {
- if (selectedPaths == null) {
- selectedPaths = new Vector();
- }
-
- selectedPaths.addElement(node.getTreePath());
- }
-
- visibleNodes.removeElement(node);
- lastYEnd = node.getYOrigin() + node.getPreferredSize().height;
- }
- }
- } else {
- while(cursor.hasMoreElements()) {
- VisibleTreeNode node = (VisibleTreeNode)cursor.nextElement();
- if (visibleNodes.contains(node)) {
- ++rowsDeleted;
- if (treeSelectionModel != null && treeSelectionModel.isRowSelected(rowsDeleted + myRow)) {
- if (selectedPaths == null) {
- selectedPaths = new Vector();
- }
-
- selectedPaths.addElement(node.getTreePath());
- }
-
- visibleNodes.removeElement(node);
- }
- }
- }
-
- if (rowsDeleted > 0 && adjustTree && myRow != -1) {
- if (!isFixed && myRow + 1 < this.treeUI.getRowCount() && startYEnd != lastYEnd) {
- int shiftAmount = startYEnd - lastYEnd;
- int counter = myRow + 1;
-
- for(int maxCounter = visibleNodes.size(); counter < maxCounter; ++counter) {
- ((VisibleTreeNode)visibleNodes.elementAt(counter)).shiftYOriginBy(shiftAmount);
- }
- }
-
- this.expanded = false;
- this.didAdjustTree();
- this.treeUI.visibleNodesChanged();
- } else {
- this.expanded = false;
- }
-
- this.treeUI.pathWasCollapsed(this.getTreePath());
- if (treeSelectionModel != null && rowsDeleted > 0 && myRow != -1) {
- if (selectedPaths != null) {
- int maxCounter = selectedPaths.size();
- TreePath[] treePaths = new TreePath[maxCounter];
- selectedPaths.copyInto(treePaths);
- treeSelectionModel.removeSelectionPaths(treePaths);
- treeSelectionModel.addSelectionPath(this.getTreePath());
- } else {
- treeSelectionModel.resetRowSelection();
- }
- }
- }
-
- }
-
- protected void didAdjustTree() {
- }
-
- public void expand() {
- this.expand(true);
- }
-
- protected void expand(boolean adjustTree) {
- if (!this.isExpanded() && !this.isLeaf()) {
- Vector visibleNodes = this.getVisibleNodes();
- boolean isFixed = this.treeUI.isFixedRowHeight();
- this.expanded = true;
- int originalRow = this.getRow();
- if (!this.hasBeenExpanded) {
- Object realNode = this.getValue();
- TreeModel treeModel = this.treeUI.getModel();
- int count = treeModel.getChildCount(realNode);
- this.hasBeenExpanded = true;
- if (originalRow == -1) {
- for(int i = 0; i < count; ++i) {
- VisibleTreeNode i = this.treeUI.createNodeForValue(treeModel.getChild(realNode, i), -1);
- ((DefaultMutableTreeNode)this).add(i);
- }
- } else {
- int offset = originalRow + 1;
-
- for(int i = 0; i < count; ++i) {
- VisibleTreeNode newNode = this.treeUI.createNodeForValue(treeModel.getChild(realNode, i), offset);
- ((DefaultMutableTreeNode)this).add(newNode);
- }
- }
- }
-
- int i = originalRow;
- Enumeration cursor = ((DefaultMutableTreeNode)this).preorderEnumeration();
- cursor.nextElement();
- int newYOrigin;
- if (isFixed) {
- newYOrigin = 0;
- } else if (this == this.treeUI.treeCacheRoot && !this.treeUI.isRootVisible()) {
- newYOrigin = 0;
- } else {
- newYOrigin = this.getYOrigin() + this.getPreferredSize().height;
- }
-
- if (!isFixed) {
- boolean updateNodeSizes = this.treeUI.updateNodeSizes;
-
- while(cursor.hasMoreElements()) {
- VisibleTreeNode var19 = (VisibleTreeNode)cursor.nextElement();
- if (!updateNodeSizes && !var19.hasValidSize()) {
- var19.updatePreferredSize(i + 1);
- }
-
- var19.setYOrigin(newYOrigin);
- newYOrigin += var19.getPreferredSize().height;
- ++i;
- visibleNodes.insertElementAt(var19, i);
- }
- } else {
- while(cursor.hasMoreElements()) {
- VisibleTreeNode aNode = (VisibleTreeNode)cursor.nextElement();
- ++i;
- visibleNodes.insertElementAt(aNode, i);
- }
- }
-
- int endRow = i;
- if (originalRow != i && adjustTree) {
- if (!isFixed && ((DefaultMutableTreeNode)this).getChildCount() > 0) {
- ++i;
- if (i < this.treeUI.getRowCount()) {
- int heightDiff = newYOrigin - (this.getYOrigin() + this.getPreferredSize().height);
-
- for(int treeSelectionModel = visibleNodes.size() - 1; treeSelectionModel >= i; --treeSelectionModel) {
- ((VisibleTreeNode)visibleNodes.elementAt(treeSelectionModel)).shiftYOriginBy(heightDiff);
- }
- }
- }
-
- this.didAdjustTree();
- this.treeUI.visibleNodesChanged();
- }
-
- this.treeUI.pathWasExpanded(this.getTreePath());
- TreeSelectionModel treeSelectionModel = this.treeUI.getSelectionModel();
- if (treeSelectionModel != null) {
- if (originalRow != -1 && originalRow < endRow && treeSelectionModel.isRowSelected(originalRow) && treeSelectionModel.isRowSelected(originalRow + 1)) {
- TreePath[] toSelect = new TreePath[endRow - originalRow];
-
- for(int var15 = endRow; var15 > originalRow; --var15) {
- toSelect[endRow - var15] = this.treeUI.getNode(var15).getTreePath();
- }
-
- treeSelectionModel.addSelectionPaths(toSelect);
- } else {
- treeSelectionModel.resetRowSelection();
- }
- }
- }
-
- }
-
- public VisibleTreeNode getLastVisibleNode() {
- VisibleTreeNode node;
- for(node = this; node.isExpanded() && ((DefaultMutableTreeNode)node).getChildCount() > 0; node = (VisibleTreeNode)((DefaultMutableTreeNode)node).getLastChild()) {
- }
-
- return node;
- }
-
- public Enumeration getLoadedChildren(boolean createIfNeeded) {
- if (createIfNeeded && !this.hasBeenExpanded) {
- Object realNode = this.getValue();
- TreeModel treeModel = this.treeUI.getModel();
- int count = treeModel.getChildCount(realNode);
- this.hasBeenExpanded = true;
- int childRow = this.getRow();
- if (childRow == -1) {
- for(int i = 0; i < count; ++i) {
- VisibleTreeNode newNode = this.treeUI.createNodeForValue(treeModel.getChild(realNode, i), -1);
- ((DefaultMutableTreeNode)this).add(newNode);
- }
- } else {
- ++childRow;
-
- for(int i = 0; i < count; ++i) {
- VisibleTreeNode var8 = this.treeUI.createNodeForValue(treeModel.getChild(realNode, i), childRow++);
- ((DefaultMutableTreeNode)this).add(var8);
- }
- }
-
- return super.children();
- } else {
- return super.children();
- }
- }
-
- public int getModelChildCount() {
- return this.hasBeenExpanded ? super.getChildCount() : this.treeUI.getModel().getChildCount(this.getValue());
- }
-
- public Rectangle getNodeBounds() {
- Dimension pSize = this.getPreferredSize();
- return new Rectangle(this.treeUI.getXOriginOfNode(this), this.getYOrigin(), pSize.width, pSize.height);
- }
-
- public Dimension getPreferredSize() {
- return this.preferredSize;
- }
-
- public int getRow() {
- return this.treeUI.visibleNodes.indexOf(this);
- }
-
- protected TreePath getTreePath() {
- return this.treeUI.createTreePathFor(this);
- }
-
- public Object getValue() {
- return ((DefaultMutableTreeNode)this).getUserObject();
- }
-
- public int getVisibleLevel() {
- return this.treeUI.isRootVisible() ? ((DefaultMutableTreeNode)this).getLevel() : ((DefaultMutableTreeNode)this).getLevel() - 1;
- }
-
- protected Vector getVisibleNodes() {
- return this.treeUI.visibleNodes;
- }
-
- public int getYOrigin() {
- if (this.treeUI.isFixedRowHeight()) {
- int aRow = this.getRow();
- return aRow == -1 ? -1 : this.treeUI.getRowHeight() * aRow;
- } else {
- return this.yOrigin;
- }
- }
-
- public boolean hasBeenExpanded() {
- return this.hasBeenExpanded;
- }
-
- public boolean hasValidSize() {
- return this.preferredSize == null || this.preferredSize.height == 0;
- }
-
- public boolean isExpanded() {
- return this.expanded;
- }
-
- public boolean isLeaf() {
- return this.treeUI.getModel().isLeaf(this.getValue());
- }
-
- public boolean isSelected() {
- return this.treeUI.isSelectedIndex(this.getRow());
- }
-
- public boolean isVisible() {
- return this.treeUI.visibleNodes.contains(this);
- }
-
- void markInvalid() {
- this.isValid = false;
- if (super.children != null) {
- for(int counter = super.children.size() - 1; counter >= 0; --counter) {
- ((VisibleTreeNode)super.children.elementAt(counter)).markInvalid();
- }
- }
-
- }
-
- public void modelChildCountChanged() {
- }
-
- public void setParent(MutableTreeNode newParent) {
- super.setParent(newParent);
- if (newParent == null) {
- this.markInvalid();
- }
-
- }
-
- protected void setYOrigin(int newYOrigin) {
- this.yOrigin = newYOrigin;
- }
-
- protected void shiftYOriginBy(int offset) {
- this.yOrigin += offset;
- }
-
- public void toggleExpanded() {
- if (this.isExpanded()) {
- this.collapse();
- } else {
- this.expand();
- }
-
- }
-
- public void updatePreferredSize() {
- this.updatePreferredSize(-1);
- }
-
- protected void updatePreferredSize(int index) {
- this.preferredSize = this.treeUI.getSizeOfNode(this, index);
- if (this.preferredSize == null) {
- this.preferredSize = EMPTY_SIZE;
- this.treeUI.updateNodeSizes = true;
- } else if (this.preferredSize.height == 0) {
- this.treeUI.updateNodeSizes = true;
- }
-
- int rh = this.treeUI.getRowHeight();
- if (rh > 0) {
- this.preferredSize.height = rh;
- }
-
- }
-
- protected void updateTreeYLocationsFrom(int row) {
- this.treeUI.updateYLocationsFrom(row);
- }
-
- public int visibleChildCount() {
- int childCount = -1;
- Enumeration cursor = ((DefaultMutableTreeNode)this).preorderEnumeration();
-
- while(cursor.hasMoreElements()) {
- ++childCount;
- cursor.nextElement();
- }
-
- return childCount;
- }
- }
-