1 15 package org.apache.tapestry.form; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.hivemind.Location; 19 import org.apache.tapestry.AbstractComponent; 20 import org.apache.tapestry.IActionListener; 21 import org.apache.tapestry.IComponent; 22 import org.apache.tapestry.IDirect; 23 import org.apache.tapestry.IForm; 24 import org.apache.tapestry.IMarkupWriter; 25 import org.apache.tapestry.IRender; 26 import org.apache.tapestry.IRequestCycle; 27 import org.apache.tapestry.RenderRewoundException; 28 import org.apache.tapestry.Tapestry; 29 import org.apache.tapestry.TapestryUtils; 30 import org.apache.tapestry.engine.ActionServiceParameter; 31 import org.apache.tapestry.engine.DirectServiceParameter; 32 import org.apache.tapestry.engine.IEngineService; 33 import org.apache.tapestry.engine.ILink; 34 import org.apache.tapestry.listener.ListenerInvoker; 35 import org.apache.tapestry.valid.IValidationDelegate; 36 import org.apache.tapestry.web.WebResponse; 37 38 62 63 public abstract class Form extends AbstractComponent implements IForm, IDirect 64 { 65 private String _name; 66 67 private FormSupport _formSupport; 68 69 private class RenderInformalParameters implements IRender 70 { 71 public void render(IMarkupWriter writer, IRequestCycle cycle) 72 { 73 renderInformalParameters(writer, cycle); 74 } 75 } 76 77 private IRender _renderInformalParameters; 78 79 86 87 public static IForm get(IRequestCycle cycle) 88 { 89 return (IForm) cycle.getAttribute(ATTRIBUTE_NAME); 90 } 91 92 98 99 public boolean isRewinding() 100 { 101 if (!isRendering()) 102 throw Tapestry.createRenderOnlyPropertyException(this, "rewinding"); 103 104 return _formSupport.isRewinding(); 105 } 106 107 112 113 public abstract IEngineService getDirectService(); 114 115 120 121 public abstract IEngineService getActionService(); 122 123 130 131 public abstract boolean isDirect(); 132 133 139 140 public boolean getRequiresSession() 141 { 142 return isStateful(); 143 } 144 145 155 156 public String getElementId(IFormComponent component) 157 { 158 return _formSupport.getElementId(component, component.getId()); 159 } 160 161 170 171 public String getElementId(IFormComponent component, String baseId) 172 { 173 return _formSupport.getElementId(component, baseId); 174 } 175 176 189 190 public String getName() 191 { 192 return _name; 193 } 194 195 196 197 protected void prepareForRender(IRequestCycle cycle) 198 { 199 super.prepareForRender(cycle); 200 201 TapestryUtils.storeForm(cycle, this); 202 } 203 204 protected void cleanupAfterRender(IRequestCycle cycle) 205 { 206 _formSupport = null; 207 208 TapestryUtils.removeForm(cycle); 209 210 IValidationDelegate delegate = getDelegate(); 211 212 if (delegate != null) 213 delegate.setFormComponent(null); 214 215 super.cleanupAfterRender(cycle); 216 } 217 218 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 219 { 220 String actionId = cycle.getNextActionId(); 221 222 _formSupport = newFormSupport(writer, cycle); 223 224 if (isRewinding()) 225 { 226 String submitType = _formSupport.rewind(); 227 228 IActionListener listener = findListener(submitType); 229 230 getListenerInvoker().invokeListener(listener, this, cycle); 231 232 234 throw new RenderRewoundException(this); 235 } 236 237 240 String baseName = isDirect() ? constructFormNameForDirectService(cycle) 241 : constructFormNameForActionService(actionId); 242 243 _name = baseName + getResponse().getNamespace(); 244 245 if (_renderInformalParameters == null) 246 _renderInformalParameters = new RenderInformalParameters(); 247 248 ILink link = getLink(cycle, actionId); 249 250 _formSupport.render(getMethod(), _renderInformalParameters, link); 251 } 252 253 IActionListener findListener(String mode) 254 { 255 IActionListener result = null; 256 257 if (mode.equals(FormConstants.SUBMIT_CANCEL)) 258 result = getCancel(); 259 else if (mode.equals(FormConstants.SUBMIT_REFRESH)) 260 result = getRefresh(); 261 262 266 if (result == null) 267 result = getListener(); 268 269 return result; 270 } 271 272 278 279 protected String constructFormNameForActionService(String actionId) 280 { 281 return "Form" + actionId; 282 } 283 284 291 292 private String constructFormNameForDirectService(IRequestCycle cycle) 293 { 294 return cycle.getUniqueId(getId()); 295 } 296 297 300 301 protected FormSupport newFormSupport(IMarkupWriter writer, IRequestCycle cycle) 302 { 303 return new FormSupportImpl(writer, cycle, this); 304 } 305 306 311 312 public void addEventHandler(FormEventType type, String functionName) 313 { 314 _formSupport.addEventHandler(type, functionName); 315 } 316 317 322 323 public void rewind(IMarkupWriter writer, IRequestCycle cycle) 324 { 325 render(writer, cycle); 326 } 327 328 333 334 public void trigger(IRequestCycle cycle) 335 { 336 cycle.rewindForm(this); 337 } 338 339 344 345 private ILink getLink(IRequestCycle cycle, String actionId) 346 { 347 if (isDirect()) 348 { 349 Object parameter = new DirectServiceParameter(this); 350 return getDirectService().getLink(cycle, parameter); 351 } 352 353 355 Object parameter = new ActionServiceParameter(this, actionId); 356 357 return getActionService().getLink(cycle, parameter); 358 } 359 360 361 362 public abstract WebResponse getResponse(); 363 364 367 368 public abstract IValidationDelegate getDelegate(); 369 370 371 public abstract IActionListener getListener(); 372 373 374 public abstract IActionListener getCancel(); 375 376 377 public abstract IActionListener getRefresh(); 378 379 380 public abstract String getMethod(); 381 382 383 public abstract boolean isStateful(); 384 385 public void setEncodingType(String encodingType) 386 { 387 _formSupport.setEncodingType(encodingType); 388 } 389 390 391 392 public void addHiddenValue(String name, String value) 393 { 394 _formSupport.addHiddenValue(name, value); 395 } 396 397 398 399 public void addHiddenValue(String name, String id, String value) 400 { 401 _formSupport.addHiddenValue(name, id, value); 402 } 403 404 public void prerenderField(IMarkupWriter writer, IComponent field, Location location) 405 { 406 _formSupport.prerenderField(writer, field, location); 407 } 408 409 public boolean wasPrerendered(IMarkupWriter writer, IComponent field) 410 { 411 return _formSupport.wasPrerendered(writer, field); 412 } 413 414 415 416 public void addDeferredRunnable(Runnable runnable) 417 { 418 _formSupport.addDeferredRunnable(runnable); 419 } 420 421 426 427 public abstract ListenerInvoker getListenerInvoker(); 428 } | Popular Tags |