KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > util > tree > ScenarioNode


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23
24 package org.objectweb.clif.scenario.util.isac.util.tree;
25
26 import java.util.Vector JavaDoc;
27
28 import org.apache.log4j.Category;
29 /**
30  * This class is the implementation of a scenario node element. This kind of
31  * element will be used to store the scenario in the scenario edition GUI
32  *
33  * @author JC Meillaud
34  * @author A Peyrard
35  */

36 public class ScenarioNode {
37     static Category cat = Category.getInstance(ScenarioNode.class.getName());
38     private String JavaDoc key;
39     private ScenarioNode parent;
40     private Vector JavaDoc children;
41
42     /**
43      * Build a new ScenarioNode object, with attributes key and parent set to
44      * null, and an empty children vector
45      */

46     public ScenarioNode() {
47         cat.debug("-> constructor");
48         this.key = null;
49         this.parent = null;
50         this.children = new Vector JavaDoc();
51     }
52
53     /**
54      * Build a new ScenarioNode object, with a specified key, a null parent and
55      * an empty children vector
56      *
57      * @param key
58      * The key for the node
59      */

60     public ScenarioNode(String JavaDoc key) {
61         cat.debug("-> constructor");
62         this.key = key;
63         this.parent = null;
64         this.children = new Vector JavaDoc();
65     }
66
67     /**
68      * Build a new ScenarioNode object, with specified parent and key, and an
69      * empty children vector
70      *
71      * @param key
72      * The key for the node
73      * @param parent
74      * The parent node
75      */

76     public ScenarioNode(String JavaDoc key, ScenarioNode parent) {
77         cat.debug("-> constructor");
78         this.key = key;
79         this.parent = parent;
80         this.children = new Vector JavaDoc();
81     }
82
83     /**
84      * Attribute key getter
85      *
86      * @return The key, it is the hashtable key which permit to get the full
87      * discribing element
88      */

89     public String JavaDoc getKey() {
90         cat.debug("-> getKey");
91         return this.key;
92     }
93
94     /**
95      * Attribute parent getter
96      *
97      * @return The parent node
98      */

99     public ScenarioNode getParent() {
100         cat.debug("-> getParent");
101         return this.parent;
102     }
103
104     /**
105      * Attribute children getter
106      *
107      * @return The children vector
108      */

109     public Vector JavaDoc getChildren() {
110         cat.debug("-> getChildren");
111         return this.children;
112     }
113
114     /**
115      * Attribute key setter
116      *
117      * @param key
118      * The key of the node
119      */

120     public void setKey(String JavaDoc key) {
121         cat.debug("-> setKey");
122         this.key = key;
123     }
124
125     /**
126      * Attribute parent setter
127      *
128      * @param p
129      * The parent node
130      */

131     public void setParent(ScenarioNode p) {
132         cat.debug("-> setParent");
133         this.parent = p;
134     }
135
136     /**
137      * Attribute children setter
138      *
139      * @param children
140      * The children nodes vector
141      */

142     public void setChildren(Vector JavaDoc children) {
143         cat.debug("-> setChildren");
144         this.children = children;
145     }
146
147     /**
148      * Add a new child to this node
149      *
150      * @param child
151      * The node to be added
152      */

153     public void addChild(ScenarioNode child) {
154         cat.debug("-> addChild");
155         child.setParent(this);
156         this.children.add(child);
157     }
158
159     /**
160      * Remove a child to this node
161      *
162      * @param child
163      * The child to be removed
164      */

165     public void removeChild(ScenarioNode child) {
166         cat.debug("-> removeChild");
167         this.children.remove(child);
168     }
169
170     /**
171      * Evaluate if two nodes are equals
172      *
173      * @param child
174      * The node to be compared with this node
175      * @return True if the two nodes have the same key, else False
176      */

177     public boolean equals(ScenarioNode child) {
178         cat.debug("-> equals");
179         return (child.getKey()).equals(this.key);
180     }
181
182     /**
183      * Delete the node from it parent
184      */

185     public void delete() {
186         cat.debug("-> delete");
187         if (this.parent != null) {
188             this.parent.removeChild(this);
189         }
190     }
191
192     /**
193      * Evaluate if the given node is a sub-node of this node
194      *
195      * @param node
196      * The node which may be a sub-node
197      * @return True if the node is a sub-node, else False
198      */

199     public boolean contains(ScenarioNode node) {
200         cat.debug("-> contains");
201         if (node.equals(this))
202             return true;
203         // explore all children to find a child which contains the node
204
for (int i = 0; i < this.children.size(); i++)
205             if (((ScenarioNode) this.children.elementAt(i)).contains(node))
206                 return true;
207         // we don't find any node equals to the given node
208
return false;
209     }
210
211     /**
212      * Change the child place
213      *
214      * @param key
215      * The key of the child
216      */

217     public void childUpper(String JavaDoc key) {
218         cat.debug("-> childUpper");
219         // search the numero of the child int the children vector
220
int number = 0;
221         for (int i = 0; i < this.children.size(); i++) {
222             if ((((ScenarioNode) this.children.elementAt(i)).getKey())
223                     .equals(key)) {
224                 number = i;
225                 break;
226             }
227         }
228         if (number == 0)
229             return;
230         ScenarioNode child = (ScenarioNode) this.children.elementAt(number);
231         this.children.remove(number);
232         this.children.add(number - 1, child);
233     }
234
235     /**
236      * Change the child place
237      *
238      * @param key
239      * The key of the child
240      */

241     public void childLower(String JavaDoc key) {
242         cat.debug("-> childLower");
243         // search the numero of the child int the children vector
244
int number = this.children.size();
245         for (int i = 0; i < this.children.size(); i++) {
246             if ((((ScenarioNode) this.children.elementAt(i)).getKey())
247                     .equals(key)) {
248                 number = i;
249                 break;
250             }
251         }
252         if (number == this.children.size() - 1)
253             return;
254         ScenarioNode child = (ScenarioNode) this.children.elementAt(number);
255         this.children.remove(number);
256         this.children.add(number + 1, child);
257     }
258
259     /**
260      * Return a string which represent the node This method will be used to
261      * serialize this object *
262      *
263      * @see java.lang.Object#toString()
264      */

265     public String JavaDoc toString() {
266         cat.debug("-> toString");
267         String JavaDoc result = "";
268         result = result.concat("key : " + this.key + "\n");
269         result = result.concat("nb children : " + this.children.size() + "\n");
270         //for (int i=0;i<this.children.size();i++)
271
// result =
272
// result.concat(((ScenarioNode)this.children.elementAt(i)).toString())
273
// ;
274

275         return result;
276     }
277
278     /**
279      * Print test
280      */

281     public void printTest() {
282         cat.warn(TreeManager.getTreeManager(null).getNodeType(this));
283         if (children != null) {
284             cat.warn("[");
285             for (int i = 0; i < children.size(); i++)
286                 ((ScenarioNode) children.elementAt(i)).printTest();
287             cat.warn("]");
288         }
289     }
290 }
Popular Tags