KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > tags > StackStateManager


1 package com.tonbeller.jpivot.tags;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Stack JavaDoc;
5
6 /**
7  * A Stack of states (name/value pairs). For every state name, there is only one value.
8  * States with different names are stacked, every name is on the stack only once. Example:
9  * <p />
10  * <pre>
11  * ps = new StackStateStrategy();
12  * -- a1 is top of stack:
13  * ps.setCurrent("A", a1);
14  * -- replace a1 with a2:
15  * ps.setCurrent("A", a2);
16  * -- push b1 on the stack
17  * ps.setCurrent("B", b1);
18  * -- stack now contains a2, b1
19  * -- push c1 on the stack
20  * ps.setCurrent("C", c1);
21  * -- pop c1 and b1 and make a3 top of stack
22  * ps.setCurrent("A", a3);
23  * -- stack contains a3 only
24  * </pre>
25  *
26  * @author av
27  * @since 15.02.2005
28  */

29 public class StackStateManager implements StateManager {
30   Stack JavaDoc stack = new Stack JavaDoc();
31   StateLogger logger = new Log4jStateLogger();
32
33   private boolean stackContainsName(String JavaDoc name) {
34     for (Iterator JavaDoc it = stack.iterator(); it.hasNext();) {
35       State s = (State) it.next();
36       if (name.equals(s.getName()))
37         return true;
38     }
39     return false;
40   }
41
42   private void hideCurrent() throws Exception JavaDoc {
43     State s = getCurrent();
44     if (s != null) {
45       logger.hide(s);
46       s.hide();
47     }
48   }
49
50   private void showCurrent() throws Exception JavaDoc {
51     State s = getCurrent();
52     if (s != null) {
53       logger.show(s);
54       s.show();
55     }
56   }
57
58   private State getCurrent() {
59     if (stack.isEmpty())
60       return null;
61     return (State) stack.peek();
62   }
63
64   /**
65    * removes all properties from the stack
66    * @throws Exception
67    */

68   public void initializeAndShow(State s) throws Exception JavaDoc {
69     hideCurrent();
70     while (stackContainsName(s.getName())) {
71       State t = (State) stack.pop();
72       logger.destroy(s);
73       t.destroy();
74     }
75     logger.initialize(s);
76     s.initialize();
77     stack.push(s);
78     showCurrent();
79   }
80
81   /**
82    * pops and destroys all states up to but not including the named one. The named
83    * state will become the visible one.
84    * @see #destroyByName
85    */

86   public void showByName(String JavaDoc name) throws Exception JavaDoc {
87     if (!stackContainsName(name)) {
88       logger.error("not found in stack: " + name);
89       return;
90     }
91
92     // already current?
93
State s = getCurrent();
94     if (name.equals(s.getName()))
95       return;
96
97     // unwind stack up to the requested name
98
hideCurrent();
99     while (!name.equals(s.getName())) {
100       State t = (State) stack.pop();
101       logger.destroy(t);
102       t.destroy();
103       s = (State) stack.peek();
104     }
105     showCurrent();
106   }
107
108   public void destroyAll() throws Exception JavaDoc {
109     hideCurrent();
110     while (!stack.isEmpty()) {
111       State s = (State) stack.pop();
112       logger.destroy(s);
113       s.destroy();
114     }
115   }
116
117   /**
118    * pops and destroys all states up to and including the named one. If there is another
119    * state beneath the named one, that will become the visible one.
120    * @see #showByName
121    */

122   public void destroyByName(String JavaDoc name) throws Exception JavaDoc {
123     hideCurrent();
124     while (stackContainsName(name)) {
125       State t = (State) stack.pop();
126       logger.destroy(t);
127       t.destroy();
128     }
129     showCurrent();
130   }
131
132   public StateLogger getLogger() {
133     return logger;
134   }
135
136   public void setLogger(StateLogger logger) {
137     this.logger = logger;
138   }
139
140 }
141
Popular Tags