1 package com.tonbeller.jpivot.tags; 2 3 import java.util.Iterator ; 4 import java.util.Stack ; 5 6 29 public class StackStateManager implements StateManager { 30 Stack stack = new Stack (); 31 StateLogger logger = new Log4jStateLogger(); 32 33 private boolean stackContainsName(String name) { 34 for (Iterator 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 { 43 State s = getCurrent(); 44 if (s != null) { 45 logger.hide(s); 46 s.hide(); 47 } 48 } 49 50 private void showCurrent() throws Exception { 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 68 public void initializeAndShow(State s) throws Exception { 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 86 public void showByName(String name) throws Exception { 87 if (!stackContainsName(name)) { 88 logger.error("not found in stack: " + name); 89 return; 90 } 91 92 State s = getCurrent(); 94 if (name.equals(s.getName())) 95 return; 96 97 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 { 109 hideCurrent(); 110 while (!stack.isEmpty()) { 111 State s = (State) stack.pop(); 112 logger.destroy(s); 113 s.destroy(); 114 } 115 } 116 117 122 public void destroyByName(String name) throws Exception { 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 |