1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.models;
17
18 import java.util.List;
19
20 import javax.swing.event.TableModelListener;
21 import javax.swing.table.TableModel;
22
23 import org.xulux.api.dataprovider.IConverter;
24 import org.xulux.api.dataprovider.IDataProvider;
25 import org.xulux.api.dataprovider.IField;
26 import org.xulux.api.dataprovider.IMapping;
27 import org.xulux.api.gui.IWidget;
28 import org.xulux.core.XuluxContext;
29 import org.xulux.dataprovider.Dictionary;
30 import org.xulux.gui.XuluxListener;
31 import org.xulux.guilayer.swing.widgets.Table;
32 import org.xulux.utils.BooleanUtils;
33
34
35
36
37
38
39
40
41 public class NyxTableModel extends XuluxListener implements TableModel {
42
43
44
45
46 protected Table table;
47
48
49
50 protected TableModel model;
51
52
53
54
55 public NyxTableModel() {
56 super();
57 }
58
59
60
61
62 public NyxTableModel(Table table) {
63 setTable(table);
64 }
65
66
67
68
69
70 public NyxTableModel(TableModel model, Table table) {
71 setTable(table);
72 setModel(model);
73 }
74
75
76
77
78
79 public void setModel(TableModel model) {
80 this.model = model;
81 }
82
83
84
85
86 public boolean hasModel() {
87 return this.model != null;
88 }
89
90
91
92
93
94 public void setTable(Table table) {
95 this.table = table;
96 }
97
98
99
100
101 public int getRowCount() {
102 if (hasModel()) {
103 return model.getRowCount();
104 }
105 int rowCount = 0;
106 if (table != null) {
107 if (table.getContent() != null) {
108 rowCount = ((List) table.getContent()).size();
109 }
110 }
111
112 return rowCount;
113 }
114
115
116
117
118
119
120 public int getColumnCount() {
121 if (hasModel()) {
122 return model.getColumnCount();
123 }
124 return 0;
125 }
126
127
128
129
130
131 public String getColumnName(int columnIndex) {
132 if (hasModel()) {
133 return model.getColumnName(columnIndex);
134 }
135 return String.valueOf(columnIndex);
136 }
137
138
139
140
141 public Class getColumnClass(int columnIndex) {
142 if (hasModel()) {
143 return model.getColumnClass(columnIndex);
144 }
145 return String.class;
146 }
147
148
149
150
151 public boolean isCellEditable(int rowIndex, int columnIndex) {
152 if (hasModel()) {
153 return model.isCellEditable(rowIndex, columnIndex);
154 }
155 String editable = ((IWidget) table.getChildWidgets().get(columnIndex)).getProperty("editable");
156 return BooleanUtils.toBoolean(editable);
157 }
158
159
160
161
162
163
164 public Object getValueAt(int rowIndex, int columnIndex) {
165 if (hasModel()) {
166 return model.getValueAt(rowIndex, columnIndex);
167 }
168 if (columnIndex == -1) {
169 return ((List) table.getContent()).get(rowIndex);
170 }
171 IWidget w = (IWidget) table.getChildWidgets().get(columnIndex);
172 if (w.getProvider() != null && w.getField() != null) {
173 IDataProvider provider = XuluxContext.getDictionary().getProvider(w.getProvider());
174 Object rowValue = ((List) table.getContent()).get(rowIndex);
175 return provider.getValue(rowValue, w.getField(), rowValue);
176 } else if (w.getField() != null) {
177 IMapping map = null;
178 String alias = w.getProperty("alias");
179 Object obj = ((List) table.getContent()).get(rowIndex);
180 if (alias != null) {
181 map = XuluxContext.getDictionary().getDefaultProvider().getMapping(alias);
182 }
183 if (map == null) {
184 map = XuluxContext.getDictionary().getDefaultProvider().getMapping(obj);
185 }
186 IField field = map.getField(w.getField());
187 if (field != null) {
188 Object value = field.getValue(obj);
189 if (value == null) {
190 return "";
191 }
192 IConverter converter = Dictionary.getConverter(value);
193 if (converter != null) {
194 value = converter.getGuiValue(value);
195 }
196 return value;
197 }
198 }
199 return "";
200 }
201
202
203
204
205
206
207 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
208 if (hasModel()) {
209 model.setValueAt(aValue, rowIndex, columnIndex);
210 return;
211 }
212 IWidget w = (IWidget) table.getChildWidgets().get(columnIndex);
213 if (w.getField() != null) {
214 IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(((List) table.getContent()).get(rowIndex));
215 IField field = map.getField(w.getField());
216 if (field != null) {
217 field.setValue(((List) table.getContent()).get(rowIndex), aValue);
218 }
219 }
220
221 this.widget = w;
222
223 completed();
224 }
225
226
227
228
229 public void addTableModelListener(TableModelListener l) {
230 if (hasModel()) {
231 model.addTableModelListener(l);
232 return;
233 }
234 }
235
236
237
238
239 public void removeTableModelListener(TableModelListener l) {
240 if (hasModel()) {
241 model.removeTableModelListener(l);
242 return;
243 }
244 }
245
246
247
248
249
250 public void destroy() {
251 this.table = null;
252 this.model = null;
253 }
254
255
256
257
258 public void refresh() {
259
260 }
261
262 }