jdk/src/share/classes/javax/swing/tree/DefaultTreeModel.java
author darcy
Fri, 24 Jan 2014 07:16:53 -0800
changeset 22574 7f8ce0c8c20a
parent 21278 ef8a3a2a72f2
child 24495 a5c854a00679
permissions -rw-r--r--
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes Reviewed-by: alexsch, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 1997, 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 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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void setAsksAllowsChildren(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        asksAllowsChildren = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      * Tells how leaf nodes are determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
      * @return true if only nodes which do not allow children are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
      *         leaf nodes, false if nodes which have no children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
      *         (even if allowed) are leaf nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      * @see #asksAllowsChildren
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public boolean asksAllowsChildren() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return asksAllowsChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Sets the root to <code>root</code>. A null <code>root</code> implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * the tree is to display nothing, and is legal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void setRoot(TreeNode root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Object oldRoot = this.root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.root = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (root == null && oldRoot != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            fireTreeStructureChanged(this, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            nodeStructureChanged(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Returns the root of the tree.  Returns null only if the tree has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * no nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return  the root of the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public Object getRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return root;
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
     * Returns the index of child in parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * If either the parent or child is <code>null</code>, returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param parent a note in the tree, obtained from this data source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param child the node we are interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return the index of the child in the parent, or -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *    if either the parent or the child is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public int getIndexOfChild(Object parent, Object child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if(parent == null || child == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return ((TreeNode)parent).getIndex((TreeNode)child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns the child of <I>parent</I> at index <I>index</I> in the parent's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * child array.  <I>parent</I> must be a node previously obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * 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
   168
     * 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
   169
     * <i>index</i> &lt; getChildCount(<i>parent</i>)).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param   parent  a node in the tree, obtained from this data source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return  the child of <I>parent</I> at index <I>index</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public Object getChild(Object parent, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return ((TreeNode)parent).getChildAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Returns the number of children of <I>parent</I>.  Returns 0 if the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * is a leaf or if it has no children.  <I>parent</I> must be a node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * previously obtained from this data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param   parent  a node in the tree, obtained from this data source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @return  the number of children of the node <I>parent</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public int getChildCount(Object parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return ((TreeNode)parent).getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Returns whether the specified node is a leaf node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The way the test is performed depends on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <code>askAllowsChildren</code> setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param node the node to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return true if the node is a leaf node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @see #asksAllowsChildren
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see TreeModel#isLeaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public boolean isLeaf(Object node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if(asksAllowsChildren)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return !((TreeNode)node).getAllowsChildren();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return ((TreeNode)node).isLeaf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Invoke this method if you've modified the {@code TreeNode}s upon which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * this model depends. The model will notify all of its listeners that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * model has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public void reload() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        reload(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
      * This sets the user object of the TreeNode identified by path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
      * and posts a node changed.  If you use custom user objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
      * the TreeModel you're going to need to subclass this and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
      * set the user object of the changed node to something meaningful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void valueForPathChanged(TreePath path, Object newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        MutableTreeNode   aNode = (MutableTreeNode)path.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        aNode.setUserObject(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        nodeChanged(aNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Invoked this to insert newChild at location index in parents children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * This will then message nodesWereInserted to create the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * event. This is the preferred way to add children as it will create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * the appropriate event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void insertNodeInto(MutableTreeNode newChild,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                               MutableTreeNode parent, int index){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        parent.insert(newChild, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int[]           newIndexs = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        newIndexs[0] = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        nodesWereInserted(parent, newIndexs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Message this to remove node from its parent. This will message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * nodesWereRemoved to create the appropriate event. This is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * preferred way to remove a node as it handles the event creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * for you.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public void removeNodeFromParent(MutableTreeNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        MutableTreeNode         parent = (MutableTreeNode)node.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if(parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new IllegalArgumentException("node does not have a parent.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int[]            childIndex = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        Object[]         removedArray = new Object[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        childIndex[0] = parent.getIndex(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        parent.remove(childIndex[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        removedArray[0] = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        nodesWereRemoved(parent, childIndex, removedArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
      * Invoke this method after you've changed how node is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
      * represented in the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public void nodeChanged(TreeNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if(listenerList != null && node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            TreeNode         parent = node.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                int        anIndex = parent.getIndex(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if(anIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    int[]        cIndexs = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    cIndexs[0] = anIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    nodesChanged(parent, cIndexs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            else if (node == getRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                nodesChanged(node, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Invoke this method if you've modified the {@code TreeNode}s upon which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * this model depends. The model will notify all of its listeners that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * model has changed below the given node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param node the node below which the model has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public void reload(TreeNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            fireTreeStructureChanged(this, getPathToRoot(node), null, null);
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 after you've inserted some TreeNodes into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
      * node.  childIndices should be the index of the new elements and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
      * must be sorted in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public void nodesWereInserted(TreeNode node, int[] childIndices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if(listenerList != null && node != null && childIndices != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
           && childIndices.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            int               cCount = childIndices.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            Object[]          newChildren = new Object[cCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            for(int counter = 0; counter < cCount; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                newChildren[counter] = node.getChildAt(childIndices[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            fireTreeNodesInserted(this, getPathToRoot(node), childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                  newChildren);
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
      * Invoke this method after you've removed some TreeNodes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
      * node.  childIndices should be the index of the removed elements and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
      * must be sorted in ascending order. And removedChildren should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
      * the array of the children objects that were removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public void nodesWereRemoved(TreeNode node, int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                 Object[] removedChildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if(node != null && childIndices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            fireTreeNodesRemoved(this, getPathToRoot(node), childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                 removedChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
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
      * Invoke this method after you've changed how the children identified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
      * childIndicies are to be represented in the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public void nodesChanged(TreeNode node, int[] childIndices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (childIndices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                int            cCount = childIndices.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                if(cCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    Object[]       cChildren = new Object[cCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    for(int counter = 0; counter < cCount; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        cChildren[counter] = node.getChildAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                            (childIndices[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    fireTreeNodesChanged(this, getPathToRoot(node),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                         childIndices, cChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            else if (node == getRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                fireTreeNodesChanged(this, getPathToRoot(node), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
      * 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
   361
      * node and its children's children...  This will post a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
      * treeStructureChanged event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public void nodeStructureChanged(TreeNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
           fireTreeStructureChanged(this, getPathToRoot(node), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Builds the parents of node up to and including the root node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * where the original node is the last element in the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * The length of the returned array gives the node's depth in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param aNode the TreeNode to get the path for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public TreeNode[] getPathToRoot(TreeNode aNode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return getPathToRoot(aNode, 0);
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
     * Builds the parents of node up to and including the root node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * where the original node is the last element in the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * The length of the returned array gives the node's depth in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param aNode  the TreeNode to get the path for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param depth  an int giving the number of steps already taken towards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *        the root (on recursive calls), used to size the returned array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @return an array of TreeNodes giving the path from the root to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *         specified node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        TreeNode[]              retNodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // This method recurses, traversing towards the root in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // size the array. On the way back, it fills in the nodes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        // starting from the root and working back to the original node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        /* Check for null, in case someone passed in a null node, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
           they passed in an element that isn't rooted at root. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if(aNode == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if(depth == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                retNodes = new TreeNode[depth];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            depth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if(aNode == root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                retNodes = new TreeNode[depth];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                retNodes = getPathToRoot(aNode.getParent(), depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            retNodes[retNodes.length - depth] = aNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return retNodes;
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
    //  Events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Adds a listener for the TreeModelEvent posted after the tree changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @see     #removeTreeModelListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param   l       the listener to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public void addTreeModelListener(TreeModelListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        listenerList.add(TreeModelListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Removes a listener previously added with <B>addTreeModelListener()</B>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @see     #addTreeModelListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param   l       the listener to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void removeTreeModelListener(TreeModelListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        listenerList.remove(TreeModelListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Returns an array of all the tree model listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * registered on this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return all of this model's <code>TreeModelListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *         or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *         array if no tree model listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see #addTreeModelListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see #removeTreeModelListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public TreeModelListener[] getTreeModelListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   457
        return listenerList.getListeners(TreeModelListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @param path the path to the parent of the nodes that changed; use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *             {@code null} to identify the root has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param childIndices the indices of the changed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param children the changed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    protected void fireTreeNodesChanged(Object source, Object[] path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                        int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                        Object[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    e = new TreeModelEvent(source, path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                           childIndices, children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                ((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param path the path to the parent the nodes were added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param childIndices the indices of the new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param children the new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    protected void fireTreeNodesInserted(Object source, Object[] path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                        int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                        Object[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    e = new TreeModelEvent(source, path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                           childIndices, children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param path the path to the parent the nodes were removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @param childIndices the indices of the removed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @param children the removed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    protected void fireTreeNodesRemoved(Object source, Object[] path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                        int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                                        Object[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    e = new TreeModelEvent(source, path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                           childIndices, children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @param path the path to the parent of the structure that has changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *             use {@code null} to identify the root has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param childIndices the indices of the affected elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param children the affected elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    protected void fireTreeStructureChanged(Object source, Object[] path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                                        int[] childIndices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                                        Object[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    e = new TreeModelEvent(source, path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                           childIndices, children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param source the source of the {@code TreeModelEvent};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *               typically {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @param path the path to the parent of the structure that has changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *             use {@code null} to identify the root has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    private void fireTreeStructureChanged(Object source, TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        TreeModelEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if (listeners[i]==TreeModelListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    e = new TreeModelEvent(source, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * as <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * upon this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * with a class literal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * For example, you can query a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <code>DefaultTreeModel</code> <code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * for its tree model listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <pre>TreeModelListener[] tmls = (TreeModelListener[])(m.getListeners(TreeModelListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param listenerType the type of listeners requested; this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *          should specify an interface that descends from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *          <code><em>Foo</em>Listener</code>s on this component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @exception ClassCastException if <code>listenerType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *          doesn't specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @see #getTreeModelListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    // Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    private void writeObject(ObjectOutputStream s) throws IOException {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   655
        Vector<Object> values = new Vector<Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        // Save the root, if its Serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if(root != null && root instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            values.addElement("root");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            values.addElement(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        s.writeObject(values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        Vector          values = (Vector)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        int             indexCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        int             maxCounter = values.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if(indexCounter < maxCounter && values.elementAt(indexCounter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
           equals("root")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            root = (TreeNode)values.elementAt(++indexCounter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            indexCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
} // End of class DefaultTreeModel