1767 lines · 61.8 KB · Java
RawDownload
/* * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing.tree; import javax.swing.event.TreeModelEvent;import java.awt.Dimension;import java.awt.Rectangle;import java.util.Enumeration;import java.util.Hashtable;import java.util.NoSuchElementException;import java.util.Stack;import java.util.Vector; /** * NOTE: This will become more open in a future release. * <p> * <strong>Warning:</strong> * 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 JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @author Rob Davis * @author Ray Ryan * @author Scott Violet */ public class VariableHeightLayoutCache extends AbstractLayoutCache {    /**     * The array of nodes that are currently visible, in the order they     * are displayed.     */    private Vector<Object> visibleNodes;     /**     * This is set to true if one of the entries has an invalid size.     */    private boolean           updateNodeSizes;     /**     * The root node of the internal cache of nodes that have been shown.     * If the treeModel is vending a network rather than a true tree,     * there may be one cached node for each path to a modeled node.     */    private TreeStateNode     root;     /**     * Used in getting sizes for nodes to avoid creating a new Rectangle     * every time a size is needed.     */    private Rectangle         boundsBuffer;     /**     * Maps from <code>TreePath</code> to a <code>TreeStateNode</code>.     */    private Hashtable<TreePath, TreeStateNode> treePathMapping;     /**     * A stack of stacks.     */    private Stack<Stack<TreePath>> tempStacks;      public VariableHeightLayoutCache() {        super();        tempStacks = new Stack<Stack<TreePath>>();        visibleNodes = new Vector<Object>();        boundsBuffer = new Rectangle();        treePathMapping = new Hashtable<TreePath, TreeStateNode>();    }     /**     * Sets the <code>TreeModel</code> that will provide the data.     *     * @param newModel the <code>TreeModel</code> that is to provide the data     * @beaninfo     *        bound: true     *  description: The TreeModel that will provide the data.     */    public void setModel(TreeModel newModel) {        super.setModel(newModel);        rebuild(false);    }     /**     * Determines whether or not the root node from     * the <code>TreeModel</code> is visible.     *     * @param rootVisible true if the root node of the tree is to be displayed     * @see #rootVisible     * @beaninfo     *        bound: true     *  description: Whether or not the root node     *               from the TreeModel is visible.     */    public void setRootVisible(boolean rootVisible) {        if(isRootVisible() != rootVisible && root != null) {            if(rootVisible) {                root.updatePreferredSize(0);                visibleNodes.insertElementAt(root, 0);            }            else if(visibleNodes.size() > 0) {                visibleNodes.removeElementAt(0);                if(treeSelectionModel != null)                    treeSelectionModel.removeSelectionPath                        (root.getTreePath());            }            if(treeSelectionModel != null)                treeSelectionModel.resetRowSelection();            if(getRowCount() > 0)                getNode(0).setYOrigin(0);            updateYLocationsFrom(0);            visibleNodesChanged();        }        super.setRootVisible(rootVisible);    }     /**     * Sets the height of each cell.  If the specified value     * is less than or equal to zero the current cell renderer is     * queried for each row's height.     *     * @param rowHeight the height of each cell, in pixels     * @beaninfo     *        bound: true     *  description: The height of each cell.     */    public void setRowHeight(int rowHeight) {        if(rowHeight != getRowHeight()) {            super.setRowHeight(rowHeight);            invalidateSizes();            this.visibleNodesChanged();        }    }     /**     * Sets the renderer that is responsible for drawing nodes in the tree.     * @param nd the renderer     */    public void setNodeDimensions(NodeDimensions nd) {        super.setNodeDimensions(nd);        invalidateSizes();        visibleNodesChanged();    }     /**     * Marks the path <code>path</code> expanded state to     * <code>isExpanded</code>.     * @param path the <code>TreePath</code> of interest     * @param isExpanded true if the path should be expanded, otherwise false     */    public void setExpandedState(TreePath path, boolean isExpanded) {        if(path != null) {            if(isExpanded)                ensurePathIsExpanded(path, true);            else {                TreeStateNode        node = getNodeForPath(path, false, true);                 if(node != null) {                    node.makeVisible();                    node.collapse();                }            }        }    }     /**     * Returns true if the path is expanded, and visible.     * @return true if the path is expanded and visible, otherwise false     */    public boolean getExpandedState(TreePath path) {        TreeStateNode       node = getNodeForPath(path, true, false);         return (node != null) ? (node.isVisible() && node.isExpanded()) :                                 false;    }     /**      * Returns the <code>Rectangle</code> enclosing the label portion      * into which the item identified by <code>path</code> will be drawn.      *      * @param path  the path to be drawn      * @param placeIn the bounds of the enclosing rectangle      * @return the bounds of the enclosing rectangle or <code>null</code>      *    if the node could not be ascertained      */    public Rectangle getBounds(TreePath path, Rectangle placeIn) {        TreeStateNode       node = getNodeForPath(path, true, false);         if(node != null) {            if(updateNodeSizes)                updateNodeSizes(false);            return node.getNodeBounds(placeIn);        }        return null;    }     /**      * Returns the path for <code>row</code>.  If <code>row</code>      * is not visible, <code>null</code> is returned.      *      * @param row the location of interest      * @return the path for <code>row</code>, or <code>null</code>      * if <code>row</code> is not visible      */    public TreePath getPathForRow(int row) {        if(row >= 0 && row < getRowCount()) {            return getNode(row).getTreePath();        }        return null;    }     /**      * Returns the row where the last item identified in path is visible.      * Will return -1 if any of the elements in path are not      * currently visible.      *      * @param path the <code>TreePath</code> of interest      * @return the row where the last item in path is visible      */    public int getRowForPath(TreePath path) {        if(path == null)            return -1;         TreeStateNode    visNode = getNodeForPath(path, true, false);         if(visNode != null)            return visNode.getRow();        return -1;    }     /**     * Returns the number of visible rows.     * @return the number of visible rows     */    public int getRowCount() {        return visibleNodes.size();    }     /**     * Instructs the <code>LayoutCache</code> that the bounds for     * <code>path</code> are invalid, and need to be updated.     *     * @param path the <code>TreePath</code> which is now invalid     */    public void invalidatePathBounds(TreePath path) {        TreeStateNode       node = getNodeForPath(path, true, false);         if(node != null) {            node.markSizeInvalid();            if(node.isVisible())                updateYLocationsFrom(node.getRow());        }    }     /**     * Returns the preferred height.     * @return the preferred height     */    public int getPreferredHeight() {        // Get the height        int           rowCount = getRowCount();         if(rowCount > 0) {            TreeStateNode  node = getNode(rowCount - 1);             return node.getYOrigin() + node.getPreferredHeight();        }        return 0;    }     /**     * Returns the preferred width and height for the region in     * <code>visibleRegion</code>.     *     * @param bounds  the region being queried     */    public int getPreferredWidth(Rectangle bounds) {        if(updateNodeSizes)            updateNodeSizes(false);         return getMaxNodeWidth();    }     /**      * Returns the path to the node that is closest to x,y.  If      * there is nothing currently visible this will return <code>null</code>,      * otherwise it will 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.      *      * @param x  the x-coordinate      * @param y  the y-coordinate      * @return the path to the node that is closest to x, y      */    public TreePath getPathClosestTo(int x, int y) {        if(getRowCount() == 0)            return null;         if(updateNodeSizes)            updateNodeSizes(false);         int                row = getRowContainingYLocation(y);         return getNode(row).getTreePath();    }     /**     * Returns an <code>Enumerator</code> 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.     *     * @param path the location in the <code>TreePath</code> to start     * @return an <code>Enumerator</code> that increments over the visible     *     paths     */    public Enumeration<TreePath> getVisiblePathsFrom(TreePath path) {        TreeStateNode       node = getNodeForPath(path, true, false);         if(node != null) {            return new VisibleTreeStateNodeEnumeration(node);        }        return null;    }     /**     * Returns the number of visible children for <code>path</code>.     * @return the number of visible children for <code>path</code>     */    public int getVisibleChildCount(TreePath path) {        TreeStateNode         node = getNodeForPath(path, true, false);         return (node != null) ? node.getVisibleChildCount() : 0;    }     /**     * Informs the <code>TreeState</code> that it needs to recalculate     * all the sizes it is referencing.     */    public void invalidateSizes() {        if(root != null)            root.deepMarkSizeInvalid();        if(!isFixedRowHeight() && visibleNodes.size() > 0) {            updateNodeSizes(true);        }    }     /**      * Returns true if the value identified by <code>path</code> is      * currently expanded.      * @return true if the value identified by <code>path</code> is      *    currently expanded      */    public boolean isExpanded(TreePath path) {        if(path != null) {            TreeStateNode     lastNode = getNodeForPath(path, true, false);             return (lastNode != null && lastNode.isExpanded());        }        return false;    }     //    // TreeModelListener methods    //     /**     * 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.     *     * <p><code>e.path</code> returns the path the parent of the     * changed node(s).     *     * <p><code>e.childIndices</code> returns the index(es) of the     * changed node(s).     *     * @param e the <code>TreeModelEvent</code> of interest     */    public void treeNodesChanged(TreeModelEvent e) {        if(e != null) {            int               changedIndexs[];            TreeStateNode     changedNode;             changedIndexs = e.getChildIndices();            changedNode = getNodeForPath(e.getTreePath(), false, false);            if(changedNode != null) {                Object            changedValue = changedNode.getValue();                 /* Update the size of the changed node, as well as all the                   child indexs that are passed in. */                changedNode.updatePreferredSize();                if(changedNode.hasBeenExpanded() && changedIndexs != null) {                    int                counter;                    TreeStateNode      changedChildNode;                     for(counter = 0; counter < changedIndexs.length;                        counter++) {                        changedChildNode = (TreeStateNode)changedNode                                    .getChildAt(changedIndexs[counter]);                        /* Reset the user object. */                        changedChildNode.setUserObject                                    (treeModel.getChild(changedValue,                                                     changedIndexs[counter]));                        changedChildNode.updatePreferredSize();                    }                }                else if (changedNode == root) {                    // Null indicies for root indicates it changed.                    changedNode.updatePreferredSize();                }                if(!isFixedRowHeight()) {                    int          aRow = changedNode.getRow();                     if(aRow != -1)                        this.updateYLocationsFrom(aRow);                }                this.visibleNodesChanged();            }        }    }      /**     * Invoked after nodes have been inserted into the tree.     *     * <p><code>e.path</code> returns the parent of the new nodes.     * <p><code>e.childIndices</code> returns the indices of the new nodes in     * ascending order.     *     * @param e the <code>TreeModelEvent</code> of interest     */    public void treeNodesInserted(TreeModelEvent e) {        if(e != null) {            int               changedIndexs[];            TreeStateNode     changedParentNode;             changedIndexs = e.getChildIndices();            changedParentNode = getNodeForPath(e.getTreePath(), false, false);            /* Only need to update the children if the node has been               expanded once. */            // PENDING(scott): make sure childIndexs is sorted!            if(changedParentNode != null && changedIndexs != null &&               changedIndexs.length > 0) {                if(changedParentNode.hasBeenExpanded()) {                    boolean            makeVisible;                    int                counter;                    Object             changedParent;                    TreeStateNode      newNode;                    int                oldChildCount = changedParentNode.                                          getChildCount();                     changedParent = changedParentNode.getValue();                    makeVisible = ((changedParentNode == root &&                                    !rootVisible) ||                                   (changedParentNode.getRow() != -1 &&                                    changedParentNode.isExpanded()));                    for(counter = 0;counter < changedIndexs.length;counter++)                    {                        newNode = this.createNodeAt(changedParentNode,                                                    changedIndexs[counter]);                    }                    if(oldChildCount == 0) {                        // Update the size of the parent.                        changedParentNode.updatePreferredSize();                    }                    if(treeSelectionModel != null)                        treeSelectionModel.resetRowSelection();                    /* Update the y origins from the index of the parent                       to the end of the visible rows. */

Lines 1–500 of 1767.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t