1 package com.tonbeller.jpivot.tags; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.Map ; 6 7 11 public class PageStateManager implements StateManager { 12 Map map = new HashMap (); 13 State current; 14 StateLogger logger = new Log4jStateLogger(); 15 16 void showCurrent() throws Exception { 17 if (current != null) { 18 logger.show(current); 19 current.show(); 20 } 21 } 22 23 void hideCurrent() throws Exception { 24 if (current != null) { 25 logger.hide(current); 26 current.hide(); 27 } 28 } 29 30 public void initializeAndShow(State next) throws Exception { 31 hideCurrent(); 32 33 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 50 public void showByName(String name) throws Exception { 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 66 public void destroyAll() throws Exception { 67 hideCurrent(); 68 current = null; 69 for (Iterator 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 80 public void destroyByName(String name) throws Exception { 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 |