1 7 8 package org.jdesktop.swing.data; 9 10 import org.jdesktop.swing.data.MetaData; 11 import org.jdesktop.swing.data.Validator; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 17 26 public abstract class AbstractDataModel implements DataModel { 27 28 protected ArrayList validators; 29 private ArrayList valueChangeListeners; 30 private HashMap valueChangeEvents; 31 32 public abstract String [] getFieldNames(); 33 34 public MetaData[] getMetaData() { 35 String fieldNames[] = getFieldNames(); 36 MetaData metaData[] = new MetaData[fieldNames.length]; 37 for(int i = 0; i < fieldNames.length; i++) { 38 metaData[i] = getMetaData(fieldNames[i]); 39 } 40 return metaData; 41 } 42 43 public abstract MetaData getMetaData(String fieldName); 44 45 public abstract Object getValue(String fieldName); 46 47 public void setValue(String fieldName, Object value) { 48 Object oldValue = getValue(fieldName); 49 setValueImpl(fieldName, value); 50 if ((oldValue != null && !oldValue.equals(value)) || 51 (oldValue == null && value != null)) { 52 fireValueChanged(fieldName); 53 } 54 } 55 56 protected abstract void setValueImpl(String fieldName, Object value); 57 58 public void addValidator(Validator validator) { 59 if (validators == null) { 60 validators = new ArrayList (); 61 } 62 validators.add(validator); 63 64 } 65 public void removeValidator(Validator validator) { 66 if (validators != null) { 67 validators.remove(validator); 68 } 69 } 70 public Validator[] getValidators() { 71 if (validators != null) { 72 return (Validator[]) validators.toArray(new Validator[validators.size()]); 73 } 74 return new Validator[0]; 75 } 76 77 public void addValueChangeListener(ValueChangeListener l) { 78 if (valueChangeListeners == null) { 79 valueChangeListeners = new ArrayList (); 80 } 81 valueChangeListeners.add(l); 82 } 83 84 public void removeValueChangeListener(ValueChangeListener l) { 85 if (valueChangeListeners != null){ 86 valueChangeListeners.remove(l); 87 } 88 } 89 90 public ValueChangeListener[] getValueChangeListeners() { 91 if (valueChangeListeners != null) { 92 return (ValueChangeListener[])valueChangeListeners.toArray(new ValueChangeListener[1]); 93 } 94 return new ValueChangeListener[0]; 95 } 96 97 protected void fireValueChanged(String fieldName) { 98 ValueChangeListener[] formListeners = getValueChangeListeners(); 99 ValueChangeEvent e = getCachedEvent(fieldName); 100 if (valueChangeListeners != null) { 101 for (int i = 0; i < valueChangeListeners.size(); i++) { 102 ValueChangeListener vcl = (ValueChangeListener) 103 valueChangeListeners.get(i); 104 vcl.valueChanged(e); 105 } 106 } 107 } 108 109 private ValueChangeEvent getCachedEvent(String fieldName) { 110 if (valueChangeEvents == null) { 111 valueChangeEvents = new HashMap (); 112 } 113 ValueChangeEvent event = (ValueChangeEvent)valueChangeEvents.get(fieldName); 114 if (event == null) { 115 event = new ValueChangeEvent(this, fieldName); 116 valueChangeEvents.put(fieldName, event); 117 } 118 return event; 119 } 120 121 122 } 123 | Popular Tags |