From f911ba985aa7fe0096c386c5be385ac5825ea527 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Sat, 16 Jul 2005 00:30:23 +0000 Subject: Initial revision From-SVN: r102074 --- libjava/classpath/javax/swing/tree/TreePath.java | 299 +++++++++++++++++++++++ 1 file changed, 299 insertions(+) create mode 100644 libjava/classpath/javax/swing/tree/TreePath.java (limited to 'libjava/classpath/javax/swing/tree/TreePath.java') diff --git a/libjava/classpath/javax/swing/tree/TreePath.java b/libjava/classpath/javax/swing/tree/TreePath.java new file mode 100644 index 0000000..37ec632 --- /dev/null +++ b/libjava/classpath/javax/swing/tree/TreePath.java @@ -0,0 +1,299 @@ +/* TreePath.java -- + Copyright (C) 2002, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.swing.tree; + +import java.io.Serializable; +import java.util.Arrays; + +/** + * A TreePath represents a sequence of tree elements that form + * a path starting from the root of a tree. A tree element can be represented + * by any {@link Object}. + * + * @author Andrew Selkirk + */ +public class TreePath implements Serializable +{ + static final long serialVersionUID = 4380036194768077479L; + + /** + * path + */ + private Object[] path = null; + + + /** + * Creates a path from the list of objects representing tree elements. The + * incoming array is copied so that subsequent changes do not affect this + * tree path. + * + * @param path the elements in the path (null not permitted). + * + * @throws IllegalArgumentException if path is null. + */ + public TreePath(Object[] path) + { + if (path == null) + throw new IllegalArgumentException("Null 'path' not permitted."); + this.path = new Object[path.length]; + System.arraycopy(path, 0, this.path, 0, path.length); + } + + /** + * Creates a new path from a single element. + * + * @param element the element (null not permitted). + * + * @throws IllegalArgumentException if element is + * null. + */ + public TreePath(Object element) + { + path = new Object[1]; + path[0] = element; + } + + /** + * Creates a new tree path by adding the specified element to + * the path. + * + * @param path a tree path. + * @param element a path element. + */ + protected TreePath(TreePath path, Object element) + { + if (element == null) + throw new NullPointerException("Null 'element' argument."); + Object[] treepath = path.getPath(); + + // Create Tree Path + this.path = new Object[treepath.length + 1]; + System.arraycopy(treepath, 0, this.path, 0, treepath.length); + this.path[treepath.length] = element; + } + + /** + * Creates a new tree path using the first length elements + * from the given array. + * + * @param path the path elements. + * @param length the path length. + */ + protected TreePath(Object[] path, int length) + { + // Create Path + this.path = new Object[length]; + System.arraycopy(path, 0, this.path, 0, length); + } + + /** + * Default constructor. + */ + protected TreePath() + { + path = new Object[0]; + } + + + /** + * Returns a hashcode for the path. + * + * @return A hashcode. + */ + public int hashCode() + { + return getLastPathComponent().hashCode(); + } + + /** + * Tests this path for equality with an arbitrary object. An object is + * considered equal to this path if and only if: + * + * + * @param object the object (null permitted). + * + * @returns true if obj is equal to this tree path, + * and false otherwise. + */ + public boolean equals(Object object) + { + Object[] treepath; + int index; + + if (object instanceof TreePath) + { + treepath = ((TreePath) object).getPath(); + if (treepath.length != path.length) + return false; + for (index = 0; index < path.length; index++) + { + if (!treepath[index].equals(path[index])) + return false; + } + + // Tree Path's are equals + return true; + } + + // Unequal + return false; + } + + /** + * Returns a string representation of this path. + * + * @return A string representation of this path. + */ + public String toString() + { + if (path.length == 1) + return String.valueOf(path[0]); + else + return Arrays.asList(path).toString(); + } + + /** + * Returns an array containing the path elements. + * + * @returns An array containing the path elements. + */ + public Object[] getPath() + { + return (Object[]) path.clone(); + } + + /** + * Returns the last object in the path. + * + * @return The last object in the path. + */ + public Object getLastPathComponent() + { + return path[path.length - 1]; + } + + /** + * Returns the number of elements in the path. + * + * @returns The number of elements in the path. + */ + public int getPathCount() + { + return path.length; + } + + /** + * Returns the element at the specified position in the path. + * + * @param position the element position (0 < N - 1, where + * N is the number of elements in the path). + * + * @return The element at the specified position. + * + * @throws IllegalArgumentException if position is outside the + * valid range. + */ + public Object getPathComponent(int position) + { + if (position < 0 || position >= getPathCount()) + throw new IllegalArgumentException("Invalid position: " + position); + return path[position]; + } + + /** + * Returns true if path is a descendant of this + * path, and false otherwise. If path is + * null, this method returns false. + * + * @param path the path to check (null permitted). + * + * @returns true if path is a descendant of this + * path, and false otherwise + */ + public boolean isDescendant(TreePath path) + { + if (path == null) + return false; + int count = getPathCount(); + if (path.getPathCount() < count) + return false; + for (int i = 0; i < count; i++) + { + if (!this.path[i].equals(path.getPathComponent(i))) + return false; + } + return true; + } + + /** + * Creates a new path that is equivalent to this path plus the specified + * element. + * + * @param element the element. + * + * @returns A tree path. + */ + public TreePath pathByAddingChild(Object element) + { + return new TreePath(this, element); + } + + /** + * Returns the parent path, which is a path containing all the same elements + * as this path, except for the last one. If this path contains only one + * element, the method returns null. + * + * @returns The parent path, or null if this path has only one + * element. + */ + public TreePath getParentPath() + { + // If this path has only one element, then we return null. That + // is what the JDK does. + if (path.length <= 1) + return null; + + return new TreePath(this.getPath(), path.length - 1); + } +} -- cgit v1.1