KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.tonbeller.jpivot.tags;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 /**
8  * @author av
9  * @since 15.02.2005
10  */

11 public class PageStateManager implements StateManager {
12   Map JavaDoc map = new HashMap JavaDoc();
13   State current;
14   StateLogger logger = new Log4jStateLogger();
15
16   void showCurrent() throws Exception JavaDoc {
17     if (current != null) {
18       logger.show(current);
19       current.show();
20     }
21   }
22   
23   void hideCurrent() throws Exception JavaDoc {
24     if (current != null) {
25       logger.hide(current);
26       current.hide();
27     }
28   }
29   
30   public void initializeAndShow(State next) throws Exception JavaDoc {
31     hideCurrent();
32     
33     // remove state with same name from map
34
State prev = (State) map.get(next.getName());
35     if (prev != null) {
36       logger.destroy(prev);
37       prev.destroy();
38     }
39
40     map.put(next.getName(), next);
41     logger.initialize(next);
42     next.initialize();
43     current = next;
44     showCurrent();
45   }
46
47   /**
48    * makes the named state the visible one
49    */

50   public void showByName(String JavaDoc name) throws Exception JavaDoc {
51     State s = (State) map.get(name);
52     if (s == null) {
53       logger.error("could not find state for " + name);
54       return;
55     }
56     if (current != s) {
57       hideCurrent();
58       current = s;
59       showCurrent();
60     }
61   }
62
63   /**
64    * removes and destroys all states
65    */

66   public void destroyAll() throws Exception JavaDoc {
67     hideCurrent();
68     current = null;
69     for (Iterator JavaDoc it = map.values().iterator(); it.hasNext();) {
70       State s = (State) it.next();
71       logger.destroy(s);
72       s.destroy();
73     }
74     map.clear();
75   }
76
77   /**
78    * removes and destroys the named state
79    */

80   public void destroyByName(String JavaDoc name) throws Exception JavaDoc {
81     State s = (State) map.get(name);
82     if (s == null) {
83       logger.error("query " + name + " not found");
84       return;
85     }
86     if (s == current) {
87       hideCurrent();
88       current = null;
89     }
90     logger.destroy(s);
91     s.destroy();
92     map.remove(name);
93   }
94
95   public StateLogger getLogger() {
96     return logger;
97   }
98
99   public void setLogger(StateLogger logger) {
100     this.logger = logger;
101   }
102
103 }
104
Popular Tags