KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > DataModelToTableModelAdapter


1 /*
2  * $Id: DataModelToTableModelAdapter.java,v 1.2 2005/03/01 13:25:01 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7 package org.jdesktop.swing.data;
8
9 import javax.swing.table.AbstractTableModel JavaDoc;
10
11
12 /**
13  * Extracted from TableBinding and changed to register
14  * as TabularValueChangeListener.
15  *
16  * @author Richard Bair
17  * @author Jeanette Winzenburg
18  *
19  */

20 public class DataModelToTableModelAdapter extends AbstractTableModel JavaDoc
21     implements MetaDataProvider {
22
23     private TabularDataModel dm;
24 // private String[] fieldNames;
25
private DefaultMetaDataProvider metaDataProvider;
26     
27     public DataModelToTableModelAdapter(TabularDataModel dm) {
28         this(dm, null);
29     }
30
31     public DataModelToTableModelAdapter(TabularDataModel dm, String JavaDoc[] visibleFieldNames) {
32         this.dm = dm;
33         initMetaDataProvider(visibleFieldNames);
34 // fieldNames = visibleFieldNames == null ? dm.getFieldNames() : visibleFieldNames;
35
installDataModelListener();
36     }
37     
38     private void initMetaDataProvider(String JavaDoc[] 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 JavaDoc getColumnClass(int columnIndex) {
52         MetaData metaData = getMetaData(columnIndex);
53         return metaData.getElementClass();
54     }
55     
56
57     public String JavaDoc getColumnName(int column) {
58         //its possible that the meta data hasn't shown up yet. In this
59
//case, use the field name until the meta data arrives
60
// JW: when would that be the case?
61
// MetaData md = dm.getMetaData(fieldNames[column]);
62
// return md == null ? fieldNames[column] : md.getLabel();
63
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 JavaDoc aValue, int rowIndex, int columnIndex) {
73         dm.setValueAt(getFieldName(columnIndex), rowIndex, aValue);
74     }
75     
76     /* (non-Javadoc)
77      * @see javax.swing.table.TableModel#getRowCount()
78      */

79     public int getRowCount() {
80         return dm.getRecordCount();
81     }
82
83     /* (non-Javadoc)
84      * @see javax.swing.table.TableModel#getColumnCount()
85      */

86     public int getColumnCount() {
87         return getFieldCount();
88     }
89
90     /* (non-Javadoc)
91      * @see javax.swing.table.TableModel#getValueAt(int, int)
92      */

93     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
94         return dm.getValueAt(getFieldName(columnIndex), rowIndex);
95     }
96
97
98     private String JavaDoc getFieldName(int columnIndex) {
99         return getDefaultMetaDataProvider().getFieldName(columnIndex);
100     }
101
102     private MetaData getMetaData(int columnIndex) {
103         String JavaDoc fieldName = getFieldName(columnIndex);
104         return getMetaData(fieldName);
105     }
106
107 // ------------------------ MetaDataProvider
108

109     public String JavaDoc[] getFieldNames() {
110         // TODO Auto-generated method stub
111
return getDefaultMetaDataProvider().getFieldNames();
112     }
113     
114     public int getFieldCount() {
115         // TODO Auto-generated method stub
116
return getDefaultMetaDataProvider().getFieldCount();
117     }
118
119
120     public MetaData getMetaData(String JavaDoc 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     // --------------------------- init listener
137

138     private void installDataModelListener() {
139         // register with the data model
140

141         // ValueChangeListener l = new ValueChangeListener() {
142
// public void valueChanged(ValueChangeEvent e) {
143
// fireTableStructureChanged();
144
// }
145
// };
146
// dm.addValueChangeListener(l);
147

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                 // PENDING: do better than this!
166
//fireTableStructureChanged();
167
}
168         };
169         dm.addTabularValueChangeListener(l);
170
171     }
172
173
174     protected int findColumnIndex(String JavaDoc fieldName) {
175         if (fieldName == null) {
176             return -1;
177         }
178         return getDefaultMetaDataProvider().getFieldIndex(fieldName);
179 // for (int i = 0; i < fieldNames.length; i++) {
180
// if (fieldName.equals(fieldNames[i])) {
181
// return i;
182
// }
183
// }
184
// return -1;
185
}
186
187
188
189 }
190
Popular Tags