0N/A/*
2362N/A * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.event;
0N/A
0N/Aimport java.util.EventObject;
0N/Aimport javax.swing.tree.TreePath;
0N/A
0N/A
0N/A/**
0N/A * Encapsulates information describing changes to a tree model, and
0N/A * used to notify tree model listeners of the change.
0N/A * For more information and examples see
0N/A * <a
0N/A href="http://java.sun.com/docs/books/tutorial/uiswing/events/treemodellistener.html">How to Write a Tree Model Listener</a>,
0N/A * a section in <em>The Java Tutorial.</em>
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @author Rob Davis
0N/A * @author Ray Ryan
0N/A * @author Scott Violet
0N/A */
0N/Apublic class TreeModelEvent extends EventObject {
0N/A /** Path to the parent of the nodes that have changed. */
0N/A protected TreePath path;
0N/A /** Indices identifying the position of where the children were. */
0N/A protected int[] childIndices;
0N/A /** Children that have been removed. */
0N/A protected Object[] children;
0N/A
0N/A /**
0N/A * Used to create an event when nodes have been changed, inserted, or
0N/A * removed, identifying the path to the parent of the modified items as
0N/A * an array of Objects. All of the modified objects are siblings which are
0N/A * direct descendents (not grandchildren) of the specified parent.
0N/A * The positions at which the inserts, deletes, or changes occurred are
0N/A * specified by an array of <code>int</code>. The indexes in that array
0N/A * must be in order, from lowest to highest.
0N/A * <p>
0N/A * For changes, the indexes in the model correspond exactly to the indexes
0N/A * of items currently displayed in the UI. As a result, it is not really
0N/A * critical if the indexes are not in their exact order. But after multiple
0N/A * inserts or deletes, the items currently in the UI no longer correspond
0N/A * to the items in the model. It is therefore critical to specify the
0N/A * indexes properly for inserts and deletes.
0N/A * <p>
0N/A * For inserts, the indexes represent the <i>final</i> state of the tree,
0N/A * after the inserts have occurred. Since the indexes must be specified in
0N/A * order, the most natural processing methodology is to do the inserts
0N/A * starting at the lowest index and working towards the highest. Accumulate
0N/A * a Vector of <code>Integer</code> objects that specify the
0N/A * insert-locations as you go, then convert the Vector to an
0N/A * array of <code>int</code> to create the event. When the postition-index
0N/A * equals zero, the node is inserted at the beginning of the list. When the
0N/A * position index equals the size of the list, the node is "inserted" at
0N/A * (appended to) the end of the list.
0N/A * <p>
0N/A * For deletes, the indexes represent the <i>initial</i> state of the tree,
0N/A * before the deletes have occurred. Since the indexes must be specified in
0N/A * order, the most natural processing methodology is to use a delete-counter.
0N/A * Start by initializing the counter to zero and start work through the
0N/A * list from lowest to higest. Every time you do a delete, add the current
0N/A * value of the delete-counter to the index-position where the delete occurred,
0N/A * and append the result to a Vector of delete-locations, using
0N/A * <code>addElement()</code>. Then increment the delete-counter. The index
0N/A * positions stored in the Vector therefore reflect the effects of all previous
0N/A * deletes, so they represent each object's position in the initial tree.
0N/A * (You could also start at the highest index and working back towards the
0N/A * lowest, accumulating a Vector of delete-locations as you go using the
0N/A * <code>insertElementAt(Integer, 0)</code>.) However you produce the Vector
0N/A * of initial-positions, you then need to convert the Vector of <code>Integer</code>
0N/A * objects to an array of <code>int</code> to create the event.
0N/A * <p>
0N/A * <b>Notes:</b><ul>
0N/A * <li>Like the <code>insertNodeInto</code> method in the
0N/A * <code>DefaultTreeModel</code> class, <code>insertElementAt</code>
0N/A * appends to the <code>Vector</code> when the index matches the size
0N/A * of the vector. So you can use <code>insertElementAt(Integer, 0)</code>
0N/A * even when the vector is empty.
0N/A * <ul>To create a node changed event for the root node, specify the parent
0N/A * and the child indices as <code>null</code>.
0N/A * </ul>
0N/A *
0N/A * @param source the Object responsible for generating the event (typically
0N/A * the creator of the event object passes <code>this</code>
0N/A * for its value)
0N/A * @param path an array of Object identifying the path to the
0N/A * parent of the modified item(s), where the first element
0N/A * of the array is the Object stored at the root node and
0N/A * the last element is the Object stored at the parent node
0N/A * @param childIndices an array of <code>int</code> that specifies the
0N/A * index values of the removed items. The indices must be
0N/A * in sorted order, from lowest to highest
0N/A * @param children an array of Object containing the inserted, removed, or
0N/A * changed objects
0N/A * @see TreePath
0N/A */
0N/A public TreeModelEvent(Object source, Object[] path, int[] childIndices,
0N/A Object[] children)
0N/A {
0N/A this(source, new TreePath(path), childIndices, children);
0N/A }
0N/A
0N/A /**
0N/A * Used to create an event when nodes have been changed, inserted, or
0N/A * removed, identifying the path to the parent of the modified items as
0N/A * a TreePath object. For more information on how to specify the indexes
0N/A * and objects, see
0N/A * <code>TreeModelEvent(Object,Object[],int[],Object[])</code>.
0N/A *
0N/A * @param source the Object responsible for generating the event (typically
0N/A * the creator of the event object passes <code>this</code>
0N/A * for its value)
0N/A * @param path a TreePath object that identifies the path to the
0N/A * parent of the modified item(s)
0N/A * @param childIndices an array of <code>int</code> that specifies the
0N/A * index values of the modified items
0N/A * @param children an array of Object containing the inserted, removed, or
0N/A * changed objects
0N/A *
0N/A * @see #TreeModelEvent(Object,Object[],int[],Object[])
0N/A */
0N/A public TreeModelEvent(Object source, TreePath path, int[] childIndices,
0N/A Object[] children)
0N/A {
0N/A super(source);
0N/A this.path = path;
0N/A this.childIndices = childIndices;
0N/A this.children = children;
0N/A }
0N/A
0N/A /**
0N/A * Used to create an event when the node structure has changed in some way,
0N/A * identifying the path to the root of a modified subtree as an array of
0N/A * Objects. A structure change event might involve nodes swapping position,
0N/A * for example, or it might encapsulate multiple inserts and deletes in the
0N/A * subtree stemming from the node, where the changes may have taken place at
0N/A * different levels of the subtree.
0N/A * <blockquote>
0N/A * <b>Note:</b><br>
0N/A * JTree collapses all nodes under the specified node, so that only its
0N/A * immediate children are visible.
0N/A * </blockquote>
0N/A *
0N/A * @param source the Object responsible for generating the event (typically
0N/A * the creator of the event object passes <code>this</code>
0N/A * for its value)
0N/A * @param path an array of Object identifying the path to the root of the
0N/A * modified subtree, where the first element of the array is
0N/A * the object stored at the root node and the last element
0N/A * is the object stored at the changed node
0N/A * @see TreePath
0N/A */
0N/A public TreeModelEvent(Object source, Object[] path)
0N/A {
0N/A this(source, new TreePath(path));
0N/A }
0N/A
0N/A /**
0N/A * Used to create an event when the node structure has changed in some way,
0N/A * identifying the path to the root of the modified subtree as a TreePath
0N/A * object. For more information on this event specification, see
0N/A * <code>TreeModelEvent(Object,Object[])</code>.
0N/A *
0N/A * @param source the Object responsible for generating the event (typically
0N/A * the creator of the event object passes <code>this</code>
0N/A * for its value)
0N/A * @param path a TreePath object that identifies the path to the
0N/A * change. In the DefaultTreeModel,
0N/A * this object contains an array of user-data objects,
0N/A * but a subclass of TreePath could use some totally
0N/A * different mechanism -- for example, a node ID number
0N/A *
0N/A * @see #TreeModelEvent(Object,Object[])
0N/A */
0N/A public TreeModelEvent(Object source, TreePath path)
0N/A {
0N/A super(source);
0N/A this.path = path;
0N/A this.childIndices = new int[0];
0N/A }
0N/A
0N/A /**
0N/A * For all events, except treeStructureChanged,
0N/A * returns the parent of the changed nodes.
0N/A * For treeStructureChanged events, returns the ancestor of the
0N/A * structure that has changed. This and
0N/A * <code>getChildIndices</code> are used to get a list of the effected
0N/A * nodes.
0N/A * <p>
0N/A * The one exception to this is a treeNodesChanged event that is to
0N/A * identify the root, in which case this will return the root
0N/A * and <code>getChildIndices</code> will return null.
0N/A *
0N/A * @return the TreePath used in identifying the changed nodes.
0N/A * @see TreePath#getLastPathComponent
0N/A */
0N/A public TreePath getTreePath() { return path; }
0N/A
0N/A /**
0N/A * Convenience method to get the array of objects from the TreePath
0N/A * instance that this event wraps.
0N/A *
0N/A * @return an array of Objects, where the first Object is the one
0N/A * stored at the root and the last object is the one
0N/A * stored at the node identified by the path
0N/A */
0N/A public Object[] getPath() {
0N/A if(path != null)
0N/A return path.getPath();
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the objects that are children of the node identified by
0N/A * <code>getPath</code> at the locations specified by
0N/A * <code>getChildIndices</code>. If this is a removal event the
0N/A * returned objects are no longer children of the parent node.
0N/A *
0N/A * @return an array of Object containing the children specified by
0N/A * the event
0N/A * @see #getPath
0N/A * @see #getChildIndices
0N/A */
0N/A public Object[] getChildren() {
0N/A if(children != null) {
0N/A int cCount = children.length;
0N/A Object[] retChildren = new Object[cCount];
0N/A
0N/A System.arraycopy(children, 0, retChildren, 0, cCount);
0N/A return retChildren;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the values of the child indexes. If this is a removal event
0N/A * the indexes point to locations in the initial list where items
0N/A * were removed. If it is an insert, the indices point to locations
0N/A * in the final list where the items were added. For node changes,
0N/A * the indices point to the locations of the modified nodes.
0N/A *
0N/A * @return an array of <code>int</code> containing index locations for
0N/A * the children specified by the event
0N/A */
0N/A public int[] getChildIndices() {
0N/A if(childIndices != null) {
0N/A int cCount = childIndices.length;
0N/A int[] retArray = new int[cCount];
0N/A
0N/A System.arraycopy(childIndices, 0, retArray, 0, cCount);
0N/A return retArray;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string that displays and identifies this object's
0N/A * properties.
0N/A *
0N/A * @return a String representation of this object
0N/A */
0N/A public String toString() {
0N/A StringBuffer retBuffer = new StringBuffer();
0N/A
0N/A retBuffer.append(getClass().getName() + " " +
0N/A Integer.toString(hashCode()));
0N/A if(path != null)
0N/A retBuffer.append(" path " + path);
0N/A if(childIndices != null) {
0N/A retBuffer.append(" indices [ ");
0N/A for(int counter = 0; counter < childIndices.length; counter++)
0N/A retBuffer.append(Integer.toString(childIndices[counter])+ " ");
0N/A retBuffer.append("]");
0N/A }
0N/A if(children != null) {
0N/A retBuffer.append(" children [ ");
0N/A for(int counter = 0; counter < children.length; counter++)
0N/A retBuffer.append(children[counter] + " ");
0N/A retBuffer.append("]");
0N/A }
0N/A return retBuffer.toString();
0N/A }
0N/A}