1 20 package org.enhydra.barracuda.core.comp; 21 22 import java.io.*; 23 import java.net.*; 24 import java.util.*; 25 26 import org.apache.log4j.*; 27 import org.w3c.dom.*; 28 import org.w3c.dom.html.*; 29 30 import org.enhydra.barracuda.core.comp.renderer.*; 31 import org.enhydra.barracuda.core.comp.renderer.html.*; 32 import org.enhydra.barracuda.core.event.*; 33 import org.enhydra.barracuda.core.event.events.*; 34 import org.enhydra.barracuda.core.util.http.URLRewriter; 35 36 37 54 public class BAction extends BComponent { 55 56 protected static final Logger logger = Logger.getLogger(BAction.class.getName()); 58 59 protected ControlEvent actionEvent = null; 61 protected String actionUrl = null; 62 protected List listeners = null; 63 protected Map params = null; 64 protected Collection scriptFunctions = null; protected boolean disableBackButton = false; 66 protected boolean disableFormLocking = false; 68 72 public BAction() {} 73 74 80 public BAction(String iactionUrl) { 81 if (iactionUrl!=null) this.setAction(iactionUrl); 82 } 83 84 90 public BAction(ControlEvent iactionEvent) { 91 if (iactionEvent!=null) this.setAction(iactionEvent); 92 } 93 94 95 99 static { 100 HTMLRendererFactory rfHTML = new HTMLRendererFactory(); 101 installRendererFactory(rfHTML, BAction.class, HTMLElement.class); 102 } 103 104 107 static class HTMLRendererFactory implements RendererFactory { 108 public Renderer getInstance() {return new HTMLActionRenderer();} 109 } 110 111 112 118 public void setAction(String iactionUrl) { 119 this.actionUrl = iactionUrl; 120 this.invalidate(); 121 } 122 123 128 public void setAction(ControlEvent iactionEvent) { 129 this.actionEvent = iactionEvent; 130 this.invalidate(); 131 } 132 133 139 public boolean hasAction() { 140 return (this.actionUrl!=null || this.actionEvent!=null || (this.listeners!=null && this.listeners.size()>0)); 141 } 142 143 150 public String getAction(ViewContext vc) { 151 return this.getAction(vc, false); 152 } 153 154 167 public String getAction(ViewContext vc, boolean preventRewriting) { 168 StringBuffer sb = new StringBuffer (200); 169 String sep = "?"; 170 171 if (this.actionUrl!=null) { 173 if (preventRewriting) sb.append(this.actionUrl); 175 else sb.append(URLRewriter.encodeURL(vc, this.actionUrl)); } else { 177 if (this.actionEvent==null) this.setAction(new ActionEvent()); 179 180 String url = this.actionEvent.getEventURL(); 184 if (preventRewriting) sb.append(url); 185 else sb.append(URLRewriter.encodeURL(vc, url)); sep = ((url.indexOf("?")>-1) ? "&" : "?"); 187 189 if (this.listeners!=null && this.listeners.size()>0) { 191 Iterator it = this.listeners.iterator(); 192 while (it.hasNext()) { 193 ListenerFactory lf = (ListenerFactory) it.next(); 194 sb.append(sep).append(BaseEvent.EVENT_ID).append("=").append(lf.getListenerID()); 195 sep = "&"; 196 } 197 } 198 } 199 200 if (this.params!=null && this.params.size()>0) { 202 Iterator it = this.params.keySet().iterator(); 203 while (it.hasNext()) { 204 Object key = it.next(); 205 Object val = this.params.get(key); 206 if (key!=null && val!=null) { 207 if (val instanceof String []) { 208 String [] vals = (String [])val; 209 for (int i = 0; i < vals.length; i++) { 210 sb.append(sep); 211 sb.append(URLEncoder.encode(key.toString())); 212 sb.append("="); 213 sb.append(URLEncoder.encode(vals[i].toString())); 214 sep = "&"; 215 } 216 } else { 217 sb.append(sep); 218 sb.append(URLEncoder.encode(key.toString())); 219 sb.append("="); 220 sb.append(URLEncoder.encode(val.toString())); 221 } 222 } 223 sep = "&"; 224 } 225 } 226 return sb.toString(); 227 } 228 229 232 public void setParam(String key, String val) { 233 if (this.params==null) this.params = new TreeMap(); 235 this.params.put(key, val); 236 } 237 238 242 public void setParam(String key, String [] val) { 243 if (this.params==null) this.params = new TreeMap(); 245 this.params.put(key, val); 246 } 247 248 251 public Map getParams() { 252 return this.params; 253 } 254 255 268 public void addScriptFunction(String functionName) { 269 if (this.scriptFunctions==null) this.scriptFunctions = new ArrayList(5); if (!this.scriptFunctions.contains(functionName)) { this.scriptFunctions.add(functionName); 272 } 273 } 274 275 282 public Collection getScriptFunctions() { 283 return this.scriptFunctions; 284 } 285 286 292 public void setDisableBackButton(boolean idisableBackButton) { 293 this.disableBackButton = idisableBackButton; 294 } 295 296 301 public boolean getDisableBackButton() { 302 return this.disableBackButton; 303 } 304 305 311 public void setDisableFormLocking(boolean idisableFormLocking) { 312 this.disableFormLocking = idisableFormLocking; 313 } 314 315 321 public boolean getDisableFormLocking() { 322 return this.disableFormLocking; 323 } 324 325 330 public void addEventListener(ListenerFactory lf) { 331 if (lf==null) return; 332 if (this.listeners==null) this.listeners = new ArrayList(5); 333 this.listeners.add(lf); 334 this.invalidate(); 335 } 336 337 342 public void removeEventListener(ListenerFactory lf) { 343 if (lf==null) return; 344 if (this.listeners==null) return; 345 this.listeners.remove(lf); 346 this.invalidate(); 347 } 348 } 349 | Popular Tags |