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