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