1 6 7 package org.jdesktop.dataset.adapter; 8 9 import java.util.List ; 10 import org.jdesktop.dataset.DataColumn; 11 import org.jdesktop.dataset.DataTable; 12 import org.jdesktop.dataset.event.DataTableListener; 13 import org.jdesktop.dataset.event.RowChangeEvent; 14 import org.jdesktop.dataset.event.TableChangeEvent; 15 import org.jdesktop.swing.data.MetaData; 16 import org.jdesktop.swing.data.MetaDataProvider; 17 18 22 public class MetaDataProviderAdapter implements MetaDataProvider { 23 private DataTable table; 24 private MetaData[] cachedMetaData; 25 private String [] cachedFieldNames; 26 27 28 public MetaDataProviderAdapter(DataTable table) { 29 assert table != null; 30 this.table = table; 31 initMetaData(); 32 table.addDataTableListener(new DataTableListener(){ 34 public void rowChanged(RowChangeEvent evt) { 35 } 37 38 public void tableChanged(TableChangeEvent evt) { 39 initMetaData(); 41 } 42 }); 43 } 44 45 public int getFieldCount() { 46 return cachedMetaData == null ? 0 : cachedMetaData.length; 47 } 48 49 public final String [] getFieldNames() { 50 return cachedFieldNames == null ? new String [0] : cachedFieldNames; 51 } 52 53 public final MetaData[] getMetaData() { 54 return cachedMetaData == null ? new MetaData[0] : cachedMetaData; 55 } 56 57 public MetaData getMetaData(String dataID) { 58 if (cachedMetaData == null) { 59 return new MetaData(dataID); 60 } 61 for (MetaData md : cachedMetaData) { 62 if (md.getName().equals(dataID)) { 63 return md; 64 } 65 } 66 return null; 67 } 68 69 private void initMetaData() { 70 List <DataColumn> cols = table.getColumns(); 71 cachedMetaData = new MetaData[cols.size()]; 72 cachedFieldNames = new String [cachedMetaData.length]; 73 for (int i=0; i<cachedMetaData.length; i++) { 74 DataColumn col = cols.get(i); 75 cachedMetaData[i] = new MetaData(col.getName(), col.getType()); 77 cachedFieldNames[i] = col.getName(); 78 } 79 } 80 } | Popular Tags |