1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package net.sf.tm.plugin;
34
35 /***
36 * Tree model for use with JFace {@link org.eclipse.jface.viewers.TreeViewer}.
37 * The class is an adapter class containing the original object in the content
38 * field.
39 * A <code>TreeNode</code> can have a parent <code>TreeNode</code>. If it is
40 * a root node the parent must be <code>null</code>.
41 *
42 * @author Andreas Pataki
43 * @version $Id: TreeNode.java,v 1.3 2004/03/28 16:23:06 apataki Exp $
44 */
45 public class TreeNode {
46
47 /***
48 * Field for storing the actual content.
49 */
50 private Object content;
51 /***
52 * Field for storing the parent reference or null if the node is a root
53 * node.
54 */
55 private TreeNode parent;
56
57 /***
58 * Constructor. The content will get stored in the content field.
59 * The parent will get set to <code>null</code>.
60 *
61 * @param newContent The actual content object.
62 */
63 public TreeNode(Object newContent) {
64 content = newContent;
65 parent = null;
66 }
67
68 /***
69 * Constructor.
70 *
71 * @param newContent The actual content object. Will get stored in the
72 * content field.
73 * @param newParent The parent <code>TreeNode</code> object.
74 */
75 public TreeNode(Object newContent, TreeNode newParent) {
76 content = newContent;
77 parent = newParent;
78 }
79
80 /***
81 * Getter for the field content.
82 *
83 * @return content object
84 */
85 public Object getContent() {
86 return content;
87 }
88
89 /***
90 * Getter for the field parent.
91 *
92 * @return parent object or null if the node is a root node.
93 */
94 public TreeNode getParent() {
95 return parent;
96 }
97
98 /***
99 * Delegates the equals method to the contained content object. This is
100 * necessary because otherwise the tree thinks that the objects have
101 * changed after a data update.
102 *
103 * @see java.lang.Object#equals(java.lang.Object)
104 */
105 public boolean equals(Object obj) {
106 return (
107 obj instanceof TreeNode
108 && getContent().equals(((TreeNode) obj).getContent()));
109 }
110
111 /***
112 * Returns the hashCode of the contained content object. <b>Attention:</b>
113 * This means the <code>TreeNode</code> has the same hash as his content!
114 *
115 * @see java.lang.Object#hashCode()
116 */
117 public int hashCode() {
118 return getContent().hashCode();
119 }
120
121 /***
122 * Delegates the toString call to the content object.
123 *
124 * @see java.lang.Object#toString()
125 */
126 public String toString() {
127 return getContent().toString();
128 }
129
130
131 }