KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > swing > TableBinding


1 package org.objectstyle.cayenne.swing;
2
3 import java.awt.Component JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.swing.JTable JavaDoc;
11 import javax.swing.table.AbstractTableModel JavaDoc;
12
13 import org.apache.commons.collections.map.SingletonMap;
14
15 /**
16  * A binding for a JTable.
17  *
18  * @author Andrei Adamchik
19  */

20 public class TableBinding extends BindingBase {
21
22     /**
23      * A variable exposed in the context of set/get cell value.
24      */

25     public static final String JavaDoc ITEM_VAR = "item";
26
27     protected JTable JavaDoc table;
28     protected String JavaDoc[] headers;
29     protected BindingExpression[] columns;
30     protected boolean[] editableState;
31     protected Class JavaDoc[] columnClass;
32     protected List JavaDoc list;
33
34     public TableBinding(JTable JavaDoc table, String JavaDoc listBinding, String JavaDoc[] headers,
35             BindingExpression[] columns, Class JavaDoc[] columnClass, boolean[] editableState) {
36
37         super(listBinding);
38         this.table = table;
39         this.headers = headers;
40         this.columns = columns;
41         this.editableState = editableState;
42         this.columnClass = columnClass;
43
44         table.setModel(new BoundTableModel());
45     }
46
47     public void setContext(Object JavaDoc object) {
48         super.setContext(object);
49
50         this.list = updateList();
51     }
52
53     public Component JavaDoc getView() {
54         return table;
55     }
56
57     public void updateView() {
58         this.list = updateList();
59         ((BoundTableModel) table.getModel()).fireTableDataChanged();
60     }
61
62     int getListSize() {
63         return (list != null) ? list.size() : 0;
64     }
65
66     List JavaDoc updateList() {
67         if (getContext() == null) {
68             return null;
69         }
70
71         Object JavaDoc list = getValue();
72         if (list == null) {
73             return null;
74         }
75
76         if (list instanceof List JavaDoc) {
77             return (List JavaDoc) list;
78         }
79
80         if (list instanceof Object JavaDoc[]) {
81             Object JavaDoc[] objects = (Object JavaDoc[]) list;
82             return Arrays.asList(objects);
83         }
84
85         if (list instanceof Collection JavaDoc) {
86             return new ArrayList JavaDoc((Collection JavaDoc) list);
87         }
88
89         throw new BindingException("List expected, got - " + list);
90     }
91
92     final class BoundTableModel extends AbstractTableModel JavaDoc {
93
94         // this map is used as "flywieght", providing on the spot context for Ognl
95
// expression evaluation
96
Map JavaDoc listContext = new SingletonMap(ITEM_VAR, null);
97
98         public int getColumnCount() {
99             return headers.length;
100         }
101
102         public int getRowCount() {
103             return getListSize();
104         }
105
106         public boolean isCellEditable(int rowIndex, int columnIndex) {
107             return editableState[columnIndex];
108         }
109
110         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
111             Object JavaDoc item = list.get(rowIndex);
112             listContext.put(ITEM_VAR, item);
113             return columns[columnIndex].getValue(getContext(), listContext);
114         }
115
116         public String JavaDoc getColumnName(int column) {
117             return headers[column];
118         }
119
120         public Class JavaDoc getColumnClass(int columnIndex) {
121             return columnClass[columnIndex];
122         }
123
124         public void setValueAt(Object JavaDoc value, int rowIndex, int columnIndex) {
125             Object JavaDoc item = list.get(rowIndex);
126             listContext.put(ITEM_VAR, item);
127             columns[columnIndex].setValue(getContext(), listContext, value);
128         }
129     }
130 }
Popular Tags