1 16 package com.google.gwt.user.client.ui; 17 18 import com.google.gwt.core.client.GWT; 19 import com.google.gwt.user.client.Command; 20 import com.google.gwt.core.client.GWT.UncaughtExceptionHandler; 21 import com.google.gwt.user.client.DOM; 22 import com.google.gwt.user.client.DeferredCommand; 23 import com.google.gwt.user.client.Element; 24 import com.google.gwt.user.client.Event; 25 import com.google.gwt.user.client.ui.impl.FormPanelImpl; 26 import com.google.gwt.user.client.ui.impl.FormPanelImplHost; 27 28 56 public class FormPanel extends SimplePanel implements FiresFormEvents, 57 FormPanelImplHost { 58 59 64 public static final String ENCODING_MULTIPART = "multipart/form-data"; 65 66 70 public static final String ENCODING_URLENCODED = "application/x-www-form-urlencoded"; 71 72 76 public static final String METHOD_GET = "get"; 77 78 83 public static final String METHOD_POST = "post"; 84 85 private static int formId = 0; 86 private static FormPanelImpl impl = (FormPanelImpl) GWT.create(FormPanelImpl.class); 87 88 private FormHandlerCollection formHandlers; 89 private String frameName; 90 private Element iframe; 91 92 111 public FormPanel() { 112 super(DOM.createForm()); 113 114 frameName = "FormPanel_" + (++formId); 115 setTarget(frameName); 116 117 sinkEvents(Event.ONLOAD); 118 } 119 120 132 public FormPanel(NamedFrame frameTarget) { 133 this(frameTarget.getName()); 134 } 135 136 150 public FormPanel(String target) { 151 super(DOM.createForm()); 152 setTarget(target); 153 } 154 155 public void addFormHandler(FormHandler handler) { 156 if (formHandlers == null) { 157 formHandlers = new FormHandlerCollection(); 158 } 159 formHandlers.add(handler); 160 } 161 162 168 public String getAction() { 169 return DOM.getElementProperty(getElement(), "action"); 170 } 171 172 178 public String getEncoding() { 179 return impl.getEncoding(getElement()); 180 } 181 182 188 public String getMethod() { 189 return DOM.getElementProperty(getElement(), "method"); 190 } 191 192 199 public String getTarget() { 200 return DOM.getElementProperty(getElement(), "target"); 201 } 202 203 public boolean onFormSubmit() { 204 UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler(); 205 if (handler != null) { 206 return onFormSubmitAndCatch(handler); 207 } else { 208 return onFormSubmitImpl(); 209 } 210 } 211 212 public void onFrameLoad() { 213 UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler(); 214 if (handler != null) { 215 onFrameLoadAndCatch(handler); 216 } else { 217 onFrameLoadImpl(); 218 } 219 } 220 221 public void removeFormHandler(FormHandler handler) { 222 if (formHandlers != null) { 223 formHandlers.remove(handler); 224 } 225 } 226 227 233 public void setAction(String url) { 234 DOM.setElementProperty(getElement(), "action", url); 235 } 236 237 243 public void setEncoding(String encodingType) { 244 impl.setEncoding(getElement(), encodingType); 245 } 246 247 253 public void setMethod(String method) { 254 DOM.setElementProperty(getElement(), "method", method); 255 } 256 257 266 public void submit() { 267 if (formHandlers != null) { 270 if (formHandlers.fireOnSubmit(this)) { 271 return; 272 } 273 } 274 275 impl.submit(getElement(), iframe); 276 } 277 278 protected void onAttach() { 279 super.onAttach(); 280 281 createFrame(); 283 DOM.appendChild(RootPanel.getBodyElement(), iframe); 284 285 impl.hookEvents(iframe, getElement(), this); 291 } 292 293 protected void onDetach() { 294 super.onDetach(); 295 296 impl.unhookEvents(iframe, getElement()); 298 299 DOM.removeChild(RootPanel.getBodyElement(), iframe); 300 iframe = null; 301 } 302 303 private void createFrame() { 304 Element dummy = DOM.createDiv(); 309 DOM.setInnerHTML(dummy, "<iframe name='" + frameName 310 + "' style='width:0;height:0;border:0'>"); 311 312 iframe = DOM.getFirstChild(dummy); 313 } 314 315 private boolean onFormSubmitAndCatch(UncaughtExceptionHandler handler) { 316 try { 317 return onFormSubmitImpl(); 318 } catch (Throwable e) { 319 handler.onUncaughtException(e); 320 return false; 321 } 322 } 323 324 private boolean onFormSubmitImpl() { 325 if (formHandlers != null) { 326 return !formHandlers.fireOnSubmit(this); 328 } 329 330 return true; 331 } 332 333 private void onFrameLoadAndCatch(UncaughtExceptionHandler handler) { 334 try { 335 onFrameLoadImpl(); 336 } catch (Throwable e) { 337 handler.onUncaughtException(e); 338 } 339 } 340 341 private void onFrameLoadImpl() { 342 if (formHandlers != null) { 343 DeferredCommand.addCommand(new Command() { 348 public void execute() { 349 formHandlers.fireOnComplete(this, impl.getContents(iframe)); 350 } 351 }); 352 } 353 } 354 355 private void setTarget(String target) { 356 DOM.setElementProperty(getElement(), "target", target); 357 } 358 } 359 | Popular Tags |