1 64 65 package com.jcorporate.expresso.services.controller; 66 67 import com.jcorporate.expresso.core.controller.Controller; 68 import com.jcorporate.expresso.core.controller.ControllerException; 69 import com.jcorporate.expresso.core.controller.ControllerRequest; 70 import com.jcorporate.expresso.core.controller.ControllerResponse; 71 import com.jcorporate.expresso.core.controller.Transition; 72 73 import java.util.Stack ; 74 75 98 public class WizardFormStack implements java.io.Serializable { 99 100 103 protected static final String MY_KEY = WizardFormStack.class.getName() + ".key"; 104 105 109 public static final String BACK_PARAM = "back"; 110 111 114 protected static Stack formStack = new Stack (); 115 116 119 protected Transition current; 120 121 125 protected WizardFormStack() { 126 } 127 128 135 public static synchronized WizardFormStack getInstance(ControllerRequest request) throws ControllerException { 136 WizardFormStack me = (WizardFormStack) request.getSession().getPersistentAttribute(MY_KEY); 137 if (me == null) { 138 me = new WizardFormStack(); 139 request.getSession().setPersistentAttribute(MY_KEY, me); 140 } 141 142 return me; 143 } 144 145 151 public synchronized Transition getPreviousTransition() { 152 if (formStack.size() > 0) { 153 return (Transition) formStack.peek(); 154 } else { 155 return null; 156 } 157 } 158 159 163 public synchronized void reset() { 164 current = null; 165 formStack = new Stack (); 166 } 167 168 176 public synchronized void processNewState(Controller c, ControllerRequest request, ControllerResponse response) throws ControllerException { 177 if ("true".equals(request.getParameter(BACK_PARAM))) { 178 if (formStack.size() == 0) { 179 throw new IllegalStateException ("There are no more previous states"); 180 } 181 current = (Transition) formStack.pop(); 182 } else { 183 if (current != null && current.getState().equals(request.getParameter(Controller.STATE_PARAM_KEY))) { 184 return; 187 } 188 189 if (current != null) { 190 formStack.push(current); 191 } 192 193 String curState = request.getParameter(Controller.STATE_PARAM_KEY); 194 if (curState == null) { 195 curState = c.getInitialState(); 196 } 197 Transition back = new Transition(curState, c); 198 back.addParam("back", "true"); 199 back.setName("back"); 200 current = back; 201 } 202 } 203 204 205 215 public synchronized void pushNewTransition(Transition t, 216 ControllerRequest request, 217 ControllerResponse response) throws ControllerException { 218 219 if (current != null) { 220 formStack.push(current); 221 } 222 223 t.addParam("back", "true"); 224 t.setName("back"); 225 current = t; 226 } 227 228 229 } | Popular Tags |