KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > dataset > adapter > MetaDataProviderAdapter


1 /*
2  * MetaDataProviderAdapter.java
3  *
4  * Created on February 24, 2005, 7:17 AM
5  */

6
7 package org.jdesktop.dataset.adapter;
8
9 import java.util.List JavaDoc;
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 /**
19  *
20  * @author rbair
21  */

22 public class MetaDataProviderAdapter implements MetaDataProvider {
23     private DataTable table;
24     private MetaData[] cachedMetaData;
25     private String JavaDoc[] cachedFieldNames;
26     
27     /** Creates a new instance of MetaDataProviderAdapter */
28     public MetaDataProviderAdapter(DataTable table) {
29         assert table != null;
30         this.table = table;
31         initMetaData();
32         //add listener to table for column change events
33
table.addDataTableListener(new DataTableListener(){
34             public void rowChanged(RowChangeEvent evt) {
35                 //null op
36
}
37
38             public void tableChanged(TableChangeEvent evt) {
39                 //reload the metaData
40
initMetaData();
41             }
42         });
43     }
44
45     public int getFieldCount() {
46         return cachedMetaData == null ? 0 : cachedMetaData.length;
47     }
48
49     public final String JavaDoc[] getFieldNames() {
50         return cachedFieldNames == null ? new String JavaDoc[0] : cachedFieldNames;
51     }
52
53     public final MetaData[] getMetaData() {
54         return cachedMetaData == null ? new MetaData[0] : cachedMetaData;
55     }
56
57     public MetaData getMetaData(String JavaDoc 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 JavaDoc<DataColumn> cols = table.getColumns();
71         cachedMetaData = new MetaData[cols.size()];
72         cachedFieldNames = new String JavaDoc[cachedMetaData.length];
73         for (int i=0; i<cachedMetaData.length; i++) {
74             DataColumn col = cols.get(i);
75             //TODO if the column name changes, my cache is invalidated!!!
76
cachedMetaData[i] = new MetaData(col.getName(), col.getType());
77             cachedFieldNames[i] = col.getName();
78         }
79     }
80 }
Popular Tags