FileDocCategorySizeDatePackage
FixedHeightLayoutCache.javaAPI DocJava SE 5 API45196Fri Aug 26 14:58:22 BST 2005javax.swing.tree

FixedHeightLayoutCache

public class FixedHeightLayoutCache extends AbstractLayoutCache
NOTE: This will become more open in a future release.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.24 05/05/04
author
Scott Violet

Fields Summary
private FHTreeStateNode
root
Root node.
private int
rowCount
Number of rows currently visible.
private Rectangle
boundsBuffer
Used in getting sizes for nodes to avoid creating a new Rectangle every time a size is needed.
private Hashtable
treePathMapping
Maps from TreePath to a FHTreeStateNode.
private SearchInfo
info
Used for getting path/row information.
private Stack
tempStacks
Constructors Summary
public FixedHeightLayoutCache()

	super();
	tempStacks = new Stack();
	boundsBuffer = new Rectangle();
	treePathMapping = new Hashtable();
	info = new SearchInfo();
	setRowHeight(1);
    
Methods Summary
private voidaddMapping(javax.swing.tree.FixedHeightLayoutCache$FHTreeStateNode node)
Adds a mapping for node.

	treePathMapping.put(node.getTreePath(), node);
    
private voidadjustRowCountBy(int changeAmount)
Adjust the large row count of the AbstractTreeUI the receiver was created with.

	rowCount += changeAmount;
    
private javax.swing.tree.FixedHeightLayoutCache$FHTreeStateNodecreateNodeForValue(java.lang.Object value, int childIndex)
Creates and returns an instance of FHTreeStateNode.

	return new FHTreeStateNode(value, childIndex, -1);
    
private booleanensurePathIsExpanded(javax.swing.tree.TreePath aPath, boolean expandLast)
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.

	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;
    
private java.awt.RectanglegetBounds(javax.swing.tree.FixedHeightLayoutCache$FHTreeStateNode parent, int childIndex, java.awt.Rectangle placeIn)
Returns the bounds for the given node. If childIndex is -1, the bounds of parent are returned, otherwise the bounds of the node at childIndex are returned.

	boolean              expanded;
	int                  level;
	int                  row;
	Object               value;

	if(childIndex == -1) {
	    // Getting bounds for parent
	    row = parent.getRow();
	    value = parent.getUserObject();
	    expanded = parent.isExpanded();
	    level = parent.getLevel();
	}
	else {
	    row = parent.getRowToModelIndex(childIndex);
	    value = treeModel.getChild(parent.getUserObject(), childIndex);
	    expanded = false;
	    level = parent.getLevel() + 1;
	}

	Rectangle      bounds = getNodeDimensions(value, row, level,
						  expanded, boundsBuffer);
	// No node dimensions, bail.
	if(bounds == null)
	    return null;

	if(placeIn == null)
	    placeIn = new Rectangle();

	placeIn.x = bounds.x;
	placeIn.height = getRowHeight();
	placeIn.y = row * placeIn.height;
	placeIn.width = bounds.width;
	return placeIn;
    
public java.awt.RectanglegetBounds(javax.swing.tree.TreePath path, java.awt.Rectangle placeIn)
Returns a rectangle giving the bounds needed to draw path.

param
path a TreePath specifying a node
param
placeIn a Rectangle object giving the available space
return
a Rectangle object specifying the space to be used

	if(path == null)
	    return null;

	FHTreeStateNode      node = getNodeForPath(path, true, false);

	if(node != null)
	    return getBounds(node, -1, placeIn);

	// node hasn't been created yet.
	TreePath       parentPath = path.getParentPath();

	node = getNodeForPath(parentPath, true, false);
	if(node != null) {
	    int              childIndex = treeModel.getIndexOfChild
		                 (parentPath.getLastPathComponent(),
				  path.getLastPathComponent());

	    if(childIndex != -1)
		return getBounds(node, childIndex, placeIn);
	}
	return null;
    
public booleangetExpandedState(javax.swing.tree.TreePath path)
Returns true if the path is expanded, and visible.

	FHTreeStateNode       node = getNodeForPath(path, true, false);

	return (node != null) ? (node.isVisible() && node.isExpanded()) :
	                         false;
    
