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