jdk/src/java.desktop/share/classes/javax/swing/tree/DefaultTreeModel.java
author darcy
Fri, 03 Apr 2015 10:41:34 -0700
changeset 29894 3e16b51732f5
parent 26037 508779ce6619
permissions -rw-r--r--
8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}} Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
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 java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A simple tree data model that uses TreeNodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * For further information and examples that use DefaultTreeModel,
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20169
diff changeset
    36
 * see <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html">How to Use Trees</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * 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: 20455
diff changeset
    44
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Rob Davis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Ray Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
    52
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class DefaultTreeModel implements Serializable, TreeModel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** Root of the tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected TreeNode root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Listeners. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
      * Determines how the <code>isLeaf</code> method figures
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
      * out if a node is a leaf node. If true, a node is a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
      * node if it does not allow children. (If it allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      * children, it is not a leaf node, even if no children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      * are present.) That lets you distinguish between <i>folder</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      * nodes and <i>file</i> nodes in a file system, for example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      * If this value is false, then any node which has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
      * children is a leaf node, and any node may acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
      * children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      * @see TreeNode#getAllowsChildren
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
      * @see TreeModel#isLeaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
      * @see #setAsksAllowsChildren
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected boolean asksAllowsChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
      * Creates a tree in which any node can have children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
      * @param root a TreeNode object that is the root of the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
      * @see #DefaultTreeModel(TreeNode, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     @ConstructorProperties({"root"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     public DefaultTreeModel(TreeNode root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this(root, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      * Creates a tree specifying whether any node can have children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      * or whether only certain nodes can have children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      * @param root a TreeNode object that is the root of the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      * @param asksAllowsChildren a boolean, false if any node can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      *        have children, true if each node is asked to see if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      *        it can have children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      * @see #asksAllowsChildren
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public DefaultTreeModel(TreeNode root, boolean asksAllowsChildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.root = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.asksAllowsChildren = asksAllowsChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
      * Sets whether or not to test leafness by asking getAllowsChildren()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      * or isLeaf() to the TreeNodes.  If newvalue is true, getAllowsChildren()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      * is messaged, otherwise isLeaf() is messaged.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   108
      *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   109
      * @param newValue if true, getAllowsChildren() is messaged, otherwise
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   110
      *                 isLeaf() is messaged
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public void setAsksAllowsChildren(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        asksAllowsChildren = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
      * Tells how leaf nodes are determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      * @return true if only nodes which do not allow children are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
      *         leaf nodes, false if nodes which have no children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
      *         (even if allowed) are leaf nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      * @see #asksAllowsChildren
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public boolean asksAllowsChildren() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return asksAllowsChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Sets the root to <code>root</code>. A null <code>root</code> implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * the tree is to display nothing, and is legal.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   131
     *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   132
     * @param root new value of tree root
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void setRoot(TreeNode root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Object oldRoot = this.root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.root = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (root == null && oldRoot != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            fireTreeStructureChanged(this, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            nodeStructureChanged(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Returns the root of the tree.  Returns null only if the tree has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * no nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return  the root of the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Object getRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns the index of child in parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * If either the parent or child is <code>null</code>, returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param parent a note in the tree, obtained from this data source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param child the node we are interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return the index of the child in the parent, or -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *    if either the parent or the child is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public int getIndexOfChild(Object parent, Object child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if(parent == null || child == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return ((TreeNode)parent).getIndex((TreeNode)child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Returns the child of <I>parent</I> at index <I>index</I> in the parent's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * child array.  <I>parent</I> must be a node previously obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * this data source. This should not return null if <i>index</i>
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   173
     * is a valid index for <i>parent</i> (that is <i>index</i> &gt;= 0 &amp;&amp;
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   174
     * <i>index</i> &lt; getChildCount(<i>parent</i>)).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param   parent  a node in the tree, obtained from this data source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return  the child of <I>parent</I> at index <I>index</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public Object getChild(Object parent, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return ((TreeNode)parent).getChildAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns the number of children of <I>parent</I>.  Returns 0 if the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * is a leaf or if it has no children.  <I>parent</I> must be a node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * previously obtained from this data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param   parent  a node in the tree, obtained from this data source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return  the number of children of the node <I>parent</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public int getChildCount(Object parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return ((TreeNode)parent).getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns whether the specified node is a leaf node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * The way the test is performed depends on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <code>askAllowsChildren</code> setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param node the node to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return true if the node is a leaf node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @see #asksAllowsChildren
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @see TreeModel#isLeaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public boolean isLeaf(Object node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if(asksAllowsChildren)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return !((TreeNode)node).getAllowsChildren();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return ((TreeNode)node).isLeaf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Invoke this method if you've modified the {@code TreeNode}s upon which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * this model depends. The model will notify all of its listeners that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * model has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public void reload() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        reload(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
      * This sets the user object of the TreeNode identified by path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
      * and posts a node changed.  If you use custom user objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
      * the TreeModel you're going to need to subclass this and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
      * set the user object of the changed node to something meaningful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void valueForPathChanged(TreePath path, Object newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        MutableTreeNode   aNode = (MutableTreeNode)path.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        aNode.setUserObject(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        nodeChanged(aNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Invoked this to insert newChild at location index in parents children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * This will then message nodesWereInserted to create the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * event. This is the preferred way to add children as it will create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * the appropriate event.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   239
     *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   240
     * @param newChild  child node to be inserted
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   241
     * @param parent    node to which children new node will be added
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   242
     * @param index     index of parent's children
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public void insertNodeInto(MutableTreeNode newChild,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                               MutableTreeNode parent, int index){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        parent.insert(newChild, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int[]           newIndexs = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        newIndexs[0] = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        nodesWereInserted(parent, newIndexs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Message this to remove node from its parent. This will message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * nodesWereRemoved to create the appropriate event. This is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * preferred way to remove a node as it handles the event creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * for you.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   259
     *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   260
     * @param node the node to be removed from it's parrent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public void removeNodeFromParent(MutableTreeNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        MutableTreeNode         parent = (MutableTreeNode)node.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if(parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw new IllegalArgumentException("node does not have a parent.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        int[]            childIndex = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        Object[]         removedArray = new Object[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        childIndex[0] = parent.getIndex(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        parent.remove(childIndex[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        removedArray[0] = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        nodesWereRemoved(parent, childIndex, removedArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
      * Invoke this method after you've changed how node is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
      * represented in the tree.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   280
      *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   281
      * @param node the changed node
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void nodeChanged(TreeNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if(listenerList != null && node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            TreeNode         parent = node.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                int        anIndex = parent.getIndex(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if(anIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    int[]        cIndexs = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    cIndexs[0] = anIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    nodesChanged(parent, cIndexs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            else if (node == getRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                nodesChanged(node, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Invoke this method if you've modified the {@code TreeNode}s upon which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * this model depends. The model will notify all of its listeners that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * model has changed below the given node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param node the node below which the model has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void reload(TreeNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            fireTreeStructureChanged(this, getPathToRoot(node), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
      * Invoke this method after you've inserted some TreeNodes into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
      * node.  childIndices should be the index of the new elements and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
      * must be sorted in ascending order.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   319
      *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   320
      * @param node         parent node which children count been incremented
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   321
      * @param childIndices indexes of inserted children
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void nodesWereInserted(TreeNode node, int[] childIndices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if(listenerList != null && node != null && childIndices != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
           && childIndices.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            int               cCount = childIndices.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            Object[]          newChildren = new Object[cCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            for(int counter = 0; counter < cCount; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                newChildren[counter] = node.getChildAt(childIndices[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            fireTreeNodesInserted(this, getPathToRoot(node), childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                  newChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
      * Invoke this method after you've removed some TreeNodes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
      * node.  childIndices should be the index of the removed elements and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
      * must be sorted in ascending order. And removedChildren should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
      * the array of the children objects that were removed.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   341
      *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   342
      * @param node             parent node which childred were removed
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   343
      * @param childIndices     indexes of removed childs
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   344
      * @param removedChildren  array of the children objects that were removed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public void nodesWereRemoved(TreeNode node, int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                 Object[] removedChildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if(node != null && childIndices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            fireTreeNodesRemoved(this, getPathToRoot(node), childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                 removedChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
      * Invoke this method after you've changed how the children identified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
      * childIndicies are to be represented in the tree.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   357
      *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   358
      * @param node         changed node
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   359
      * @param childIndices indexes of changed children
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public void nodesChanged(TreeNode node, int[] childIndices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (childIndices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                int            cCount = childIndices.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                if(cCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    Object[]       cChildren = new Object[cCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    for(int counter = 0; counter < cCount; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        cChildren[counter] = node.getChildAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                            (childIndices[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    fireTreeNodesChanged(this, getPathToRoot(node),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                         childIndices, cChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            else if (node == getRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                fireTreeNodesChanged(this, getPathToRoot(node), null, null);
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
      * Invoke this method if you've totally changed the children of
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   384
      * node and its children's children...  This will post a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
      * treeStructureChanged event.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   386
      *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   387
      * @param node changed node
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public void nodeStructureChanged(TreeNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
           fireTreeStructureChanged(this, getPathToRoot(node), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Builds the parents of node up to and including the root node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * where the original node is the last element in the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * The length of the returned array gives the node's depth in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param aNode the TreeNode to get the path for
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   402
     * @return an array of TreeNodes giving the path from the root
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public TreeNode[] getPathToRoot(TreeNode aNode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return getPathToRoot(aNode, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Builds the parents of node up to and including the root node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * where the original node is the last element in the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * The length of the returned array gives the node's depth in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param aNode  the TreeNode to get the path for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param depth  an int giving the number of steps already taken towards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *        the root (on recursive calls), used to size the returned array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return an array of TreeNodes giving the path from the root to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         specified node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        TreeNode[]              retNodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        // This method recurses, traversing towards the root in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // size the array. On the way back, it fills in the nodes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // starting from the root and working back to the original node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        /* Check for null, in case someone passed in a null node, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
           they passed in an element that isn't rooted at root. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if(aNode == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            if(depth == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                retNodes = new TreeNode[depth];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            depth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if(aNode == root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                retNodes = new TreeNode[depth];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                retNodes = getPathToRoot(aNode.getParent(), depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            retNodes[retNodes.length - depth] = aNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return retNodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    //  Events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Adds a listener for the TreeModelEvent posted after the tree changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see     #removeTreeModelListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @param   l       the listener to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public void addTreeModelListener(TreeModelListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        listenerList.add(TreeModelListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Removes a listener previously added with <B>addTreeModelListener()</B>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @see     #addTreeModelListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param   l       the listener to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public void removeTreeModelListener(TreeModelListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        listenerList.remove(TreeModelListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Returns an array of all the tree model listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * registered on this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @return all of this model's <code>TreeModelListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *         or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *         array if no tree model listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @see #addTreeModelListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @see #removeTreeModelListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public TreeModelListener[] getTreeModelListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   483
        return listenerList.getListeners(TreeModelListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param path the path to the parent of the nodes that changed; use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *             {@code null} to identify the root has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param childIndices the indices of the changed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @param children the changed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    protected void fireTreeNodesChanged(Object source, Object[] path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                        int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                                        Object[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    e = new TreeModelEvent(source, path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                           childIndices, children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                ((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
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
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param path the path to the parent the nodes were added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param childIndices the indices of the new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param children the new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    protected void fireTreeNodesInserted(Object source, Object[] path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                        int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                        Object[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    e = new TreeModelEvent(source, path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                                           childIndices, children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @param path the path to the parent the nodes were removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @param childIndices the indices of the removed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param children the removed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    protected void fireTreeNodesRemoved(Object source, Object[] path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                        int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                        Object[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    e = new TreeModelEvent(source, path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                           childIndices, children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param path the path to the parent of the structure that has changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *             use {@code null} to identify the root has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param childIndices the indices of the affected elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param children the affected elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    protected void fireTreeStructureChanged(Object source, Object[] path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                        int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                        Object[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    e = new TreeModelEvent(source, path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                                           childIndices, children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param path the path to the parent of the structure that has changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *             use {@code null} to identify the root has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    private void fireTreeStructureChanged(Object source, TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    e = new TreeModelEvent(source, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * as <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * upon this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * with a class literal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * For example, you can query a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * <code>DefaultTreeModel</code> <code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * for its tree model listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <pre>TreeModelListener[] tmls = (TreeModelListener[])(m.getListeners(TreeModelListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   660
     * @param <T> the listener type
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   661
     * @param listenerType the type of listeners requested
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *          <code><em>Foo</em>Listener</code>s on this component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @exception ClassCastException if <code>listenerType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *          doesn't specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @see #getTreeModelListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    // Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    private void writeObject(ObjectOutputStream s) throws IOException {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   680
        Vector<Object> values = new Vector<Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        // Save the root, if its Serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if(root != null && root instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            values.addElement("root");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            values.addElement(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        s.writeObject(values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        throws IOException, ClassNotFoundException {
26001
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   693
        ObjectInputStream.GetField f = s.readFields();
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   694
        EventListenerList newListenerList = (EventListenerList) f.get("listenerList", null);
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   695
        if (newListenerList == null) {
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   696
            throw new InvalidObjectException("Null listenerList");
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   697
        }
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   698
        listenerList = newListenerList;
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   699
        asksAllowsChildren = f.get("asksAllowsChildren", false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 24495
diff changeset
   701
        Vector<?>       values = (Vector)s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        int             indexCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        int             maxCounter = values.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if(indexCounter < maxCounter && values.elementAt(indexCounter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
           equals("root")) {
26001
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   707
            TreeNode newRoot  = (TreeNode)values.elementAt(++indexCounter);
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   708
            if (newRoot == null) {
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   709
                throw new InvalidObjectException("Null root");
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   710
            }
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
   711
            root = newRoot;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            indexCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
} // End of class DefaultTreeModel