KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package org.jdesktop.dataset.adapter;
8
9 import java.beans.PropertyChangeEvent JavaDoc;
10 import java.beans.PropertyChangeListener JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import org.jdesktop.dataset.DataSelector;
14 import org.jdesktop.dataset.event.DataTableListener;
15 import org.jdesktop.dataset.event.RowChangeEvent;
16 import org.jdesktop.dataset.event.TableChangeEvent;
17 import org.jdesktop.swing.data.DataModel;
18 import org.jdesktop.swing.data.MetaData;
19 import org.jdesktop.swing.data.SelectionModelEvent;
20 import org.jdesktop.swing.data.Validator;
21 import org.jdesktop.swing.data.ValueChangeEvent;
22 import org.jdesktop.swing.data.ValueChangeListener;
23
24 /**
25  *
26  * @author rbair
27  */

28 public class DataModelAdapter implements DataModel {
29     private DataSelector selector;
30     private MetaDataProviderAdapter mdp;
31     private List JavaDoc<Validator> validators = new ArrayList JavaDoc<Validator>();
32     private List JavaDoc<ValueChangeListener> listeners = new ArrayList JavaDoc<ValueChangeListener>();
33     
34     /** Creates a new instance of DataModelAdapter */
35     public DataModelAdapter(DataSelector s) {
36         assert s != null;
37         this.selector = s;
38         mdp = new MetaDataProviderAdapter(selector.getDataTable());
39         selector.addPropertyChangeListener("rowIndices", new PropertyChangeListener JavaDoc() {
40             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
41                 for (String JavaDoc fieldName : mdp.getFieldNames()) {
42                     fireValueChanged(fieldName);
43                 }
44             }
45         });
46         selector.getDataTable().addDataTableListener(new DataTableListener() {
47            public void rowChanged(RowChangeEvent evt) {
48            }
49            
50            public void tableChanged(TableChangeEvent evt) {
51 // fireModelChanged();
52
for (String JavaDoc fieldName : mdp.getFieldNames()) {
53                     fireValueChanged(fieldName);
54                 }
55            }
56         });
57     }
58
59     public void addValidator(Validator validator) {
60         if (!validators.contains(validator)) {
61             validators.add(validator);
62         }
63     }
64
65     public void removeValidator(Validator validator) {
66         validators.remove(validator);
67     }
68
69     public Validator[] getValidators() {
70         return validators.toArray(new Validator[validators.size()]);
71     }
72
73     public void addValueChangeListener(ValueChangeListener valueChangeListener) {
74         if (!listeners.contains(valueChangeListener)) {
75             listeners.add(valueChangeListener);
76         }
77     }
78
79     public void removeValueChangeListener(ValueChangeListener valueChangeListener) {
80         listeners.remove(valueChangeListener);
81     }
82
83     public ValueChangeListener[] getValueChangeListeners() {
84         return listeners.toArray(new ValueChangeListener[listeners.size()]);
85     }
86
87     public int getFieldCount() {
88         return mdp.getFieldCount();
89     }
90
91     public String JavaDoc[] getFieldNames() {
92         return mdp.getFieldNames();
93     }
94
95     public MetaData[] getMetaData() {
96         return mdp.getMetaData();
97     }
98
99     public MetaData getMetaData(String JavaDoc dataID) {
100         return mdp.getMetaData(dataID);
101     }
102
103     public Object JavaDoc getValue(String JavaDoc fieldName) {
104         List JavaDoc<Integer JavaDoc> indices = selector.getRowIndices();
105         if (indices.size() > 0) {
106             return selector.getDataTable().getValue(indices.get(0), fieldName);
107         }
108         return null;
109     }
110
111     public void setValue(String JavaDoc fieldName, Object JavaDoc value) {
112         List JavaDoc<Integer JavaDoc> indices = selector.getRowIndices();
113         for (int index : indices) {
114             selector.getDataTable().setValue(index, fieldName, value);
115         }
116     }
117     
118     //-----------------event handling code-----------------------
119
/**
120      * Fires the value change event. This is called normally during the set operation
121      * @param fieldName
122      */

123     protected void fireValueChanged(String JavaDoc fieldName) {
124         ValueChangeEvent e = new ValueChangeEvent(this, fieldName);//getCachedEvent(fieldName);
125
for (ValueChangeListener listener : listeners) {
126             try {
127                 listener.valueChanged(e);
128             } catch (Exception JavaDoc ex) {
129                 //TODO some real exception handling needs to occur
130
ex.printStackTrace();
131             }
132         }
133     }
134
135     /**
136      * Fires the model change event.
137      */

138 // protected void fireModelChanged() {
139
// ModelChangeEvent e = new ModelChangeEvent(this);
140
// for (int i = 0; i < dataModelListeners.size(); i++) {
141
// DataModelListener dml = (DataModelListener)dataModelListeners.get(i);
142
// try {
143
// dml.modelChanged(e);
144
// } catch (Exception ex) {
145
// ex.printStackTrace();
146
// }
147
// }
148
// //If the row has changed, then details of this master
149
// //need to be updated as well. Loop through the detail model
150
// //and tell them to reload themselves, etc.
151
//// notifyMasterChanged();
152
// }
153

154 // protected void fireModelMetaDataChanged() {
155
// MetaDataChangeEvent e = new MetaDataChangeEvent(this);
156
// for (int i = 0; i < dataModelListeners.size(); i++) {
157
// DataModelListener dml = (DataModelListener)dataModelListeners.get(i);
158
// try {
159
// dml.metaDataChanged(e);
160
// } catch (Exception ex) {
161
// ex.printStackTrace();
162
// }
163
// }
164
// //If the row has changed, then details of this master
165
// //need to be updated as well. Loop through the detail model
166
// //and tell them to reload themselves, etc.
167
//// notifyMasterChanged();
168
// }
169

170     /**
171      * Fires a MetaDataChangeEvent if the *set* of meta data has changed. To
172      * know whether the contents of a specific MetaData object have changed,
173      * register with that MetaData object.
174      */

175 // protected void fireMetaDataChanged() {
176
// MetaDataChangeEvent e = new MetaDataChangeEvent(this);
177
// for (DataModelListener dml : dataModelListeners) {
178
// try {
179
// dml.metaDataChanged(e);
180
// } catch (Exception ex) {
181
// //TODO some real exception handling needs to occur
182
// ex.printStackTrace();
183
// }
184
// }
185
// }
186

187 // private final class DataSetListener implements PropertyChangeListener {
188
// public void propertyChange(PropertyChangeEvent evt) {
189
// if (evt.getPropertyName().equals("rowIndices") && evt.getSource() == s) {
190
// fireSelectionModelChanged(new SelectionModelEvent(DataModelImpl.this, 0, getRecordCount()));
191
// }
192
// }
193
// }
194
}
Popular Tags