KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > binding > DirectTableBinding


1 package org.jdesktop.swing.binding;
2
3 import javax.swing.JComponent JavaDoc;
4 import javax.swing.JTable JavaDoc;
5 import javax.swing.table.AbstractTableModel JavaDoc;
6 import javax.swing.table.TableModel JavaDoc;
7 import org.jdesktop.swing.data.DataModel;
8 import org.jdesktop.swing.data.DataModelToTableModelAdapter;
9 import org.jdesktop.swing.data.MetaData;
10 import org.jdesktop.swing.data.SelectionModel;
11 import org.jdesktop.swing.data.TabularDataModel;
12 import org.jdesktop.swing.data.ValueChangeEvent;
13 import org.jdesktop.swing.data.ValueChangeListener;
14
15 /**
16  * This "Binding" happens to the given DataModel as a whole (as
17  * opposed to a single field of the model).
18  *
19  * @author Richard Bair
20  */

21 public class DirectTableBinding extends AbstractBinding {
22     private JTable JavaDoc table;
23     
24     /**
25      * @param component
26      * @param dataModel
27      */

28     public DirectTableBinding(JTable JavaDoc component, TabularDataModel dataModel) {
29         this(component, dataModel, null);
30 // super(component, dataModel, "", DirectTableBinding.AUTO_VALIDATE_NONE);
31
// //construct a TableModel for the table based on the given dataModel.
32
// TableModel tm = createAdapter(dataModel, null);
33
// table.setModel(tm);
34
}
35     
36     public DirectTableBinding(JTable JavaDoc component, TabularDataModel dataModel, String JavaDoc[] fieldNames) {
37 // super(component, dataModel, "", DirectTableBinding.AUTO_VALIDATE_NONE);
38
// //construct a TableModel for the table based on the given dataModel.
39
// TableModel tm = createAdapter(dataModel, fieldNames);
40
// table.setModel(tm);
41
this(component, dataModel, fieldNames, null);
42     }
43
44     public DirectTableBinding(JTable JavaDoc component, TabularDataModel dataModel,
45             String JavaDoc[] fieldNames, SelectionModel selectionModel) {
46         super(component, dataModel, "", DirectTableBinding.AUTO_VALIDATE_NONE);
47         //construct a TableModel for the table based on the given dataModel.
48
TableModel JavaDoc tm = createAdapter(dataModel, fieldNames);
49         table.setModel(tm);
50         if (selectionModel != null) {
51            new ListSelectionBinding(selectionModel, table.getSelectionModel());
52         }
53     }
54
55     public boolean push() {
56         return true;
57     }
58     
59     public boolean pull() {
60         return true;
61     }
62     
63     public boolean isValid() {
64         return true;
65     }
66     
67     protected TableModel JavaDoc createAdapter(TabularDataModel tabularDataModel, String JavaDoc[] fieldNames) {
68         return new DataModelToTableModelAdapter(tabularDataModel, fieldNames);
69     }
70     /* (non-Javadoc)
71      * @see org.jdesktop.swing.binding.AbstractUIBinding#getBoundComponent()
72      */

73     protected JComponent JavaDoc getBoundComponent() {
74         return table;
75     }
76
77     /* (non-Javadoc)
78      * @see org.jdesktop.swing.binding.AbstractUIBinding#setBoundComponent(javax.swing.JComponent)
79      */

80     protected void setBoundComponent(JComponent JavaDoc component) {
81         if (!(component instanceof JTable JavaDoc)) {
82             throw new IllegalArgumentException JavaDoc("TableBindings only accept a JTable or one of its child classes");
83         }
84         this.table = (JTable JavaDoc)component;
85     }
86
87     /* (non-Javadoc)
88      * @see org.jdesktop.swing.binding.AbstractUIBinding#getComponentValue()
89      */

90     protected Object JavaDoc getComponentValue() {
91         //a table component never updates its parent data model in this way
92
return null;
93     }
94
95     /* (non-Javadoc)
96      * @see org.jdesktop.swing.binding.AbstractUIBinding#setComponentValue(java.lang.Object)
97      */

98     protected void setComponentValue(Object JavaDoc value) {
99         //means nothing to this binding
100
}
101
102     // JW: this is extracted as DataModelToTableModelAdapter
103
// what an ugly name but couldn't come up with anything nicer
104

105 // private static final class DataModelTableModel extends AbstractTableModel {
106
// private TabularDataModel dm;
107
// private String[] fieldNames;
108
//
109
// public DataModelTableModel(final TabularDataModel dm) {
110
// this.dm = dm;
111
// fieldNames = dm.getFieldNames();
112
// //register with the data model
113
// dm.addValueChangeListener(new ValueChangeListener() {
114
// public void valueChanged(ValueChangeEvent e) {
115
// fireTableStructureChanged();
116
//// fireTableDataChanged();
117
// }
118
// });
119
// }
120
//
121
// public DataModelTableModel(final TabularDataModel dm, final String[] visibleFieldNames) {
122
// this.dm = dm;
123
// fieldNames = visibleFieldNames/*dm.getFieldNames()*/;
124
// //register with the data model
125
// dm.addValueChangeListener(new ValueChangeListener() {
126
// public void valueChanged(ValueChangeEvent e) {
127
// fireTableStructureChanged();
128
//// fireTableDataChanged();
129
// }
130
// });
131
// }
132
//
133
// public Class getColumnClass(int columnIndex) {
134
// return dm.getMetaData(fieldNames[columnIndex]).getElementClass();
135
// }
136
//
137
// public String getColumnName(int column) {
138
// //its possible that the meta data hasn't shown up yet. In this
139
// //case, use the field name until the meta data arrives
140
// MetaData md = dm.getMetaData(fieldNames[column]);
141
// return md == null ? fieldNames[column] : md.getLabel();
142
// }
143
//
144
// public boolean isCellEditable(int rowIndex, int columnIndex) {
145
// return !dm.getMetaData(fieldNames[columnIndex]).isReadOnly();
146
// }
147
//
148
// public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
149
//// dm.setValue(fieldNames[columnIndex], aValue, rowIndex);
150
// }
151
//
152
// /* (non-Javadoc)
153
// * @see javax.swing.table.TableModel#getRowCount()
154
// */
155
// public int getRowCount() {
156
// return dm.getRecordCount();
157
// }
158
//
159
// /* (non-Javadoc)
160
// * @see javax.swing.table.TableModel#getColumnCount()
161
// */
162
// public int getColumnCount() {
163
// return fieldNames == null ? dm.getFieldCount() : fieldNames.length;
164
// }
165
//
166
// /* (non-Javadoc)
167
// * @see javax.swing.table.TableModel#getValueAt(int, int)
168
// */
169
// public Object getValueAt(int rowIndex, int columnIndex) {
170
// return dm.getValueAt(fieldNames[columnIndex], rowIndex);
171
// }
172
//
173
// }
174

175     /* (non-Javadoc)
176      * @see org.jdesktop.jdnc.incubator.rbair.swing.binding.AbstractBinding#getComponent()
177      */

178     public JComponent JavaDoc getComponent() {
179         return getBoundComponent();
180     }
181
182     /* (non-Javadoc)
183      * @see org.jdesktop.jdnc.incubator.rbair.swing.binding.AbstractBinding#setComponent(javax.swing.JComponent)
184      */

185     public void setComponent(JComponent JavaDoc component) {
186         setBoundComponent(component);
187     }
188
189 }
190
Popular Tags