1 7 8 package org.jdesktop.swing.data; 9 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 import java.beans.PropertyChangeSupport ; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 52 53 public class MetaData { 54 protected String name; 55 protected Class klass = String .class; 56 protected String label; 57 protected Converter converter = null; 58 protected Object decodeFormat = null; 59 protected Object encodeFormat = null; 60 protected boolean readOnly = false; 61 protected int minValueCount = 0; protected int maxValueCount = 1; protected int displayWidth = 24; 64 protected ArrayList validators = null; 65 66 protected Map customProps = new HashMap (); 67 protected PropertyChangeSupport pcs; 68 69 76 public MetaData() { 77 this("value"); 78 } 79 80 85 public MetaData(String name) { 86 this.name = name; 87 } 88 89 95 public MetaData(String name, Class klass) { 96 this(name); 97 this.klass = klass; 98 } 99 100 108 public MetaData(String name, Class klass, String label) { 109 this(name, klass); 110 this.label = label; 111 } 112 113 119 public String getName() { 120 return name; 121 } 122 123 128 public void setName(String name) { 129 String oldName = this.name; 130 this.name = name; 131 firePropertyChange("name", oldName, name); 132 } 133 134 141 public Class getElementClass() { 142 return klass; 143 } 144 145 153 public void setElementClass(Class klass) { 154 Class oldClass = this.klass; 155 this.klass = klass; 156 firePropertyChange("elementClass", oldClass, klass); 157 } 158 159 169 public String getLabel() { 170 return label == null? name : label; 171 } 172 173 181 public void setLabel(String label) { 182 String oldLabel = this.label; 183 this.label = label; 184 firePropertyChange("label", oldLabel, label); 185 } 186 187 200 public Converter getConverter() { 201 if (converter == null) { 202 return Converters.get(klass); 203 } 204 return converter; 205 } 206 207 214 public void setConverter(Converter converter) { 215 Converter oldConverter = this.converter; 216 this.converter = converter; 217 firePropertyChange("converter", oldConverter, converter); 218 } 219 220 229 public Object getDecodeFormat() { 230 return decodeFormat; 231 } 232 233 240 public void setDecodeFormat(Object format) { 241 Object oldDecodeFormat = this.decodeFormat; 242 this.decodeFormat = format; 243 firePropertyChange("decodeFormat", oldDecodeFormat, format); 244 } 245 246 255 public Object getEncodeFormat() { 256 return encodeFormat; 257 } 258 259 266 public void setEncodeFormat(Object format) { 267 Object oldEncodeFormat = this.encodeFormat; 268 this.encodeFormat = format; 269 firePropertyChange("encodeFormat", oldEncodeFormat, format); 270 } 271 272 276 public int getDisplayWidth() { 277 return displayWidth; 278 } 279 280 289 public void setDisplayWidth(int displayWidth) { 290 if (displayWidth < 0) { 291 throw new IllegalArgumentException ("displayWidth must be >= 0"); 292 } 293 int oldDisplayWidth = this.displayWidth; 294 this.displayWidth = displayWidth; 295 firePropertyChange("displayWidth", oldDisplayWidth, displayWidth); 296 } 297 298 299 306 public boolean isReadOnly() { 307 return readOnly; 308 } 309 310 315 public void setReadOnly(boolean readOnly) { 316 boolean oldReadOnly = this.readOnly; 317 this.readOnly = readOnly; 318 firePropertyChange("readOnly", oldReadOnly, readOnly); 319 } 320 321 330 public int getMinValueCount() { 331 return minValueCount; 332 } 333 334 339 public void setMinValueCount(int minValueCount) { 340 int oldMinValueCount = this.minValueCount; 341 this.minValueCount = minValueCount; 342 firePropertyChange("minValueCount", oldMinValueCount, minValueCount); 343 } 344 345 351 public boolean isRequired() { 352 return getMinValueCount() > 0; 353 } 354 355 public void setRequired(boolean required) { 356 if (required) { 357 if (getMinValueCount() <= 0) { 358 setMinValueCount(1); 359 } 360 } 361 else { 362 if (getMinValueCount() > 0) { 363 setMinValueCount(0); 364 } 365 } 366 } 367 368 379 public int getMaxValueCount() { 380 return maxValueCount; 381 } 382 383 388 public void setMaxValueCount(int maxValueCount) { 389 int oldMaxValueCount = this.maxValueCount; 390 this.maxValueCount = maxValueCount; 391 firePropertyChange("maxValueCount", oldMaxValueCount, maxValueCount); 392 } 393 394 401 public void setCustomProperty(String propertyName, Object value) { 402 if (propertyName == null) { 403 throw new NullPointerException ("The propertyName for a custom property " + 404 "on MetaData cannot be null"); 405 } 406 Object oldValue = customProps.get(propertyName); 407 customProps.put(propertyName, value); 408 firePropertyChange(propertyName, oldValue, value); 409 } 410 411 416 public Object getCustomProperty(String propertyName) { 417 if (propertyName == null) { 418 throw new NullPointerException ("The propertyName for a custom property " + 419 "on MetaData cannot be null"); 420 } 421 return customProps.get(propertyName); 422 } 423 424 430 public Object getCustomProperty(String propertyName, Object defaultValue) { 431 if (propertyName == null) { 432 throw new NullPointerException ("The propertyName for a custom property " + 433 "on MetaData cannot be null"); 434 } 435 return customProps.containsKey(propertyName) ? 436 customProps.get(propertyName) : defaultValue; 437 } 438 439 443 public void removeCustomProperty(String propertyName) { 444 if (propertyName == null) { 445 throw new NullPointerException ("The propertyName for a custom property " + 446 "on MetaData cannot be null"); 447 } 448 Object oldValue = customProps.get(propertyName); 449 customProps.remove(propertyName); 450 firePropertyChange(propertyName, oldValue, null); 451 } 452 453 457 public String [] getCustomPropertyKeys() { 458 Object keys[] = customProps.keySet().toArray(); 459 String propertyNames[] = new String [keys.length]; 460 System.arraycopy(keys, 0, propertyNames, 0, keys.length); 461 return propertyNames; 462 } 463 464 473 public void addValidator(Validator validator) { 474 if (validators == null) { 475 validators = new ArrayList (); 476 } 477 validators.add(validator); 478 } 479 480 486 public void removeValidator(Validator validator) { 487 if (validators != null) { 488 validators.remove(validator); 489 if (validators.size() == 0) { 490 validators = null; 491 } 492 } 493 } 494 495 499 public Validator[] getValidators() { 500 if (validators != null) { 501 return (Validator[])validators.toArray(new Validator[1]); 502 } 503 return new Validator[0]; 504 } 505 506 511 public void addPropertyChangeListener(PropertyChangeListener pcl) { 512 if (pcs == null) { 513 pcs = new PropertyChangeSupport (this); 514 } 515 pcs.addPropertyChangeListener(pcl); 516 } 517 518 523 public void removePropertyChangeListener(PropertyChangeListener pcl) { 524 if (pcs != null) { 525 pcs.removePropertyChangeListener(pcl); 526 } 527 } 528 529 534 public PropertyChangeListener [] getPropertyChangeListeners() { 535 if (pcs != null) { 536 return pcs.getPropertyChangeListeners(); 537 } 538 return new PropertyChangeListener [0]; 539 } 540 541 protected void firePropertyChange(String propertyName, 542 int oldValue, int newValue) { 543 if (newValue != oldValue) { 544 firePropertyChange(propertyName, 545 new Integer (oldValue), new Integer (newValue)); 546 } 547 } 548 549 protected void firePropertyChange(String propertyName, 550 boolean oldValue, boolean newValue) { 551 if (newValue != oldValue) { 552 firePropertyChange(propertyName, 553 Boolean.valueOf(oldValue), 554 Boolean.valueOf(newValue)); 555 } 556 } 557 558 protected void firePropertyChange(String propertyName, Object oldValue, 559 Object newValue) { 560 if (pcs != null) { 561 pcs.firePropertyChange(propertyName, oldValue, newValue); 562 } 563 } 564 } 565 | Popular Tags |