private javax.swing.tree.FixedHeightLayoutCache$FHTreeStateNodegetMapping(javax.swing.tree.TreePath path)
Returns the node previously added for path. This may return null, if you to create a node use getNodeForPath.

	return (FHTreeStateNode)treePathMapping.get(path);
    
private javax.swing.tree.FixedHeightLayoutCache$FHTreeStateNodegetNodeForPath(javax.swing.tree.TreePath path, boolean onlyIfVisible, boolean shouldCreate)
Messages getTreeNodeForPage(path, onlyIfVisible, shouldCreate, path.length) as long as path is non-null and the length is > 0. Otherwise returns null.

	if(path != null) {
	    FHTreeStateNode      node;

	    node = getMapping(path);
	    if(node != null) {
		if(onlyIfVisible && !node.isVisible())
		    return null;
		return node;
	    }
	    if(onlyIfVisible)
		return null;

	    // Check all the parent paths, until a match is found.
	    Stack                paths;

	    if(tempStacks.size() == 0) {
		paths = new Stack();
	    }
	    else {
		paths = (Stack)tempStacks.pop();
	    }

	    try {
		paths.push(path);
		path = path.getParentPath();
		node = null;
		while(path != null) {
		    node = getMapping(path);
		    if(node != null) {
			// Found a match, create entries for all paths in
			// paths.
			while(node != null && paths.size() > 0) {
			    path = (TreePath)paths.pop();
			    node = node.createChildFor(path.
						       getLastPathComponent());
			}
			return node;
		    }
		    paths.push(path);
		    path = path.getParentPath();
		}
	    }
	    finally {
		paths.removeAllElements();
		tempStacks.push(paths);
	    }
	    // If we get here it means they share a different root!
	    return null;
	}
	return null;
    
public javax.swing.tree.TreePathgetPathClosestTo(int x, int y)
Returns the path to the node that is closest to x,y. If there is nothing currently visible this will return null, otherwise it'll always return a valid path. If you need to test if the returned object is exactly at x, y you should get the bounds for the returned path and test x, y against that.

	if(getRowCount() == 0)
	    return null;

	int                row = getRowContainingYLocation(y);

	return getPathForRow(row);
    
public javax.swing.tree.TreePathgetPathForRow(int row)
Returns the path for passed in row. If row is not visible null is returned.

	if(row >= 0 && row < getRowCount()) {
	    if(root.getPathForRow(row, getRowCount(), info)) {
		return info.getPath();
	    }
	}
	return null;
    
private intgetRowContainingYLocation(int location)
Returns the index of the row containing location. If there are no rows, -1 is returned. If location is beyond the last row index, the last row index is returned.

	if(getRowCount() == 0)
	    return -1;
	return Math.max(0, Math.min(getRowCount() - 1,
				    location / getRowHeight()));
    
public intgetRowCount()
Returns the number of visible rows.

	return rowCount;
    
public intgetRowForPath(javax.swing.tree.TreePath path)
Returns the row that the last item identified in path is visible at. Will return -1 if any of the elements in path are not currently visible.

	if(path == null || root == null)
	    return -1;

	FHTreeStateNode         node = getNodeForPath(path, true, false);

	if(node != null)
	    return node.getRow();

	TreePath       parentPath = path.getParentPath();

	node = getNodeForPath(parentPath, true, false);
	if(node != null && node.isExpanded()) {
	    return node.getRowToModelIndex(treeModel.getIndexOfChild
					   (parentPath.getLastPathComponent(),
					    path.getLastPathComponent()));
	}
	return -1;
    
public intgetVisibleChildCount(javax.swing.tree.TreePath path)
Returns the number of visible children for row.

	FHTreeStateNode         node = getNodeForPath(path, true, false);

	if(node == null)
	    return 0;
	return node.getTotalChildCount();
    
