root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / Table.java @ 2645
History | View | Annotate | Download (14.5 KB)
1 | 1103 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 312 | fernando | package com.iver.cit.gvsig.gui; |
42 | |||
43 | 1047 | fernando | import java.beans.PropertyChangeEvent; |
44 | import java.beans.PropertyChangeListener; |
||
45 | 2266 | fernando | import java.io.IOException; |
46 | 312 | fernando | import java.text.DateFormat; |
47 | import java.text.NumberFormat; |
||
48 | 2481 | fernando | import java.text.ParseException; |
49 | 457 | fernando | import java.util.BitSet; |
50 | |||
51 | import javax.swing.DefaultListSelectionModel; |
||
52 | 312 | fernando | import javax.swing.JLabel; |
53 | import javax.swing.JPanel; |
||
54 | 457 | fernando | import javax.swing.event.ListSelectionEvent; |
55 | import javax.swing.event.ListSelectionListener; |
||
56 | 312 | fernando | import javax.swing.table.AbstractTableModel; |
57 | |||
58 | 464 | fernando | import org.apache.log4j.Logger; |
59 | 312 | fernando | |
60 | 2481 | fernando | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
61 | 1830 | fernando | import com.hardcode.gdbms.engine.data.driver.DriverException; |
62 | 2481 | fernando | import com.hardcode.gdbms.engine.data.edition.DataWare; |
63 | import com.hardcode.gdbms.engine.values.Value; |
||
64 | 464 | fernando | import com.hardcode.gdbms.engine.values.ValueFactory; |
65 | 596 | fernando | import com.iver.andami.PluginServices; |
66 | 732 | fernando | import com.iver.andami.messages.NotificationManager; |
67 | 596 | fernando | import com.iver.andami.ui.mdiManager.SingletonView; |
68 | import com.iver.andami.ui.mdiManager.ViewInfo; |
||
69 | 464 | fernando | import com.iver.cit.gvsig.fmap.FMap; |
70 | 694 | fernando | import com.iver.cit.gvsig.fmap.layers.FLayer; |
71 | 464 | fernando | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
72 | import com.iver.cit.gvsig.fmap.layers.SelectionEvent; |
||
73 | import com.iver.cit.gvsig.fmap.layers.SelectionListener; |
||
74 | 1221 | fernando | import com.iver.cit.gvsig.project.ProjectTable; |
75 | 2241 | fernando | import com.iver.utiles.swing.jtable.FieldSelectionEvent; |
76 | import com.iver.utiles.swing.jtable.FieldSelectionListener; |
||
77 | 2481 | fernando | import com.iver.utiles.swing.jtable.JTable; |
78 | 2241 | fernando | import com.iver.utiles.swing.jtable.SelectionHeaderSupport; |
79 | 2481 | fernando | import com.iver.utiles.swing.jtable.TextFieldCellEditor; |
80 | 464 | fernando | |
81 | |||
82 | 428 | fernando | /**
|
83 | * DOCUMENT ME!
|
||
84 | *
|
||
85 | * @author Fernando Gonz?lez Cort?s
|
||
86 | */
|
||
87 | 1830 | fernando | public class Table extends JPanel implements SingletonView { |
88 | 428 | fernando | private static Logger logger = Logger.getLogger(Table.class.getName()); |
89 | private javax.swing.JScrollPane jScrollPane = null; |
||
90 | 2481 | fernando | private JTable table = null; |
91 | 457 | fernando | private ProjectTable model = null; |
92 | 428 | fernando | private DateFormat df = DateFormat.getDateInstance(); |
93 | private NumberFormat nf = NumberFormat.getInstance(); |
||
94 | private JLabel jLabelStatus = null; |
||
95 | private ViewInfo m_viewInfo = null; |
||
96 | 457 | fernando | private FMap fmap;
|
97 | |||
98 | private boolean updating = false; |
||
99 | |||
100 | private TableSelectionListener selectionListener = new TableSelectionListener(); |
||
101 | 2202 | vcaballero | private long numReg=0; |
102 | 2266 | fernando | private SelectionHeaderSupport headerSelectionSupport = new SelectionHeaderSupport(); |
103 | 2241 | fernando | |
104 | 2266 | fernando | private long[] orderIndexes = null; |
105 | private long[] orderIndexesInverted = null; |
||
106 | |||
107 | 2481 | fernando | private DataWare dw;
|
108 | |||
109 | 428 | fernando | /**
|
110 | * This is the default constructor
|
||
111 | */
|
||
112 | public Table() {
|
||
113 | super();
|
||
114 | initialize(); |
||
115 | } |
||
116 | 312 | fernando | |
117 | 428 | fernando | /**
|
118 | * DOCUMENT ME!
|
||
119 | *
|
||
120 | * @return DOCUMENT ME!
|
||
121 | */
|
||
122 | 457 | fernando | public ProjectTable getModel() {
|
123 | return model;
|
||
124 | 428 | fernando | } |
125 | 2241 | fernando | |
126 | public BitSet getSelectedFieldIndices(){ |
||
127 | 2266 | fernando | return headerSelectionSupport.getSelectedColumns();
|
128 | 2241 | fernando | } |
129 | 312 | fernando | |
130 | 1830 | fernando | private void refreshControls(){ |
131 | try{
|
||
132 | PluginServices.getMDIManager().getViewInfo(Table.this).setTitle(PluginServices.getText(this, "Tabla")+": " + model.getName()); |
||
133 | |||
134 | |||
135 | |||
136 | if (model.getAssociatedTable() != null){ |
||
137 | this.fmap = ((FLayer)model.getAssociatedTable()).getFMap();
|
||
138 | }else{
|
||
139 | this.fmap = null; |
||
140 | } |
||
141 | |||
142 | SelectableDataSource dataSource = model.getModelo(); |
||
143 | logger.debug("dataSource.start()");
|
||
144 | dataSource.start(); |
||
145 | |||
146 | 2565 | fernando | AbstractTableModel dataModel = new DataSourceDataModel(model); |
147 | 1830 | fernando | |
148 | 2241 | fernando | getTable().setModel(dataModel); |
149 | |||
150 | 2266 | fernando | headerSelectionSupport.setTableHeader(getTable().getTableHeader()); |
151 | headerSelectionSupport.addFieldSelectionListener(new FieldSelectionListener() {
|
||
152 | 2241 | fernando | public void fieldSelected(FieldSelectionEvent e) { |
153 | PluginServices.getMainFrame().enableControls(); |
||
154 | } |
||
155 | }); |
||
156 | 1830 | fernando | |
157 | model.getModelo().addSelectionListener(selectionListener); |
||
158 | |||
159 | updateSelection(); |
||
160 | }catch (DriverException e){
|
||
161 | NotificationManager.addError("No se pudo leer la informaci?n", e);
|
||
162 | } |
||
163 | |||
164 | } |
||
165 | |||
166 | 428 | fernando | /**
|
167 | * DOCUMENT ME!
|
||
168 | *
|
||
169 | 457 | fernando | * @param table DOCUMENT ME!
|
170 | 428 | fernando | */
|
171 | 694 | fernando | public void setModel(ProjectTable table) { |
172 | 1830 | fernando | SelectableDataSource dataSource; |
173 | model = table; |
||
174 | 457 | fernando | |
175 | 1830 | fernando | //Gesti?n del nombre de la ventana
|
176 | model.addPropertyChangeListener(new PropertyChangeListener() { |
||
177 | public void propertyChange(PropertyChangeEvent evt) { |
||
178 | if (evt.getPropertyName().equals("name")){ |
||
179 | PluginServices.getMDIManager().getViewInfo(Table.this).setTitle(PluginServices.getText(this, "Tabla")+": " + (String)evt.getNewValue()); |
||
180 | }else if (evt.getPropertyName().equals("model")){ |
||
181 | refreshControls(); |
||
182 | } |
||
183 | } |
||
184 | }); |
||
185 | |||
186 | refreshControls(); |
||
187 | 428 | fernando | } |
188 | |||
189 | /**
|
||
190 | 457 | fernando | *
|
191 | */
|
||
192 | private void updateSelection() { |
||
193 | updating = true;
|
||
194 | |||
195 | DefaultListSelectionModel sm = (DefaultListSelectionModel) getTable() |
||
196 | 2266 | fernando | .getSelectionModel(); |
197 | 457 | fernando | sm.clearSelection(); |
198 | 464 | fernando | BitSet bs = model.getModelo().getSelection();
|
199 | 457 | fernando | sm.setValueIsAdjusting(true);
|
200 | |||
201 | 2481 | fernando | if (orderIndexes != null){ |
202 | for (int i = 0; i < orderIndexes.length; i++) { |
||
203 | if (bs.get(i)){
|
||
204 | sm.addSelectionInterval((int) orderIndexesInverted[i], (int) orderIndexesInverted[i]); |
||
205 | } |
||
206 | } |
||
207 | }else{
|
||
208 | for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) { |
||
209 | 2266 | fernando | sm.addSelectionInterval(i, i); |
210 | } |
||
211 | 457 | fernando | } |
212 | |||
213 | sm.setValueIsAdjusting(false);
|
||
214 | |||
215 | 2530 | ldiaz | try {
|
216 | numReg=model.getModelo().getRowCount(); |
||
217 | } catch (DriverException e) {
|
||
218 | NotificationManager.addError("", e);
|
||
219 | } |
||
220 | jLabelStatus.setText(" " + model.getModelo().getSelection().cardinality() + " / " + numReg +" "+ |
||
221 | PluginServices.getText(this, "registros_seleccionados_total") + "."); |
||
222 | |||
223 | 457 | fernando | updating = false;
|
224 | } |
||
225 | |||
226 | /**
|
||
227 | 428 | fernando | * This method initializes this
|
228 | */
|
||
229 | private void initialize() { |
||
230 | jLabelStatus = new JLabel(); |
||
231 | setLayout(new java.awt.BorderLayout());
|
||
232 | jLabelStatus.setText("");
|
||
233 | jLabelStatus.setName("");
|
||
234 | jLabelStatus.setPreferredSize(new java.awt.Dimension(100, 18)); |
||
235 | add(getJScrollPane(), java.awt.BorderLayout.CENTER); |
||
236 | this.add(jLabelStatus, java.awt.BorderLayout.SOUTH);
|
||
237 | } |
||
238 | |||
239 | /**
|
||
240 | * This method initializes table
|
||
241 | *
|
||
242 | * @return javax.swing.JTable
|
||
243 | */
|
||
244 | private javax.swing.JTable getTable() {
|
||
245 | if (table == null) { |
||
246 | 2481 | fernando | table = new JTable(); |
247 | 428 | fernando | table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); |
248 | 457 | fernando | table.setSelectionModel(new DefaultListSelectionModel()); |
249 | table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
||
250 | public void valueChanged(ListSelectionEvent e) { |
||
251 | if (updating) return; |
||
252 | 2266 | fernando | |
253 | 464 | fernando | SelectableDataSource dataModel = Table.this.model.getModelo(); |
254 | |||
255 | DefaultListSelectionModel model = (DefaultListSelectionModel) table.getSelectionModel(); |
||
256 | BitSet selection = dataModel.getSelection();
|
||
257 | 428 | fernando | |
258 | 464 | fernando | for (int i = e.getFirstIndex(); |
259 | i <= e.getLastIndex(); i++) { |
||
260 | 2266 | fernando | if (orderIndexes != null){ |
261 | selection.set((int) orderIndexes[i], model.isSelectedIndex(i));
|
||
262 | }else{
|
||
263 | selection.set(i, model.isSelectedIndex(i)); |
||
264 | } |
||
265 | 457 | fernando | } |
266 | |||
267 | if (e.getValueIsAdjusting() == false) { |
||
268 | if (fmap != null){ |
||
269 | 681 | fernando | fmap.endAtomicEvent(); |
270 | 457 | fernando | } |
271 | 464 | fernando | dataModel.fireSelectionEvents(); |
272 | 681 | fernando | }else{
|
273 | 694 | fernando | if (fmap != null) |
274 | 681 | fernando | fmap.beginAtomicEvent(); |
275 | 457 | fernando | } |
276 | 2202 | vcaballero | jLabelStatus.setText(" " + selection.cardinality() + " / " + numReg + " " + |
277 | PluginServices.getText(this, "registros_seleccionados_total") + "."); |
||
278 | 428 | fernando | } |
279 | }); |
||
280 | 2481 | fernando | TextFieldCellEditor ce = new TextFieldCellEditor();
|
281 | for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) { |
||
282 | table.getColumnModel().getColumn(i).setCellEditor(ce); |
||
283 | } |
||
284 | 428 | fernando | } |
285 | |||
286 | return table;
|
||
287 | } |
||
288 | |||
289 | /**
|
||
290 | * This method initializes jScrollPane
|
||
291 | *
|
||
292 | * @return javax.swing.JScrollPane
|
||
293 | */
|
||
294 | private javax.swing.JScrollPane getJScrollPane() {
|
||
295 | if (jScrollPane == null) { |
||
296 | jScrollPane = new javax.swing.JScrollPane();
|
||
297 | jScrollPane.setViewportView(getTable()); |
||
298 | } |
||
299 | |||
300 | return jScrollPane;
|
||
301 | } |
||
302 | |||
303 | /**
|
||
304 | * @see com.iver.mdiApp.ui.MDIManager.SingletonView#getViewModel()
|
||
305 | */
|
||
306 | public Object getViewModel() { |
||
307 | 457 | fernando | return model;
|
308 | 428 | fernando | } |
309 | |||
310 | /**
|
||
311 | * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
|
||
312 | */
|
||
313 | public ViewInfo getViewInfo() {
|
||
314 | 596 | fernando | ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE | ViewInfo.MAXIMIZABLE |
|
315 | 428 | fernando | ViewInfo.RESIZABLE); |
316 | 596 | fernando | m_viewInfo.setTitle(PluginServices.getText(this, "Tabla")); |
317 | 428 | fernando | m_viewInfo.setWidth(300);
|
318 | m_viewInfo.setHeight(200);
|
||
319 | |||
320 | return m_viewInfo;
|
||
321 | } |
||
322 | |||
323 | /**
|
||
324 | 457 | fernando | * DOCUMENT ME!
|
325 | *
|
||
326 | * @author Fernando Gonz?lez Cort?s
|
||
327 | */
|
||
328 | public class DataSourceDataModel extends AbstractTableModel { |
||
329 | 464 | fernando | private SelectableDataSource dataSource;
|
330 | 2565 | fernando | private ProjectTable pt;
|
331 | private String[] aliases; |
||
332 | private int[] mapping; |
||
333 | 457 | fernando | |
334 | /**
|
||
335 | * Crea un nuevo DataSourceDataModel.
|
||
336 | *
|
||
337 | * @param ds DOCUMENT ME!
|
||
338 | */
|
||
339 | 2565 | fernando | public DataSourceDataModel(ProjectTable pt) {
|
340 | this.pt = pt;
|
||
341 | dataSource = pt.getModelo(); |
||
342 | aliases = pt.getAliases(); |
||
343 | mapping = pt.getMapping(); |
||
344 | 457 | fernando | } |
345 | |||
346 | /**
|
||
347 | * DOCUMENT ME!
|
||
348 | *
|
||
349 | * @param col DOCUMENT ME!
|
||
350 | *
|
||
351 | * @return DOCUMENT ME!
|
||
352 | */
|
||
353 | public String getColumnName(int col) { |
||
354 | 2565 | fernando | return aliases[mapping[col]];
|
355 | 457 | fernando | } |
356 | |||
357 | /**
|
||
358 | * DOCUMENT ME!
|
||
359 | *
|
||
360 | * @return DOCUMENT ME!
|
||
361 | */
|
||
362 | public int getColumnCount() { |
||
363 | 2565 | fernando | return mapping.length;
|
364 | 457 | fernando | } |
365 | |||
366 | /**
|
||
367 | * DOCUMENT ME!
|
||
368 | *
|
||
369 | * @return DOCUMENT ME!
|
||
370 | */
|
||
371 | public int getRowCount() { |
||
372 | try {
|
||
373 | return (int) dataSource.getRowCount(); |
||
374 | } catch (DriverException e) {
|
||
375 | return 0; |
||
376 | } |
||
377 | } |
||
378 | |||
379 | /**
|
||
380 | * DOCUMENT ME!
|
||
381 | *
|
||
382 | * @param row DOCUMENT ME!
|
||
383 | * @param col DOCUMENT ME!
|
||
384 | *
|
||
385 | * @return DOCUMENT ME!
|
||
386 | */
|
||
387 | public Object getValueAt(int row, int col) { |
||
388 | try {
|
||
389 | 2481 | fernando | if (dw != null){ |
390 | 2565 | fernando | return dw.getFieldValue(row, mapping[col]);
|
391 | 2266 | fernando | }else{
|
392 | 2481 | fernando | if (orderIndexes != null){ |
393 | 2565 | fernando | return dataSource.getFieldValue(orderIndexes[row], mapping[col]);
|
394 | 2481 | fernando | }else{
|
395 | 2565 | fernando | return dataSource.getFieldValue(row, mapping[col]);
|
396 | 2481 | fernando | } |
397 | 2266 | fernando | } |
398 | 457 | fernando | } catch (DriverException e) {
|
399 | return ValueFactory.createValue("").toString(); |
||
400 | } |
||
401 | } |
||
402 | 2481 | fernando | public boolean isCellEditable(int rowIndex, int columnIndex) { |
403 | return (dw != null); |
||
404 | } |
||
405 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
||
406 | Value v; |
||
407 | try {
|
||
408 | v = ValueFactory.createValueByType(aValue.toString(), dataSource.getFieldType(columnIndex)); |
||
409 | 2565 | fernando | dw.setFieldValue(rowIndex, mapping[columnIndex], v); |
410 | 2481 | fernando | } catch (DriverException e1) {
|
411 | throw new RuntimeException(e1); |
||
412 | } catch (ParseException e) { |
||
413 | throw new RuntimeException(e); |
||
414 | } |
||
415 | } |
||
416 | 457 | fernando | } |
417 | |||
418 | 464 | fernando | public class TableSelectionListener implements SelectionListener{ |
419 | 457 | fernando | |
420 | /**
|
||
421 | 564 | fernando | * @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
422 | 457 | fernando | */
|
423 | 464 | fernando | public void selectionChanged(SelectionEvent e) { |
424 | 457 | fernando | updateSelection(); |
425 | Table.this.repaint(); |
||
426 | } |
||
427 | |||
428 | } |
||
429 | 2241 | fernando | |
430 | /**
|
||
431 | * @param ds
|
||
432 | 2266 | fernando | * @throws IOException
|
433 | 2241 | fernando | */
|
434 | 2266 | fernando | public void setOrder(long[] indexes) throws IOException { |
435 | orderIndexes = indexes; |
||
436 | orderIndexesInverted = new long[orderIndexes.length]; |
||
437 | for (int i = 0; i < indexes.length; i++) { |
||
438 | orderIndexesInverted[(int) indexes[i]] = i;
|
||
439 | } |
||
440 | 2481 | fernando | |
441 | updating=true;
|
||
442 | 2266 | fernando | ((DataSourceDataModel)getTable().getModel()).fireTableDataChanged(); |
443 | 2481 | fernando | updating=false;
|
444 | updateSelection(); |
||
445 | |||
446 | 2241 | fernando | } |
447 | 2481 | fernando | |
448 | public void startEditing() throws DriverException{ |
||
449 | dw = model.getModelo().getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER); |
||
450 | dw.beginTrans(); |
||
451 | } |
||
452 | |||
453 | /**
|
||
454 | * @throws DriverException
|
||
455 | *
|
||
456 | */
|
||
457 | public void cancelEditing() throws DriverException { |
||
458 | dw.rollBackTrans(); |
||
459 | dw = null;
|
||
460 | } |
||
461 | |||
462 | /**
|
||
463 | * @throws DriverException
|
||
464 | *
|
||
465 | */
|
||
466 | public void stopEditing() throws DriverException { |
||
467 | dw.commitTrans(); |
||
468 | dw = null;
|
||
469 | model.getModelo().stop(); |
||
470 | model.getModelo().start(); |
||
471 | } |
||
472 | |||
473 | /**
|
||
474 | * @return
|
||
475 | */
|
||
476 | public boolean isEditing() { |
||
477 | return dw != null; |
||
478 | } |
||
479 | 312 | fernando | } |