gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynformset / simpletable / SimpleTableJDynFormSet.java @ 2596
History | View | Annotate | Download (14.7 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.dynform.services.dynformset.simpletable; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.GridBagConstraints; |
29 |
import java.awt.GridBagLayout; |
30 |
import java.awt.Insets; |
31 |
import java.awt.event.ActionEvent; |
32 |
import java.awt.event.ActionListener; |
33 |
import java.net.URL; |
34 |
import java.util.ArrayList; |
35 |
import java.util.Arrays; |
36 |
import java.util.Collections; |
37 |
import java.util.Comparator; |
38 |
import java.util.Iterator; |
39 |
import java.util.List; |
40 |
import javax.swing.Action; |
41 |
|
42 |
import javax.swing.Icon; |
43 |
import javax.swing.ImageIcon; |
44 |
import javax.swing.JButton; |
45 |
import javax.swing.JComponent; |
46 |
import javax.swing.JPanel; |
47 |
import javax.swing.JScrollPane; |
48 |
import javax.swing.JTable; |
49 |
import javax.swing.ListSelectionModel; |
50 |
import javax.swing.event.ListSelectionEvent; |
51 |
import javax.swing.event.ListSelectionListener; |
52 |
import javax.swing.event.TableModelEvent; |
53 |
import javax.swing.event.TableModelListener; |
54 |
import javax.swing.table.DefaultTableColumnModel; |
55 |
import javax.swing.table.TableCellEditor; |
56 |
import javax.swing.table.TableColumn; |
57 |
import javax.swing.table.TableColumnModel; |
58 |
import javax.swing.table.TableModel; |
59 |
|
60 |
import org.gvsig.tools.ToolsLocator; |
61 |
import org.gvsig.tools.dataTypes.CoercionException; |
62 |
import org.gvsig.tools.dynform.DynFormDefinition; |
63 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
64 |
import org.gvsig.tools.dynform.JDynForm.DynFormContext; |
65 |
import org.gvsig.tools.dynform.spi.dynformset.AbstractJDynFormSet; |
66 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
67 |
import org.gvsig.tools.dynform.spi.dynformset.JDynFormSetFactory; |
68 |
import org.gvsig.tools.dynobject.DynField; |
69 |
import org.gvsig.tools.dynobject.DynObject; |
70 |
import org.gvsig.tools.dynobject.DynStruct; |
71 |
import org.gvsig.tools.dynobject.Tags; |
72 |
import org.gvsig.tools.service.ServiceException; |
73 |
|
74 |
public class SimpleTableJDynFormSet extends AbstractJDynFormSet { |
75 |
|
76 |
private JButton addButton; |
77 |
private JButton deleteButton; |
78 |
private JTable table; |
79 |
private DynObjectListTableModel tableModel;
|
80 |
private int currentIndex; |
81 |
private int currentCol; |
82 |
|
83 |
public SimpleTableJDynFormSet(
|
84 |
DynFormSPIManager manager, |
85 |
JDynFormSetFactory factory, |
86 |
DynFormContext context, |
87 |
DynFormDefinition definition, |
88 |
Tags contextTags |
89 |
) { |
90 |
super(manager, factory, context, definition, contextTags);
|
91 |
this.formHeight = 80; |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public void setValues(List values) { |
96 |
super.setValues(values);
|
97 |
this.tableModel.setValues(values);
|
98 |
if (values == null) { |
99 |
addButton.setEnabled(false);
|
100 |
deleteButton.setEnabled(false);
|
101 |
} else {
|
102 |
addButton.setEnabled(true);
|
103 |
deleteButton.setEnabled(!values.isEmpty()); |
104 |
} |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public JComponent asJComponent() { |
109 |
if (this.contents == null) { |
110 |
try {
|
111 |
this.initComponents();
|
112 |
} catch (ServiceException e) {
|
113 |
throw new RuntimeException(e.getLocalizedMessage(), e); |
114 |
} |
115 |
} |
116 |
this.fireFormMovedToEvent(0); |
117 |
return this.contents; |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public int getCurrentIndex() { |
122 |
return currentIndex;
|
123 |
} |
124 |
|
125 |
@Override
|
126 |
public void setCurrentIndex(int index) { |
127 |
if (index < tableModel.getRowCount()) {
|
128 |
this.currentIndex = index;
|
129 |
} |
130 |
} |
131 |
|
132 |
private void initComponents() throws ServiceException { |
133 |
this.contents = getTablePanel();
|
134 |
} |
135 |
|
136 |
private JPanel getTablePanel() { |
137 |
JPanel panel_chartType = new JPanel(); |
138 |
GridBagLayout gbl_panel_chartType = new GridBagLayout(); |
139 |
panel_chartType.setLayout(gbl_panel_chartType); |
140 |
createJTable(); |
141 |
GridBagConstraints gbc_table = new GridBagConstraints(); |
142 |
gbc_table.gridheight = 3;
|
143 |
gbc_table.gridwidth = 2;
|
144 |
gbc_table.insets = new Insets(0, 0, 0, 5); |
145 |
gbc_table.fill = GridBagConstraints.BOTH;
|
146 |
gbc_table.weightx = 1.0;
|
147 |
gbc_table.weighty = 0.0;
|
148 |
gbc_table.gridx = 0;
|
149 |
gbc_table.gridy = 0;
|
150 |
|
151 |
JScrollPane scrollPane = new JScrollPane(table); |
152 |
scrollPane.setPreferredSize(new Dimension(scrollPane.getWidth(), this.formHeight)); |
153 |
panel_chartType.add(scrollPane, gbc_table); |
154 |
|
155 |
GridBagConstraints gbc_addButton = new GridBagConstraints(); |
156 |
gbc_addButton.anchor = GridBagConstraints.EAST;
|
157 |
gbc_addButton.insets = new Insets(0, 0, 5, 0); |
158 |
gbc_addButton.gridx = 2;
|
159 |
gbc_addButton.gridy = 0;
|
160 |
panel_chartType.add(getAddButton(), gbc_addButton); |
161 |
|
162 |
GridBagConstraints gbc_deleteButton = new GridBagConstraints(); |
163 |
gbc_deleteButton.anchor = GridBagConstraints.NORTHEAST;
|
164 |
gbc_deleteButton.gridx = 2;
|
165 |
gbc_deleteButton.gridy = 2;
|
166 |
panel_chartType.add(getDeleteButton(), gbc_deleteButton); |
167 |
|
168 |
return panel_chartType;
|
169 |
|
170 |
} |
171 |
|
172 |
private void createJTable() { |
173 |
table = new JTable(); |
174 |
table.setBackground(Color.white);
|
175 |
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
176 |
table.setColumnModel(getColumnModel()); |
177 |
|
178 |
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
179 |
@Override
|
180 |
public void valueChanged(ListSelectionEvent event) { |
181 |
setCurrentIndex(table.getSelectedRow()); |
182 |
setCurrentCol(table.getSelectedColumn()); |
183 |
} |
184 |
}); |
185 |
|
186 |
this.tableModel = new DynObjectListTableModel(this.values, this.getDefinition().getElementsType()); |
187 |
table.setModel(tableModel); |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public void addAction(Action action) { |
192 |
LOGGER.info("AddAction not implemented over SimpleTableJDynFormSet.");
|
193 |
} |
194 |
|
195 |
@Override
|
196 |
public Action getAction(String actionId) { |
197 |
LOGGER.info("getAction not implemented over SimpleTableJDynFormSet.");
|
198 |
return null; |
199 |
} |
200 |
|
201 |
@Override
|
202 |
public List<Action> getActions() { |
203 |
LOGGER.info("getActions not implemented over SimpleTableJDynFormSet.");
|
204 |
return Collections.EMPTY_LIST; |
205 |
} |
206 |
|
207 |
@Override
|
208 |
public void setActionEnabled(String action, boolean enabled) { |
209 |
LOGGER.info("setActionEnabled not implemented over SimpleTableJDynFormSet.");
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public void setActionVisible(String action, boolean visible) { |
214 |
LOGGER.info("setActionVisible not implemented over SimpleTableJDynFormSet.");
|
215 |
} |
216 |
|
217 |
@Override
|
218 |
public boolean isActionEnabled(String action) { |
219 |
LOGGER.info("isActionEnabled not implemented over SimpleTableJDynFormSet.");
|
220 |
return true; |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public boolean isActionVisible(String action) { |
225 |
LOGGER.info("isActionVisible not implemented over SimpleTableJDynFormSet.");
|
226 |
return true; |
227 |
} |
228 |
|
229 |
@Override
|
230 |
public JComponent getActionButton(String action) { |
231 |
return null; |
232 |
} |
233 |
|
234 |
public static class DynObjectListTableModel implements TableModel { |
235 |
|
236 |
private List<DynObject> values; |
237 |
private DynStruct definition;
|
238 |
private final List<TableModelListener> listeners; |
239 |
private DynField[] fields; |
240 |
|
241 |
public DynObjectListTableModel(List<DynObject> objs, DynStruct struct) { |
242 |
this.values = objs;
|
243 |
this.definition = struct;
|
244 |
fields = this.definition.getDynFields();
|
245 |
Arrays.sort(fields, new Comparator<DynField>() { |
246 |
@Override
|
247 |
public int compare(DynField o1, DynField o2) { |
248 |
return o1.getOder() - o2.getOder();
|
249 |
} |
250 |
; |
251 |
}); |
252 |
this.listeners = new ArrayList<>(); |
253 |
} |
254 |
|
255 |
public void setValues(List objs) { |
256 |
this.values = objs;
|
257 |
fireTableChangeEvent(-1, -1); |
258 |
} |
259 |
|
260 |
@Override
|
261 |
public void addTableModelListener(TableModelListener arg0) { |
262 |
listeners.add(arg0); |
263 |
} |
264 |
|
265 |
@Override
|
266 |
public Class<?> getColumnClass(int arg0) { |
267 |
DynField field = this.fields[arg0];
|
268 |
return field.getDataType().getDefaultClass();
|
269 |
} |
270 |
|
271 |
@Override
|
272 |
public int getColumnCount() { |
273 |
return this.fields.length; |
274 |
} |
275 |
|
276 |
@Override
|
277 |
public String getColumnName(int index) { |
278 |
return this.fields[index].getName(); |
279 |
} |
280 |
|
281 |
@Override
|
282 |
public int getRowCount() { |
283 |
if (this.values == null) { |
284 |
return 0; |
285 |
} |
286 |
return this.values.size(); |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public Object getValueAt(int row, int col) { |
291 |
if (this.values == null) { |
292 |
return null; |
293 |
} |
294 |
DynObject value = this.values.get(row);
|
295 |
String colName = this.getColumnName(col); |
296 |
return value.getDynValue(colName);
|
297 |
} |
298 |
|
299 |
@Override
|
300 |
public boolean isCellEditable(int arg0, int arg1) { |
301 |
return true; |
302 |
} |
303 |
|
304 |
@Override
|
305 |
public void removeTableModelListener(TableModelListener arg0) { |
306 |
this.listeners.remove(arg0);
|
307 |
} |
308 |
|
309 |
@Override
|
310 |
public void setValueAt(Object arg0, int row, int col) { |
311 |
if (this.values == null) { |
312 |
return;
|
313 |
} |
314 |
DynObject value = this.values.get(row);
|
315 |
String colName = this.getColumnName(col); |
316 |
value.setDynValue(colName, arg0); |
317 |
fireTableChangeEvent(row, TableModelEvent.UPDATE);
|
318 |
} |
319 |
|
320 |
public void addNew() { |
321 |
if (this.values == null) { |
322 |
return;
|
323 |
} |
324 |
DynObject dynobj = ToolsLocator.getDynObjectManager().createDynObject(this.definition);
|
325 |
this.values.add(dynobj);
|
326 |
fireTableChangeEvent(this.values.size(), TableModelEvent.INSERT); |
327 |
} |
328 |
|
329 |
public void remove(int index) { |
330 |
if (this.values == null) { |
331 |
return;
|
332 |
} |
333 |
this.values.remove(index);
|
334 |
fireTableChangeEvent(index, TableModelEvent.DELETE);
|
335 |
} |
336 |
|
337 |
private void fireTableChangeEvent(int row, int type) { |
338 |
Iterator<TableModelListener> it = this.listeners.iterator(); |
339 |
while (it.hasNext()) {
|
340 |
TableModelListener listener = (TableModelListener) it.next(); |
341 |
TableModelEvent ev;
|
342 |
if (row < 0) { |
343 |
ev = new TableModelEvent(this); |
344 |
} else {
|
345 |
ev = new TableModelEvent(this, row, row, TableModelEvent.ALL_COLUMNS, type); |
346 |
} |
347 |
listener.tableChanged(ev); |
348 |
} |
349 |
} |
350 |
|
351 |
} |
352 |
|
353 |
private TableColumnModel getColumnModel() { |
354 |
TableColumnModel colModel = new DefaultTableColumnModel(); |
355 |
List<String> names = new ArrayList<>(); |
356 |
|
357 |
for (int i = 0; i < this.getDefinition().size(); i++) { |
358 |
TableColumn col = new TableColumn(); |
359 |
DynFormFieldDefinition field = this.getDefinition().get(i);
|
360 |
if (field.getTags().has(DynFormSPIManager.TAG_DYNFORM_WIDTH)) {
|
361 |
try {
|
362 |
int width = field.getTags().getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH);
|
363 |
col.setMaxWidth(width); |
364 |
} catch (CoercionException ex) {
|
365 |
// Do nothing
|
366 |
} |
367 |
} |
368 |
col.setHeaderValue(field.getLabel()); |
369 |
colModel.addColumn(col); |
370 |
} |
371 |
|
372 |
return colModel;
|
373 |
} |
374 |
|
375 |
private SimpleTableCellEditor getDefaultTableCellEditor(DynFormFieldDefinition field) {
|
376 |
return new SimpleTableCellEditor(this.getContext(), field, this.getServiceManager()); |
377 |
} |
378 |
|
379 |
private SimpleTableCellRenderer getDefaultTableCellRenderer(DynFormFieldDefinition field) {
|
380 |
return new SimpleTableCellRenderer(field); |
381 |
} |
382 |
|
383 |
private JButton getAddButton() { |
384 |
if (addButton == null) { |
385 |
addButton = new JButton(""); |
386 |
addButton.setIcon(getIcon("dynform-table-add.png"));
|
387 |
addButton.addActionListener(new ActionListener() { |
388 |
@Override
|
389 |
public void actionPerformed(ActionEvent arg0) { |
390 |
tableModel.addNew(); |
391 |
deleteButton.setEnabled(true);
|
392 |
} |
393 |
}); |
394 |
if (this.values == null) { |
395 |
addButton.setEnabled(false);
|
396 |
} |
397 |
} |
398 |
return addButton;
|
399 |
} |
400 |
|
401 |
private Icon getIcon(String fielName) { |
402 |
URL url = this.getClass().getClassLoader().getResource("org/gvsig/tools/dynform/services/" + fielName); |
403 |
return new ImageIcon(url); |
404 |
} |
405 |
|
406 |
private JButton getDeleteButton() { |
407 |
if (deleteButton == null) { |
408 |
deleteButton = new JButton(""); |
409 |
deleteButton.setIcon(getIcon("dynform-table-delete.png"));
|
410 |
deleteButton.setEnabled(false);
|
411 |
deleteButton.addActionListener(new ActionListener() { |
412 |
@Override
|
413 |
public void actionPerformed(ActionEvent arg0) { |
414 |
int index = table.getSelectedRow();
|
415 |
if (index >= 0) { |
416 |
tableModel.remove(index); |
417 |
if (table.getRowCount() == 0) { |
418 |
deleteButton.setEnabled(false);
|
419 |
} |
420 |
} |
421 |
} |
422 |
}); |
423 |
} |
424 |
return deleteButton;
|
425 |
} |
426 |
|
427 |
@Override
|
428 |
public void setReadOnly(boolean readOnly) { |
429 |
super.setReadOnly(readOnly);
|
430 |
this.table.setEnabled(!readOnly);
|
431 |
this.getAddButton().setEnabled(!readOnly);
|
432 |
this.getDeleteButton().setEnabled(!readOnly);
|
433 |
} |
434 |
|
435 |
void setCurrentCol(int col) { |
436 |
this.currentCol = col;
|
437 |
} |
438 |
|
439 |
@Override
|
440 |
public boolean hasValidValues() { |
441 |
return true; |
442 |
} |
443 |
|
444 |
@Override
|
445 |
public boolean hasValidValues(List<String> fieldsName) { |
446 |
return true; |
447 |
} |
448 |
|
449 |
@Override
|
450 |
public List getValues() { |
451 |
TableCellEditor editor = this.table.getCellEditor(); |
452 |
if (editor != null) { |
453 |
editor.stopCellEditing(); |
454 |
} |
455 |
return this.values; |
456 |
} |
457 |
|
458 |
} |