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