KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > util > TreeItem


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.util;
19
20
21 /**
22  * Class TreeItem
23  *
24  *
25  * @author Wendel D. de Witte
26  * @version %I%, %G%
27  *
28  *
29  *
30  */

31 public class TreeItem {
32
33    /** Field down */
34    private TreeItem up = null;
35
36    /** Field down */
37    private TreeItem down = null;
38
39    /** Field right */
40    private TreeItem right = null;
41
42    /** Field right */
43    private TreeItem left = null;
44
45    /** Field dataField */
46    private Object JavaDoc dataField = null;
47
48
49    /**
50     * Add a node to the end of the child list for this node
51     *
52     * @param node
53     */

54    public void addChild(TreeItem node) {
55
56       if (node == null) {
57          return;
58       }
59
60       TreeItem t = this.down;
61
62       if (t != null) {
63          // only the most left must point to the parent
64
while (t.right != null) t = t.right;
65          t.right = (TreeItem) node;
66          node.left = t;
67          node.up = null;
68       }
69       else {
70          // don't reset the right pointer;
71
this.down = (TreeItem) node;
72          node.left = null;
73          node.up = this;
74       }
75    }
76
77
78    /**
79     * Get the first child of this node; null if not children
80     *
81     * @return
82     */

83    public TreeItem getFirstChild() {
84       return down;
85    }
86
87
88    /**
89     * Get the next sibling in line after this one
90     *
91     * @return
92     */

93    public TreeItem getNextSibling() {
94       return right;
95    }
96
97
98    /**
99     * Get the next sibling in line after this one
100     *
101     * @return
102     */

103    public TreeItem getPrevSibling() {
104       return left;
105    }
106
107
108    public TreeItem getLastChild() {
109       TreeItem childItem = getFirstChild();
110       while (childItem != null && childItem.getNextSibling() != null)
111          childItem = childItem.getNextSibling();
112       return childItem;
113    }
114
115
116    public TreeItem getParent() {
117       TreeItem tmp = this;
118       while (tmp.getPrevSibling() != null)
119          tmp = tmp.getPrevSibling();
120       return tmp.up;
121    }
122
123
124    public TreeItem getRoot() {
125       TreeItem tmp = this;
126       while (tmp.getParent() != null)
127          tmp = tmp.getParent();
128       return tmp;
129    }
130
131
132    /** Remove all children */
133    public TreeItem disconnectChildren() {
134       TreeItem treeitem = down;
135       if (down != null) {
136          treeitem.up = null;
137          down = null;
138       }
139       return treeitem;
140    }
141
142
143    public TreeItem disconnectLastChild() {
144       TreeItem child = this.getFirstChild();
145       while (child != null) {
146          if (child.getNextSibling() == null) {
147             if (child.left == null)
148                return disconnectChildren();
149
150             return child.left.disconnectSiblings();
151          }
152          child = child.getNextSibling();
153       }
154       return null;
155    }
156
157
158    /** Remove all children */
159    public TreeItem disconnectSiblings() {
160       TreeItem treeitem = right;
161       if (right != null) {
162          right.left = null;
163          right = null;
164       }
165       return treeitem;
166    }
167
168
169    /**
170     * Method setFirstChild
171     *
172     *
173     * @param c
174     *
175     */

176    public TreeItem setFirstChild(TreeItem c) {
177       TreeItem oldChild = disconnectChildren();
178       down = (TreeItem) c;
179       return oldChild;
180    }
181
182
183    /**
184     * Method setNextSibling
185     *
186     * @param n
187     */

188    public TreeItem setNextSibling(TreeItem n) {
189       TreeItem oldSibling = disconnectSiblings();
190       right = n;
191       n.left = this;
192       return oldSibling;
193    }
194
195
196    /**
197     * Method getDataItem
198     *
199     *
200     * @return
201     *
202     */

203    public Object JavaDoc getDataItem() {
204       return dataField;
205    }
206
207
208    /**
209     * Method setDataItem
210     *
211     *
212     * @param dataField
213     *
214     */

215    public void setDataItem(Object JavaDoc dataField) {
216       this.dataField = dataField;
217    }
218
219
220    public String JavaDoc toString() {
221       String JavaDoc buffer = new String JavaDoc();
222       buffer += "up : ";
223       buffer += (up != null) ? "1" : "null";
224       buffer += " down : ";
225       buffer += (down != null) ? "1" : "null";
226       buffer += " right : ";
227       buffer += (right != null) ? "1" : "null";
228       buffer += " left : ";
229       buffer += (left != null) ? "1" : "null";
230       return buffer;
231    }
232 }
233
234 ;
Popular Tags