001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.tree;
021:
022: import java.io.Serializable;
023:
024: import org.apache.harmony.x.swing.Utilities;
025:
026: import org.apache.harmony.x.swing.internal.nls.Messages;
027:
028: public class TreePath implements Serializable {
029:
030: private Object[] elements;
031: private TreePath parent;
032: private final int pathCount;
033:
034: public TreePath(final Object[] path) {
035: if (Utilities.isEmptyArray(path)) {
036: throw new IllegalArgumentException(Messages
037: .getString("swing.82")); //$NON-NLS-1$
038: }
039:
040: pathCount = path.length;
041: elements = new Object[pathCount];
042: System.arraycopy(path, 0, elements, 0, pathCount);
043: parent = null;
044: }
045:
046: public TreePath(final Object singlePath) {
047: if (singlePath == null) {
048: throw new IllegalArgumentException(Messages
049: .getString("swing.76")); //$NON-NLS-1$
050: }
051: elements = new Object[] { singlePath };
052: pathCount = 1;
053: parent = null;
054: }
055:
056: protected TreePath() {
057: elements = new Object[] { null };
058: pathCount = 1;
059: parent = null;
060: }
061:
062: protected TreePath(final Object[] path, final int length) {
063: pathCount = length;
064: elements = new Object[pathCount];
065: System.arraycopy(path, 0, elements, 0, pathCount);
066: parent = null;
067: }
068:
069: protected TreePath(final TreePath parentPath,
070: final Object lastElement) {
071: if (lastElement == null) {
072: throw new IllegalArgumentException(Messages
073: .getString("swing.76")); //$NON-NLS-1$
074: }
075:
076: elements = new Object[] { lastElement };
077: parent = parentPath;
078: pathCount = (parent != null) ? parent.getPathCount() + 1 : 1;
079: }
080:
081: public boolean equals(final Object o) {
082: if (!(o instanceof TreePath)) {
083: return false;
084: }
085:
086: TreePath path = (TreePath) o;
087: final int numPathComponents = getPathCount();
088: if (path.getPathCount() != numPathComponents) {
089: return false;
090: }
091:
092: for (int i = 0; i < numPathComponents; i++) {
093: if (!path.getPathComponent(i).equals(getPathComponent(i))) {
094: return false;
095: }
096: }
097:
098: return true;
099: }
100:
101: public Object getLastPathComponent() {
102: return elements[elements.length - 1];
103: }
104:
105: public TreePath getParentPath() {
106: if (parent != null) {
107: return parent;
108: }
109:
110: int numParentPaths = getPathCount() - 1;
111: if (numParentPaths <= 0) {
112: return null;
113: }
114:
115: return new TreePath(getPath(), numParentPaths);
116: }
117:
118: public Object[] getPath() {
119: if (parent == null) {
120: return elements;
121: }
122:
123: Object[] parentPath = parent.getPath();
124: Object[] result = new Object[parentPath.length + 1];
125: System.arraycopy(parentPath, 0, result, 0, parentPath.length);
126: result[result.length - 1] = getLastPathComponent();
127:
128: elements = (Object[]) result.clone();
129: parent = null;
130: return result;
131: }
132:
133: public Object getPathComponent(final int element) {
134: final int pathCount = getPathCount();
135: if (element < 0 || element >= pathCount) {
136: throw new IllegalArgumentException(Messages.getString(
137: "swing.75", element)); //$NON-NLS-1$
138: }
139: if (parent == null) {
140: return elements[element];
141: }
142:
143: return (element < pathCount - 1) ? parent
144: .getPathComponent(element) : getLastPathComponent();
145: }
146:
147: public int getPathCount() {
148: return pathCount;
149: }
150:
151: public boolean isDescendant(final TreePath child) {
152: if (child == null) {
153: return false;
154: }
155:
156: final int numPathComponents = getPathCount();
157: if (child.getPathCount() < numPathComponents) {
158: return false;
159: }
160:
161: for (int i = 0; i < numPathComponents; i++) {
162: if (!child.getPathComponent(i).equals(getPathComponent(i))) {
163: return false;
164: }
165: }
166:
167: return true;
168: }
169:
170: public TreePath pathByAddingChild(final Object child) {
171: if (child == null) {
172: throw new NullPointerException(Messages
173: .getString("swing.72")); //$NON-NLS-1$
174: }
175:
176: return new TreePath(this , child);
177: }
178:
179: public int hashCode() {
180: return getLastPathComponent().hashCode();
181: }
182:
183: public String toString() {
184: String result = null;
185: final int numPathComponents = getPathCount();
186: for (int i = 0; i < numPathComponents; i++) {
187: if (result != null) {
188: result += ", ";
189: } else {
190: result = "";
191: }
192: result += getPathComponent(i);
193: }
194: return "[" + result + "]";
195: }
196:
197: }
|