1 6 7 package org.jdesktop.dataset.adapter; 8 9 import java.beans.PropertyChangeEvent ; 10 import java.beans.PropertyChangeListener ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 import org.jdesktop.dataset.DataSelector; 14 import org.jdesktop.dataset.event.DataTableListener; 15 import org.jdesktop.dataset.event.RowChangeEvent; 16 import org.jdesktop.dataset.event.TableChangeEvent; 17 import org.jdesktop.swing.data.DataModel; 18 import org.jdesktop.swing.data.MetaData; 19 import org.jdesktop.swing.data.SelectionModelEvent; 20 import org.jdesktop.swing.data.Validator; 21 import org.jdesktop.swing.data.ValueChangeEvent; 22 import org.jdesktop.swing.data.ValueChangeListener; 23 24 28 public class DataModelAdapter implements DataModel { 29 private DataSelector selector; 30 private MetaDataProviderAdapter mdp; 31 private List <Validator> validators = new ArrayList <Validator>(); 32 private List <ValueChangeListener> listeners = new ArrayList <ValueChangeListener>(); 33 34 35 public DataModelAdapter(DataSelector s) { 36 assert s != null; 37 this.selector = s; 38 mdp = new MetaDataProviderAdapter(selector.getDataTable()); 39 selector.addPropertyChangeListener("rowIndices", new PropertyChangeListener () { 40 public void propertyChange(PropertyChangeEvent evt) { 41 for (String fieldName : mdp.getFieldNames()) { 42 fireValueChanged(fieldName); 43 } 44 } 45 }); 46 selector.getDataTable().addDataTableListener(new DataTableListener() { 47 public void rowChanged(RowChangeEvent evt) { 48 } 49 50 public void tableChanged(TableChangeEvent evt) { 51 for (String fieldName : mdp.getFieldNames()) { 53 fireValueChanged(fieldName); 54 } 55 } 56 }); 57 } 58 59 public void addValidator(Validator validator) { 60 if (!validators.contains(validator)) { 61 validators.add(validator); 62 } 63 } 64 65 public void removeValidator(Validator validator) { 66 validators.remove(validator); 67 } 68 69 public Validator[] getValidators() { 70 return validators.toArray(new Validator[validators.size()]); 71 } 72 73 public void addValueChangeListener(ValueChangeListener valueChangeListener) { 74 if (!listeners.contains(valueChangeListener)) { 75 listeners.add(valueChangeListener); 76 } 77 } 78 79 public void removeValueChangeListener(ValueChangeListener valueChangeListener) { 80 listeners.remove(valueChangeListener); 81 } 82 83 public ValueChangeListener[] getValueChangeListeners() { 84 return listeners.toArray(new ValueChangeListener[listeners.size()]); 85 } 86 87 public int getFieldCount() { 88 return mdp.getFieldCount(); 89 } 90 91 public String [] getFieldNames() { 92 return mdp.getFieldNames(); 93 } 94 95 public MetaData[] getMetaData() { 96 return mdp.getMetaData(); 97 } 98 99 public MetaData getMetaData(String dataID) { 100 return mdp.getMetaData(dataID); 101 } 102 103 public Object getValue(String fieldName) { 104 List <Integer > indices = selector.getRowIndices(); 105 if (indices.size() > 0) { 106 return selector.getDataTable().getValue(indices.get(0), fieldName); 107 } 108 return null; 109 } 110 111 public void setValue(String fieldName, Object value) { 112 List <Integer > indices = selector.getRowIndices(); 113 for (int index : indices) { 114 selector.getDataTable().setValue(index, fieldName, value); 115 } 116 } 117 118 123 protected void fireValueChanged(String fieldName) { 124 ValueChangeEvent e = new ValueChangeEvent(this, fieldName); for (ValueChangeListener listener : listeners) { 126 try { 127 listener.valueChanged(e); 128 } catch (Exception ex) { 129 ex.printStackTrace(); 131 } 132 } 133 } 134 135 138 154 170 175 187 } | Popular Tags |