001: /*******************************************************************************
002: * Copyright (c) 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.jface.viewers;
011:
012: import org.eclipse.jface.util.Util;
013:
014: /**
015: * A simple data structure that is useful for implemented tree models. This can
016: * be returned by
017: * {@link org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)}.
018: * It allows simple delegation of methods from
019: * {@link org.eclipse.jface.viewers.ITreeContentProvider} such as
020: * {@link org.eclipse.jface.viewers.ITreeContentProvider#getChildren(Object)},
021: * {@link org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object)} and
022: * {@link org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(Object)}
023: *
024: * @since 3.2
025: */
026: public class TreeNode {
027:
028: /**
029: * The array of child tree nodes for this tree node. If there are no
030: * children, then this value may either by an empty array or
031: * <code>null</code>. There should be no <code>null</code> children in
032: * the array.
033: */
034: private TreeNode[] children;
035:
036: /**
037: * The parent tree node for this tree node. This value may be
038: * <code>null</code> if there is no parent.
039: */
040: private TreeNode parent;
041:
042: /**
043: * The value contained in this node. This value may be anything.
044: */
045: protected Object value;
046:
047: /**
048: * Constructs a new instance of <code>TreeNode</code>.
049: *
050: * @param value
051: * The value held by this node; may be anything.
052: */
053: public TreeNode(final Object value) {
054: this .value = value;
055: }
056:
057: public boolean equals(final Object object) {
058: if (object instanceof TreeNode) {
059: return Util.equals(this .value, ((TreeNode) object).value);
060: }
061:
062: return false;
063: }
064:
065: /**
066: * Returns the child nodes. Empty arrays are converted to <code>null</code>
067: * before being returned.
068: *
069: * @return The child nodes; may be <code>null</code>, but never empty.
070: * There should be no <code>null</code> children in the array.
071: */
072: public TreeNode[] getChildren() {
073: if (children != null && children.length == 0) {
074: return null;
075: }
076: return children;
077: }
078:
079: /**
080: * Returns the parent node.
081: *
082: * @return The parent node; may be <code>null</code> if there are no
083: * parent nodes.
084: */
085: public TreeNode getParent() {
086: return parent;
087: }
088:
089: /**
090: * Returns the value held by this node.
091: *
092: * @return The value; may be anything.
093: */
094: public Object getValue() {
095: return value;
096: }
097:
098: /**
099: * Returns whether the tree has any children.
100: *
101: * @return <code>true</code> if its array of children is not
102: * <code>null</code> and is non-empty; <code>false</code>
103: * otherwise.
104: */
105: public boolean hasChildren() {
106: return children != null && children.length > 0;
107: }
108:
109: public int hashCode() {
110: return Util.hashCode(value);
111: }
112:
113: /**
114: * Sets the children for this node.
115: *
116: * @param children
117: * The child nodes; may be <code>null</code> or empty. There
118: * should be no <code>null</code> children in the array.
119: */
120: public void setChildren(final TreeNode[] children) {
121: this .children = children;
122: }
123:
124: /**
125: * Sets the parent for this node.
126: *
127: * @param parent
128: * The parent node; may be <code>null</code>.
129: */
130: public void setParent(final TreeNode parent) {
131: this.parent = parent;
132: }
133: }
|