1 7 8 package org.jdesktop.swing.form; 9 10 import org.jdesktop.swing.data.DataModel; 11 import org.jdesktop.swing.data.JavaBeanDataModel; 12 import org.jdesktop.swing.data.MetaData; 13 import org.jdesktop.swing.data.DefaultTableModelExt; 14 import org.jdesktop.swing.data.TableModelExtAdapter; 15 import org.jdesktop.swing.data.Validator; 16 17 import org.jdesktop.swing.binding.Binding; 18 import org.jdesktop.swing.binding.BindException; 19 20 import org.jdesktop.swing.UIAction; 21 22 import java.awt.Color ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.Insets ; 26 27 import java.awt.event.ActionEvent ; 28 29 import java.beans.IntrospectionException ; 30 31 import java.util.ArrayList ; 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 37 import javax.swing.ActionMap ; 38 import javax.swing.JComponent ; 39 import javax.swing.JLabel ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.JPanel ; 42 43 44 116 117 public class JForm extends JPanel { 118 119 private FormFactory formFactory; 120 private ArrayList bindings; 121 private DataModel model; 122 123 private boolean autoLayout = true; 124 125 private HashMap models; 127 130 public JForm() { 131 initActions(); 132 } 133 134 139 public JForm(DataModel model) throws BindException { 140 initActions(); 141 bind(model); 142 pull(); 143 } 144 145 protected void initActions() { 146 ActionMap map = getActionMap(); 148 map.put("submit", new Actions("submit")); 149 map.put("reset", new Actions("reset")); 150 } 151 152 156 private class Actions extends UIAction { 157 Actions(String name) { 158 super(name); 159 } 160 161 public void actionPerformed(ActionEvent evt) { 162 if ("submit".equals(getName())) { 163 doSubmit(); 164 } 165 else if ("reset".equals(getName())) { 166 doReset(); 167 } 168 } 169 } 170 171 180 public void setAutoLayout(boolean autoLayout) { 181 this.autoLayout = autoLayout; 182 } 183 184 190 public boolean getAutoLayout() { 191 return autoLayout; 192 } 193 194 199 public void setFormFactory(FormFactory factory) { 200 201 this.formFactory = factory; 202 } 203 204 211 public FormFactory getFormFactory() { 212 if (formFactory == null) { 213 return FormFactory.getDefaultFormFactory(); 214 } 215 return formFactory; 216 } 217 218 221 234 public Binding[] bind(DefaultTableModelExt tabularData) throws BindException { 235 return bind(getDataModelWrapper(tabularData)); 236 } 237 238 251 public Binding bind(DefaultTableModelExt tabularData, String columnName) throws BindException { 252 return bind(getDataModelWrapper(tabularData), columnName); 253 } 254 255 267 public Binding[] bind(Object bean) throws BindException { 268 return bind(getDataModelWrapper(bean)); 269 } 270 271 284 public Binding bind(Object bean, String propertyName) throws BindException { 285 return bind(getDataModelWrapper(bean), propertyName); 286 } 287 288 300 public Binding[] bind(DataModel model) throws BindException { 301 String fieldNames[] = model.getFieldNames(); 302 List bindings = new ArrayList (); 303 for(int i = 0; i < fieldNames.length; i++) { 304 Binding binding = bind(model, fieldNames[i]); 305 if (binding != null) { 306 bindings.add(binding); 307 } 308 } 309 return (Binding[]) bindings.toArray(new Binding[bindings.size()]); 310 } 311 312 326 public Binding bind(DataModel model, String fieldName) throws BindException { 327 if (getFormFactory().isNonVisual(model.getMetaData(fieldName))) return null; 328 JComponent component = getFormFactory(). 329 createComponent(model.getMetaData(fieldName)); 330 331 if (component instanceof JForm) { 332 DataModel nestedModel = (DataModel)model.getValue(fieldName); 333 if (nestedModel != null) { 334 JForm nestedForm = (JForm) component; 335 nestedForm.bind(nestedModel); 336 return bind(model, fieldName, nestedForm); 337 } else { 338 throw new BindException(model, fieldName); 339 } 340 } else { 341 return bind(model, fieldName, component); 342 } 343 } 344 345 358 public Binding bind(DataModel model, String fieldName, JComponent component) 359 throws BindException { 360 Binding binding = getFormFactory().createBinding(model, fieldName, component); 361 362 if (binding != null) { 363 return bind(binding, component); 364 } else { 365 throw new BindException("could not create binding for component "+ 366 component.getClass().getName()); 367 } 368 } 369 370 383 public Binding bind(Binding binding, JComponent component) throws BindException { 384 addBinding(binding); 385 if (autoLayout) { 386 DataModel model = binding.getDataModel(); 387 MetaData metaData = model.getMetaData(binding.getFieldName()); 388 getFormFactory().addComponent(this, component, metaData); 389 } 390 return binding; 391 } 392 393 399 protected void addBinding(Binding binding) { 400 if (binding == null) return; 401 if (bindings == null) { 402 bindings = new ArrayList (); 403 } 404 bindings.add(binding); 405 } 406 407 411 public void unbind(Binding binding) { 412 if (bindings != null) { 413 bindings.remove(binding); 414 } 415 } 416 417 421 public Binding[] getBindings() { 422 if (bindings != null) { 423 return (Binding[]) bindings.toArray(new Binding[0]); 424 } 425 return new Binding[0]; 426 } 427 428 434 public boolean pull() { 435 boolean result = true; 436 Binding bindings[] = getBindings(); 437 for (int i = 0; i < bindings.length; i++) { 438 if (!bindings[i].pull()) { 439 result = false; 440 } 441 } 442 return result; 443 } 444 445 450 public boolean isFormValid() { 451 boolean result = true; 452 Binding bindings[] = getBindings(); 453 ArrayList models = new ArrayList (); 454 for (int i = 0; i < bindings.length; i++) { 455 DataModel bindingModel = bindings[i].getDataModel(); 456 if (!models.contains(bindingModel)) { 457 models.add(bindingModel); 458 } 459 if (!bindings[i].isValid()) { 460 result = false; 461 } 462 } 463 if (result) { 464 for(int i = 0; i < models.size(); i++) { 465 DataModel model = (DataModel)models.get(i); 466 Validator validators[] = model.getValidators(); 467 for(int j = 0; j < validators.length; j++) { 468 String error[] = new String [1]; 469 470 if (!validators[j].validate(model, getLocale(), error)) { 471 result = false; 472 } 473 } 474 } 475 476 } 477 return result; 478 } 479 480 486 public boolean isModified() { 487 boolean result = false; 488 Binding bindings[] = getBindings(); 489 for (int i = 0; i < bindings.length; i++) { 490 if (bindings[i].isModified()) { 491 result = true; 492 } 493 } 494 return result; 495 } 496 497 500 public void doSubmit() { 501 if (push()) { 502 executeSubmit(); 503 } 504 else { 505 JOptionPane.showMessageDialog(JForm.this, 506 "Form contains invalid values.\nPlease correct values before submitting.", 507 "Form Submission Error", JOptionPane.ERROR_MESSAGE); 508 } 509 } 510 511 514 public void doReset() { 515 pull(); 516 } 517 518 525 public boolean push() { 526 if (!isFormValid()) { 527 return false; 528 } 529 boolean result = true; 530 Binding bindings[] = getBindings(); 531 for (int i = 0; i < bindings.length; i++) { 532 if (!bindings[i].push()) { 533 result = false; 534 } 535 } 536 return result; 537 } 538 539 544 protected void executeSubmit() { 545 JOptionPane.showMessageDialog(this, 546 "Form submitted.\nThanks", 547 "Form Submission", JOptionPane.PLAIN_MESSAGE); 548 } 549 550 private DataModel getDataModelWrapper(Object model) throws BindException { 551 if (models == null) { 552 models = new HashMap (); 553 } 554 DataModel wrapper = (DataModel)models.get(model); 555 if (wrapper == null) { 556 if (model instanceof DefaultTableModelExt) { 557 wrapper = new TableModelExtAdapter((DefaultTableModelExt)model); 558 } else { 559 try { 560 wrapper = new JavaBeanDataModel(model.getClass(), model); 561 } catch (IntrospectionException e) { 562 throw new BindException(model, e); 563 } 564 } 565 models.put(model, wrapper); 566 } 567 return wrapper; 568 } 569 } 570 | Popular Tags |