svn-gvsig-desktop / tags / gvSIGv0_6_1RELEASE / applications / appgvSIG / src / com / iver / cit / gvsig / gui / Table.java @ 5222
History | View | Annotate | Download (17.9 KB)
1 |
/* 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 |
package com.iver.cit.gvsig.gui; |
42 |
|
43 |
import java.awt.event.MouseAdapter; |
44 |
import java.awt.event.MouseEvent; |
45 |
import java.beans.PropertyChangeEvent; |
46 |
import java.beans.PropertyChangeListener; |
47 |
import java.io.IOException; |
48 |
import java.sql.Date; |
49 |
import java.sql.Time; |
50 |
import java.sql.Timestamp; |
51 |
import java.text.DateFormat; |
52 |
import java.text.NumberFormat; |
53 |
import java.text.ParseException; |
54 |
import java.util.ArrayList; |
55 |
import java.util.BitSet; |
56 |
|
57 |
import javax.swing.DefaultListSelectionModel; |
58 |
import javax.swing.JLabel; |
59 |
import javax.swing.JPanel; |
60 |
import javax.swing.event.ListSelectionEvent; |
61 |
import javax.swing.event.ListSelectionListener; |
62 |
import javax.swing.table.AbstractTableModel; |
63 |
import javax.swing.table.DefaultTableModel; |
64 |
import javax.swing.table.JTableHeader; |
65 |
import javax.swing.table.TableColumnModel; |
66 |
|
67 |
import org.apache.log4j.Logger; |
68 |
|
69 |
import com.hardcode.gdbms.engine.data.DataSource; |
70 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
71 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
72 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
73 |
import com.hardcode.gdbms.engine.values.LongValue; |
74 |
import com.hardcode.gdbms.engine.values.Value; |
75 |
import com.hardcode.gdbms.engine.values.ValueCollection; |
76 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
77 |
import com.iver.andami.PluginServices; |
78 |
import com.iver.andami.messages.NotificationManager; |
79 |
import com.iver.andami.ui.mdiFrame.MainFrame; |
80 |
import com.iver.andami.ui.mdiManager.SingletonView; |
81 |
import com.iver.andami.ui.mdiManager.ViewInfo; |
82 |
import com.iver.cit.gvsig.fmap.FMap; |
83 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
84 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
85 |
import com.iver.cit.gvsig.fmap.layers.SelectionEvent; |
86 |
import com.iver.cit.gvsig.fmap.layers.SelectionListener; |
87 |
import com.iver.cit.gvsig.gui.layout.Popupmenu; |
88 |
import com.iver.cit.gvsig.gui.tables.PopupMenu; |
89 |
import com.iver.cit.gvsig.project.ProjectTable; |
90 |
import com.iver.utiles.swing.jtable.FieldSelectionEvent; |
91 |
import com.iver.utiles.swing.jtable.FieldSelectionListener; |
92 |
import com.iver.utiles.swing.jtable.JTable; |
93 |
import com.iver.utiles.swing.jtable.SelectionHeaderSupport; |
94 |
import com.iver.utiles.swing.jtable.TextFieldCellEditor; |
95 |
|
96 |
|
97 |
/**
|
98 |
* DOCUMENT ME!
|
99 |
*
|
100 |
* @author Fernando Gonz?lez Cort?s
|
101 |
*/
|
102 |
public class Table extends JPanel implements SingletonView { |
103 |
private static Logger logger = Logger.getLogger(Table.class.getName()); |
104 |
private javax.swing.JScrollPane jScrollPane = null; |
105 |
private JTable table = null; |
106 |
private ProjectTable model = null; |
107 |
private DateFormat df = DateFormat.getDateInstance(); |
108 |
private NumberFormat nf = NumberFormat.getInstance(); |
109 |
private JLabel jLabelStatus = null; |
110 |
private ViewInfo m_viewInfo = null; |
111 |
private FMap fmap;
|
112 |
|
113 |
private boolean updating = false; |
114 |
|
115 |
private TableSelectionListener selectionListener = new TableSelectionListener(); |
116 |
private long numReg=0; |
117 |
private SelectionHeaderSupport headerSelectionSupport = new SelectionHeaderSupport(); |
118 |
|
119 |
private long[] orderIndexes = null; |
120 |
private long[] orderIndexesInverted = null; |
121 |
|
122 |
private DataWare dw;
|
123 |
private ArrayList rowsCopied=new ArrayList(); |
124 |
|
125 |
/**
|
126 |
* This is the default constructor
|
127 |
*/
|
128 |
public Table() {
|
129 |
super();
|
130 |
initialize(); |
131 |
} |
132 |
|
133 |
/**
|
134 |
* DOCUMENT ME!
|
135 |
*
|
136 |
* @return DOCUMENT ME!
|
137 |
*/
|
138 |
public ProjectTable getModel() {
|
139 |
return model;
|
140 |
} |
141 |
|
142 |
public BitSet getSelectedFieldIndices(){ |
143 |
return headerSelectionSupport.getSelectedColumns();
|
144 |
} |
145 |
public int[] getSelectedRowIndices(){ |
146 |
return getTable().getSelectedRows();
|
147 |
} |
148 |
private void refreshControls(){ |
149 |
try{
|
150 |
MainFrame mF = PluginServices.getMainFrame(); |
151 |
if (mF != null) |
152 |
PluginServices.getMDIManager().getViewInfo(Table.this).setTitle(PluginServices.getText(this, "Tabla")+": " + model.getName()); |
153 |
|
154 |
if (model.getAssociatedTable() != null){ |
155 |
this.fmap = ((FLayer)model.getAssociatedTable()).getFMap();
|
156 |
}else{
|
157 |
this.fmap = null; |
158 |
} |
159 |
|
160 |
SelectableDataSource dataSource = model.getModelo(); |
161 |
logger.debug("dataSource.start()");
|
162 |
dataSource.start(); |
163 |
|
164 |
AbstractTableModel dataModel = new DataSourceDataModel(model); |
165 |
|
166 |
getTable().setModel(dataModel); |
167 |
|
168 |
headerSelectionSupport.setTableHeader(getTable().getTableHeader()); |
169 |
headerSelectionSupport.addFieldSelectionListener(new FieldSelectionListener() {
|
170 |
public void fieldSelected(FieldSelectionEvent e) { |
171 |
if (PluginServices.getMainFrame() != null) |
172 |
PluginServices.getMainFrame().enableControls(); |
173 |
} |
174 |
}); |
175 |
|
176 |
model.getModelo().addSelectionListener(selectionListener); |
177 |
|
178 |
updateSelection(); |
179 |
}catch (DriverException e){
|
180 |
NotificationManager.addError("No se pudo leer la informaci?n", e);
|
181 |
} |
182 |
|
183 |
} |
184 |
|
185 |
/**
|
186 |
* DOCUMENT ME!
|
187 |
*
|
188 |
* @param table DOCUMENT ME!
|
189 |
*/
|
190 |
public void setModel(ProjectTable table) { |
191 |
SelectableDataSource dataSource; |
192 |
model = table; |
193 |
|
194 |
//Gesti?n del nombre de la ventana
|
195 |
model.addPropertyChangeListener(new PropertyChangeListener() { |
196 |
public void propertyChange(PropertyChangeEvent evt) { |
197 |
if (evt.getPropertyName().equals("name")){ |
198 |
PluginServices.getMDIManager().getViewInfo(Table.this).setTitle(PluginServices.getText(this, "Tabla")+": " + (String)evt.getNewValue()); |
199 |
}else if (evt.getPropertyName().equals("model")){ |
200 |
refreshControls(); |
201 |
} |
202 |
} |
203 |
}); |
204 |
|
205 |
refreshControls(); |
206 |
} |
207 |
|
208 |
/**
|
209 |
*
|
210 |
*/
|
211 |
private void updateSelection() { |
212 |
updating = true;
|
213 |
|
214 |
DefaultListSelectionModel sm = (DefaultListSelectionModel) getTable() |
215 |
.getSelectionModel(); |
216 |
sm.clearSelection(); |
217 |
BitSet bs = model.getModelo().getSelection();
|
218 |
sm.setValueIsAdjusting(true);
|
219 |
|
220 |
if (orderIndexes != null){ |
221 |
for (int i = 0; i < orderIndexes.length; i++) { |
222 |
if (bs.get(i)){
|
223 |
sm.addSelectionInterval((int) orderIndexesInverted[i], (int) orderIndexesInverted[i]); |
224 |
} |
225 |
} |
226 |
}else{
|
227 |
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) { |
228 |
sm.addSelectionInterval(i, i); |
229 |
} |
230 |
} |
231 |
|
232 |
sm.setValueIsAdjusting(false);
|
233 |
|
234 |
try {
|
235 |
numReg=model.getModelo().getRowCount(); |
236 |
} catch (DriverException e) {
|
237 |
NotificationManager.addError("", e);
|
238 |
} |
239 |
jLabelStatus.setText(" " + model.getModelo().getSelection().cardinality() + " / " + numReg +" "+ |
240 |
PluginServices.getText(this, "registros_seleccionados_total") + "."); |
241 |
|
242 |
updating = false;
|
243 |
} |
244 |
|
245 |
/**
|
246 |
* This method initializes this
|
247 |
*/
|
248 |
private void initialize() { |
249 |
jLabelStatus = new JLabel(); |
250 |
setLayout(new java.awt.BorderLayout());
|
251 |
jLabelStatus.setText("");
|
252 |
jLabelStatus.setName("");
|
253 |
jLabelStatus.setPreferredSize(new java.awt.Dimension(100, 18)); |
254 |
add(getJScrollPane(), java.awt.BorderLayout.CENTER); |
255 |
this.add(jLabelStatus, java.awt.BorderLayout.SOUTH);
|
256 |
} |
257 |
|
258 |
/**
|
259 |
* This method initializes table
|
260 |
*
|
261 |
* @return javax.swing.JTable
|
262 |
*/
|
263 |
private javax.swing.JTable getTable() {
|
264 |
if (table == null) { |
265 |
table = new JTable(); |
266 |
table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); |
267 |
table.setSelectionModel(new DefaultListSelectionModel()); |
268 |
table.getTableHeader().addMouseListener(new MouseHandler());
|
269 |
table.addMouseListener(new MouseRow());
|
270 |
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
271 |
public void valueChanged(ListSelectionEvent e) { |
272 |
if (updating) return; |
273 |
|
274 |
SelectableDataSource dataModel = Table.this.model.getModelo(); |
275 |
|
276 |
DefaultListSelectionModel model = (DefaultListSelectionModel) table.getSelectionModel(); |
277 |
BitSet selection = dataModel.getSelection();
|
278 |
|
279 |
for (int i = e.getFirstIndex(); |
280 |
i <= e.getLastIndex(); i++) { |
281 |
if (orderIndexes != null){ |
282 |
selection.set((int) orderIndexes[i], model.isSelectedIndex(i));
|
283 |
}else{
|
284 |
selection.set(i, model.isSelectedIndex(i)); |
285 |
} |
286 |
} |
287 |
|
288 |
if (e.getValueIsAdjusting() == false) { |
289 |
if (fmap != null){ |
290 |
fmap.endAtomicEvent(); |
291 |
} |
292 |
dataModel.fireSelectionEvents(); |
293 |
}else{
|
294 |
if (fmap != null) |
295 |
fmap.beginAtomicEvent(); |
296 |
} |
297 |
jLabelStatus.setText(" " + selection.cardinality() + " / " + numReg + " " + |
298 |
PluginServices.getText(this, "registros_seleccionados_total") + "."); |
299 |
} |
300 |
}); |
301 |
TextFieldCellEditor ce = new TextFieldCellEditor();
|
302 |
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) { |
303 |
table.getColumnModel().getColumn(i).setCellEditor(ce); |
304 |
} |
305 |
} |
306 |
|
307 |
return table;
|
308 |
} |
309 |
|
310 |
/**
|
311 |
* This method initializes jScrollPane
|
312 |
*
|
313 |
* @return javax.swing.JScrollPane
|
314 |
*/
|
315 |
private javax.swing.JScrollPane getJScrollPane() {
|
316 |
if (jScrollPane == null) { |
317 |
jScrollPane = new javax.swing.JScrollPane();
|
318 |
jScrollPane.setViewportView(getTable()); |
319 |
} |
320 |
|
321 |
return jScrollPane;
|
322 |
} |
323 |
|
324 |
/**
|
325 |
* @see com.iver.mdiApp.ui.MDIManager.SingletonView#getViewModel()
|
326 |
*/
|
327 |
public Object getViewModel() { |
328 |
return model;
|
329 |
} |
330 |
|
331 |
/**
|
332 |
* @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
|
333 |
*/
|
334 |
public ViewInfo getViewInfo() {
|
335 |
ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE | ViewInfo.MAXIMIZABLE |
|
336 |
ViewInfo.RESIZABLE); |
337 |
m_viewInfo.setTitle(PluginServices.getText(this, "Tabla")); |
338 |
m_viewInfo.setWidth(300);
|
339 |
m_viewInfo.setHeight(200);
|
340 |
|
341 |
return m_viewInfo;
|
342 |
} |
343 |
|
344 |
/**
|
345 |
* DOCUMENT ME!
|
346 |
*
|
347 |
* @author Fernando Gonz?lez Cort?s
|
348 |
*/
|
349 |
public class DataSourceDataModel extends AbstractTableModel { |
350 |
private SelectableDataSource dataSource;
|
351 |
private ProjectTable pt;
|
352 |
private String[] aliases; |
353 |
private int[] mapping; |
354 |
|
355 |
/**
|
356 |
* Crea un nuevo DataSourceDataModel.
|
357 |
*
|
358 |
* @param ds DOCUMENT ME!
|
359 |
*/
|
360 |
public DataSourceDataModel(ProjectTable pt) {
|
361 |
this.pt = pt;
|
362 |
dataSource = pt.getModelo(); |
363 |
aliases = pt.getAliases(); |
364 |
mapping = pt.getMapping(); |
365 |
} |
366 |
|
367 |
/**
|
368 |
* Returns the name of the field.
|
369 |
*
|
370 |
* @param col index of field
|
371 |
*
|
372 |
* @return Name of field
|
373 |
*/
|
374 |
public String getColumnName(int col) { |
375 |
return aliases[mapping[col]];
|
376 |
} |
377 |
|
378 |
/**
|
379 |
* Returns the number of fields.
|
380 |
*
|
381 |
* @return number of fields
|
382 |
*/
|
383 |
public int getColumnCount() { |
384 |
return mapping.length;
|
385 |
} |
386 |
|
387 |
/**
|
388 |
* Returns number of rows.
|
389 |
*
|
390 |
* @return number of rows.
|
391 |
*/
|
392 |
public int getRowCount() { |
393 |
try {
|
394 |
return (int) dataSource.getRowCount(); |
395 |
} catch (DriverException e) {
|
396 |
return 0; |
397 |
} |
398 |
} |
399 |
|
400 |
/**
|
401 |
* DOCUMENT ME!
|
402 |
*
|
403 |
* @param row DOCUMENT ME!
|
404 |
* @param col DOCUMENT ME!
|
405 |
*
|
406 |
* @return DOCUMENT ME!
|
407 |
*/
|
408 |
public Object getValueAt(int row, int col) { |
409 |
try {
|
410 |
if (dw != null){ |
411 |
return dw.getFieldValue(row, mapping[col]);
|
412 |
}else{
|
413 |
if (orderIndexes != null){ |
414 |
return dataSource.getFieldValue(orderIndexes[row], mapping[col]);
|
415 |
}else{
|
416 |
return dataSource.getFieldValue(row, mapping[col]);
|
417 |
} |
418 |
} |
419 |
} catch (DriverException e) {
|
420 |
return ValueFactory.createValue("").toString(); |
421 |
} |
422 |
} |
423 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
424 |
return (dw != null); |
425 |
} |
426 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
427 |
Value v; |
428 |
try {
|
429 |
v = ValueFactory.createValueByType(aValue.toString(), dataSource.getFieldType(columnIndex)); |
430 |
dw.setFieldValue(rowIndex, mapping[columnIndex], v); |
431 |
} catch (DriverException e1) {
|
432 |
throw new RuntimeException(e1); |
433 |
} catch (ParseException e) { |
434 |
throw new RuntimeException(e); |
435 |
} |
436 |
} |
437 |
} |
438 |
|
439 |
public class TableSelectionListener implements SelectionListener{ |
440 |
|
441 |
/**
|
442 |
* @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
443 |
*/
|
444 |
public void selectionChanged(SelectionEvent e) { |
445 |
updateSelection(); |
446 |
Table.this.repaint(); |
447 |
} |
448 |
|
449 |
} |
450 |
|
451 |
/**
|
452 |
* @param ds
|
453 |
* @throws IOException
|
454 |
*/
|
455 |
public void setOrder(long[] indexes) throws IOException { |
456 |
orderIndexes = indexes; |
457 |
orderIndexesInverted = new long[orderIndexes.length]; |
458 |
for (int i = 0; i < indexes.length; i++) { |
459 |
orderIndexesInverted[(int) indexes[i]] = i;
|
460 |
} |
461 |
|
462 |
updating=true;
|
463 |
((DataSourceDataModel)getTable().getModel()).fireTableDataChanged(); |
464 |
updating=false;
|
465 |
updateSelection(); |
466 |
|
467 |
} |
468 |
|
469 |
public void startEditing() throws DriverException{ |
470 |
dw = model.getModelo().getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER); |
471 |
dw.beginTrans(); |
472 |
} |
473 |
|
474 |
/**
|
475 |
* @throws DriverException
|
476 |
*
|
477 |
*/
|
478 |
public void cancelEditing() throws DriverException { |
479 |
dw.rollBackTrans(); |
480 |
dw = null;
|
481 |
} |
482 |
|
483 |
/**
|
484 |
* @throws DriverException
|
485 |
*
|
486 |
*/
|
487 |
public void stopEditing() throws DriverException { |
488 |
dw.commitTrans(); |
489 |
dw = null;
|
490 |
model.getModelo().stop(); |
491 |
model.getModelo().start(); |
492 |
} |
493 |
|
494 |
/**
|
495 |
* @return
|
496 |
*/
|
497 |
public boolean isEditing() { |
498 |
return dw != null; |
499 |
} |
500 |
|
501 |
/**
|
502 |
* Quita los campos seleccionados
|
503 |
*/
|
504 |
public void clearSelectedFields() |
505 |
{ |
506 |
headerSelectionSupport.clearSelectedColumns(); |
507 |
} |
508 |
private class MouseHandler extends MouseAdapter { |
509 |
public void mouseClicked(MouseEvent e) { |
510 |
/* JTableHeader h = (JTableHeader) e.getSource();
|
511 |
TableColumnModel columnModel = h.getColumnModel();
|
512 |
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
|
513 |
int column = columnModel.getColumn(viewColumn).getModelIndex();
|
514 |
if (column != -1) {
|
515 |
|
516 |
}*/
|
517 |
} |
518 |
} |
519 |
private class MouseRow extends MouseAdapter { |
520 |
public void mouseClicked(MouseEvent e) { |
521 |
PluginServices.getMainFrame().enableControls(); |
522 |
if (e.getButton()==MouseEvent.BUTTON3){ |
523 |
new PopupMenu(e.getPoint()); |
524 |
} |
525 |
} |
526 |
|
527 |
} |
528 |
public Value[] getValueRow(int index){ |
529 |
DataSourceDataModel dsdm=(DataSourceDataModel)getTable().getModel(); |
530 |
Value [] values=new Value[dsdm.getColumnCount()]; |
531 |
for (int i=0;i<dsdm.getColumnCount();i++){ |
532 |
values[i]=(Value)dsdm.getValueAt(index,i); |
533 |
} |
534 |
return values;
|
535 |
} |
536 |
private void refresh() throws DriverException{ |
537 |
dw.commitTrans(); |
538 |
model.getModelo().stop(); |
539 |
dw.beginTrans(); |
540 |
DataSourceDataModel dsdm=(DataSourceDataModel)getTable().getModel(); |
541 |
dsdm.fireTableDataChanged(); |
542 |
} |
543 |
public void addEmptyRow() throws DriverException{ |
544 |
ValueCollection valuePK=new ValueCollection();
|
545 |
valuePK.setValues(new Value[]{ValueFactory.createValue(dw.getRowCount())}); |
546 |
dw.insertEmptyRow(valuePK); |
547 |
refresh(); |
548 |
} |
549 |
|
550 |
public void copySelectedRows() throws DriverException{ |
551 |
int[] sel=getSelectedRowIndices(); |
552 |
for(int i=0;i<sel.length;i++){ |
553 |
rowsCopied.add(getValueRow(sel[i])); |
554 |
} |
555 |
} |
556 |
|
557 |
public void addSelectionToEnd() throws DriverException { |
558 |
for (int i=0;i<rowsCopied.size();i++){ |
559 |
dw.insertFilledRow((Value[])rowsCopied.get(i));
|
560 |
} |
561 |
refresh(); |
562 |
} |
563 |
public void delSelectionRow() throws DriverException{ |
564 |
int[] sel=getSelectedRowIndices(); |
565 |
for(int i=sel.length-1;i>=0;i--){ |
566 |
dw.deleteRow(sel[i]); |
567 |
} |
568 |
refresh(); |
569 |
|
570 |
} |
571 |
public boolean isCopy(){ |
572 |
return !rowsCopied.isEmpty();
|
573 |
} |
574 |
/*
|
575 |
public void addSelectionToRow() throws DriverException {
|
576 |
int[] sel=getSelectedRowIndices();
|
577 |
dw.insertFilledRow((Value[])rowsCopied.get(i),sel[0]);
|
578 |
refresh();
|
579 |
}
|
580 |
*/
|
581 |
|
582 |
} |