1 7 8 package org.jdesktop.swing.form; 9 10 import java.awt.Cursor ; 11 import java.awt.Image ; 12 import java.util.ArrayList ; 13 import java.util.Calendar ; 14 import java.util.Date ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import javax.swing.Icon ; 21 import javax.swing.JCheckBox ; 22 import javax.swing.JComboBox ; 23 import javax.swing.JComponent ; 24 import javax.swing.JLabel ; 25 import javax.swing.JList ; 26 import javax.swing.JScrollPane ; 27 import javax.swing.JSpinner ; 28 import javax.swing.JTextArea ; 29 import javax.swing.JTextField ; 30 import javax.swing.ListModel ; 31 import javax.swing.SpinnerModel ; 32 import javax.swing.SpinnerNumberModel ; 33 34 import org.jdesktop.swing.JXDatePicker; 35 import org.jdesktop.swing.JXImagePanel; 36 import org.jdesktop.swing.JXListPanel; 37 import org.jdesktop.swing.JXRadioGroup; 38 import org.jdesktop.swing.JXTable; 39 import org.jdesktop.swing.LinkHandler; 40 import org.jdesktop.swing.data.DataModel; 41 import org.jdesktop.swing.data.DefaultTableModelExt; 42 import org.jdesktop.swing.data.EnumeratedMetaData; 43 import org.jdesktop.swing.data.Link; 44 import org.jdesktop.swing.data.MetaData; 45 import org.jdesktop.swing.data.NumberMetaData; 46 import org.jdesktop.swing.data.StringMetaData; 47 import org.jdesktop.swing.data.TabularDataModel; 48 import org.jdesktop.swing.data.TabularMetaData; 49 50 71 public class ComponentMap { 72 private Map creatorMap; 73 74 private ComponentCreator fallBackCreator; 75 76 private List assignables; 77 78 private static ComponentMap instance = new ComponentMap(); 79 80 public static ComponentMap getInstance() { 81 return instance; 82 } 83 84 public static void setInstance(ComponentMap map) { 85 instance = map; 86 } 87 93 public JComponent createComponent(MetaData metaData) { 94 ComponentCreator creator = getComponentCreator(metaData); 95 return creator.createComponent(metaData); 96 } 97 98 113 protected ComponentCreator getComponentCreator(MetaData metaData) { 114 ComponentCreator creator = findByClass(metaData.getClass()); 115 if (creator == null) { 116 creator = findByElementClass(metaData.getElementClass()); 117 } 118 if (creator == null) { 119 creator = getFallBackComponentCreator(); 120 } 121 return creator; 122 } 123 124 protected ComponentCreator findByElementClass(Class elementClass) { 125 elementClass = checkForArray(elementClass); 126 List classes = getAssignableClasses(); 127 for (Iterator iter = classes.iterator(); iter.hasNext();) { 128 Class clazz = (Class ) iter.next(); 129 if (clazz.isAssignableFrom(elementClass)) { 130 return findByClass(clazz); 131 } 132 } 133 return findByClass(elementClass); 134 } 135 136 protected Class checkForArray(Class elementClass) { 137 return elementClass.isArray() ? Object [].class : elementClass; 139 } 140 141 protected ComponentCreator findByClass(Class elementClass) { 142 Object mappedObject = getCreatorMap().get(elementClass); 143 return (ComponentCreator) mappedObject; 144 } 145 146 protected ComponentCreator getFallBackComponentCreator() { 147 if (fallBackCreator == null) { 148 fallBackCreator = new LabelCreator(); 149 } 150 return fallBackCreator; 151 } 152 153 protected void initCreators(Map map) { 154 map.put(TabularMetaData.class, new TableCreator()); 155 map.put(EnumeratedMetaData.class, new EnumerationCreator()); 156 map.put(NumberMetaData.class, new NumberCreator()); 157 ComponentCreator textCreator = new TextCreator(); 158 map.put(StringMetaData.class, textCreator); 159 map.put(String .class, textCreator); 160 ComponentCreator booleanCreator = new BooleanCreator(); 161 map.put(Boolean .class, booleanCreator); 162 map.put(boolean.class, booleanCreator); 163 map.put(DataModel.class, new FormCreator()); 164 map.put(DefaultTableModelExt.class, new TableCreator()); 165 ComponentCreator listCreator = new ListCreator(); 166 map.put(ListModel .class, listCreator); 168 ComponentCreator listPanelCreator = new ListPanelCreator(); 169 map.put(List .class, listPanelCreator); 170 map.put(Object [].class, listPanelCreator); 171 map.put(Link.class, new LinkLabelCreator()); 172 ImagePanelCreator imagePanelCreator = new ImagePanelCreator(); 173 map.put(Image .class, imagePanelCreator); 174 map.put(Icon .class, imagePanelCreator); 175 DatePickerCreator datePickerCreator = new DatePickerCreator(); 176 map.put(Date .class, datePickerCreator); 177 map.put(Calendar .class, datePickerCreator); 178 } 179 180 protected void initAssignablesClasses(List assignables) { 181 assignables.add(DataModel.class); 182 assignables.add(DefaultTableModelExt.class); 183 assignables.add(List .class); 184 } 188 189 private List getAssignableClasses() { 191 if (assignables == null) { 192 assignables = new ArrayList (); 193 initAssignablesClasses(assignables); 194 } 195 return assignables; 196 } 197 198 private Map getCreatorMap() { 199 if (creatorMap == null) { 200 creatorMap = new HashMap (); 201 initCreators(creatorMap); 202 } 203 return creatorMap; 204 } 205 206 208 210 public static class ListPanelCreator implements ComponentCreator { 211 212 215 public JComponent createComponent(MetaData metaData) { 216 JXListPanel list = new JXListPanel(); 217 list.setName(metaData.getName()); 218 list.getList().setName(metaData.getName()); 219 return list; 220 } 221 222 } 261 262 264 public static class ListCreator implements ComponentCreator { 265 266 269 public JComponent createComponent(MetaData metaData) { 270 JList list = new JList (); 271 272 JScrollPane pane = new JScrollPane (list); 274 pane.setName(metaData.getName()); 275 list.setName(metaData.getName()); 276 return pane; 277 } 278 } 279 280 public static class TableCreator implements ComponentCreator { 281 282 285 public JComponent createComponent(MetaData metaData) { 286 JXTable table = new JXTable(); 287 JScrollPane pane = new JScrollPane (table); 288 table.setName(metaData.getName()); 289 pane.setName(metaData.getName()); 290 return pane; 291 } 292 } 293 294 297 public static class FormCreator implements ComponentCreator { 298 299 303 public JComponent createComponent(MetaData metaData) { 304 JForm form = new JForm(); 305 form.setName(metaData.getName()); 306 return form; 309 } 310 } 311 312 314 public static class BooleanCreator implements ComponentCreator { 315 316 319 public JComponent createComponent(MetaData metaData) { 320 JCheckBox box = new JCheckBox (); 321 box.setName(metaData.getName()); 322 box.setEnabled(!metaData.isReadOnly()); 324 return box; 325 } 326 } 327 328 333 public class NumberCreator implements ComponentCreator { 334 335 public JComponent createComponent(MetaData metaData) { 336 if (!(metaData instanceof NumberMetaData)) { 337 return getFallBackComponentCreator().createComponent(metaData); 339 } 340 JComponent comp = createComponent((NumberMetaData) metaData); 341 comp.setName(metaData.getName()); 342 return comp; 343 } 344 345 public JComponent createComponent(NumberMetaData numberMetaData) { 346 Class fieldClass = numberMetaData.getElementClass(); 347 Number min = numberMetaData.getMinimum(); 348 Number max = numberMetaData.getMaximum(); 349 if ((min != null) && (max != null)) { 351 SpinnerModel spinnerModel = null; 352 if ((fieldClass == Integer .class) || (fieldClass == Long .class) 353 || (fieldClass == Short .class) 354 || (fieldClass == int.class)) { 355 spinnerModel = new SpinnerNumberModel (min.intValue(), min 356 .intValue(), max.intValue(), 1); 357 } else if ((fieldClass == Float .class) 358 || (fieldClass == Double .class) 359 || (fieldClass == float.class) 360 || (fieldClass == double.class)) { 361 362 spinnerModel = new SpinnerNumberModel (min.doubleValue(), 364 min.doubleValue(), max.doubleValue(), .01); 365 } 366 if (spinnerModel != null) { 367 return new JSpinner (spinnerModel); 368 } 369 } 370 return getFallBackComponentCreator() 372 .createComponent(numberMetaData); 373 } 374 } 375 376 378 public static class TextCreator implements ComponentCreator { 379 382 383 public JComponent createComponent(MetaData metaData) { 384 if (metaData instanceof StringMetaData) { 385 return createComponent((StringMetaData) metaData); 386 } 387 int fieldLength = metaData.getDisplayWidth(); 388 JTextField field = new JTextField ((fieldLength > 0) ? fieldLength 389 : 24); 390 field.setName(metaData.getName()); 391 return field; 392 } 393 394 public JComponent createComponent(StringMetaData stringMetaData) { 395 if (stringMetaData.isMultiLine()) { 396 int columns = stringMetaData.getDisplayWidth(); 397 int rows = Math.min(stringMetaData.getMaxLength() / columns, 5); 398 JTextArea area = new JTextArea (rows, columns); 399 JScrollPane pane = new JScrollPane (area); 400 area.setName(stringMetaData.getName()); 401 pane.setName(stringMetaData.getName()); 402 return pane; 403 } 404 int fieldLength = Math.min(stringMetaData.getDisplayWidth(), 405 stringMetaData.getMaxLength()); 406 JTextField field = new JTextField (fieldLength); 407 field.setName(stringMetaData.getName()); 408 return field; 409 } 410 } 411 412 414 public static class EnumerationCreator implements ComponentCreator { 415 416 public JComponent createComponent(MetaData metaData) { 417 JComponent box = null; 418 if (metaData instanceof EnumeratedMetaData) { 419 box = createComponent((EnumeratedMetaData) metaData); 420 } else { 421 box = new JComboBox (); 422 } 423 box.setName(metaData.getName()); 424 return box; 425 } 426 427 public JComponent createComponent(EnumeratedMetaData enumMetaData) { 428 Object values[] = enumMetaData.getEnumeration(); 429 if (values.length > 3) { 430 return new JComboBox (); 431 } 432 return new JXRadioGroup(values); 434 } 435 } 436 437 440 public static class LabelCreator implements ComponentCreator { 441 442 public JComponent createComponent(MetaData metaData) { 443 JLabel label = new JLabel (); 444 label.setName(metaData.getName()); 445 return label; 446 } 447 } 448 449 451 public static class LinkLabelCreator implements ComponentCreator { 452 453 public JComponent createComponent(MetaData metaData) { 454 JLabel label = new JLabel (); 455 label.setName(metaData.getName()); 456 label.addMouseListener(new LinkHandler()); 457 label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 458 return label; 459 } 460 } 461 462 465 public static class ImagePanelCreator implements ComponentCreator { 466 467 public JComponent createComponent(MetaData metaData) { 468 JXImagePanel label = new JXImagePanel(); 469 label.setName(metaData.getName()); 470 return label; 471 } 472 } 473 474 476 public static class DatePickerCreator implements ComponentCreator { 477 478 public JComponent createComponent(MetaData metaData) { 479 JXDatePicker label = new JXDatePicker(); 480 label.setName(metaData.getName()); 481 return label; 482 } 483 } 484 } 485 | Popular Tags |