KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > util > TreeNode


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * TreeNode.java
28  *
29  * Created on 28 novembre 2005, 12.12
30  *
31  */

32
33 package it.businesslogic.ireport.util;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 /**
39  *
40  * @author Administrator
41  */

42 public class TreeNode {
43     
44     private List JavaDoc childs = new ArrayList JavaDoc();
45     private Object JavaDoc userObject = null;
46     private TreeNode parent = null;
47     
48     /** Creates a new instance of TreeNode */
49     public TreeNode(Object JavaDoc aUserObject) {
50         setUserObject( aUserObject );
51     }
52     
53     public String JavaDoc getName()
54     {
55         return "" + getUserObject();
56     }
57
58     public List JavaDoc getChilds() {
59         return childs;
60     }
61
62     public boolean isLeaf() {
63         return getChilds().isEmpty();
64     }
65
66     public Object JavaDoc getUserObject() {
67         return userObject;
68     }
69
70     public void setUserObject(Object JavaDoc userObject) {
71         this.userObject = userObject;
72     }
73     
74     public void addChild(TreeNode node)
75     {
76         node.setParent( this );
77         getChilds().add(node);
78     }
79     
80     public void addChilds(Collection JavaDoc nodes)
81     {
82         Iterator JavaDoc myIterator = nodes.iterator();
83         while (myIterator.hasNext())
84         {
85             addChild( (TreeNode)(myIterator.next()));
86         }
87     }
88     
89    
90     public boolean moveUp( Object JavaDoc userObject)
91     {
92         if ( ((TreeNode)(getChilds().get(0))).getUserObject() == userObject ) return true;
93         else
94         {
95             for (int i=1; i<getChilds().size(); ++i)
96             {
97                 TreeNode node = (TreeNode)(getChilds().get(i));
98                 if (node.getUserObject() == userObject)
99                 {
100                     int oldPosition = i;
101                     getChilds().remove( i );
102                     getChilds().add( i-1, node );
103                     return true;
104                 }
105             }
106         }
107         
108         // Search for the node in the
109
for (int i=0; i< getChilds().size(); ++i)
110         {
111             TreeNode parent = (TreeNode)getChilds().get(i);
112             if (parent.isLeaf()) continue;
113             else
114             {
115                 if (parent.moveUp( userObject )) return true;
116             }
117         }
118        
119         return false;
120     }
121     
122     public boolean moveDown(Object JavaDoc userObject)
123     {
124         if ( ((TreeNode)(getChilds().get(getChilds().size()-1))).getUserObject() == userObject ) return true;
125         else
126         {
127             for (int i=0; i<getChilds().size()-1; ++i)
128             {
129                 TreeNode node = (TreeNode)(getChilds().get(i));
130                 if (node.getUserObject() == userObject)
131                 {
132                     int oldPosition = i;
133                     getChilds().remove( i );
134                     getChilds().add( i+1, node );
135                     return true;
136                 }
137             }
138         }
139         
140         // Search for the node in the
141
for (int i=0; i< getChilds().size(); ++i)
142         {
143             TreeNode parent = (TreeNode)getChilds().get(i);
144             if (parent.isLeaf()) continue;
145             else
146             {
147                 if (parent.moveDown( userObject )) return true;
148             }
149         }
150        
151         return false;
152     }
153
154     public TreeNode getParent() {
155         return parent;
156     }
157
158     public void setParent(TreeNode parent) {
159         this.parent = parent;
160     }
161     
162 }
163
Popular Tags