jdk/src/java.desktop/share/classes/javax/swing/tree/VariableHeightLayoutCache.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 25568 jdk/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java@b906a74c6882
child 29894 3e16b51732f5
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.event.TreeModelEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
    36
import sun.swing.SwingUtilities2;
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
    37
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * NOTE: This will become more open in a future release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 18133
diff changeset
    46
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Rob Davis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Ray Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
    54
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class VariableHeightLayoutCache extends AbstractLayoutCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * The array of nodes that are currently visible, in the order they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * are displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    60
    private Vector<Object> visibleNodes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * This is set to true if one of the entries has an invalid size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean           updateNodeSizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The root node of the internal cache of nodes that have been shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * If the treeModel is vending a network rather than a true tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * there may be one cached node for each path to a modeled node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private TreeStateNode     root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Used in getting sizes for nodes to avoid creating a new Rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * every time a size is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Rectangle         boundsBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Maps from <code>TreePath</code> to a <code>TreeStateNode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    83
    private Hashtable<TreePath, TreeStateNode> treePathMapping;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * A stack of stacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    88
    private Stack<Stack<TreePath>> tempStacks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public VariableHeightLayoutCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        super();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    93
        tempStacks = new Stack<Stack<TreePath>>();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    94
        visibleNodes = new Vector<Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        boundsBuffer = new Rectangle();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    96
        treePathMapping = new Hashtable<TreePath, TreeStateNode>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Sets the <code>TreeModel</code> that will provide the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param newModel the <code>TreeModel</code> that is to provide the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *  description: The TreeModel that will provide the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void setModel(TreeModel newModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        super.setModel(newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        rebuild(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Determines whether or not the root node from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * the <code>TreeModel</code> is visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param rootVisible true if the root node of the tree is to be displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @see #rootVisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *  description: Whether or not the root node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *               from the TreeModel is visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void setRootVisible(boolean rootVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if(isRootVisible() != rootVisible && root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if(rootVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                root.updatePreferredSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                visibleNodes.insertElementAt(root, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            else if(visibleNodes.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                visibleNodes.removeElementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if(treeSelectionModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    treeSelectionModel.removeSelectionPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        (root.getTreePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if(treeSelectionModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if(getRowCount() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                getNode(0).setYOrigin(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            updateYLocationsFrom(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        super.setRootVisible(rootVisible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Sets the height of each cell.  If the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * is less than or equal to zero the current cell renderer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * queried for each row's height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param rowHeight the height of each cell, in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *  description: The height of each cell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void setRowHeight(int rowHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if(rowHeight != getRowHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            super.setRowHeight(rowHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            invalidateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Sets the renderer that is responsible for drawing nodes in the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param nd the renderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void setNodeDimensions(NodeDimensions nd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        super.setNodeDimensions(nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        invalidateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Marks the path <code>path</code> expanded state to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <code>isExpanded</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param path the <code>TreePath</code> of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param isExpanded true if the path should be expanded, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public void setExpandedState(TreePath path, boolean isExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if(isExpanded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                ensurePathIsExpanded(path, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                TreeStateNode        node = getNodeForPath(path, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    node.makeVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    node.collapse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns true if the path is expanded, and visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return true if the path is expanded and visible, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public boolean getExpandedState(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        TreeStateNode       node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return (node != null) ? (node.isVisible() && node.isExpanded()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                 false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
      * Returns the <code>Rectangle</code> enclosing the label portion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
      * into which the item identified by <code>path</code> will be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
      * @param path  the path to be drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
      * @param placeIn the bounds of the enclosing rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
      * @return the bounds of the enclosing rectangle or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
      *    if the node could not be ascertained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public Rectangle getBounds(TreePath path, Rectangle placeIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        TreeStateNode       node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if(updateNodeSizes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                updateNodeSizes(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return node.getNodeBounds(placeIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      * Returns the path for <code>row</code>.  If <code>row</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
      * is not visible, <code>null</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
      * @param row the location of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
      * @return the path for <code>row</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
      * if <code>row</code> is not visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public TreePath getPathForRow(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if(row >= 0 && row < getRowCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return getNode(row).getTreePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
      * Returns the row where the last item identified in path is visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      * Will return -1 if any of the elements in path are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
      * currently visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
      * @param path the <code>TreePath</code> of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      * @return the row where the last item in path is visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public int getRowForPath(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if(path == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        TreeStateNode    visNode = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if(visNode != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return visNode.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Returns the number of visible rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return the number of visible rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public int getRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return visibleNodes.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Instructs the <code>LayoutCache</code> that the bounds for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <code>path</code> are invalid, and need to be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param path the <code>TreePath</code> which is now invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public void invalidatePathBounds(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        TreeStateNode       node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            node.markSizeInvalid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if(node.isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                updateYLocationsFrom(node.getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Returns the preferred height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return the preferred height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public int getPreferredHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // Get the height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int           rowCount = getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if(rowCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            TreeStateNode  node = getNode(rowCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return node.getYOrigin() + node.getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Returns the preferred width and height for the region in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <code>visibleRegion</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param bounds  the region being queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public int getPreferredWidth(Rectangle bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if(updateNodeSizes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            updateNodeSizes(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return getMaxNodeWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
      * Returns the path to the node that is closest to x,y.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
      * there is nothing currently visible this will return <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
      * otherwise it will always return a valid path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
      * If you need to test if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
      * returned object is exactly at x, y you should get the bounds for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
      * the returned path and test x, y against that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
      * @param x  the x-coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
      * @param y  the y-coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
      * @return the path to the node that is closest to x, y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public TreePath getPathClosestTo(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if(getRowCount() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if(updateNodeSizes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            updateNodeSizes(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int                row = getRowContainingYLocation(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return getNode(row).getTreePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Returns an <code>Enumerator</code> that increments over the visible paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * starting at the passed in location. The ordering of the enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * is based on how the paths are displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param path the location in the <code>TreePath</code> to start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return an <code>Enumerator</code> that increments over the visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *     paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public Enumeration<TreePath> getVisiblePathsFrom(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        TreeStateNode       node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return new VisibleTreeStateNodeEnumeration(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Returns the number of visible children for <code>path</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return the number of visible children for <code>path</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public int getVisibleChildCount(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        TreeStateNode         node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return (node != null) ? node.getVisibleChildCount() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Informs the <code>TreeState</code> that it needs to recalculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * all the sizes it is referencing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public void invalidateSizes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if(root != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            root.deepMarkSizeInvalid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if(!isFixedRowHeight() && visibleNodes.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            updateNodeSizes(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
      * Returns true if the value identified by <code>path</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
      * currently expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
      * @return true if the value identified by <code>path</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
      *    currently expanded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public boolean isExpanded(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            TreeStateNode     lastNode = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return (lastNode != null && lastNode.isExpanded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    // TreeModelListener methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Invoked after a node (or a set of siblings) has changed in some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * way. The node(s) have not changed locations in the tree or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * altered their children arrays, but other attributes have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * changed and may affect presentation. Example: the name of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * file has changed, but it is in the same location in the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <p><code>e.path</code> returns the path the parent of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * changed node(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p><code>e.childIndices</code> returns the index(es) of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * changed node(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param e the <code>TreeModelEvent</code> of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public void treeNodesChanged(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if(e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            int               changedIndexs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            TreeStateNode     changedNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            changedIndexs = e.getChildIndices();
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
   417
            changedNode = getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if(changedNode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                Object            changedValue = changedNode.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                /* Update the size of the changed node, as well as all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                   child indexs that are passed in. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                changedNode.updatePreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                if(changedNode.hasBeenExpanded() && changedIndexs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    int                counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    TreeStateNode      changedChildNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    for(counter = 0; counter < changedIndexs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        changedChildNode = (TreeStateNode)changedNode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                    .getChildAt(changedIndexs[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        /* Reset the user object. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        changedChildNode.setUserObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                    (treeModel.getChild(changedValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                                     changedIndexs[counter]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        changedChildNode.updatePreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                else if (changedNode == root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    // Null indicies for root indicates it changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    changedNode.updatePreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                if(!isFixedRowHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    int          aRow = changedNode.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    if(aRow != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        this.updateYLocationsFrom(aRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Invoked after nodes have been inserted into the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * <p><code>e.path</code> returns the parent of the new nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * <p><code>e.childIndices</code> returns the indices of the new nodes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param e the <code>TreeModelEvent</code> of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public void treeNodesInserted(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if(e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            int               changedIndexs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            TreeStateNode     changedParentNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            changedIndexs = e.getChildIndices();
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
   470
            changedParentNode = getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            /* Only need to update the children if the node has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
               expanded once. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            // PENDING(scott): make sure childIndexs is sorted!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if(changedParentNode != null && changedIndexs != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
               changedIndexs.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                if(changedParentNode.hasBeenExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    boolean            makeVisible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    int                counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    Object             changedParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    TreeStateNode      newNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    int                oldChildCount = changedParentNode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                          getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    changedParent = changedParentNode.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    makeVisible = ((changedParentNode == root &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                    !rootVisible) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                   (changedParentNode.getRow() != -1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                                    changedParentNode.isExpanded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    for(counter = 0;counter < changedIndexs.length;counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        newNode = this.createNodeAt(changedParentNode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                                    changedIndexs[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    if(oldChildCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        // Update the size of the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        changedParentNode.updatePreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    if(treeSelectionModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    /* Update the y origins from the index of the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                       to the end of the visible rows. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    if(!isFixedRowHeight() && (makeVisible ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                                               (oldChildCount == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                                        changedParentNode.isVisible()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        if(changedParentNode == root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            this.updateYLocationsFrom(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                            this.updateYLocationsFrom(changedParentNode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                                      getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                        this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    else if(makeVisible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                else if(treeModel.getChildCount(changedParentNode.getValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                        - changedIndexs.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    changedParentNode.updatePreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    if(!isFixedRowHeight() && changedParentNode.isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        updateYLocationsFrom(changedParentNode.getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Invoked after nodes have been removed from the tree.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * if a subtree is removed from the tree, this method may only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * invoked once for the root of the removed subtree, not once for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * each individual set of siblings removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <p><code>e.path</code> returns the former parent of the deleted nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * <p><code>e.childIndices</code> returns the indices the nodes had
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * before they were deleted in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param e the <code>TreeModelEvent</code> of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public void treeNodesRemoved(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if(e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            int               changedIndexs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            TreeStateNode     changedParentNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            changedIndexs = e.getChildIndices();
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
   544
            changedParentNode = getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            // PENDING(scott): make sure that changedIndexs are sorted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            // ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if(changedParentNode != null && changedIndexs != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
               changedIndexs.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if(changedParentNode.hasBeenExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    boolean            makeInvisible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    int                counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    int                removedRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    TreeStateNode      removedNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    makeInvisible = ((changedParentNode == root &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                                      !rootVisible) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                     (changedParentNode.getRow() != -1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                      changedParentNode.isExpanded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    for(counter = changedIndexs.length - 1;counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        removedNode = (TreeStateNode)changedParentNode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                getChildAt(changedIndexs[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        if(removedNode.isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                            removedNode.collapse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        /* Let the selection model now. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        if(makeInvisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                            removedRow = removedNode.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                            if(removedRow != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                                visibleNodes.removeElementAt(removedRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        changedParentNode.remove(changedIndexs[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    if(changedParentNode.getChildCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        // Update the size of the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        changedParentNode.updatePreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                        if (changedParentNode.isExpanded() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                   changedParentNode.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            // Node has become a leaf, collapse it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                            changedParentNode.collapse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    if(treeSelectionModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    /* Update the y origins from the index of the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                       to the end of the visible rows. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    if(!isFixedRowHeight() && (makeInvisible ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                               (changedParentNode.getChildCount() == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                changedParentNode.isVisible()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        if(changedParentNode == root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                            /* It is possible for first row to have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                               removed if the root isn't visible, in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                               case ylocations will be off! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                            if(getRowCount() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                getNode(0).setYOrigin(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            updateYLocationsFrom(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                            updateYLocationsFrom(changedParentNode.getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                        this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    else if(makeInvisible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                else if(treeModel.getChildCount(changedParentNode.getValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    changedParentNode.updatePreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    if(!isFixedRowHeight() && changedParentNode.isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        this.updateYLocationsFrom(changedParentNode.getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Invoked after the tree has drastically changed structure from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * given node down.  If the path returned by <code>e.getPath</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * is of length one and the first element does not identify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * current root node the first element should become the new root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * of the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <p><code>e.path</code> holds the path to the node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <p><code>e.childIndices</code> returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @param e the <code>TreeModelEvent</code> of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public void treeStructureChanged(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if(e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        {
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
   632
            TreePath          changedPath = SwingUtilities2.getTreePath(e, getModel());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            TreeStateNode     changedNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            changedNode = getNodeForPath(changedPath, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            // Check if root has changed, either to a null root, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            // to an entirely new root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            if(changedNode == root ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
               (changedNode == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                ((changedPath == null && treeModel != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                  treeModel.getRoot() == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                 (changedPath != null && changedPath.getPathCount() == 1)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                rebuild(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            else if(changedNode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                int                              nodeIndex, oldRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                TreeStateNode                    newNode, parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                boolean                          wasExpanded, wasVisible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                int                              newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                wasExpanded = changedNode.isExpanded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                wasVisible = (changedNode.getRow() != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                /* Remove the current node and recreate a new one. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                parent = (TreeStateNode)changedNode.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                nodeIndex = parent.getIndex(changedNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                if(wasVisible && wasExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    changedNode.collapse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                if(wasVisible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    visibleNodes.removeElement(changedNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                changedNode.removeFromParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                createNodeAt(parent, nodeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                newNode = (TreeStateNode)parent.getChildAt(nodeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                if(wasVisible && wasExpanded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    newNode.expand(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                newIndex = newNode.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                if(!isFixedRowHeight() && wasVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    if(newIndex == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        updateYLocationsFrom(newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        updateYLocationsFrom(newIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                else if(wasVisible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    // Local methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    private void visibleNodesChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * Adds a mapping for node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    private void addMapping(TreeStateNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        treePathMapping.put(node.getTreePath(), node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Removes the mapping for a previously added node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    private void removeMapping(TreeStateNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        treePathMapping.remove(node.getTreePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Returns the node previously added for <code>path</code>. This may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * return null, if you to create a node use getNodeForPath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    private TreeStateNode getMapping(TreePath path) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   708
        return treePathMapping.get(path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Retursn the bounds for row, <code>row</code> by reference in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <code>placeIn</code>. If <code>placeIn</code> is null a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Rectangle will be created and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    private Rectangle getBounds(int row, Rectangle placeIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if(updateNodeSizes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            updateNodeSizes(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if(row >= 0 && row < getRowCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            return getNode(row).getNodeBounds(placeIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * Completely rebuild the tree, all expanded state, and node caches are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * removed. All nodes are collapsed, except the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    private void rebuild(boolean clearSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        Object rootObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        treePathMapping.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        if(treeModel != null && (rootObject = treeModel.getRoot()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            root = createNodeForValue(rootObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            root.path = new TreePath(rootObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            addMapping(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            root.updatePreferredSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            visibleNodes.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            if (isRootVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                visibleNodes.addElement(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            if(!root.isExpanded())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                root.expand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            else {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 22574
diff changeset
   745
                Enumeration<?> cursor = root.children();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                while(cursor.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    visibleNodes.addElement(cursor.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                if(!isFixedRowHeight())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    updateYLocationsFrom(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            visibleNodes.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if(clearSelection && treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            treeSelectionModel.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
      * Creates a new node to represent the node at <I>childIndex</I> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
      * <I>parent</I>s children.  This should be called if the node doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
      * already exist and <I>parent</I> has been expanded at least once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
      * The newly created node will be made visible if <I>parent</I> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
      * currently expanded.  This does not update the position of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
      * cells, nor update the selection if it needs to be.  If succesful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
      * in creating the new TreeStateNode, it is returned, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
      * null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    private TreeStateNode createNodeAt(TreeStateNode parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                                         int childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        boolean                isParentRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        Object                 newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        TreeStateNode          newChildNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        newValue = treeModel.getChild(parent.getValue(), childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        newChildNode = createNodeForValue(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        parent.insert(newChildNode, childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        newChildNode.updatePreferredSize(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        isParentRoot = (parent == root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        if(newChildNode != null && parent.isExpanded() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
           (parent.getRow() != -1 || isParentRoot)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            int                 newRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            /* Find the new row to insert this newly visible node at. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if(childIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                if(isParentRoot && !isRootVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    newRow = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    newRow = parent.getRow() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            else if(childIndex == parent.getChildCount())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                newRow = parent.getLastVisibleNode().getRow() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                TreeStateNode          previousNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                previousNode = (TreeStateNode)parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    getChildAt(childIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                newRow = previousNode.getLastVisibleNode().getRow() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            visibleNodes.insertElementAt(newChildNode, newRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        return newChildNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
      * Returns the TreeStateNode identified by path.  This mirrors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
      * the behavior of getNodeForPath, but tries to take advantage of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
      * path if it is an instance of AbstractTreePath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    private TreeStateNode getNodeForPath(TreePath path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                                           boolean onlyIfVisible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                                           boolean shouldCreate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            TreeStateNode      node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            node = getMapping(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                if(onlyIfVisible && !node.isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            // Check all the parent paths, until a match is found.
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   828
            Stack<TreePath> paths;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            if(tempStacks.size() == 0) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   831
                paths = new Stack<TreePath>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            else {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   834
                paths = tempStacks.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                paths.push(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                path = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                node = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                while(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    node = getMapping(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        // Found a match, create entries for all paths in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                        // paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        while(node != null && paths.size() > 0) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   847
                            path = paths.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                            node.getLoadedChildren(shouldCreate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                            int            childIndex = treeModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                      getIndexOfChild(node.getUserObject(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                                                  path.getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                            if(childIndex == -1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                               childIndex >= node.getChildCount() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                               (onlyIfVisible && !node.isVisible())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                                node = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                                node = (TreeStateNode)node.getChildAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                                               (childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    paths.push(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    path = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                paths.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                tempStacks.push(paths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            // If we get here it means they share a different root!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            // We could throw an exception...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
      * Updates the y locations of all of the visible nodes after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
      * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    private void updateYLocationsFrom(int location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if(location >= 0 && location < getRowCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            int                    counter, maxCounter, newYOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            TreeStateNode          aNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            aNode = getNode(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            newYOrigin = aNode.getYOrigin() + aNode.getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            for(counter = location + 1, maxCounter = visibleNodes.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                counter < maxCounter;counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                aNode = (TreeStateNode)visibleNodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                aNode.setYOrigin(newYOrigin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                newYOrigin += aNode.getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
      * Resets the y origin of all the visible nodes as well as messaging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
      * all the visible nodes to updatePreferredSize().  You should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
      * normally have to call this.  Expanding and contracting the nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
      * automaticly adjusts the locations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
      * updateAll determines if updatePreferredSize() is call on all nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
      * or just those that don't have a valid size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    private void updateNodeSizes(boolean updateAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        int                      aY, counter, maxCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        TreeStateNode            node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        updateNodeSizes = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        for(aY = counter = 0, maxCounter = visibleNodes.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            node = (TreeStateNode)visibleNodes.elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            node.setYOrigin(aY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            if(updateAll || !node.hasValidSize())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                node.updatePreferredSize(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            aY += node.getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
      * Returns the index of the row containing location.  If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
      * are no rows, -1 is returned.  If location is beyond the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
      * row index, the last row index is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    private int getRowContainingYLocation(int location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        if(isFixedRowHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            if(getRowCount() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            return Math.max(0, Math.min(getRowCount() - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                                        location / getRowHeight()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        int                    max, maxY, mid, min, minY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        TreeStateNode          node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        if((max = getRowCount()) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        mid = min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        while(min < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            mid = (max - min) / 2 + min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            node = (TreeStateNode)visibleNodes.elementAt(mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            minY = node.getYOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            maxY = minY + node.getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            if(location < minY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                max = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            else if(location >= maxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                min = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        if(min == max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            mid = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            if(mid >= getRowCount())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                mid = getRowCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        return mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * Ensures that all the path components in path are expanded, accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * for the last component which will only be expanded if expandLast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Returns true if succesful in finding the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    private void ensurePathIsExpanded(TreePath aPath, boolean expandLast) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if(aPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            // Make sure the last entry isn't a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            if(treeModel.isLeaf(aPath.getLastPathComponent())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                aPath = aPath.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                expandLast = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if(aPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                TreeStateNode     lastNode = getNodeForPath(aPath, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                                                            true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                if(lastNode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                    lastNode.makeVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    if(expandLast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                        lastNode.expand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Returns the AbstractTreeUI.VisibleNode displayed at the given row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    private TreeStateNode getNode(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        return (TreeStateNode)visibleNodes.elementAt(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
      * Returns the maximum node width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    private int getMaxNodeWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        int                     maxWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        int                     nodeWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        int                     counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        TreeStateNode           node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        for(counter = getRowCount() - 1;counter >= 0;counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            node = this.getNode(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            nodeWidth = node.getPreferredWidth() + node.getXOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            if(nodeWidth > maxWidth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                maxWidth = nodeWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        return maxWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
      * Responsible for creating a TreeStateNode that will be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
      * to track display information about value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    private TreeStateNode createNodeForValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        return new TreeStateNode(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * TreeStateNode is used to keep track of each of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * the nodes that have been expanded. This will also cache the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * size of the value it represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    private class TreeStateNode extends DefaultMutableTreeNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        /** Preferred size needed to draw the user object. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        protected int             preferredWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        protected int             preferredHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        /** X location that the user object will be drawn at. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        protected int             xOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        /** Y location that the user object will be drawn at. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        protected int             yOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        /** Is this node currently expanded? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        protected boolean         expanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        /** Has this node been expanded at least once? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        protected boolean         hasBeenExpanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        /** Path of this node. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        protected TreePath        path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        public TreeStateNode(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            super(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        // Overriden DefaultMutableTreeNode methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
         * Messaged when this node is added somewhere, resets the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
         * and adds a mapping from path to this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        public void setParent(MutableTreeNode parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            super.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            if(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                path = ((TreeStateNode)parent).getTreePath().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                                       pathByAddingChild(getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                addMapping(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
         * Messaged when this node is removed from its parent, this messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         * <code>removedFromMapping</code> to remove all the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        public void remove(int childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            TreeStateNode     node = (TreeStateNode)getChildAt(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            node.removeFromMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            super.remove(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
         * Messaged to set the user object. This resets the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        public void setUserObject(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            super.setUserObject(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                TreeStateNode      parent = (TreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                if(parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                    resetChildrenPaths(parent.getTreePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    resetChildrenPaths(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
         * Returns the children of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
         * If the receiver is not currently expanded, this will return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
         * empty enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
         */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 22574
diff changeset
  1102
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 22574
diff changeset
  1103
        public Enumeration<TreeNode> children() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            if (!this.isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                return DefaultMutableTreeNode.EMPTY_ENUMERATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                return super.children();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
         * Returns true if the receiver is a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        public boolean isLeaf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            return getModel().isLeaf(this.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        // VariableHeightLayoutCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
         * Returns the location and size of this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        public Rectangle getNodeBounds(Rectangle placeIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            if(placeIn == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                placeIn = new Rectangle(getXOrigin(), getYOrigin(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                                        getPreferredWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                                        getPreferredHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                placeIn.x = getXOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                placeIn.y = getYOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                placeIn.width = getPreferredWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                placeIn.height = getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            return placeIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
         * @return x location to draw node at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        public int getXOrigin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            if(!hasValidSize())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                updatePreferredSize(getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            return xOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
         * Returns the y origin the user object will be drawn at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        public int getYOrigin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            if(isFixedRowHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                int      aRow = getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                if(aRow == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                return getRowHeight() * aRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            return yOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
         * Returns the preferred height of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        public int getPreferredHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            if(isFixedRowHeight())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                return getRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            else if(!hasValidSize())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                updatePreferredSize(getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            return preferredHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         * Returns the preferred width of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        public int getPreferredWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            if(!hasValidSize())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                updatePreferredSize(getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            return preferredWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
         * Returns true if this node has a valid size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        public boolean hasValidSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            return (preferredHeight != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
         * Returns the row of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        public int getRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            return visibleNodes.indexOf(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
         * Returns true if this node has been expanded at least once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        public boolean hasBeenExpanded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            return hasBeenExpanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
         * Returns true if the receiver has been expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        public boolean isExpanded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            return expanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
         * Returns the last visible node that is a child of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
         * instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        public TreeStateNode getLastVisibleNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            TreeStateNode                node = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            while(node.isExpanded() && node.getChildCount() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                node = (TreeStateNode)node.getLastChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
         * Returns true if the receiver is currently visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        public boolean isVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            if(this == root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            TreeStateNode        parent = (TreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            return (parent != null && parent.isExpanded() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                    parent.isVisible());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
         * Returns the number of children this will have. If the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
         * have not yet been loaded, this messages the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        public int getModelChildCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if(hasBeenExpanded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                return super.getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            return getModel().getChildCount(getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
         * Returns the number of visible children, that is the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
         * children that are expanded, or leafs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        public int getVisibleChildCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            int               childCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            if(isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                int         maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                childCount += maxCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                for(int counter = 0; counter < maxCounter; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                    childCount += ((TreeStateNode)getChildAt(counter)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                                    getVisibleChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            return childCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
         * Toggles the receiver between expanded and collapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        public void toggleExpanded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            if (isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                collapse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                expand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
         * Makes the receiver visible, but invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
         * <code>expandParentAndReceiver</code> on the superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        public void makeVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            TreeStateNode       parent = (TreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            if(parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                parent.expandParentAndReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
         * Expands the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        public void expand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            expand(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
         * Collapses the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        public void collapse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            collapse(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
         * Returns the value the receiver is representing. This is a cover
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
         * for getUserObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            return getUserObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
         * Returns a TreePath instance for this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        public TreePath getTreePath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        // Local methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1319
         * Recreates the receivers path, and all its children's paths.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        protected void resetChildrenPaths(TreePath parentPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            removeMapping(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            if(parentPath == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                path = new TreePath(getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                path = parentPath.pathByAddingChild(getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            addMapping(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            for(int counter = getChildCount() - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                ((TreeStateNode)getChildAt(counter)).resetChildrenPaths(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
         * Sets y origin the user object will be drawn at to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
         * <I>newYOrigin</I>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        protected void setYOrigin(int newYOrigin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            yOrigin = newYOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
         * Shifts the y origin by <code>offset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        protected void shiftYOriginBy(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            yOrigin += offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
         * Updates the receivers preferredSize by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
         * <code>updatePreferredSize</code> with an argument of -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        protected void updatePreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            updatePreferredSize(getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
         * Updates the preferred size by asking the current renderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
         * for the Dimension needed to draw the user object this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
         * instance represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        protected void updatePreferredSize(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            Rectangle       bounds = getNodeDimensions(this.getUserObject(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                                                       index, getLevel(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                                                       isExpanded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                                                       boundsBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            if(bounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                xOrigin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                preferredWidth = preferredHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                updateNodeSizes = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            else if(bounds.height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                xOrigin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                preferredWidth = preferredHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                updateNodeSizes = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                xOrigin = bounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                preferredWidth = bounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                if(isFixedRowHeight())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    preferredHeight = getRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    preferredHeight = bounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
         * Marks the receivers size as invalid. Next time the size, location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
         * is asked for it will be obtained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        protected void markSizeInvalid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            preferredHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
         * Marks the receivers size, and all its descendants sizes, as invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        protected void deepMarkSizeInvalid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            markSizeInvalid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            for(int counter = getChildCount() - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                ((TreeStateNode)getChildAt(counter)).deepMarkSizeInvalid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
         * Returns the children of the receiver. If the children haven't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
         * been loaded from the model and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
         * <code>createIfNeeded</code> is true, the children are first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
         * loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
         */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 22574
diff changeset
  1409
        protected Enumeration<TreeNode> getLoadedChildren(boolean createIfNeeded) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            if(!createIfNeeded || hasBeenExpanded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                return super.children();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            TreeStateNode   newNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            Object          realNode = getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            TreeModel       treeModel = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            int             count = treeModel.getChildCount(realNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            hasBeenExpanded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            int    childRow = getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            if(childRow == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                    newNode = createNodeForValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                        (treeModel.getChild(realNode, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                    this.add(newNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                    newNode.updatePreferredSize(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                childRow++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                    newNode = createNodeForValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                        (treeModel.getChild(realNode, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                    this.add(newNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                    newNode.updatePreferredSize(childRow++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            return super.children();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
         * Messaged from expand and collapse. This is meant for subclassers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
         * that may wish to do something interesting with this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        protected void didAdjustTree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
         * Invokes <code>expandParentAndReceiver</code> on the parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
         * and expands the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        protected void expandParentAndReceiver() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            TreeStateNode       parent = (TreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            if(parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                parent.expandParentAndReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            expand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
         * Expands this node in the tree.  This will load the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
         * from the treeModel if this node has not previously been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
         * expanded.  If <I>adjustTree</I> is true the tree and selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
         * are updated accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        protected void expand(boolean adjustTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            if (!isExpanded() && !isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                boolean         isFixed = isFixedRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                int             startHeight = getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                int             originalRow = getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                expanded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                updatePreferredSize(originalRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                if (!hasBeenExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                    TreeStateNode  newNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                    Object         realNode = getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                    TreeModel      treeModel = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                    int            count = treeModel.getChildCount(realNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                    hasBeenExpanded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                    if(originalRow == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                            newNode = createNodeForValue(treeModel.getChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                                                            (realNode, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                            this.add(newNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                            newNode.updatePreferredSize(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                        int offset = originalRow + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                            newNode = createNodeForValue(treeModel.getChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                                                       (realNode, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                            this.add(newNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                            newNode.updatePreferredSize(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                int i = originalRow;
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 22574
diff changeset
  1503
                Enumeration<TreeNode> cursor = preorderEnumeration();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                cursor.nextElement(); // don't add me, I'm already in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                int newYOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                if(isFixed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                    newYOrigin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                else if(this == root && !isRootVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                    newYOrigin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                    newYOrigin = getYOrigin() + this.getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                TreeStateNode   aNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                if(!isFixed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                    while (cursor.hasMoreElements()) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 22574
diff changeset
  1517
                        aNode = (TreeStateNode) cursor.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                        if(!updateNodeSizes && !aNode.hasValidSize())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                            aNode.updatePreferredSize(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                        aNode.setYOrigin(newYOrigin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                        newYOrigin += aNode.getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                        visibleNodes.insertElementAt(aNode, ++i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                    while (cursor.hasMoreElements()) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 22574
diff changeset
  1527
                        aNode = (TreeStateNode) cursor.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                        visibleNodes.insertElementAt(aNode, ++i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                if(adjustTree && (originalRow != i ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                                  getPreferredHeight() != startHeight)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                    // Adjust the Y origin of any nodes following this row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                    if(!isFixed && ++i < getRowCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                        int              counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                        int              heightDiff = newYOrigin -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                            (getYOrigin() + getPreferredHeight()) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                            (getPreferredHeight() - startHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                        for(counter = visibleNodes.size() - 1;counter >= i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                            counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                            ((TreeStateNode)visibleNodes.elementAt(counter)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                                shiftYOriginBy(heightDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    didAdjustTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                    visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                // Update the rows in the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                if(treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
         * Collapses this node in the tree.  If <I>adjustTree</I> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
         * true the tree and selection are updated accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        protected void collapse(boolean adjustTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            if (isExpanded()) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 22574
diff changeset
  1563
                Enumeration<TreeNode> cursor = preorderEnumeration();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                cursor.nextElement(); // don't remove me, I'm still visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                int rowsDeleted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                boolean isFixed = isFixedRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                int lastYEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                if(isFixed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                    lastYEnd = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                    lastYEnd = getPreferredHeight() + getYOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                int startHeight = getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                int startYEnd = lastYEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                int myRow = getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                if(!isFixed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                    while(cursor.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                        TreeStateNode node = (TreeStateNode)cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                            nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                        if (node.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                            rowsDeleted++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                            //visibleNodes.removeElement(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                            lastYEnd = node.getYOrigin() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                                node.getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                    while(cursor.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                        TreeStateNode node = (TreeStateNode)cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                            nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                        if (node.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                            rowsDeleted++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                            //visibleNodes.removeElement(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                // Clean up the visible nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                for (int counter = rowsDeleted + myRow; counter > myRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                     counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                    visibleNodes.removeElementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                expanded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                if(myRow == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                    markSizeInvalid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                else if (adjustTree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                    updatePreferredSize(myRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                if(myRow != -1 && adjustTree &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                   (rowsDeleted > 0 || startHeight != getPreferredHeight())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                    // Adjust the Y origin of any rows following this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                    startYEnd += (getPreferredHeight() - startHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                    if(!isFixed && (myRow + 1) < getRowCount() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                       startYEnd != lastYEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                        int                 counter, maxCounter, shiftAmount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                        shiftAmount = startYEnd - lastYEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                        for(counter = myRow + 1, maxCounter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                                visibleNodes.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                            counter < maxCounter;counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                            ((TreeStateNode)visibleNodes.elementAt(counter))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                                .shiftYOriginBy(shiftAmount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                    didAdjustTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                    visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                if(treeSelectionModel != null && rowsDeleted > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                   myRow != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                    treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
         * Removes the receiver, and all its children, from the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
         * table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        protected void removeFromMapping() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                removeMapping(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                for(int counter = getChildCount() - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                    ((TreeStateNode)getChildAt(counter)).removeFromMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    } // End of VariableHeightLayoutCache.TreeStateNode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * An enumerator to iterate through visible nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    private class VisibleTreeStateNodeEnumeration implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                     Enumeration<TreePath> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        /** Parent thats children are being enumerated. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        protected TreeStateNode       parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        /** Index of next child. An index of -1 signifies parent should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
         * visibled next. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        protected int                 nextIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        /** Number of children in parent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        protected int                 childCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        protected VisibleTreeStateNodeEnumeration(TreeStateNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            this(node, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        protected VisibleTreeStateNodeEnumeration(TreeStateNode parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                                                  int startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            this.nextIndex = startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            this.childCount = this.parent.getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
         * @return true if more visible nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            return (parent != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
         * @return next visible TreePath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        public TreePath nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            if(!hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                throw new NoSuchElementException("No more visible paths");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            TreePath                retObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            if(nextIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                retObject = parent.getTreePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                TreeStateNode   node = (TreeStateNode)parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                                        getChildAt(nextIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                retObject = node.getTreePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            updateNextObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            return retObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
         * Determines the next object by invoking <code>updateNextIndex</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
         * and if not succesful <code>findNextValidParent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        protected void updateNextObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            if(!updateNextIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                findNextValidParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
         * Finds the next valid parent, this should be called when nextIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
         * is beyond the number of children of the current parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        protected boolean findNextValidParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            if(parent == root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                // mark as invalid!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            while(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                TreeStateNode      newParent = (TreeStateNode)parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                                                  getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                if(newParent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    nextIndex = newParent.getIndex(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                    parent = newParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                    childCount = parent.getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                    if(updateNextIndex())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                    parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
         * Updates <code>nextIndex</code> returning false if it is beyond
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
         * the number of children of parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        protected boolean updateNextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            // nextIndex == -1 identifies receiver, make sure is expanded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            // before descend.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            if(nextIndex == -1 && !parent.isExpanded())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            // Check that it can have kids
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            if(childCount == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            // Make sure next index not beyond child count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            else if(++nextIndex >= childCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            TreeStateNode       child = (TreeStateNode)parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                                        getChildAt(nextIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            if(child != null && child.isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                parent = child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                nextIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                childCount = child.getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    } // VariableHeightLayoutCache.VisibleTreeStateNodeEnumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
}