jdk/src/share/classes/javax/swing/tree/FixedHeightLayoutCache.java
author darcy
Thu, 03 Jul 2014 15:24:27 -0700
changeset 25568 b906a74c6882
parent 22574 7f8ce0c8c20a
permissions -rw-r--r--
8043550: Fix raw and unchecked lint warnings in javax.swing.* Reviewed-by: pchelko, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
     2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.event.TreeModelEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
    35
import sun.swing.SwingUtilities2;
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * NOTE: This will become more open in a future release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * 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
    45
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
    51
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class FixedHeightLayoutCache extends AbstractLayoutCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** Root node. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private FHTreeStateNode    root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Number of rows currently visible. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int                rowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Used in getting sizes for nodes to avoid creating a new Rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * every time a size is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Rectangle          boundsBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Maps from TreePath to a FHTreeStateNode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    68
    private Hashtable<TreePath, FHTreeStateNode> treePathMapping;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Used for getting path/row information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private SearchInfo         info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    75
    private Stack<Stack<TreePath>> tempStacks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public FixedHeightLayoutCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    80
        tempStacks = new Stack<Stack<TreePath>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        boundsBuffer = new Rectangle();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    82
        treePathMapping = new Hashtable<TreePath, FHTreeStateNode>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        info = new SearchInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        setRowHeight(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Sets the TreeModel that will provide the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param newModel the TreeModel that is to provide the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void setModel(TreeModel newModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        super.setModel(newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        rebuild(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Determines whether or not the root node from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * the TreeModel is visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param rootVisible true if the root node of the tree is to be displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @see #rootVisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void setRootVisible(boolean rootVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if(isRootVisible() != rootVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            super.setRootVisible(rootVisible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if(root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if(rootVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    rowCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    root.adjustRowBy(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    rowCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    root.adjustRowBy(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Sets the height of each cell. If rowHeight is less than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * 0 this will throw an IllegalArgumentException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param rowHeight the height of each cell, in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void setRowHeight(int rowHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if(rowHeight <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IllegalArgumentException("FixedHeightLayoutCache only supports row heights greater than 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if(getRowHeight() != rowHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            super.setRowHeight(rowHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Returns the number of visible rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public int getRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return rowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Does nothing, FixedHeightLayoutCache doesn't cache width, and that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * is all that could change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void invalidatePathBounds(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Informs the TreeState that it needs to recalculate all the sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * it is referencing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void invalidateSizes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // Nothing to do here, rowHeight still same, which is all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // this is interested in, visible region may have changed though.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        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
      * Returns true if the value identified by row is currently expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public boolean isExpanded(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            FHTreeStateNode     lastNode = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return (lastNode != null && lastNode.isExpanded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns a rectangle giving the bounds needed to draw path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param path     a TreePath specifying a node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param placeIn  a Rectangle object giving the available space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return a Rectangle object specifying the space to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public Rectangle getBounds(TreePath path, Rectangle placeIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if(path == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        FHTreeStateNode      node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if(node != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return getBounds(node, -1, placeIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // node hasn't been created yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        TreePath       parentPath = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        node = getNodeForPath(parentPath, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (node != null && node.isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            int              childIndex = treeModel.getIndexOfChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                 (parentPath.getLastPathComponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                  path.getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if(childIndex != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return getBounds(node, childIndex, placeIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return null;
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 path for passed in row.  If row is not visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
      * null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public TreePath getPathForRow(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if(row >= 0 && row < getRowCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if(root.getPathForRow(row, getRowCount(), info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                return info.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
      * Returns the row that the last item identified in path is visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
      * at.  Will return -1 if any of the elements in path are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
      * currently visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public int getRowForPath(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if(path == null || root == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        FHTreeStateNode         node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if(node != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return node.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        TreePath       parentPath = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        node = getNodeForPath(parentPath, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if(node != null && node.isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return node.getRowToModelIndex(treeModel.getIndexOfChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                           (parentPath.getLastPathComponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                            path.getLastPathComponent()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
      * Returns the path to the node that is closest to x,y.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
      * there is nothing currently visible this will return null, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
      * it'll always return a valid path.  If you need to test if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      * returned object is exactly at x, y you should get the bounds for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
      * the returned path and test x, y against that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public TreePath getPathClosestTo(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if(getRowCount() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        int                row = getRowContainingYLocation(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return getPathForRow(row);
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 children for row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public int getVisibleChildCount(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        FHTreeStateNode         node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if(node == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return node.getTotalChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns an Enumerator that increments over the visible paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * starting at the passed in location. The ordering of the enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * is based on how the paths are displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public Enumeration<TreePath> getVisiblePathsFrom(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if(path == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        FHTreeStateNode         node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return new VisibleFHTreeStateNodeEnumeration(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        TreePath            parentPath = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        node = getNodeForPath(parentPath, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if(node != null && node.isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return new VisibleFHTreeStateNodeEnumeration(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                  treeModel.getIndexOfChild(parentPath.getLastPathComponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                            path.getLastPathComponent()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Marks the path <code>path</code> expanded state to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <code>isExpanded</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public void setExpandedState(TreePath path, boolean isExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if(isExpanded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            ensurePathIsExpanded(path, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        else if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            TreePath              parentPath = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // YECK! Make the parent expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if(parentPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                FHTreeStateNode     parentNode = getNodeForPath(parentPath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                                                false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if(parentNode != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    parentNode.makeVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            // And collapse the child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            FHTreeStateNode         childNode = getNodeForPath(path, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                                               false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if(childNode != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                childNode.collapse(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Returns true if the path is expanded, and visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public boolean getExpandedState(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        FHTreeStateNode       node = getNodeForPath(path, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return (node != null) ? (node.isVisible() && node.isExpanded()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                 false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    // TreeModelListener methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <p>Invoked after a node (or a set of siblings) has changed in some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * way. The node(s) have not changed locations in the tree or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * altered their children arrays, but other attributes have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * changed and may affect presentation. Example: the name of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * file has changed, but it is in the same location in the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * system.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p>e.path() returns the path the parent of the changed node(s).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <p>e.childIndices() returns the index(es) of the changed node(s).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public void treeNodesChanged(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if(e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            int                 changedIndexs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            FHTreeStateNode     changedParent = getNodeForPath
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
   350
                                  (SwingUtilities2.getTreePath(e, getModel()), false, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            int                 maxCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            changedIndexs = e.getChildIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            /* Only need to update the children if the node has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
               expanded once. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            // PENDING(scott): make sure childIndexs is sorted!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (changedParent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                if (changedIndexs != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    (maxCounter = changedIndexs.length) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    Object       parentValue = changedParent.getUserObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    for(int counter = 0; counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        FHTreeStateNode    child = changedParent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                 getChildAtModelIndex(changedIndexs[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        if(child != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                            child.setUserObject(treeModel.getChild(parentValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                                     changedIndexs[counter]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    if(changedParent.isVisible() && changedParent.isExpanded())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                // Null for root indicates it changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                else if (changedParent == root && changedParent.isVisible() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                         changedParent.isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <p>Invoked after nodes have been inserted into the tree.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <p>e.path() returns the parent of the new nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>e.childIndices() returns the indices of the new nodes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public void treeNodesInserted(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if(e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            int                 changedIndexs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            FHTreeStateNode     changedParent = getNodeForPath
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
   394
                                  (SwingUtilities2.getTreePath(e, getModel()), false, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            int                 maxCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            changedIndexs = e.getChildIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            /* Only need to update the children if the node has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
               expanded once. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            // PENDING(scott): make sure childIndexs is sorted!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if(changedParent != null && changedIndexs != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
               (maxCounter = changedIndexs.length) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                boolean          isVisible =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    (changedParent.isVisible() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                     changedParent.isExpanded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                for(int counter = 0; counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    changedParent.childInsertedAtModelIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        (changedIndexs[counter], isVisible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                if(isVisible && treeSelectionModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                if(changedParent.isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p>Invoked after nodes have been removed from the tree.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * if a subtree is removed from the tree, this method may only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * invoked once for the root of the removed subtree, not once for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * each individual set of siblings removed.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <p>e.path() returns the former parent of the deleted nodes.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <p>e.childIndices() returns the indices the nodes had before they were deleted in ascending order.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public void treeNodesRemoved(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if(e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            int                  changedIndexs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            int                  maxCounter;
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
   433
            TreePath             parentPath = SwingUtilities2.getTreePath(e, getModel());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            FHTreeStateNode      changedParentNode = getNodeForPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                       (parentPath, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            changedIndexs = e.getChildIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            // PENDING(scott): make sure that changedIndexs are sorted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            // ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if(changedParentNode != null && changedIndexs != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
               (maxCounter = changedIndexs.length) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                Object[]           children = e.getChildren();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                boolean            isVisible =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    (changedParentNode.isVisible() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                     changedParentNode.isExpanded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                for(int counter = maxCounter - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    changedParentNode.removeChildAtModelIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                     (changedIndexs[counter], isVisible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if(isVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    if(treeSelectionModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    if (treeModel.getChildCount(changedParentNode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                                getUserObject()) == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                  changedParentNode.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        // Node has become a leaf, collapse it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        changedParentNode.collapse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                else if(changedParentNode.isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * <p>Invoked after the tree has drastically changed structure from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * given node down.  If the path returned by e.getPath() is of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * one and the first element does not identify the current root node
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
   472
     * the first element should become the new root of the tree.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>e.path() holds the path to the node.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <p>e.childIndices() returns null.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public void treeStructureChanged(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if(e != null) {
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 5506
diff changeset
   479
            TreePath          changedPath = SwingUtilities2.getTreePath(e, getModel());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            FHTreeStateNode   changedNode = getNodeForPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                                (changedPath, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            // Check if root has changed, either to a null root, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            // to an entirely new root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (changedNode == root ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                (changedNode == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                 ((changedPath == null && treeModel != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                   treeModel.getRoot() == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                  (changedPath != null && changedPath.getPathCount() <= 1)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                rebuild(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            else if(changedNode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                boolean             wasExpanded, wasVisible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                FHTreeStateNode     parent = (FHTreeStateNode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                              changedNode.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                wasExpanded = changedNode.isExpanded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                wasVisible = changedNode.isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                int index = parent.getIndex(changedNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                changedNode.collapse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                parent.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                if(wasVisible && wasExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    int row = changedNode.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    parent.resetChildrenRowsFrom(row, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                                 changedNode.getChildIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    changedNode = getNodeForPath(changedPath, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    changedNode.expand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                if(treeSelectionModel != null && wasVisible && wasExpanded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                if(wasVisible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    // Local methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    private void visibleNodesChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Returns the bounds for the given node. If <code>childIndex</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * is -1, the bounds of <code>parent</code> are returned, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * the bounds of the node at <code>childIndex</code> are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private Rectangle getBounds(FHTreeStateNode parent, int childIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                                  Rectangle placeIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        boolean              expanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        int                  level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int                  row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        Object               value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if(childIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            // Getting bounds for parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            row = parent.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            value = parent.getUserObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            expanded = parent.isExpanded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            level = parent.getLevel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            row = parent.getRowToModelIndex(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            value = treeModel.getChild(parent.getUserObject(), childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            expanded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            level = parent.getLevel() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        Rectangle      bounds = getNodeDimensions(value, row, level,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                                  expanded, boundsBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        // No node dimensions, bail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if(bounds == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if(placeIn == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            placeIn = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        placeIn.x = bounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        placeIn.height = getRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        placeIn.y = row * placeIn.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        placeIn.width = bounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        return placeIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * Adjust the large row count of the AbstractTreeUI the receiver was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * created with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    private void adjustRowCountBy(int changeAmount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        rowCount += changeAmount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Adds a mapping for node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private void addMapping(FHTreeStateNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        treePathMapping.put(node.getTreePath(), node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * Removes the mapping for a previously added node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    private void removeMapping(FHTreeStateNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        treePathMapping.remove(node.getTreePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Returns the node previously added for <code>path</code>. This may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * return null, if you to create a node use getNodeForPath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    private FHTreeStateNode getMapping(TreePath path) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   596
        return treePathMapping.get(path);
2
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
     * Sent to completely rebuild the visible tree. All nodes are collapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private void rebuild(boolean clearSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        Object            rootUO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        treePathMapping.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if(treeModel != null && (rootUO = treeModel.getRoot()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            root = createNodeForValue(rootUO, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            root.path = new TreePath(rootUO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            addMapping(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            if(isRootVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                rowCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                root.row = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                rowCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                root.row = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            root.expand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            rowCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if(clearSelection && treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            treeSelectionModel.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        this.visibleNodesChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
      * Returns the index of the row containing location.  If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
      * are no rows, -1 is returned.  If location is beyond the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
      * row index, the last row index is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    private int getRowContainingYLocation(int location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if(getRowCount() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return Math.max(0, Math.min(getRowCount() - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                                    location / getRowHeight()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * Ensures that all the path components in path are expanded, accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * for the last component which will only be expanded if expandLast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * Returns true if succesful in finding the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    private boolean ensurePathIsExpanded(TreePath aPath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                           boolean expandLast) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        if(aPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            // Make sure the last entry isn't a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            if(treeModel.isLeaf(aPath.getLastPathComponent())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                aPath = aPath.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                expandLast = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            if(aPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                FHTreeStateNode     lastNode = getNodeForPath(aPath, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                                              true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                if(lastNode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    lastNode.makeVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    if(expandLast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        lastNode.expand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    return true;
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
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * Creates and returns an instance of FHTreeStateNode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    private FHTreeStateNode createNodeForValue(Object value,int childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        return new FHTreeStateNode(value, childIndex, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * Messages getTreeNodeForPage(path, onlyIfVisible, shouldCreate,
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
   680
     * path.length) as long as path is non-null and the length is {@literal >} 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Otherwise returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    private FHTreeStateNode getNodeForPath(TreePath path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                             boolean onlyIfVisible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                             boolean shouldCreate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            FHTreeStateNode      node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            node = getMapping(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                if(onlyIfVisible && !node.isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            if(onlyIfVisible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            // 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
   699
            Stack<TreePath> paths;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            if(tempStacks.size() == 0) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   702
                paths = new Stack<TreePath>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            else {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   705
                paths = tempStacks.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                paths.push(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                path = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                node = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                while(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    node = getMapping(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        // Found a match, create entries for all paths in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        // paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                        while(node != null && paths.size() > 0) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   718
                            path = paths.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                            node = node.createChildFor(path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                                                       getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    paths.push(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    path = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                paths.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                tempStacks.push(paths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            // If we get here it means they share a different root!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * FHTreeStateNode is used to track what has been expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * FHTreeStateNode differs from VariableHeightTreeState.TreeStateNode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * in that it is highly model intensive. That is almost all queries to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * FHTreeStateNode result in the TreeModel being queried. And it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * obviously does not support variable sized row heights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    private class FHTreeStateNode extends DefaultMutableTreeNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        /** Is this node expanded? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        protected boolean         isExpanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        /** Index of this node from the model. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        protected int             childIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        /** Child count of the receiver. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        protected int             childCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        /** Row of the receiver. This is only valid if the row is expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        protected int             row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        /** Path of this node. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        protected TreePath        path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        public FHTreeStateNode(Object userObject, int childIndex, int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            super(userObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            this.childIndex = childIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            this.row = row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        // Overriden DefaultMutableTreeNode methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
         * Messaged when this node is added somewhere, resets the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         * and adds a mapping from path to this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        public void setParent(MutableTreeNode parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            super.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                path = ((FHTreeStateNode)parent).getTreePath().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                            pathByAddingChild(getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                addMapping(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         * Messaged when this node is removed from its parent, this messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         * <code>removedFromMapping</code> to remove all the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        public void remove(int childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            FHTreeStateNode     node = (FHTreeStateNode)getChildAt(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            node.removeFromMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            super.remove(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
         * Messaged to set the user object. This resets the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        public void setUserObject(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            super.setUserObject(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                FHTreeStateNode      parent = (FHTreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                if(parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    resetChildrenPaths(parent.getTreePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    resetChildrenPaths(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * Returns the index of the receiver in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        public int getChildIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            return childIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         * Returns the <code>TreePath</code> of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        public TreePath getTreePath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         * Returns the child for the passed in model index, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
         * return <code>null</code> if the child for <code>index</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
         * has not yet been created (expanded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        public FHTreeStateNode getChildAtModelIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            // PENDING: Make this a binary search!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            for(int counter = getChildCount() - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                if(((FHTreeStateNode)getChildAt(counter)).childIndex == index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    return (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
         * Returns true if this node is visible. This is determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
         * asking all the parents if they are expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        public boolean isVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            FHTreeStateNode         parent = (FHTreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if(parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            return (parent.isExpanded() && parent.isVisible());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         * Returns the row of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        public int getRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            return row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         * Returns the row of the child with a model index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        public int getRowToModelIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            FHTreeStateNode      child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            int                  lastRow = getRow() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            int                  retValue = lastRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            // This too could be a binary search!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            for(int counter = 0, maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                child = (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                if(child.childIndex >= index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    if(child.childIndex == index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        return child.row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    if(counter == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                        return getRow() + 1 + index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    return child.row - (child.childIndex - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            // YECK!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            return getRow() + 1 + getTotalChildCount() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                             (childCount - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
         * Returns the number of children in the receiver by descending all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
         * expanded nodes and messaging them with getTotalChildCount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        public int getTotalChildCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            if(isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                FHTreeStateNode      parent = (FHTreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                int                  pIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                if(parent != null && (pIndex = parent.getIndex(this)) + 1 <
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                   parent.getChildCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    // This node has a created sibling, to calc total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    // child count directly from that!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                    FHTreeStateNode  nextSibling = (FHTreeStateNode)parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                                           getChildAt(pIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    return nextSibling.row - row -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                           (nextSibling.childIndex - childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    int retCount = childCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    for(int counter = getChildCount() - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                        counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                        retCount += ((FHTreeStateNode)getChildAt(counter))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                                                  .getTotalChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    return retCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
         * Returns true if this node is expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        public boolean isExpanded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            return isExpanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * The highest visible nodes have a depth of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        public int getVisibleLevel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            if (isRootVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                return getLevel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                return getLevel() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   939
         * Recreates the receivers path, and all its children's paths.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        protected void resetChildrenPaths(TreePath parentPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            removeMapping(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if(parentPath == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                path = new TreePath(getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                path = parentPath.pathByAddingChild(getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            addMapping(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            for(int counter = getChildCount() - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                ((FHTreeStateNode)getChildAt(counter)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                               resetChildrenPaths(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
         * Removes the receiver, and all its children, from the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
         * table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        protected void removeFromMapping() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                removeMapping(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                for(int counter = getChildCount() - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    ((FHTreeStateNode)getChildAt(counter)).removeFromMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * Creates a new node to represent <code>userObject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * This does NOT check to ensure there isn't already a child node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         * to manage <code>userObject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        protected FHTreeStateNode createChildFor(Object userObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            int      newChildIndex = treeModel.getIndexOfChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                                     (getUserObject(), userObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            if(newChildIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            FHTreeStateNode     aNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            FHTreeStateNode     child = createNodeForValue(userObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                                                           newChildIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            int                 childRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            if(isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                childRow = getRowToModelIndex(newChildIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                childRow = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            child.row = childRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            for(int counter = 0, maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                aNode = (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                if(aNode.childIndex > newChildIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                    insert(child, counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            add(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         * Adjusts the receiver, and all its children rows by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
         * <code>amount</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        protected void adjustRowBy(int amount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            row += amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            if(isExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                for(int counter = getChildCount() - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                    counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    ((FHTreeStateNode)getChildAt(counter)).adjustRowBy(amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         * Adjusts this node, its child, and its parent starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
         * an index of <code>index</code> index is the index of the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
         * to start adjusting from, which is not necessarily the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
         * index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        protected void adjustRowBy(int amount, int startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            // Could check isVisible, but probably isn't worth it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            if(isExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                // children following startIndex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                for(int counter = getChildCount() - 1; counter >= startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    ((FHTreeStateNode)getChildAt(counter)).adjustRowBy(amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            // Parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            FHTreeStateNode        parent = (FHTreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            if(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                parent.adjustRowBy(amount, parent.getIndex(this) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
         * Messaged when the node has expanded. This updates all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
         * the receivers children rows, as well as the total row count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        protected void didExpand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            int               nextRow = setRowAndChildren(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            FHTreeStateNode   parent = (FHTreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            int               childRowCount = nextRow - row - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            if(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                parent.adjustRowBy(childRowCount, parent.getIndex(this) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            adjustRowCountBy(childRowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
         * Sets the receivers row to <code>nextRow</code> and recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
         * updates all the children of the receivers rows. The index the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
         * next row is to be placed as is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        protected int setRowAndChildren(int nextRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            row = nextRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            if(!isExpanded())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                return row + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            int              lastRow = row + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            int              lastModelIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            FHTreeStateNode  child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            int              maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            for(int counter = 0; counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                child = (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                lastRow += (child.childIndex - lastModelIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                lastModelIndex = child.childIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                if(child.isExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                    lastRow = child.setRowAndChildren(lastRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    child.row = lastRow++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            return lastRow + childCount - lastModelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1082
         * Resets the receivers children's rows. Starting with the child
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
         * at <code>childIndex</code> (and <code>modelIndex</code>) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
         * <code>newRow</code>. This uses <code>setRowAndChildren</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
         * to recursively descend children, and uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
         * <code>resetRowSelection</code> to ascend parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        // This can be rather expensive, but is needed for the collapse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        // case this is resulting from a remove (although I could fix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        // that by having instances of FHTreeStateNode hold a ref to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        // the number of children). I prefer this though, making determing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        // the row of a particular node fast is very nice!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        protected void resetChildrenRowsFrom(int newRow, int childIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                                            int modelIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            int              lastRow = newRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            int              lastModelIndex = modelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            FHTreeStateNode  node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            int              maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            for(int counter = childIndex; counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                node = (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                lastRow += (node.childIndex - lastModelIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                lastModelIndex = node.childIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                if(node.isExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    lastRow = node.setRowAndChildren(lastRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    node.row = lastRow++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            lastRow += childCount - lastModelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            node = (FHTreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                node.resetChildrenRowsFrom(lastRow, node.getIndex(this) + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                                           this.childIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            else { // This is the root, reset total ROWCOUNT!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                rowCount = lastRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            }
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
         * Makes the receiver visible, but invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
         * <code>expandParentAndReceiver</code> on the superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        protected void makeVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            FHTreeStateNode       parent = (FHTreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            if(parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                parent.expandParentAndReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
         * Invokes <code>expandParentAndReceiver</code> on the parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         * and expands the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        protected void expandParentAndReceiver() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            FHTreeStateNode       parent = (FHTreeStateNode)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            if(parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                parent.expandParentAndReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            expand();
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
         * Expands the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        protected void expand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            if(!isExpanded && !isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                boolean            visible = isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                isExpanded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                childCount = treeModel.getChildCount(getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                if(visible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    didExpand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                // Update the selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                if(visible && treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                }
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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
         * Collapses the receiver. If <code>adjustRows</code> is true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
         * the rows of nodes after the receiver are adjusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        protected void collapse(boolean adjustRows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            if(isExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                if(isVisible() && adjustRows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                    int              childCount = getTotalChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                    isExpanded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                    adjustRowCountBy(-childCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    // We can do this because adjustRowBy won't descend
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                    // the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                    adjustRowBy(-childCount, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                    isExpanded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                if(adjustRows && isVisible() && treeSelectionModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                    treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
         * Returns true if the receiver is a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        public boolean isLeaf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            TreeModel model = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            return (model != null) ? model.isLeaf(this.getUserObject()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                   true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
         * Adds newChild to this nodes children at the appropriate location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
         * The location is determined from the childIndex of newChild.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        protected void addNode(FHTreeStateNode newChild) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            boolean         added = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            int             childIndex = newChild.getChildIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            for(int counter = 0, maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                if(((FHTreeStateNode)getChildAt(counter)).getChildIndex() >
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                   childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    added = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    insert(newChild, counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                    counter = maxCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            if(!added)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                add(newChild);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
         * Removes the child at <code>modelIndex</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         * <code>isChildVisible</code> should be true if the receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
         * is visible and expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        protected void removeChildAtModelIndex(int modelIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                                               boolean isChildVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            FHTreeStateNode     childNode = getChildAtModelIndex(modelIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            if(childNode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                int          row = childNode.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                int          index = getIndex(childNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                childNode.collapse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                adjustChildIndexs(index, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                childCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                if(isChildVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    // Adjust the rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                    resetChildrenRowsFrom(row, index, modelIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                int                  maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                FHTreeStateNode      aChild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                for(int counter = 0; counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                    aChild = (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                    if(aChild.childIndex >= modelIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                        if(isChildVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                            adjustRowBy(-1, counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                            adjustRowCountBy(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                        // Since matched and children are always sorted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                        // index, no need to continue testing with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                        // above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                        for(; counter < maxCounter; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                            ((FHTreeStateNode)getChildAt(counter)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                                              childIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                        childCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                // No children to adjust, but it was a child, so we still need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                // to adjust nodes after this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                if(isChildVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                    adjustRowBy(-1, maxCounter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                    adjustRowCountBy(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                childCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
         * Adjusts the child indexs of the receivers children by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
         * <code>amount</code>, starting at <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        protected void adjustChildIndexs(int index, int amount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            for(int counter = index, maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                ((FHTreeStateNode)getChildAt(counter)).childIndex += amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
         * Messaged when a child has been inserted at index. For all the
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
  1286
         * children that have a childIndex &ge; index their index is incremented
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
         * by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        protected void childInsertedAtModelIndex(int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                                               boolean isExpandedAndVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            FHTreeStateNode                aChild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            int                            maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            for(int counter = 0; counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                aChild = (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                if(aChild.childIndex >= index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                    if(isExpandedAndVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                        adjustRowBy(1, counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                        adjustRowCountBy(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                    /* Since matched and children are always sorted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                       index, no need to continue testing with the above. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                    for(; counter < maxCounter; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                        ((FHTreeStateNode)getChildAt(counter)).childIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                    childCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            // No children to adjust, but it was a child, so we still need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            // to adjust nodes after this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            if(isExpandedAndVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                adjustRowBy(1, maxCounter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                adjustRowCountBy(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            childCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
         * Returns true if there is a row for <code>row</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
         * <code>nextRow</code> gives the bounds of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
         * Information about the found row is returned in <code>info</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
         * This should be invoked on root with <code>nextRow</code> set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
         * to <code>getRowCount</code>().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        protected boolean getPathForRow(int row, int nextRow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                                        SearchInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            if(this.row == row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                info.node = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                info.isNodeParentNode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                info.childIndex = childIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            FHTreeStateNode            child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            FHTreeStateNode            lastChild = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            for(int counter = 0, maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                child = (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                if(child.row > row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                    if(counter == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                        // No node exists for it, and is first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                        info.node = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                        info.isNodeParentNode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                        info.childIndex = row - this.row - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    else {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1349
                        // May have been in last child's bounds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                        int          lastChildEndRow = 1 + child.row -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                                     (child.childIndex - lastChild.childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                        if(row < lastChildEndRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                            return lastChild.getPathForRow(row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                                                       lastChildEndRow, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                        // Between last child and child, but not in last child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                        info.node = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                        info.isNodeParentNode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                        info.childIndex = row - lastChildEndRow +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                                                lastChild.childIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                lastChild = child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            // Not in children, but we should have it, offset from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            // nextRow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            if(lastChild != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                int        lastChildEndRow = nextRow -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                                  (childCount - lastChild.childIndex) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                if(row < lastChildEndRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                    return lastChild.getPathForRow(row, lastChildEndRow, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                // Between last child and child, but not in last child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                info.node = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                info.isNodeParentNode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                info.childIndex = row - lastChildEndRow +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                                             lastChild.childIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                // No children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                int         retChildIndex = row - this.row - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                if(retChildIndex >= childCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                info.node = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                info.isNodeParentNode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                info.childIndex = retChildIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
         * Asks all the children of the receiver for their totalChildCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
         * and returns this value (plus stopIndex).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        protected int getCountTo(int stopIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            FHTreeStateNode    aChild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            int                retCount = stopIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            for(int counter = 0, maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                aChild = (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                if(aChild.childIndex >= stopIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                    counter = maxCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                    retCount += aChild.getTotalChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            if(parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                return retCount + ((FHTreeStateNode)getParent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                                   .getCountTo(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            if(!isRootVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                return (retCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            return retCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
         * Returns the number of children that are expanded to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
         * <code>stopIndex</code>. This does not include the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
         * of children that the child at <code>stopIndex</code> might
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
         * have.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        protected int getNumExpandedChildrenTo(int stopIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            FHTreeStateNode    aChild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            int                retCount = stopIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            for(int counter = 0, maxCounter = getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                aChild = (FHTreeStateNode)getChildAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                if(aChild.childIndex >= stopIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                    return retCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                    retCount += aChild.getTotalChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            return retCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
         * Messaged when this node either expands or collapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        protected void didAdjustTree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    } // FixedHeightLayoutCache.FHTreeStateNode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * Used as a placeholder when getting the path in FHTreeStateNodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    private class SearchInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        protected FHTreeStateNode   node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        protected boolean           isNodeParentNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        protected int               childIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        protected TreePath getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            if(node == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            if(isNodeParentNode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                return node.getTreePath().pathByAddingChild(treeModel.getChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                                            (node.getUserObject(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                                             childIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            return node.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    } // FixedHeightLayoutCache.SearchInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * An enumerator to iterate through visible nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     */
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1477
    // This is very similar to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    // VariableHeightTreeState.VisibleTreeStateNodeEnumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    private class VisibleFHTreeStateNodeEnumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        implements Enumeration<TreePath>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        /** Parent thats children are being enumerated. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        protected FHTreeStateNode     parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        /** Index of next child. An index of -1 signifies parent should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
         * visibled next. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        protected int                 nextIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        /** Number of children in parent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        protected int                 childCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        protected VisibleFHTreeStateNodeEnumeration(FHTreeStateNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            this(node, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        protected VisibleFHTreeStateNodeEnumeration(FHTreeStateNode parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                                                    int startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            this.nextIndex = startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            this.childCount = treeModel.getChildCount(this.parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                                                      getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
         * @return true if more visible nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            return (parent != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
         * @return next visible TreePath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        public TreePath nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            if(!hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                throw new NoSuchElementException("No more visible paths");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            TreePath                retObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            if(nextIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                retObject = parent.getTreePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                FHTreeStateNode  node = parent.getChildAtModelIndex(nextIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                if(node == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                    retObject = parent.getTreePath().pathByAddingChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                                  (treeModel.getChild(parent.getUserObject(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                                                      nextIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                    retObject = node.getTreePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            updateNextObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            return retObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
         * Determines the next object by invoking <code>updateNextIndex</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
         * and if not succesful <code>findNextValidParent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        protected void updateNextObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            if(!updateNextIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                findNextValidParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
         * Finds the next valid parent, this should be called when nextIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
         * is beyond the number of children of the current parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        protected boolean findNextValidParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            if(parent == root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                // mark as invalid!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            while(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                FHTreeStateNode      newParent = (FHTreeStateNode)parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                                                  getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                if(newParent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                    nextIndex = parent.childIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                    parent = newParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                    childCount = treeModel.getChildCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                                            (parent.getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                    if(updateNextIndex())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                    parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
         * Updates <code>nextIndex</code> returning false if it is beyond
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
         * the number of children of parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        protected boolean updateNextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            // nextIndex == -1 identifies receiver, make sure is expanded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            // before descend.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            if(nextIndex == -1 && !parent.isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            // Check that it can have kids
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            if(childCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            // Make sure next index not beyond child count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            else if(++nextIndex >= childCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            FHTreeStateNode    child = parent.getChildAtModelIndex(nextIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            if(child != null && child.isExpanded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                parent = child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                nextIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                childCount = treeModel.getChildCount(child.getUserObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    } // FixedHeightLayoutCache.VisibleFHTreeStateNodeEnumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
}