1 /*******************************************************************************
2 * Copyright (c) 2005, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.browsing.ui;
13 import java.util.Arrays;
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.IAdaptable;
19 * A {@link GraphExplorer} tree {@link NodeContext} path denotes a model element
20 * in a tree viewer. Tree path objects have value semantics. A model element is
21 * represented by a path of elements in the tree from the root element to the
24 * Clients may instantiate this class. Not intended to be subclassed.
28 * Directly imitated for the Simantics browsing framework from JFace
29 * <code>org.eclipse.jface.viewer.TreePath</code> class.
32 * @author Tuukka Lehtonen
34 public final class NodeContextPath implements IAdaptable {
36 public static final NodeContextPath[] NONE = {};
39 * Constant for representing an empty tree path.
41 public static final NodeContextPath EMPTY = new NodeContextPath(NodeContext.NONE);
43 private final NodeContext[] segments;
48 * Constructs a path identifying a leaf node in a tree.
51 * path of elements to a leaf node in a tree, starting with the
54 public NodeContextPath(NodeContext... segments) {
55 Assert.isNotNull(segments);
56 for (int i = 0; i < segments.length; i++) {
57 Assert.isNotNull(segments[i]);
59 this.segments = segments;
63 * Returns the element at the specified index in this path.
66 * index of element to return
67 * @return element at the specified index
69 public NodeContext getSegment(int index) {
70 return segments[index];
74 * Returns the number of elements in this path.
76 * @return the number of elements in this path
78 public int getSegmentCount() {
79 return segments.length;
83 * Returns the first element in this path, or <code>null</code> if this
84 * path has no segments.
86 * @return the first element in this path
88 public NodeContext getFirstSegment() {
89 if (segments.length == 0) {
96 * Returns the last element in this path, or <code>null</code> if this
97 * path has no segments.
99 * @return the last element in this path
101 public NodeContext getLastSegment() {
102 if (segments.length == 0) {
105 return segments[segments.length - 1];
109 * @return all the segments in the path as an array
111 public NodeContext[] getSegments() {
112 if (segments.length == 0)
114 return Arrays.copyOf(segments, segments.length);
120 * @see java.lang.Object#equals(java.lang.Object)
122 public boolean equals(Object other) {
123 if (!(other instanceof NodeContextPath)) {
126 return equals((NodeContextPath) other);
132 * @see java.lang.Object#hashCode()
134 public int hashCode() {
142 * Returns a hash code computed from the hash codes of the segments, using
143 * the given comparer to compute the hash codes of the segments.
146 * comparer to use or <code>null</code> if the segments' hash
147 * codes should be computed by calling their hashCode() methods.
148 * @return the computed hash code
150 private int hashCode0() {
152 for (int i = 0; i < segments.length; i++) {
153 result += segments[i].hashCode();
159 * Returns whether this path is equivalent to the given path using the
160 * specified comparer to compare individual elements.
163 * tree path to compare to
165 * comparator to use or <code>null</code> if segments should be
166 * compared using equals()
167 * @return whether the paths are equal
169 public boolean equals(NodeContextPath otherPath) {
170 if (otherPath == null) {
173 if (segments.length != otherPath.segments.length) {
176 for (int i = 0; i < segments.length; i++) {
177 if (!segments[i].equals(otherPath.segments[i])) {
185 * Returns whether this path starts with the same segments as the given
186 * path, using the given comparer to compare segments.
191 * the comparer to use, or <code>null</code> if equals() should
192 * be used to compare segments
193 * @return whether the given path is a prefix of this path, or the same as
196 public boolean startsWith(NodeContextPath treePath) {
197 int thisSegmentCount = getSegmentCount();
198 int otherSegmentCount = treePath.getSegmentCount();
199 if (otherSegmentCount == thisSegmentCount) {
200 return equals(treePath);
202 if (otherSegmentCount > thisSegmentCount) {
205 for (int i = 0; i < otherSegmentCount; i++) {
206 Object otherSegment = treePath.getSegment(i);
207 if (!otherSegment.equals(segments[i])) {
215 * Returns a copy of this tree path with one segment removed from the end,
216 * or <code>null</code> if this tree path has no segments.
217 * @return a tree path
219 public NodeContextPath getParentPath() {
220 int segmentCount = getSegmentCount();
221 if (segmentCount < 1) {
223 } else if (segmentCount == 1) {
226 NodeContext[] parentSegments = new NodeContext[segmentCount - 1];
227 System.arraycopy(segments, 0, parentSegments, 0, segmentCount - 1);
228 return new NodeContextPath(parentSegments);
232 * Returns a copy of this tree path with the given segment added at the end.
234 * @return a tree path
236 public NodeContextPath createChildPath(NodeContext newSegment) {
237 int segmentCount = getSegmentCount();
238 NodeContext[] childSegments = new NodeContext[segmentCount + 1];
240 System.arraycopy(segments, 0, childSegments, 0, segmentCount);
242 childSegments[segmentCount] = newSegment;
243 return new NodeContextPath(childSegments);
246 @SuppressWarnings("unchecked")
248 public <T> T getAdapter(Class<T> adapter) {
249 if (NodeContext.class == adapter)
250 return (T) getLastSegment();
251 NodeContext last = getLastSegment();
252 if (last instanceof IAdaptable)
253 return ((IAdaptable) last).getAdapter(adapter);