KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > TreeNode


1 /*******************************************************************************
2  * Copyright (c) 2005 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.jface.viewers;
13
14 import org.eclipse.jface.util.Util;
15
16 /**
17  * A simple data structure that is useful for implemented tree models. This can
18  * be returned by
19  * {@link org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)}.
20  * It allows simple delegation of methods from
21  * {@link org.eclipse.jface.viewers.ITreeContentProvider} such as
22  * {@link org.eclipse.jface.viewers.ITreeContentProvider#getChildren(Object)},
23  * {@link org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object)} and
24  * {@link org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(Object)}
25  *
26  * @since 3.2
27  */

28 public class TreeNode {
29
30     /**
31      * The array of child tree nodes for this tree node. If there are no
32      * children, then this value may either by an empty array or
33      * <code>null</code>. There should be no <code>null</code> children in
34      * the array.
35      */

36     private TreeNode[] children;
37
38     /**
39      * The parent tree node for this tree node. This value may be
40      * <code>null</code> if there is no parent.
41      */

42     private TreeNode parent;
43
44     /**
45      * The value contained in this node. This value may be anything.
46      */

47     protected Object JavaDoc value;
48
49     /**
50      * Constructs a new instance of <code>TreeNode</code>.
51      *
52      * @param value
53      * The value held by this node; may be anything.
54      */

55     public TreeNode(final Object JavaDoc value) {
56         this.value = value;
57     }
58     
59     public boolean equals(final Object JavaDoc object) {
60         if (object instanceof TreeNode) {
61             return Util.equals(this.value, ((TreeNode) object).value);
62         }
63
64         return false;
65     }
66
67     /**
68      * Returns the child nodes. Empty arrays are converted to <code>null</code>
69      * before being returned.
70      *
71      * @return The child nodes; may be <code>null</code>, but never empty.
72      * There should be no <code>null</code> children in the array.
73      */

74     public TreeNode[] getChildren() {
75         if (children != null && children.length == 0) {
76             return null;
77         }
78         return children;
79     }
80
81     /**
82      * Returns the parent node.
83      *
84      * @return The parent node; may be <code>null</code> if there are no
85      * parent nodes.
86      */

87     public TreeNode getParent() {
88         return parent;
89     }
90
91     /**
92      * Returns the value held by this node.
93      *
94      * @return The value; may be anything.
95      */

96     public Object JavaDoc getValue() {
97         return value;
98     }
99
100     /**
101      * Returns whether the tree has any children.
102      *
103      * @return <code>true</code> if its array of children is not
104      * <code>null</code> and is non-empty; <code>false</code>
105      * otherwise.
106      */

107     public boolean hasChildren() {
108         return children != null && children.length > 0;
109     }
110     
111     public int hashCode() {
112         return Util.hashCode(value);
113     }
114
115     /**
116      * Sets the children for this node.
117      *
118      * @param children
119      * The child nodes; may be <code>null</code> or empty. There
120      * should be no <code>null</code> children in the array.
121      */

122     public void setChildren(final TreeNode[] children) {
123         this.children = children;
124     }
125
126     /**
127      * Sets the parent for this node.
128      *
129      * @param parent
130      * The parent node; may be <code>null</code>.
131      */

132     public void setParent(final TreeNode parent) {
133         this.parent = parent;
134     }
135 }
136
Popular Tags