1 6 7 package org.jdesktop.dataset.adapter; 8 9 import java.util.ArrayList ; 10 import java.util.List ; 11 12 import org.jdesktop.dataset.DataRow; 13 import org.jdesktop.dataset.DataTable; 14 import org.jdesktop.dataset.event.DataTableListener; 15 import org.jdesktop.swing.data.MetaData; 16 import org.jdesktop.swing.data.TabularDataModel; 17 import org.jdesktop.swing.data.TabularValueChangeEvent; 18 import org.jdesktop.swing.data.TabularValueChangeListener; 19 import org.jdesktop.swing.data.Validator; 20 import org.jdesktop.swing.data.ValueChangeEvent; 21 import org.jdesktop.swing.data.ValueChangeListener; 22 23 27 public class TabularDataModelAdapter implements TabularDataModel { 28 private DataTable table; 29 private MetaDataProviderAdapter mdp; 30 private List <Validator> validators = new ArrayList <Validator>(); 31 private List <ValueChangeListener> listeners = new ArrayList <ValueChangeListener>(); 32 private List <TabularValueChangeListener> tabularListeners = new ArrayList <TabularValueChangeListener>(); 33 34 35 36 public TabularDataModelAdapter(DataTable table) { 37 assert table != null; 38 this.table = table; 39 mdp = new MetaDataProviderAdapter(table); 40 table.addDataTableListener(new DataTableListener() { 41 public void rowChanged(org.jdesktop.dataset.event.RowChangeEvent evt) { 42 fireRowChanged((DataRow) evt.getSource()); 43 } 44 45 public void tableChanged(org.jdesktop.dataset.event.TableChangeEvent evt) { 46 fireTableChanged(); 47 } 48 49 }); 50 } 51 52 public int getRecordCount() { 53 return table.getRows().size(); 54 } 55 56 public DataTable getDataTable() { 57 return table; 58 } 59 60 public void addValidator(Validator validator) { 61 if (!validators.contains(validator)) { 62 validators.add(validator); 63 } 64 } 65 66 public void removeValidator(Validator validator) { 67 validators.remove(validator); 68 } 69 70 public Validator[] getValidators() { 71 return validators.toArray(new Validator[validators.size()]); 72 } 73 74 public void addValueChangeListener(ValueChangeListener valueChangeListener) { 75 if (!listeners.contains(valueChangeListener)) { 76 listeners.add(valueChangeListener); 77 } 78 } 79 80 public void removeValueChangeListener(ValueChangeListener valueChangeListener) { 81 listeners.remove(valueChangeListener); 82 } 83 84 public ValueChangeListener[] getValueChangeListeners() { 85 return listeners.toArray(new ValueChangeListener[listeners.size()]); 86 } 87 88 89 public void addTabularValueChangeListener(TabularValueChangeListener l) { 90 if (!tabularListeners.contains(l)) { 91 tabularListeners.add(l); 92 } 93 94 } 95 public TabularValueChangeListener[] getTabularValueChangeListeners() { 96 return tabularListeners.toArray(new TabularValueChangeListener[tabularListeners.size()]); 97 } 98 public void removeTabularValueChangeListener(TabularValueChangeListener l) { 99 tabularListeners.remove(l); 100 101 } 102 public int getFieldCount() { 103 return mdp.getFieldCount(); 104 } 105 106 public String [] getFieldNames() { 107 return mdp.getFieldNames(); 108 } 109 110 public MetaData[] getMetaData() { 111 return mdp.getMetaData(); 112 } 113 114 public MetaData getMetaData(String dataID) { 115 return mdp.getMetaData(dataID); 116 } 117 118 public Object getValueAt(String fieldName, int index) { 119 return table.getValue(index, fieldName); 120 } 121 122 public void setValueAt(String fieldName, int index, Object value) { 123 table.setValue(index, fieldName, value); 124 fireTabularValueChanged(index, fieldName); 127 } 128 129 public Object getValue(String fieldName) { 130 return null; 132 } 133 134 public void setValue(String fieldName, Object value) { 135 } 137 138 protected void fireValueChanged(String fieldName) { 139 ValueChangeEvent e = new ValueChangeEvent(this, fieldName); for (ValueChangeListener listener : listeners) { 141 try { 142 listener.valueChanged(e); 143 } catch (Exception ex) { 144 ex.printStackTrace(); 146 } 147 } 148 } 149 150 protected void fireRowChanged(DataRow row) { 151 int rowIndex = table.getRows().indexOf(row); 153 fireTabularValueChanged(rowIndex, null); 154 } 155 156 157 protected void fireTableChanged() { 158 fireTabularValueChanged(-1, null); 160 } 161 162 protected void fireTabularValueChanged(int rowIndex, String fieldName) { 163 TabularValueChangeEvent e = new TabularValueChangeEvent(this, rowIndex, fieldName); 164 for (TabularValueChangeListener l: tabularListeners) { 165 l.tabularValueChanged(e); 166 } 167 168 } 169 protected void fireAllFieldsChanged() { 170 for (String fieldName : mdp.getFieldNames()) { 171 fireValueChanged(fieldName); 172 } 173 fireValueChanged(""); 175 } 176 } | Popular Tags |