public java.util.EnumerationgetVisiblePathsFrom(javax.swing.tree.TreePath path)
Returns an Enumerator that increments over the visible paths starting at the passed in location. The ordering of the enumeration is based on how the paths are displayed.

	if(path == null)
	    return null;

	FHTreeStateNode         node = getNodeForPath(path, true, false);

	if(node != null) {
	    return new VisibleFHTreeStateNodeEnumeration(node);
	}
	TreePath            parentPath = path.getParentPath();

	node = getNodeForPath(parentPath, true, false);
	if(node != null && node.isExpanded()) {
	    return new VisibleFHTreeStateNodeEnumeration(node,
		  treeModel.getIndexOfChild(parentPath.getLastPathComponent(),
					    path.getLastPathComponent()));
	}
	return null;
    
public voidinvalidatePathBounds(javax.swing.tree.TreePath path)
Does nothing, FixedHeightLayoutCache doesn't cache width, and that is all that could change.

    
public voidinvalidateSizes()
Informs the TreeState that it needs to recalculate all the sizes it is referencing.

	// Nothing to do here, rowHeight still same, which is all
	// this is interested in, visible region may have changed though.
	visibleNodesChanged();
    
public booleanisExpanded(javax.swing.tree.TreePath path)
Returns true if the value identified by row is currently expanded.

	if(path != null) {
	    FHTreeStateNode     lastNode = getNodeForPath(path, true, false);

	    return (lastNode != null && lastNode.isExpanded());
	}
	return false;
    
private voidrebuild(boolean clearSelection)
Sent to completely rebuild the visible tree. All nodes are collapsed.

        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();
    
private voidremoveMapping(javax.swing.tree.FixedHeightLayoutCache$FHTreeStateNode node)
Removes the mapping for a previously added node.

	treePathMapping.remove(node.getTreePath());
    
public voidsetExpandedState(javax.swing.tree.TreePath path, boolean isExpanded)
Marks the path path expanded state to isExpanded.

	if(isExpanded)
	    ensurePathIsExpanded(path, true);
	else if(path != null) {
	    TreePath              parentPath = path.getParentPath();

	    // YECK! Make the parent expanded.
	    if(parentPath != null) {
		FHTreeStateNode     parentNode = getNodeForPath(parentPath,
								false, true);
		if(parentNode != null)
		    parentNode.makeVisible();
	    }
	    // And collapse the child.
	    FHTreeStateNode         childNode = getNodeForPath(path, true,
							       false);

	    if(childNode != null)
		childNode.collapse(true);
	}
    
public voidsetModel(javax.swing.tree.TreeModel newModel)
Sets the TreeModel that will provide the data.

param
newModel the TreeModel that is to provide the data

	super.setModel(newModel);
	rebuild(false);
    
public voidsetRootVisible(boolean rootVisible)
Determines whether or not the root node from the TreeModel is visible.

param
rootVisible true if the root node of the tree is to be displayed
see
#rootVisible

	if(isRootVisible() != rootVisible) {
	    super.setRootVisible(rootVisible);
	    if(root != null) {
		if(rootVisible) {
		    rowCount++;
		    root.adjustRowBy(1);
		}
		else {
		    rowCount--;
		    root.adjustRowBy(-1);
		}
		visibleNodesChanged();
	    }
	}
    
public voidsetRowHeight(int rowHeight)
Sets the height of each cell. If rowHeight is less than or equal to 0 this will throw an IllegalArgumentException.

param
rowHeight the height of each cell, in pixels

	if(rowHeight <= 0)
	    throw new IllegalArgumentException("FixedHeightLayoutCache only supports row heights greater than 0");
	if(getRowHeight() != rowHeight) {
	    super.setRowHeight(rowHeight);
	    visibleNodesChanged();
	}
    
public voidtreeNodesChanged(javax.swing.event.TreeModelEvent e)

Invoked after a node (or a set of siblings) has changed in some way. The node(s) have not changed locations in the tree or altered their children arrays, but other attributes have changed and may affect presentation. Example: the name of a file has changed, but it is in the same location in the file system.

e.path() returns the path the parent of the changed node(s).

