View Javadoc

1   
2   
3   package com.sri.emo.wizard.branch;
4   
5   import java.util.Map;
6   import java.util.HashMap;
7   import com.sri.emo.wizard.WizardPage;
8   import java.util.Collection;
9   import java.util.*;
10  
11  /***
12   * <p>Title: </p>
13   *
14   * <p>Description: </p>
15   *
16   * <p>Copyright: Copyright (c) 2003</p>
17   *
18   * <p>Company: </p>
19   *
20   * @author not attributable
21   * @version 1.0
22   */
23  public class BranchNode {
24      public static final String DEFAULT_TRANSITION = "DEFAULT_TR";
25      Map map;
26      WizardPage page;
27      String transitionKey;
28  
29      public BranchNode(WizardPage page, String transitionKey) {
30          if(page == null){
31              throw new IllegalArgumentException("wizard page must not be null within BranchNode");
32          }
33          this.page = page;
34          this.transitionKey = transitionKey;
35          map = new HashMap();
36      }
37  
38      public WizardPage getPage(){
39          return page;
40      }
41  
42      public List getAllNodes(){
43          List list = new Vector(5);
44          Collection currentNodes = getAllTransitions();
45  //        addNodeIfNotExist(list, currentNodes);
46          list.addAll(currentNodes);
47          Iterator iter = currentNodes.iterator();
48          while (iter.hasNext()) {
49              BranchNode item = (BranchNode) iter.next();
50              addNodeIfNotExist(list, item.getAllNodes());
51  //            list.addAll(item.getAllNodes());
52          }
53          return list;
54      }
55  
56      private static void addNodeIfNotExist(List list, Collection branchNodes){
57          Iterator iter = list.iterator();
58          while (iter.hasNext()) {
59              BranchNode item = (BranchNode) iter.next();
60              if(!branchNodes.contains(item)){
61                  list.add(item);
62              }
63          }
64      }
65  
66      public BranchNode getNext(String transition){
67          BranchNode next = (BranchNode)map.get(transition);
68          if(next == null){
69              return (BranchNode)map.get(DEFAULT_TRANSITION);
70          }
71          return next;
72      }
73  
74      public BranchNode getDefault(){
75          return (BranchNode)map.get(DEFAULT_TRANSITION);
76      }
77  
78      public Collection getAllTransitions(){
79          return map.values();
80      }
81  
82      public boolean addDefault(WizardPage node){
83          return map.put(DEFAULT_TRANSITION, new BranchNode(node, DEFAULT_TRANSITION)) == null;
84      }
85  
86  
87      public boolean addNext(String transition, String transitionKey, WizardPage node){
88          return addNext(transition, new BranchNode(node, transitionKey));
89      }
90  
91      public boolean hasTransition(String transition){
92          return map.containsKey(transition);
93      }
94  
95      public boolean addNext(String transition, BranchNode node){
96          return map.put(transition, node)==null;
97      }
98  
99      public boolean containsTransitionTo(BranchNode node){
100 //        Iterator iter = map.values().iterator();
101 //        System.out.println("BranchNode.containsTransitionTo("+node+")");
102 //        while (iter.hasNext()) {
103 //            BranchNode item = (BranchNode) iter.next();
104 //            System.out.println("checking against " + item);
105 //            if(item.equals(node)){
106 //                System.out.println("match found");
107 //                return true;
108 //            }
109 //        }
110 //        return false;
111         return map.containsValue(node);
112     }
113 
114     public String getTransitionKey(){
115         return transitionKey;
116     }
117 
118     public String toString(){
119         StringBuffer b = new StringBuffer();
120         b.append(page.getId());
121         b.append(" branching to ");
122         b.append(map);
123         return b.toString();
124     }
125 
126     public boolean equals(Object o){
127         if(o instanceof BranchNode){
128             BranchNode node = (BranchNode)o;
129 //            System.out.println("BranchNode.equals() this.id = " +
130 //                               page.getId() + ", other.id = " +
131 //                               node.page.getId());
132             return node.page.getId()== page.getId();
133         }
134         return false;
135     }
136 }