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