e.childIndices() returns the index(es) of the changed node(s).

	if(e != null) {
	    int                 changedIndexs[];
	    FHTreeStateNode     changedParent = getNodeForPath
		                  (e.getTreePath(), false, false);
	    int                 maxCounter;

	    changedIndexs = e.getChildIndices();
	    /* Only need to update the children if the node has been
	       expanded once. */
	    // PENDING(scott): make sure childIndexs is sorted!
	    if (changedParent != null) {
		if (changedIndexs != null &&
		    (maxCounter = changedIndexs.length) > 0) {
		    Object       parentValue = changedParent.getUserObject();

		    for(int counter = 0; counter < maxCounter; counter++) {
			FHTreeStateNode    child = changedParent.
			         getChildAtModelIndex(changedIndexs[counter]);

			if(child != null) {
			    child.setUserObject(treeModel.getChild(parentValue,
						     changedIndexs[counter]));
			}
		    }
		    if(changedParent.isVisible() && changedParent.isExpanded())
			visibleNodesChanged();
		}
		// Null for root indicates it changed.
		else if (changedParent == root && changedParent.isVisible() &&
			 changedParent.isExpanded()) {
		    visibleNodesChanged();
		}
	    }
	}
    
public voidtreeNodesInserted(javax.swing.event.TreeModelEvent e)

Invoked after nodes have been inserted into the tree.

e.path() returns the parent of the new nodes

e.childIndices() returns the indices of the new nodes in ascending order.

	if(e != null) {
	    int                 changedIndexs[];
	    FHTreeStateNode     changedParent = getNodeForPath
		                  (e.getTreePath(), false, false);
	    int                 maxCounter;

	    changedIndexs = e.getChildIndices();
	    /* Only need to update the children if the node has been
	       expanded once. */
	    // PENDING(scott): make sure childIndexs is sorted!
	    if(changedParent != null && changedIndexs != null &&
	       (maxCounter = changedIndexs.length) > 0) {
		boolean          isVisible =
		    (changedParent.isVisible() &&
		     changedParent.isExpanded());

		for(int counter = 0; counter < maxCounter; counter++) {
		    changedParent.childInsertedAtModelIndex
			(changedIndexs[counter], isVisible);
		}
		if(isVisible && treeSelectionModel != null)
		    treeSelectionModel.resetRowSelection();
		if(changedParent.isVisible())
		    this.visibleNodesChanged();
	    }
	}
    
public voidtreeNodesRemoved(javax.swing.event.TreeModelEvent e)

Invoked after nodes have been removed from the tree. Note that if a subtree is removed from the tree, this method may only be invoked once for the root of the removed subtree, not once for each individual set of siblings removed.

e.path() returns the former parent of the deleted nodes.

e.childIndices() returns the indices the nodes had before they were deleted in ascending order.

	if(e != null) {
	    int                  changedIndexs[];
	    int                  maxCounter;
	    TreePath             parentPath = e.getTreePath();
	    FHTreeStateNode      changedParentNode = getNodeForPath
		                       (parentPath, false, false);

	    changedIndexs = e.getChildIndices();
	    // PENDING(scott): make sure that changedIndexs are sorted in
	    // ascending order.
	    if(changedParentNode != null && changedIndexs != null &&
	       (maxCounter = changedIndexs.length) > 0) {
		Object[]           children = e.getChildren();
		boolean            isVisible =
		    (changedParentNode.isVisible() &&
		     changedParentNode.isExpanded());

		for(int counter = maxCounter - 1; counter >= 0; counter--) {
		    changedParentNode.removeChildAtModelIndex
			             (changedIndexs[counter], isVisible);
		}
		if(isVisible) {
		    if(treeSelectionModel != null)
			treeSelectionModel.resetRowSelection();
                    if (treeModel.getChildCount(changedParentNode.
                                                getUserObject()) == 0 &&
                                  changedParentNode.isLeaf()) {
                        // Node has become a leaf, collapse it.
                        changedParentNode.collapse(false);
                    }
		    visibleNodesChanged();
		}
		else if(changedParentNode.isVisible())
		    visibleNodesChanged();
	    }
	}
    
public voidtreeStructureChanged(javax.swing.event.TreeModelEvent e)

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.

e.path() holds the path to the node.

e.childIndices() returns null.

	if(e != null) {
	    TreePath          changedPath = e.getTreePath();
            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();
	    }
	}
    
private voidvisibleNodesChanged()