KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.objectweb.clif.scenario.util.isac.util.tree;
24
25 import java.io.PrintStream JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.apache.log4j.Category;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.window.ApplicationWindow;
33 import org.eclipse.swt.widgets.Display;
34 import org.objectweb.clif.scenario.util.isac.FileName;
35 import org.objectweb.clif.scenario.util.isac.gui.ScenarioGUIEditor;
36 import org.objectweb.clif.scenario.util.isac.loadprofile.LoadProfileManager;
37 import org.objectweb.clif.scenario.util.isac.plugin.PluginManager;
38 import org.objectweb.clif.scenario.util.isac.util.tree.nodes.BehaviorNode;
39 import org.objectweb.clif.scenario.util.isac.util.tree.nodes.ControlerNode;
40 import org.objectweb.clif.scenario.util.isac.util.tree.nodes.NonePluginNode;
41 import org.objectweb.clif.scenario.util.isac.util.xml.Tools;
42 /**
43  * @author JC Meillaud
44  * @author A Peyrard
45  *
46  *
47  */

48 /**
49  * This is the implementation of the object that will be used to manage the
50  * nodes descriptions and the scenario tree
51  *
52  * @author JC Meillaud
53  * @author A Peyrard
54  */

55 public class TreeManager {
56     static Category cat = Category.getInstance(TreeManager.class.getName());
57     /**
58      * The instance of the TreeManager, to get this use the TreeManager getter,
59      * like this we are sure that the instance of tree manager is unique
60      */

61     protected static TreeManager instance = null;
62
63     // this tree is the behaviors tree, to be put in the gui TreeViewer
64
private ScenarioNode tree;
65     private Hashtable JavaDoc nodes;
66     private SyntaxAnalyser analyser;
67     private ScenarioGUIEditor window;
68
69     /**
70      * Build a new TreeManager initialise the syntax analyser, the tree, and the
71      * nodes table
72      */

73     protected TreeManager(ApplicationWindow window) {
74         cat.debug("-> constructor");
75         // initialise the analyser
76
this.analyser = new SyntaxAnalyser(FileName.DTD_SCENARIO);
77         this.nodes = new Hashtable JavaDoc();
78         this.tree = null;
79         this.initTree(NodeDescription.createNonePluginNode(Node.BEHAVIORS));
80         this.window = (ScenarioGUIEditor) window;
81     }
82
83     /**
84      * This static method permit to get an instance of the tree manager
85      *
86      * @return The instance of the tree manager
87      */

88     public static TreeManager getTreeManager(ApplicationWindow window) {
89         cat.debug("-> getTreeManager");
90         if (instance == null) {
91             if (window != null)
92                 instance = new TreeManager(window);
93             else
94                 return null;
95         }
96         return instance;
97     }
98
99     /**
100      * Create a new tree Warning : you will lose the current tree, you could use
101      * the save method in order to print it in a file
102      * @return The new tree created
103      */

104     public ScenarioNode createNewTree() {
105         cat.debug("-> createNewTree");
106         // inititialise the tree
107
ScenarioNode tree = this.initTree(new NodeDescription(Node.BEHAVIORS));
108         
109         return tree ;
110     }
111
112     /**
113      * Init the tree with a specified node
114      *
115      * @param node
116      * The description of the node to be added
117      * @return the tree initialized
118      */

119     private ScenarioNode initTree(NodeDescription node) {
120         cat.debug("-> initTree");
121         // delete the previous tree
122
if (this.tree != null)
123             this.tree.delete();
124         // initialise the table which will store nodes descriptions
125
if (this.nodes != null)
126             this.nodes.clear();
127         // search a new key
128
int keyInt = node.hashCode();
129         // check in the table if the key already exist
130
while (this.nodes.containsKey(new String JavaDoc((new Integer JavaDoc(keyInt))
131                 .toString())))
132             keyInt++;
133         // add the description node in the table
134
this.nodes.put(new String JavaDoc((new Integer JavaDoc(keyInt)).toString()), node);
135         // build the new node, it will be the tree root node
136
this.tree = new ScenarioNode(new String JavaDoc((new Integer JavaDoc(keyInt))
137                 .toString()));
138         return this.tree ;
139     }
140
141     /**
142      * Add a new node, the descritpion of the node must be given in parameter,
143      * and the parent scenario node too.
144      *
145      * @param node
146      * The description of the node to be added
147      * @param parent
148      * The parent scenario node
149      */

150     public ScenarioNode addNode(NodeDescription node, ScenarioNode parent) {
151         cat.debug("-> addNode");
152         // verify that the parent isn't null
153
if (parent == null) {
154             parent = this.tree;
155         }
156         // search a new key
157
int keyInt = node.hashCode();
158         // check in the table if the key already exist
159
while (this.nodes.containsKey(new String JavaDoc((new Integer JavaDoc(keyInt))
160                 .toString())))
161             keyInt++;
162         // add the description node in the table
163
this.nodes.put(new String JavaDoc((new Integer JavaDoc(keyInt)).toString()), node);
164         // build the new node to be added in the tree
165
ScenarioNode nodeLight = new ScenarioNode(new String JavaDoc((new Integer JavaDoc(
166                 keyInt)).toString()));
167         // add the scenario node to the tree
168
parent.addChild(nodeLight);
169
170         return nodeLight;
171     }
172
173     /**
174      * Delete a node of the behaviors tree node
175      *
176      * @param node
177      * The node to be deleted
178      */

179     public void deleteBehaviorsTreeNode(ScenarioNode node) {
180         cat.debug("-> deleteBehaviorsTreeNode");
181         this.deleteNodesDescriptions(node);
182         node.delete();
183     }
184
185     /**
186      * Delete all the descriptions of this node, and it children
187      *
188      * @param node
189      * The parent node
190      */

191     private void deleteNodesDescriptions(ScenarioNode node) {
192         cat.debug("-> deleteNodesDescriptions");
193         this.nodes.remove(node.getKey());
194         Vector JavaDoc children = node.getChildren();
195         if (children == null)
196             return;
197         for (int i = 0; i < children.size(); i++) {
198             deleteNodesDescriptions((ScenarioNode) children.elementAt(i));
199         }
200     }
201
202     /**
203      * Return the type of the node specified in parameter
204      *
205      * @param node
206      * The scenarioNode to be analysed
207      * @return The type of the scenario node
208      */

209     public String JavaDoc getNodeType(ScenarioNode node) {
210         cat.debug("-> getNodeType");
211         if (node == null)
212             return null;
213         // get the key of the node in order to get the description
214
String JavaDoc key = node.getKey();
215         NodeDescription temp = (NodeDescription) this.nodes.get(key);
216         if (temp == null)
217             return Node.UNKNOW;
218         else
219             return temp.getType();
220     }
221
222     /**
223      * Return the action name of the node specified in parameter
224      *
225      * @param node
226      * The scenarioNode to be analysed
227      * @return The action name of the scenario node
228      */

229     private String JavaDoc getNodeActionName(ScenarioNode node) {
230         cat.debug("-> getNodeActionName");
231         // get the key of the node in order to get the description
232
String JavaDoc key = node.getKey();
233         NodeDescription temp = (NodeDescription) this.nodes.get(key);
234         if (temp == null)
235             return Node.UNKNOW;
236         else
237             return temp.getActionName();
238     }
239
240     /**
241      * This method returns the gui key of a node of the behaviors tree
242      *
243      * @param node
244      * The node
245      * @return The gui key of this node
246      */

247     public String JavaDoc getNodeGUIKey(ScenarioNode node) {
248         cat.debug("-> getNodeGUIKey");
249         // get the type of the node
250
String JavaDoc type = this.getNodeType(node);
251         String JavaDoc plugin = this.getNodePlugin(node);
252         String JavaDoc actionName = this.getNodeActionName(node);
253         if ((plugin != null) && (type != null) && (actionName != null)) {
254             PluginManager pluginManager = PluginManager.getPluginManager();
255             return pluginManager
256                     .getPluginActionGUIKey(plugin, type, actionName);
257         }
258         return null;
259     }
260
261     /**
262      * Return the help of the node specified in parameter
263      *
264      * @param node
265      * The scenarioNode to be analysed
266      * @return The help lines of the scenario node
267      */

268     public Vector JavaDoc getNodeHelp(ScenarioNode node) {
269         cat.debug("-> getNodeHelp");
270         PluginManager plugin = PluginManager.getPluginManager();
271         // get the plugin and the action name of the node
272
String JavaDoc actionName = this.getNodeActionName(node);
273         String JavaDoc pluginName = this.getNodePlugin(node);
274         String JavaDoc type = this.getNodeType(node);
275         Vector JavaDoc helpLines = null;
276         if ((pluginName != null) && (actionName != null))
277             helpLines = plugin
278                     .getPluginActionHelp(pluginName, type, actionName);
279         else if (Node.isControllerNode(type)) {
280             helpLines = ControlerNode.getHelp(type);
281         } else if (!Node.isPluginNode(type)) {
282             helpLines = NonePluginNode.getHelp(type);
283         }
284         return helpLines;
285     }
286
287     /**
288      * Return the plugin name of the node specified in parameter
289      *
290      * @param node
291      * The scenarioNode to be analysed
292      * @return The plugin name of the scenario node
293      */

294     private String JavaDoc getNodePlugin(ScenarioNode node) {
295         cat.debug("-> getNodePlugin");
296         // get the key of the node in order to get the description
297
String JavaDoc key = node.getKey();
298         NodeDescription temp = (NodeDescription) this.nodes.get(key);
299         if (temp == null)
300             return Node.UNKNOW;
301         else
302             return temp.getPlugin();
303     }
304
305     /**
306      * Return the id of the plugin used by a selected node
307      *
308      * @param node
309      * The node
310      * @return The id
311      */

312     public String JavaDoc getNodeId(ScenarioNode node) {
313         cat.debug("-> getNodeId");
314         NodeDescription desc = getNodeDescription(node);
315         if (Node.isPluginNode(desc.getType())) {
316             if (desc.getParams() == null)
317                 return null;
318             if (desc.getParams().containsKey("id"))
319                 return (String JavaDoc) desc.getParams().get("id");
320         }
321         return null;
322     }
323
324     /**
325      * Search in the tree all the scenario node of a specified type
326      *
327      * @param type
328      * The type to be search
329      * @param node
330      * The node to be scanned
331      * @return The nodes of the type
332      */

333     private Vector JavaDoc getNodesByType(String JavaDoc type, ScenarioNode node) {
334         cat.debug("-> getNodesByType");
335         Vector JavaDoc result = new Vector JavaDoc();
336         if (node == null)
337             return result;
338         if (type.equals(this.getNodeType(node)))
339             result.add(node);
340         Vector JavaDoc children = node.getChildren();
341         if (children == null)
342             return result;
343         for (int i = 0; i < children.size(); i++)
344             result.addAll(getNodesByType(type, (ScenarioNode) children
345                     .elementAt(i)));
346         return result;
347     }
348
349     /**
350      * Search in nodes descs all the node of a specified type
351      *
352      * @param type
353      * The type to be search
354      * @param the
355      * nodes descs table
356      * @return The nodes desc of the type
357      */

358     private static Vector JavaDoc getNodesDescriptionsByType(String JavaDoc type,
359             Hashtable JavaDoc nodes) {
360         cat.debug("-> getNodesDescriptionsByType");
361         Vector JavaDoc result = new Vector JavaDoc();
362         // check all the elements in the nodes hashtable
363
Enumeration JavaDoc elements = nodes.elements();
364         while (elements.hasMoreElements()) {
365             NodeDescription desc = (NodeDescription) elements.nextElement();
366             if (desc.getType().equals(type))
367                 result.add(desc);
368         }
369         return result;
370     }
371
372     /**
373      * Return a string repreesenting the label of the node
374      *
375      * @param node
376      * The scenario node to be analysed
377      * @return A string representing the node
378      */

379     public String JavaDoc getNodeLabel(ScenarioNode node) {
380         cat.debug("-> getNodeLabel");
381         String JavaDoc type = getNodeType(node);
382         if (Node.USE.equals(type))
383             return new String JavaDoc(type + "." + getNodePlugin(node) + "."
384                     + getNodeActionName(node) + ":" + getNodeId(node));
385         if (Node.isPluginNode(type))
386             return new String JavaDoc(type + "." + getNodePlugin(node) + "."
387                     + getNodeActionName(node));
388         return type;
389     }
390
391     /**
392      * Analyse the plugin which are defined to be used in a nodes descs table
393      *
394      * @param nodes
395      * The nodes descs
396      * @return The plugins name used in the table
397      */

398     public static Hashtable JavaDoc getUsedPlugins(Hashtable JavaDoc nodes) {
399         cat.debug("-> getUsedPlugins");
400         Hashtable JavaDoc result = new Hashtable JavaDoc();
401         // get used plugins
402
Vector JavaDoc nodeUse = getNodesDescriptionsByType(Node.USE, nodes);
403         for (int i = 0; i < nodeUse.size(); i++) {
404             NodeDescription desc = (NodeDescription) nodeUse.elementAt(i);
405             result.put(desc.getParams().get("id"), desc);
406         }
407         return result;
408     }
409
410     /**
411      * Build a vector containig all the types accepted to be a children of the
412      * given node
413      *
414      * @param node
415      * The node to receive children
416      * @return The vector containing the types
417      */

418     public Vector JavaDoc childrenAllowed(ScenarioNode node) {
419         cat.debug("-> childrenAllowed");
420         if (node == null)
421             node = this.tree;
422         // get the table which will store all allowed nodes and them occurences
423
Hashtable JavaDoc childrens = this.analyser.childrenAllowed(this
424                 .getNodeType(node));
425         // build a new empty vector
426
Vector JavaDoc result = new Vector JavaDoc();
427         // get the keys of the table
428
Enumeration JavaDoc keys = childrens.keys();
429         // for each children, which is allow, if the occurence is unique, so
430
// verify that the node
431
// is not containing a node of this type
432
while (keys.hasMoreElements()) {
433             String JavaDoc typeName = (String JavaDoc) keys.nextElement();
434             Character JavaDoc occ = (Character JavaDoc) childrens.get(typeName);
435             switch (occ.charValue()) {
436                 case SyntaxAnalyser.UNIQUE :
437                     if (!hasChildren(node, typeName)) {
438                         result.clear();
439                         result.add(typeName);
440                         return result;
441                     }
442                     break;
443                 case SyntaxAnalyser.MAYBE :
444                     if (!hasChildren(node, typeName)) {
445                         result.add(typeName);
446                     }
447                     break;
448                 default :
449                     result.add(typeName);
450             }
451         }
452         // return the vector builded
453
return result;
454     }
455
456     /**
457      * Set the parameters values of a selected node
458      *
459      * @param node
460      * The node which has parameters values changed
461      * @param values
462      * The values to be setted
463      * @return true if the parameters are well set
464      */

465     public boolean setParametersValues(ScenarioNode node, Hashtable JavaDoc values) {
466         cat.debug("-> setParametersValues");
467         boolean flag = true;
468         NodeDescription desc = this.getNodeDescription(node);
469         if (desc != null) {
470             if (desc.getParams().equals(values)) {
471                 cat.warn("NONE CHANGE HAVE BEEN MADE SO DON'T MAKE ANYTHING...*************") ;
472                 return true ;
473             }
474             // if the node is a session object node
475
if (desc.getType().equals(Node.USE)) {
476                 // test if the id already exist if there is a field id
477
String JavaDoc newId = (String JavaDoc) values.get("id");
478                 String JavaDoc oldId = (String JavaDoc) desc.getParams().get("id");
479                 if (!newId.equals(oldId)) {
480                     if (getPluginObjectByName(null).contains(newId)) {
481                         // open an error dialog, because the identifiant
482
// must be unique
483
MessageDialog.openError(Display.getCurrent()
484                                 .getActiveShell(), "Editing error",
485                                 "Error the id \"" + newId
486                                         + "\" is already defined !");
487                         values.remove("id");
488                         values.put("id", oldId);
489                         flag = false;
490                     } else {
491                         changeIdName(oldId, newId, desc.getPlugin());
492                         // refresh the tree viewer
493
this.window.getTreeViewer().refresh() ;
494                     }
495                 }
496             }
497             if (desc.getType().equals(Node.BEHAVIOR)) {
498                 // test if the id already exist if there is a field id
499
String JavaDoc newId = (String JavaDoc) values.get("id");
500                 String JavaDoc oldId = (String JavaDoc) desc.getParams().get("id");
501                 if (!newId.equals(oldId)) {
502                     if (getBehaviorsIds().contains(newId)) {
503                         // open an error dialog, because the identifiant
504
// must be unique
505
MessageDialog.openError(Display.getCurrent()
506                                 .getActiveShell(), "Editing error",
507                                 "Error the id \"" + newId
508                                         + "\" is already defined !");
509                         values.remove("id");
510                         values.put("id", oldId);
511                         flag = false;
512                     }
513                     else {
514                         // change the id in the loadDrawablePart
515
LoadProfileManager.getInstance().changeBehaviorId(oldId, newId) ;
516                     }
517                 }
518             }
519             desc.setParams(values);
520             // set the saved state
521
this.window.setScenarioSavedState(false) ;
522             return flag;
523         }
524         return false;
525     }
526
527     /**
528      * Check if the given behaviors tree node has got a child node of the given
529      * type
530      *
531      * @param node
532      * The node to be analysed
533      * @param type
534      * The type which we are looking for
535      * @return True is the type has be found
536      */

537     private boolean hasChildren(ScenarioNode node, String JavaDoc type) {
538         cat.debug("-> hasChildren");
539         Vector JavaDoc children = node.getChildren();
540         // analyse each children types
541
for (int i = 0; i < children.size(); i++) {
542             ScenarioNode tempChild = (ScenarioNode) children.elementAt(i);
543             if (type.equals(this.getNodeType(tempChild)))
544                 return true;
545         }
546         // no children has the same type as the given type
547
return false;
548     }
549
550     /**
551      * Change the place of a child of a selected node
552      *
553      * @param child
554      * The child node
555      * @param upper
556      * True if the node need to be upper, false -> lower
557      */

558     public void changeChildPlace(ScenarioNode child, boolean upper) {
559         cat.debug("-> changeChildPlace");
560         ScenarioNode parent = child.getParent();
561         String JavaDoc key = child.getKey();
562         // change the place
563
if (upper)
564             parent.childUpper(key);
565         else
566             parent.childLower(key);
567     }
568
569     /**
570      * Return the node description of a behaviors tree node
571      *
572      * @param node
573      * The tree node
574      * @return The node description
575      */

576     public NodeDescription getNodeDescription(ScenarioNode node) {
577         cat.debug("-> getNodeDescription");
578         if (node == null)
579             return null;
580         String JavaDoc key = node.getKey();
581         return (NodeDescription) this.nodes.get(key);
582     }
583
584     /**
585      * Return the tree
586      *
587      * @return The tree
588      */

589     public ScenarioNode getTree() {
590         cat.debug("-> getTree");
591         return tree;
592     }
593
594     /**
595      * This method return the session objects which is an implementation of the
596      * plugin object selected
597      *
598      * @param name
599      * The plugin name
600      * @return The id of the session objects
601      */

602     public Vector JavaDoc getPluginObjectByName(String JavaDoc name) {
603         cat.debug("-> getPluginObjectByName");
604         // build the result vector
605
Vector JavaDoc ids = new Vector JavaDoc();
606
607         Enumeration JavaDoc elements = this.nodes.elements();
608         // for each node, test if it's a session object, and if it the right
609
// plugin
610
while (elements.hasMoreElements()) {
611             NodeDescription desc = (NodeDescription) elements.nextElement();
612             if (desc.getType().equals(Node.USE)) {
613                 if (name != null) {
614                     if (desc.getPlugin().equals(name)) {
615                         String JavaDoc id = (String JavaDoc) desc.getParams().get("id");
616                         ids.add(id);
617                     }
618                 }
619                 else {
620                     String JavaDoc id = (String JavaDoc) desc.getParams().get("id");
621                     ids.add(id);
622                 }
623             }
624         }
625         return ids;
626     }
627
628     /**
629      * Get the behaviors ids
630      *
631      * @return The behaviors ids
632      */

633     public Vector JavaDoc getBehaviorsIds() {
634         cat.debug("-> getBehaviorsIds");
635         // build the result vector
636
Vector JavaDoc ids = new Vector JavaDoc();
637
638         Enumeration JavaDoc elements = this.nodes.elements();
639         // for each node, test if it's a behavior
640
while (elements.hasMoreElements()) {
641             NodeDescription desc = (NodeDescription) elements.nextElement();
642             if (desc.getType().equals(Node.BEHAVIOR)) {
643                 String JavaDoc id = (String JavaDoc) desc.getParams().get("id");
644                 ids.add(id);
645             }
646         }
647         return ids;
648     }
649
650     /**
651      * Return the plugin used
652      *
653      * @return The plugins used names
654      */

655     public Vector JavaDoc getUsedPluginsName() {
656         cat.debug("-> getUsedPluginsName");
657         // build the result vector
658
Vector JavaDoc names = new Vector JavaDoc();
659         Enumeration JavaDoc elements = this.nodes.elements();
660         // for each node, test if it's a session object, and if it the right
661
// plugin
662
while (elements.hasMoreElements()) {
663             NodeDescription desc = (NodeDescription) elements.nextElement();
664             if (desc.getType().equals(Node.USE)) {
665                 String JavaDoc name = desc.getPlugin();
666                 if (!names.contains(name))
667                     names.add(desc.getPlugin());
668             }
669         }
670         return names;
671     }
672
673     /**
674      * Create the id of a object session node
675      *
676      * @param node
677      * The node to generate the id
678      */

679     public void idGenerator(NodeDescription node) {
680         cat.debug("-> idGenerator");
681         int hash = node.hashCode();
682         // get the params
683
Hashtable JavaDoc params = node.getParams();
684         // change the id param value
685
params.remove("id");
686         params.put("id", new String JavaDoc("plug" + hash));
687         // set the parameters
688
node.setParams(params);
689     }
690
691     /**
692      * Set the plugin and the test names, of the condition of this controler
693      * node
694      *
695      * @param node
696      * The controler node
697      * @param plugin
698      * The plugin name
699      * @param test
700      * The test name
701      */

702     public void setNodeCondition(ScenarioNode node, String JavaDoc plugin, String JavaDoc test) {
703         cat.debug("-> setNodeCondition");
704         // get the node description
705
NodeDescription desc = getNodeDescription(node);
706         // verify that is a controler node
707
if (!Node.isControllerNode(desc.getType()))
708             return;
709         // set the plugin and the test names
710
desc.setPlugin(plugin);
711         desc.setActionName(test);
712     }
713
714     /**
715      * Copy the scenario node with new ids, and create new nodes descs for the
716      * scenario nodes created
717      *
718      * @param node
719      * The node to copy
720      * @return The node copied
721      */

722     public ScenarioNode copyScenarioNode(ScenarioNode node) {
723         cat.debug("-> copyScenarioNode");
724         if (node == null)
725             return null;
726         // get the key, and create a new one
727
String JavaDoc key = node.getKey();
728         int intKey = new Integer JavaDoc(key).intValue();
729         while (this.nodes.containsKey(new Integer JavaDoc(intKey).toString()))
730             intKey++;
731         String JavaDoc newKey = new Integer JavaDoc(intKey).toString();
732
733         // create the scenario node copy
734
ScenarioNode result = new ScenarioNode(newKey);
735         NodeDescription newDesc = new NodeDescription(
736                 (NodeDescription) this.nodes.get(key));
737         // if the node is a plugin session object
738
if (newDesc.getType().equals(Node.USE)) {
739             // generate a new id for the node
740
this.idGenerator(newDesc);
741         }
742         if (newDesc.getType().equals(Node.BEHAVIOR)) {
743             // generate a new id
744
String JavaDoc newId = BehaviorNode.idGenerator() ;
745             Hashtable JavaDoc params = newDesc.getParams() ;
746             // change the id
747
params.remove("id") ;
748             params.put("id", newId) ;
749         }
750         this.nodes.put(newKey, newDesc);
751
752         // copy each child
753
Vector JavaDoc children = node.getChildren();
754         for (int i = 0; i < children.size(); i++) {
755             ScenarioNode child = copyScenarioNode((ScenarioNode) children
756                     .elementAt(i));
757             if (child != null)
758                 result.addChild(child);
759         }
760         return result;
761     }
762
763     /**
764      * Change the id of a session object in the plugin desc which are using it
765      *
766      * @param oldId
767      * The old id
768      * @param newId
769      * The new Id
770      * @param pluginName
771      * The session object plugin name
772      */

773     private void changeIdName(String JavaDoc oldId, String JavaDoc newId, String JavaDoc pluginName) {
774         cat.debug("-> changeIdName");
775         // get the nodes description
776
Enumeration JavaDoc elements = this.nodes.elements();
777         // search nodesdesc using the plugin selected
778
while (elements.hasMoreElements()) {
779             NodeDescription desc = (NodeDescription) elements.nextElement();
780             String JavaDoc plugin = desc.getPlugin();
781             if (plugin == null)
782                 continue;
783             if (plugin.equals(pluginName)) {
784                 Hashtable JavaDoc params = desc.getParams();
785                 String JavaDoc id = (String JavaDoc) params.get("id");
786                 if (id.equals(oldId)) {
787                     // remove the old id
788
params.remove("id");
789                     // set the new one
790
params.put("id", newId);
791                 }
792             }
793         }
794     }
795
796     /**
797      * Save the Scenario in a file
798      *
799      * @param out
800      * The output Stream to save the scenario in a file
801      */

802     public void saveScenario(PrintStream JavaDoc out) {
803         cat.debug("-> saveBehaviors");
804         Vector JavaDoc errors=new Vector JavaDoc();
805         int ind = 0;
806         try {
807             cat.debug("Begin of a Scenario Saving");
808             Tools.printXMLLine(out, "<"+Node.SCENARIO+">", ind);
809             Tools.printXMLLine(out, "<"+Node.BEHAVIORS+">", ind+2);
810             SaveScenario.saveBehaviors(this.tree, out, ind+4,errors);
811             Tools.printXMLLine(out, "</"+Node.BEHAVIORS+">", ind+2);
812             Tools.printXMLLine(out, "<"+Node.LOAD_PROFILE+">", ind+2);
813             SaveScenario.saveLoadProfile(this.tree, out, ind+4,errors);
814             Tools.printXMLLine(out, "</"+Node.LOAD_PROFILE+">", ind+2);
815             Tools.printXMLLine(out, "</"+Node.SCENARIO+">", ind);
816             cat.debug("End of a Scenario Saving");
817             cat.debug(errors);
818         } catch (Exception JavaDoc e) {
819             cat.debug("Error in the tree manager:saveBehaviors");
820             e.printStackTrace();
821         }
822     }
823
824     /**
825      * @return Returns the nodes.
826      */

827     public Hashtable JavaDoc getNodes() {
828         return nodes;
829     }
830
831     /**
832      * @param tree
833      * The tree to set.
834      */

835     public void setTree(ScenarioNode tree) {
836         this.tree = tree;
837     }
838 }
Popular Tags