View Javadoc

1   /*
2    * Copyright (c) 2003, Andreas Pataki 
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    *
9    * Redistributions of source code must retain the above copyright notice,
10   * this list of conditions and the following disclaimer.
11   * 
12   * Redistributions in binary form must reproduce the above copyright notice,
13   * this list of conditions and the following disclaimer in the documentation
14   * and/or other materials provided with the distribution.
15   * 
16   * Neither the name of the author nor the names of its contributors
17   * may be used to endorse or promote products derived from this software
18   * without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS
21   * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
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 }