1 23 package org.objectweb.clif.scenario.util.isac.util.tree; 24 25 import java.io.PrintStream ; 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 import java.util.Vector ; 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 48 55 public class TreeManager { 56 static Category cat = Category.getInstance(TreeManager.class.getName()); 57 61 protected static TreeManager instance = null; 62 63 private ScenarioNode tree; 65 private Hashtable nodes; 66 private SyntaxAnalyser analyser; 67 private ScenarioGUIEditor window; 68 69 73 protected TreeManager(ApplicationWindow window) { 74 cat.debug("-> constructor"); 75 this.analyser = new SyntaxAnalyser(FileName.DTD_SCENARIO); 77 this.nodes = new Hashtable (); 78 this.tree = null; 79 this.initTree(NodeDescription.createNonePluginNode(Node.BEHAVIORS)); 80 this.window = (ScenarioGUIEditor) window; 81 } 82 83 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 104 public ScenarioNode createNewTree() { 105 cat.debug("-> createNewTree"); 106 ScenarioNode tree = this.initTree(new NodeDescription(Node.BEHAVIORS)); 108 109 return tree ; 110 } 111 112 119 private ScenarioNode initTree(NodeDescription node) { 120 cat.debug("-> initTree"); 121 if (this.tree != null) 123 this.tree.delete(); 124 if (this.nodes != null) 126 this.nodes.clear(); 127 int keyInt = node.hashCode(); 129 while (this.nodes.containsKey(new String ((new Integer (keyInt)) 131 .toString()))) 132 keyInt++; 133 this.nodes.put(new String ((new Integer (keyInt)).toString()), node); 135 this.tree = new ScenarioNode(new String ((new Integer (keyInt)) 137 .toString())); 138 return this.tree ; 139 } 140 141 150 public ScenarioNode addNode(NodeDescription node, ScenarioNode parent) { 151 cat.debug("-> addNode"); 152 if (parent == null) { 154 parent = this.tree; 155 } 156 int keyInt = node.hashCode(); 158 while (this.nodes.containsKey(new String ((new Integer (keyInt)) 160 .toString()))) 161 keyInt++; 162 this.nodes.put(new String ((new Integer (keyInt)).toString()), node); 164 ScenarioNode nodeLight = new ScenarioNode(new String ((new Integer ( 166 keyInt)).toString())); 167 parent.addChild(nodeLight); 169 170 return nodeLight; 171 } 172 173 179 public void deleteBehaviorsTreeNode(ScenarioNode node) { 180 cat.debug("-> deleteBehaviorsTreeNode"); 181 this.deleteNodesDescriptions(node); 182 node.delete(); 183 } 184 185 191 private void deleteNodesDescriptions(ScenarioNode node) { 192 cat.debug("-> deleteNodesDescriptions"); 193 this.nodes.remove(node.getKey()); 194 Vector 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 209 public String getNodeType(ScenarioNode node) { 210 cat.debug("-> getNodeType"); 211 if (node == null) 212 return null; 213 String 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 229 private String getNodeActionName(ScenarioNode node) { 230 cat.debug("-> getNodeActionName"); 231 String 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 247 public String getNodeGUIKey(ScenarioNode node) { 248 cat.debug("-> getNodeGUIKey"); 249 String type = this.getNodeType(node); 251 String plugin = this.getNodePlugin(node); 252 String 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 268 public Vector getNodeHelp(ScenarioNode node) { 269 cat.debug("-> getNodeHelp"); 270 PluginManager plugin = PluginManager.getPluginManager(); 271 String actionName = this.getNodeActionName(node); 273 String pluginName = this.getNodePlugin(node); 274 String type = this.getNodeType(node); 275 Vector 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 294 private String getNodePlugin(ScenarioNode node) { 295 cat.debug("-> getNodePlugin"); 296 String 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 312 public String 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 ) desc.getParams().get("id"); 320 } 321 return null; 322 } 323 324 333 private Vector getNodesByType(String type, ScenarioNode node) { 334 cat.debug("-> getNodesByType"); 335 Vector result = new Vector (); 336 if (node == null) 337 return result; 338 if (type.equals(this.getNodeType(node))) 339 result.add(node); 340 Vector 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 358 private static Vector getNodesDescriptionsByType(String type, 359 Hashtable nodes) { 360 cat.debug("-> getNodesDescriptionsByType"); 361 Vector result = new Vector (); 362 Enumeration 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 379 public String getNodeLabel(ScenarioNode node) { 380 cat.debug("-> getNodeLabel"); 381 String type = getNodeType(node); 382 if (Node.USE.equals(type)) 383 return new String (type + "." + getNodePlugin(node) + "." 384 + getNodeActionName(node) + ":" + getNodeId(node)); 385 if (Node.isPluginNode(type)) 386 return new String (type + "." + getNodePlugin(node) + "." 387 + getNodeActionName(node)); 388 return type; 389 } 390 391 398 public static Hashtable getUsedPlugins(Hashtable nodes) { 399 cat.debug("-> getUsedPlugins"); 400 Hashtable result = new Hashtable (); 401 Vector 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 418 public Vector childrenAllowed(ScenarioNode node) { 419 cat.debug("-> childrenAllowed"); 420 if (node == null) 421 node = this.tree; 422 Hashtable childrens = this.analyser.childrenAllowed(this 424 .getNodeType(node)); 425 Vector result = new Vector (); 427 Enumeration keys = childrens.keys(); 429 while (keys.hasMoreElements()) { 433 String typeName = (String ) keys.nextElement(); 434 Character occ = (Character ) 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 result; 454 } 455 456 465 public boolean setParametersValues(ScenarioNode node, Hashtable 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 (desc.getType().equals(Node.USE)) { 476 String newId = (String ) values.get("id"); 478 String oldId = (String ) desc.getParams().get("id"); 479 if (!newId.equals(oldId)) { 480 if (getPluginObjectByName(null).contains(newId)) { 481 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 this.window.getTreeViewer().refresh() ; 494 } 495 } 496 } 497 if (desc.getType().equals(Node.BEHAVIOR)) { 498 String newId = (String ) values.get("id"); 500 String oldId = (String ) desc.getParams().get("id"); 501 if (!newId.equals(oldId)) { 502 if (getBehaviorsIds().contains(newId)) { 503 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 LoadProfileManager.getInstance().changeBehaviorId(oldId, newId) ; 516 } 517 } 518 } 519 desc.setParams(values); 520 this.window.setScenarioSavedState(false) ; 522 return flag; 523 } 524 return false; 525 } 526 527 537 private boolean hasChildren(ScenarioNode node, String type) { 538 cat.debug("-> hasChildren"); 539 Vector children = node.getChildren(); 540 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 return false; 548 } 549 550 558 public void changeChildPlace(ScenarioNode child, boolean upper) { 559 cat.debug("-> changeChildPlace"); 560 ScenarioNode parent = child.getParent(); 561 String key = child.getKey(); 562 if (upper) 564 parent.childUpper(key); 565 else 566 parent.childLower(key); 567 } 568 569 576 public NodeDescription getNodeDescription(ScenarioNode node) { 577 cat.debug("-> getNodeDescription"); 578 if (node == null) 579 return null; 580 String key = node.getKey(); 581 return (NodeDescription) this.nodes.get(key); 582 } 583 584 589 public ScenarioNode getTree() { 590 cat.debug("-> getTree"); 591 return tree; 592 } 593 594 602 public Vector getPluginObjectByName(String name) { 603 cat.debug("-> getPluginObjectByName"); 604 Vector ids = new Vector (); 606 607 Enumeration elements = this.nodes.elements(); 608 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 id = (String ) desc.getParams().get("id"); 616 ids.add(id); 617 } 618 } 619 else { 620 String id = (String ) desc.getParams().get("id"); 621 ids.add(id); 622 } 623 } 624 } 625 return ids; 626 } 627 628 633 public Vector getBehaviorsIds() { 634 cat.debug("-> getBehaviorsIds"); 635 Vector ids = new Vector (); 637 638 Enumeration elements = this.nodes.elements(); 639 while (elements.hasMoreElements()) { 641 NodeDescription desc = (NodeDescription) elements.nextElement(); 642 if (desc.getType().equals(Node.BEHAVIOR)) { 643 String id = (String ) desc.getParams().get("id"); 644 ids.add(id); 645 } 646 } 647 return ids; 648 } 649 650 655 public Vector getUsedPluginsName() { 656 cat.debug("-> getUsedPluginsName"); 657 Vector names = new Vector (); 659 Enumeration elements = this.nodes.elements(); 660 while (elements.hasMoreElements()) { 663 NodeDescription desc = (NodeDescription) elements.nextElement(); 664 if (desc.getType().equals(Node.USE)) { 665 String name = desc.getPlugin(); 666 if (!names.contains(name)) 667 names.add(desc.getPlugin()); 668 } 669 } 670 return names; 671 } 672 673 679 public void idGenerator(NodeDescription node) { 680 cat.debug("-> idGenerator"); 681 int hash = node.hashCode(); 682 Hashtable params = node.getParams(); 684 params.remove("id"); 686 params.put("id", new String ("plug" + hash)); 687 node.setParams(params); 689 } 690 691 702 public void setNodeCondition(ScenarioNode node, String plugin, String test) { 703 cat.debug("-> setNodeCondition"); 704 NodeDescription desc = getNodeDescription(node); 706 if (!Node.isControllerNode(desc.getType())) 708 return; 709 desc.setPlugin(plugin); 711 desc.setActionName(test); 712 } 713 714 722 public ScenarioNode copyScenarioNode(ScenarioNode node) { 723 cat.debug("-> copyScenarioNode"); 724 if (node == null) 725 return null; 726 String key = node.getKey(); 728 int intKey = new Integer (key).intValue(); 729 while (this.nodes.containsKey(new Integer (intKey).toString())) 730 intKey++; 731 String newKey = new Integer (intKey).toString(); 732 733 ScenarioNode result = new ScenarioNode(newKey); 735 NodeDescription newDesc = new NodeDescription( 736 (NodeDescription) this.nodes.get(key)); 737 if (newDesc.getType().equals(Node.USE)) { 739 this.idGenerator(newDesc); 741 } 742 if (newDesc.getType().equals(Node.BEHAVIOR)) { 743 String newId = BehaviorNode.idGenerator() ; 745 Hashtable params = newDesc.getParams() ; 746 params.remove("id") ; 748 params.put("id", newId) ; 749 } 750 this.nodes.put(newKey, newDesc); 751 752 Vector 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 773 private void changeIdName(String oldId, String newId, String pluginName) { 774 cat.debug("-> changeIdName"); 775 Enumeration elements = this.nodes.elements(); 777 while (elements.hasMoreElements()) { 779 NodeDescription desc = (NodeDescription) elements.nextElement(); 780 String plugin = desc.getPlugin(); 781 if (plugin == null) 782 continue; 783 if (plugin.equals(pluginName)) { 784 Hashtable params = desc.getParams(); 785 String id = (String ) params.get("id"); 786 if (id.equals(oldId)) { 787 params.remove("id"); 789 params.put("id", newId); 791 } 792 } 793 } 794 } 795 796 802 public void saveScenario(PrintStream out) { 803 cat.debug("-> saveBehaviors"); 804 Vector errors=new Vector (); 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 e) { 819 cat.debug("Error in the tree manager:saveBehaviors"); 820 e.printStackTrace(); 821 } 822 } 823 824 827 public Hashtable getNodes() { 828 return nodes; 829 } 830 831 835 public void setTree(ScenarioNode tree) { 836 this.tree = tree; 837 } 838 } | Popular Tags |