KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TabularDataModelAdapter.java
3  *
4  * Created on February 24, 2005, 6:57 AM
5  */

6
7 package org.jdesktop.dataset.adapter;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
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 /**
24  *
25  * @author rbair
26  */

27 public class TabularDataModelAdapter implements TabularDataModel {
28     private DataTable table;
29     private MetaDataProviderAdapter mdp;
30     private List JavaDoc<Validator> validators = new ArrayList JavaDoc<Validator>();
31     private List JavaDoc<ValueChangeListener> listeners = new ArrayList JavaDoc<ValueChangeListener>();
32     private List JavaDoc<TabularValueChangeListener> tabularListeners = new ArrayList JavaDoc<TabularValueChangeListener>();
33  
34     
35     /** Creates a new instance of TabularDataModelAdapter */
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 JavaDoc[] getFieldNames() {
107         return mdp.getFieldNames();
108     }
109
110     public MetaData[] getMetaData() {
111         return mdp.getMetaData();
112     }
113
114     public MetaData getMetaData(String JavaDoc dataID) {
115         return mdp.getMetaData(dataID);
116     }
117
118     public Object JavaDoc getValueAt(String JavaDoc fieldName, int index) {
119         return table.getValue(index, fieldName);
120     }
121
122     public void setValueAt(String JavaDoc fieldName, int index, Object JavaDoc value) {
123         table.setValue(index, fieldName, value);
124         // JW: as long as the DataTable doesn't fire anything on updates
125
// we must do it manually
126
fireTabularValueChanged(index, fieldName);
127     }
128
129     public Object JavaDoc getValue(String JavaDoc fieldName) {
130         //null op
131
return null;
132     }
133
134     public void setValue(String JavaDoc fieldName, Object JavaDoc value) {
135         //null op
136
}
137     
138     protected void fireValueChanged(String JavaDoc fieldName) {
139         ValueChangeEvent e = new ValueChangeEvent(this, fieldName);//getCachedEvent(fieldName);
140
for (ValueChangeListener listener : listeners) {
141             try {
142                 listener.valueChanged(e);
143             } catch (Exception JavaDoc ex) {
144                 //TODO some real exception handling needs to occur
145
ex.printStackTrace();
146             }
147         }
148     }
149
150     protected void fireRowChanged(DataRow row) {
151        // fireAllFieldsChanged();
152
int rowIndex = table.getRows().indexOf(row);
153         fireTabularValueChanged(rowIndex, null);
154     }
155
156
157     protected void fireTableChanged() {
158        // fireAllFieldsChanged();
159
fireTabularValueChanged(-1, null);
160     }
161
162     protected void fireTabularValueChanged(int rowIndex, String JavaDoc 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 JavaDoc fieldName : mdp.getFieldNames()) {
171             fireValueChanged(fieldName);
172         }
173         // JW: why?
174
fireValueChanged("");
175     }
176 }
Popular Tags