1 14 package org.wings; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.plaf.FormCG; 19 20 import javax.swing.event.EventListenerList ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 29 30 42 public class SForm extends SContainer implements LowLevelEventListener { 43 private final transient static Log log = LogFactory.getLog(SForm.class); 44 45 48 public final static String ENC_TYPE_TEXT_PLAIN = "text/plain"; 50 53 public final static String ENC_TYPE_MULTIPART_FORM = "multipart/form-data"; 54 57 public static final String URL_ENCODING = "application/x-www-form-urlencoded"; 58 59 62 private boolean postMethod = true; 63 64 67 private String encType; 68 69 73 private URL action; 74 75 protected final EventListenerList listenerList = new EventListenerList (); 76 77 protected String actionCommand; 78 79 83 private SButton defaultButton; 84 85 91 private static ThreadLocal threadArmedComponents = new ThreadLocal () { 92 protected synchronized Object initialValue() { 93 return new ArrayList (2); 94 } 95 }; 96 97 100 public SForm() { 101 } 102 103 109 public SForm(URL action) { 110 setAction(action); 111 } 112 113 114 120 public SForm(SLayoutManager layout) { 121 super(layout); 122 } 123 124 129 public void setActionCommand(String actionCommand) { 130 this.actionCommand = actionCommand; 131 } 132 133 136 public String getActionCommand() { 137 return actionCommand; 138 } 139 140 146 public void setDefaultButton(SButton defaultButton) { 147 this.defaultButton = defaultButton; 148 } 149 150 153 public SButton getDefaultButton() { 154 return this.defaultButton; 155 } 156 157 176 public void addActionListener(ActionListener listener) { 177 listenerList.add(ActionListener .class, listener); 178 } 179 180 184 public void removeActionListener(ActionListener listener) { 185 listenerList.remove(ActionListener .class, listener); 186 } 187 188 191 protected void fireActionPerformed(String pActionCommand) { 192 ActionEvent e = null; 193 Object [] listeners = listenerList.getListenerList(); 195 for (int i = listeners.length - 2; i >= 0; i -= 2) { 198 if (listeners[i] == ActionListener .class) { 199 if (e == null) { 201 e = new ActionEvent (this, ActionEvent.ACTION_PERFORMED, 202 pActionCommand); 203 } 204 ((ActionListener ) listeners[i + 1]).actionPerformed(e); 205 } 206 } 207 } 208 209 213 214 public final static void addArmedComponent(LowLevelEventListener component) { 215 List armedComponents = (List ) threadArmedComponents.get(); 216 armedComponents.add(component); 217 } 218 219 224 public static void clearArmedComponents() { 225 List armedComponents = (List ) threadArmedComponents.get(); 226 armedComponents.clear(); 227 } 228 229 239 250 public static void fireEvents() { 251 List armedComponents = (List ) threadArmedComponents.get(); 252 try { 253 LowLevelEventListener component; 254 Iterator iterator = armedComponents.iterator(); 257 LinkedList formEvents = null; 258 LinkedList buttonEvents = null; 259 260 while (iterator.hasNext()) { 261 component = (LowLevelEventListener) iterator.next(); 262 267 if (component instanceof SForm) { 268 if (formEvents == null) { 269 formEvents = new LinkedList (); 270 } formEvents.add(component); 272 } else if (component instanceof SAbstractIconTextCompound) { 273 if (buttonEvents == null) { 274 buttonEvents = new LinkedList (); 275 } 276 buttonEvents.add(component); 277 } else { 278 component.fireIntermediateEvents(); 279 } 280 } 281 282 285 if (buttonEvents == null && formEvents != null) { 286 Iterator fit = formEvents.iterator(); 287 while (fit.hasNext()) { 288 SForm form = (SForm) fit.next(); 289 SButton defaultButton = form.getDefaultButton(); 290 if (defaultButton != null) { 291 if (buttonEvents == null) { 292 buttonEvents = new LinkedList (); 293 } 294 buttonEvents.add(defaultButton); 295 } 296 } 297 } 298 299 if (buttonEvents != null) { 300 iterator = buttonEvents.iterator(); 301 while (iterator.hasNext()) { 302 ((SAbstractIconTextCompound) iterator.next()).fireIntermediateEvents(); 303 } 304 } 305 306 if (formEvents != null) { 307 iterator = formEvents.iterator(); 308 while (iterator.hasNext()) { 309 ((SForm) iterator.next()).fireIntermediateEvents(); 310 } 311 } 312 313 iterator = armedComponents.iterator(); 314 while (iterator.hasNext()) { 315 component = (LowLevelEventListener) iterator.next(); 316 if (!(component instanceof SForm || component instanceof SAbstractIconTextCompound)) { 318 component.fireFinalEvents(); 319 } 320 } 321 322 if (buttonEvents != null) { 323 iterator = buttonEvents.iterator(); 324 while (iterator.hasNext()) { 325 ((SAbstractIconTextCompound) iterator.next()).fireFinalEvents(); 326 } 327 buttonEvents.clear(); 328 } 329 330 if (formEvents != null) { 331 iterator = formEvents.iterator(); 332 while (iterator.hasNext()) { 333 ((SForm) iterator.next()).fireFinalEvents(); 334 } 335 formEvents.clear(); 336 } 337 } finally { 338 armedComponents.clear(); 339 } 340 } 341 342 343 348 public void setPostMethod(boolean postMethod) { 349 this.postMethod = postMethod; 350 } 351 352 360 public boolean isPostMethod() { 361 return postMethod; 362 } 363 364 379 public void setEncodingType(String type) { 380 encType = type; 381 } 382 383 392 public String getEncodingType() { 393 if (encType == null) { 394 return detectEncodingType(this); 395 } else { 396 return encType; 397 } 398 } 399 400 protected String detectEncodingType(SContainer pContainer) { 401 for (int i = 0; i < pContainer.getComponentCount(); i++) { 402 SComponent tComponent = pContainer.getComponent(i); 403 404 if (tComponent instanceof SFileChooser) { 405 return ENC_TYPE_MULTIPART_FORM; 406 } else if (tComponent instanceof SContainer) { 407 String tContainerEncoding = detectEncodingType((SContainer) tComponent); 408 409 if (tContainerEncoding != null) { 410 return tContainerEncoding; 411 } 412 } 413 } 414 415 return null; 416 } 417 418 419 public void setAction(URL action) { 420 this.action = action; 421 } 422 423 424 public URL getAction() { 425 return action; 426 } 427 428 429 public RequestURL getRequestURL() { 430 RequestURL addr = super.getRequestURL(); 431 if (getAction() != null) { 432 addr.addParameter(getAction().toString()); } 434 return addr; 435 } 436 437 public void processLowLevelEvent(String action, String [] values) { 438 processKeyEvents(values); 439 440 SForm.addArmedComponent(this); 443 } 444 445 public void fireIntermediateEvents() { 446 } 447 448 public void fireFinalEvents() { 449 fireKeyEvents(); 450 fireActionPerformed(getActionCommand()); 451 } 452 453 454 private boolean epochCheckEnabled = true; 455 456 457 public boolean isEpochCheckEnabled() { 458 return epochCheckEnabled; 459 } 460 461 462 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 463 this.epochCheckEnabled = epochCheckEnabled; 464 } 465 466 public SComponent addComponent(SComponent c, Object constraint, int index) { 467 if (c instanceof SForm) 468 log.warn("WARNING: attempt to nest forms; won't work."); 469 return super.addComponent(c, constraint, index); 470 } 471 472 public void setCG(FormCG cg) { 473 super.setCG(cg); 474 } 475 } 476 | Popular Tags |