1 13 package com.tonbeller.wcf.component; 14 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Locale ; 18 import java.util.Map ; 19 20 import javax.servlet.http.HttpSession ; 21 import javax.servlet.http.HttpSessionBindingEvent ; 22 import javax.servlet.http.HttpSessionBindingListener ; 23 24 import org.apache.log4j.Logger; 25 26 import com.tonbeller.wcf.bookmarks.Bookmarkable; 27 import com.tonbeller.wcf.controller.Controller; 28 import com.tonbeller.wcf.controller.Dispatcher; 29 import com.tonbeller.wcf.controller.DispatcherSupport; 30 import com.tonbeller.wcf.controller.RequestContext; 31 32 36 public abstract class ComponentSupport 37 implements Component, Form, HttpSessionBindingListener , Visible, RoleExprHolder, Bookmarkable { 38 private static Logger logger = Logger.getLogger(ComponentSupport.class); 39 private String id; 40 private String roleExpr; 41 42 private Dispatcher dispatcher = new DispatcherSupport(); 43 private Form form = new FormSupport(); 44 45 private Locale locale; 46 private boolean visible = true; 47 48 private Controller controller; 50 private Component parent; 51 52 private boolean autoValidate; 53 54 58 public ComponentSupport(String id, Component parent) { 59 this.id = id; 60 this.parent = parent; 61 } 62 63 68 public Dispatcher getDispatcher() { 69 return dispatcher; 70 } 71 72 73 80 public void initialize(RequestContext context) throws Exception { 81 logger.info(id); 82 locale = context.getLocale(); 83 controller = Controller.instance(context.getSession()); 84 controller.addRequestListener(this); 85 } 86 87 92 public void destroy(HttpSession session) throws Exception { 93 logger.info(id); 94 dispatcher.clear(); 95 if (controller == null) 96 throw new IllegalStateException ("not initialized"); 97 controller.removeRequestListener(this); 98 controller = null; 99 } 100 101 public void request(RequestContext context) throws Exception { 102 if (autoValidate) 103 validate(context); 104 dispatcher.request(context); 105 } 106 107 110 public void valueBound(HttpSessionBindingEvent e) { 111 } 112 113 116 public void valueUnbound(HttpSessionBindingEvent ev) { 117 try { 118 destroy(ev.getSession()); 119 } catch (Exception ex) { 120 ex.printStackTrace(); 121 logger.error(id, ex); 122 } 123 } 124 125 128 public String getId() { 129 return id; 130 } 131 132 136 public boolean isVisible() { 137 return visible; 138 } 139 140 144 public void setVisible(boolean b) { 145 visible = b; 146 } 147 148 155 public Form getForm() { 156 return form; 157 } 158 159 162 public void setId(String string) { 163 id = string; 164 } 165 166 169 public Locale getLocale() { 170 return locale; 171 } 172 173 176 public String getRoleExpr() { 177 return roleExpr; 178 } 179 180 183 public void setRoleExpr(String roleExpr) { 184 this.roleExpr = roleExpr; 185 } 186 187 192 public Object getBookmarkState(int levelOfDetail) { 193 Map map = new HashMap (); 194 map.put("visible", new Boolean (isVisible())); 195 return map; 196 } 197 198 202 public void setBookmarkState(Object state) { 203 if (!(state instanceof Map )) 204 return; 205 Map map = (Map ) state; 206 Boolean b = (Boolean ) map.get("visible"); 207 if (b != null) 208 setVisible(b.booleanValue()); 209 } 210 211 214 public void addFormListener(FormListener listener) { 215 form.addFormListener(listener); 216 } 217 220 public void removeFormListener(FormListener listener) { 221 form.removeFormListener(listener); 222 } 223 226 public void revert(RequestContext context) { 227 form.revert(context); 228 } 229 233 public boolean validate(RequestContext context) { 234 return form.validate(context); 235 } 236 239 public Component getParent() { 240 return parent; 241 } 242 246 public void setParent(Component parent) { 247 this.parent = parent; 248 } 249 250 258 public void setNextView(String uri) { 259 controller.setNextView(uri); 260 } 261 262 protected String getNextView() { 263 return controller.getNextView(); 264 } 265 266 269 public boolean isAutoValidate() { 270 return autoValidate; 271 } 272 273 276 public void setAutoValidate(boolean autoValidate) { 277 this.autoValidate = autoValidate; 278 } 279 280 public boolean isListeningTo(Map httpParams) { 281 List list = dispatcher.findMatchingListeners(httpParams); 282 return !list.isEmpty(); 283 } 284 } 285 | Popular Tags |