1 7 package org.jdesktop.swing.data; 8 9 import javax.swing.table.AbstractTableModel ; 10 11 12 20 public class DataModelToTableModelAdapter extends AbstractTableModel 21 implements MetaDataProvider { 22 23 private TabularDataModel dm; 24 private DefaultMetaDataProvider metaDataProvider; 26 27 public DataModelToTableModelAdapter(TabularDataModel dm) { 28 this(dm, null); 29 } 30 31 public DataModelToTableModelAdapter(TabularDataModel dm, String [] visibleFieldNames) { 32 this.dm = dm; 33 initMetaDataProvider(visibleFieldNames); 34 installDataModelListener(); 36 } 37 38 private void initMetaDataProvider(String [] visibleFieldNames) { 39 if (visibleFieldNames == null) { 40 metaDataProvider = new DefaultMetaDataProvider(dm); 41 } else { 42 MetaData[] metas = new MetaData[visibleFieldNames.length]; 43 for (int i = 0; i < metas.length; i++) { 44 metas[i] = dm.getMetaData(visibleFieldNames[i]); 45 } 46 metaDataProvider = new DefaultMetaDataProvider(metas); 47 } 48 49 } 50 51 public Class getColumnClass(int columnIndex) { 52 MetaData metaData = getMetaData(columnIndex); 53 return metaData.getElementClass(); 54 } 55 56 57 public String getColumnName(int column) { 58 MetaData md = getMetaData(column); 64 return md.getLabel(); 65 } 66 67 public boolean isCellEditable(int rowIndex, int columnIndex) { 68 MetaData md = getMetaData(columnIndex); 69 return !md.isReadOnly(); 70 } 71 72 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 73 dm.setValueAt(getFieldName(columnIndex), rowIndex, aValue); 74 } 75 76 79 public int getRowCount() { 80 return dm.getRecordCount(); 81 } 82 83 86 public int getColumnCount() { 87 return getFieldCount(); 88 } 89 90 93 public Object getValueAt(int rowIndex, int columnIndex) { 94 return dm.getValueAt(getFieldName(columnIndex), rowIndex); 95 } 96 97 98 private String getFieldName(int columnIndex) { 99 return getDefaultMetaDataProvider().getFieldName(columnIndex); 100 } 101 102 private MetaData getMetaData(int columnIndex) { 103 String fieldName = getFieldName(columnIndex); 104 return getMetaData(fieldName); 105 } 106 107 109 public String [] getFieldNames() { 110 return getDefaultMetaDataProvider().getFieldNames(); 112 } 113 114 public int getFieldCount() { 115 return getDefaultMetaDataProvider().getFieldCount(); 117 } 118 119 120 public MetaData getMetaData(String dataID) { 121 return getDefaultMetaDataProvider().getMetaData(dataID); 122 } 123 124 125 public MetaData[] getMetaData() { 126 return getDefaultMetaDataProvider().getMetaData(); 127 } 128 129 private DefaultMetaDataProvider getDefaultMetaDataProvider() { 130 if (metaDataProvider == null) { 131 metaDataProvider = new DefaultMetaDataProvider(); 132 } 133 return metaDataProvider; 134 } 135 136 138 private void installDataModelListener() { 139 141 148 TabularValueChangeListener l = new TabularValueChangeListener() { 149 public void tabularValueChanged(TabularValueChangeEvent e) { 150 int rowIndex = e.getRowIndex(); 151 int columnIndex = findColumnIndex(e.getFieldName()); 152 if (rowIndex < 0) { 153 if (columnIndex < 0) { 154 fireTableStructureChanged(); 155 } else { 156 fireTableDataChanged(); 157 } 158 } else { 159 if (columnIndex < 0) { 160 fireTableRowsUpdated(rowIndex, rowIndex); 161 } else { 162 fireTableCellUpdated(rowIndex, columnIndex); 163 } 164 } 165 } 168 }; 169 dm.addTabularValueChangeListener(l); 170 171 } 172 173 174 protected int findColumnIndex(String fieldName) { 175 if (fieldName == null) { 176 return -1; 177 } 178 return getDefaultMetaDataProvider().getFieldIndex(fieldName); 179 } 186 187 188 189 } 190 | Popular Tags |