svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / Table.java @ 6313
History | View | Annotate | Download (47.7 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.Color; |
44 |
import java.awt.Component; |
45 |
import java.awt.event.KeyAdapter; |
46 |
import java.awt.event.KeyEvent; |
47 |
import java.awt.event.KeyListener; |
48 |
import java.awt.event.MouseAdapter; |
49 |
import java.awt.event.MouseEvent; |
50 |
import java.beans.PropertyChangeEvent; |
51 |
import java.beans.PropertyChangeListener; |
52 |
import java.io.IOException; |
53 |
import java.sql.Types; |
54 |
import java.text.ParseException; |
55 |
import java.util.BitSet; |
56 |
import java.util.Enumeration; |
57 |
import java.util.EventListener; |
58 |
import java.util.EventObject; |
59 |
|
60 |
import javax.swing.BorderFactory; |
61 |
import javax.swing.DefaultCellEditor; |
62 |
import javax.swing.DefaultListSelectionModel; |
63 |
import javax.swing.JComponent; |
64 |
import javax.swing.JFrame; |
65 |
import javax.swing.JLabel; |
66 |
import javax.swing.JOptionPane; |
67 |
import javax.swing.JPanel; |
68 |
import javax.swing.JTextField; |
69 |
import javax.swing.ListSelectionModel; |
70 |
import javax.swing.UIManager; |
71 |
import javax.swing.border.Border; |
72 |
import javax.swing.event.ListSelectionEvent; |
73 |
import javax.swing.event.ListSelectionListener; |
74 |
import javax.swing.event.TableColumnModelEvent; |
75 |
import javax.swing.event.TableColumnModelListener; |
76 |
import javax.swing.table.AbstractTableModel; |
77 |
import javax.swing.table.DefaultTableColumnModel; |
78 |
import javax.swing.table.TableCellRenderer; |
79 |
import javax.swing.table.TableColumn; |
80 |
|
81 |
import org.apache.log4j.Logger; |
82 |
|
83 |
import com.hardcode.driverManager.DriverLoadException; |
84 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
85 |
import com.hardcode.gdbms.engine.values.Value; |
86 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
87 |
import com.iver.andami.PluginServices; |
88 |
import com.iver.andami.messages.NotificationManager; |
89 |
import com.iver.andami.ui.mdiFrame.MainFrame; |
90 |
import com.iver.andami.ui.mdiManager.SingletonView; |
91 |
import com.iver.andami.ui.mdiManager.ViewInfo; |
92 |
import com.iver.andami.ui.mdiManager.ViewListener; |
93 |
import com.iver.andami.ui.mdiManager.ViewTransform; |
94 |
import com.iver.cit.gvsig.fmap.FMap; |
95 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
96 |
import com.iver.cit.gvsig.fmap.core.DefaultRow; |
97 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
98 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
99 |
import com.iver.cit.gvsig.fmap.core.IRow; |
100 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
101 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
102 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
103 |
import com.iver.cit.gvsig.fmap.edition.EditableAdapter; |
104 |
import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
105 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
106 |
import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
107 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
108 |
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
109 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
110 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
111 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
112 |
import com.iver.cit.gvsig.fmap.edition.commands.CommandListener; |
113 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
114 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
115 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
116 |
import com.iver.cit.gvsig.fmap.layers.SelectionEvent; |
117 |
import com.iver.cit.gvsig.fmap.layers.SelectionListener; |
118 |
import com.iver.cit.gvsig.gui.tables.Column; |
119 |
import com.iver.cit.gvsig.gui.tables.EditionTable; |
120 |
import com.iver.cit.gvsig.project.ProjectTable; |
121 |
import com.iver.utiles.swing.jtable.FieldSelectionEvent; |
122 |
import com.iver.utiles.swing.jtable.FieldSelectionListener; |
123 |
import com.iver.utiles.swing.jtable.SelectionHeaderSupport; |
124 |
|
125 |
/**
|
126 |
* DOCUMENT ME!
|
127 |
*
|
128 |
* @author Fernando Gonz?lez Cort?s
|
129 |
*/
|
130 |
public class Table extends JPanel implements SingletonView, EditionTable, |
131 |
ViewListener,CommandListener, ViewTransform { |
132 |
private static Logger logger = Logger.getLogger(Table.class.getName()); |
133 |
private javax.swing.JScrollPane jScrollPane = null; |
134 |
protected javax.swing.JTable table = null; |
135 |
protected ProjectTable model = null; |
136 |
protected JLabel jLabelStatus = null; |
137 |
protected FMap fmap;
|
138 |
protected boolean updating = false; |
139 |
private TableSelectionListener selectionListener = new TableSelectionListener(); |
140 |
private long numReg = 0; |
141 |
protected SelectionHeaderSupport headerSelectionSupport = new SelectionHeaderSupport(); |
142 |
private long[] orderIndexes = null; |
143 |
private long[] orderIndexesInverted = null; |
144 |
private IRow[] rowsCopied = null; |
145 |
private ViewInfo m_viewInfo = null; |
146 |
private boolean isPalette=false; |
147 |
/**
|
148 |
* This is the default constructor
|
149 |
*/
|
150 |
public Table() {
|
151 |
super();
|
152 |
initialize(); |
153 |
} |
154 |
|
155 |
/**
|
156 |
* DOCUMENT ME!
|
157 |
*
|
158 |
* @return DOCUMENT ME!
|
159 |
*/
|
160 |
public ProjectTable getModel() {
|
161 |
return model;
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* DOCUMENT ME!
|
166 |
*
|
167 |
* @return DOCUMENT ME!
|
168 |
*/
|
169 |
public BitSet getSelectedFieldIndices() { |
170 |
BitSet bs=headerSelectionSupport.getSelectedColumns();
|
171 |
BitSet newbs=new BitSet(); |
172 |
for (int i =1;i<bs.length();i++){ |
173 |
if (bs.get(i))
|
174 |
newbs.set(i-1);
|
175 |
} |
176 |
return newbs;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* DOCUMENT ME!
|
181 |
*
|
182 |
* @return DOCUMENT ME!
|
183 |
*/
|
184 |
public int[] getSelectedRowIndices() { |
185 |
return getTable().getSelectedRows();
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* DOCUMENT ME!
|
190 |
*/
|
191 |
protected void refreshControls() { |
192 |
try {
|
193 |
MainFrame mF = PluginServices.getMainFrame(); |
194 |
|
195 |
if (mF != null) { |
196 |
PluginServices.getMDIManager().getViewInfo(Table.this).setTitle(PluginServices.getText( |
197 |
this, "Tabla") + ": " + model.getName()); |
198 |
} |
199 |
|
200 |
if (model.getAssociatedTable() != null) { |
201 |
this.fmap = ((FLayer) model.getAssociatedTable()).getFMap();
|
202 |
} else {
|
203 |
this.fmap = null; |
204 |
} |
205 |
|
206 |
SelectableDataSource dataSource = (SelectableDataSource) model.getModelo() |
207 |
.getRecordset(); |
208 |
logger.debug("dataSource.start()");
|
209 |
dataSource.start(); |
210 |
|
211 |
|
212 |
ColumnModel cm=new ColumnModel();
|
213 |
cm.setDataModel(model); |
214 |
getTable().setColumnModel(cm); |
215 |
|
216 |
AbstractTableModel dataModel = new DataSourceDataModel(model); |
217 |
|
218 |
getTable().setModel(dataModel); |
219 |
TableColumn column = null; |
220 |
for (int i = 0; i < model.getColumnCount(); i++) { |
221 |
column = table.getColumnModel().getColumn(i); |
222 |
int w=model.getColumn(i).getWidth();
|
223 |
column.setPreferredWidth(w); //sport column is bigger
|
224 |
System.err.println("Table.Dentro de refreshControls. column=" + column.toString()); |
225 |
|
226 |
} |
227 |
headerSelectionSupport.setTableHeader(getTable().getTableHeader()); |
228 |
headerSelectionSupport.addFieldSelectionListener(new FieldSelectionListener() {
|
229 |
public void fieldSelected(FieldSelectionEvent e) { |
230 |
if (PluginServices.getMainFrame() != null) { |
231 |
PluginServices.getMainFrame().enableControls(); |
232 |
} |
233 |
} |
234 |
}); |
235 |
|
236 |
model.getModelo().getRecordset().addSelectionListener(selectionListener); |
237 |
|
238 |
updateSelection(); |
239 |
} catch (DriverException e) {
|
240 |
NotificationManager.addError("No se pudo leer la informaci?n", e);
|
241 |
} catch (DriverLoadException e) {
|
242 |
NotificationManager.addError("No se pudo leer la informaci?n", e);
|
243 |
} |
244 |
} |
245 |
|
246 |
/**
|
247 |
* DOCUMENT ME!
|
248 |
*
|
249 |
* @param table DOCUMENT ME!
|
250 |
*/
|
251 |
public void setModel(ProjectTable table) { |
252 |
model = table; |
253 |
|
254 |
//Gesti?n del nombre de la ventana
|
255 |
model.addPropertyChangeListener(new PropertyChangeListener() { |
256 |
public void propertyChange(PropertyChangeEvent evt) { |
257 |
if (evt.getPropertyName().equals("name")) { |
258 |
PluginServices.getMDIManager().getViewInfo(Table.this) |
259 |
.setTitle(PluginServices.getText(this,
|
260 |
"Tabla") + ": " + (String) evt.getNewValue()); |
261 |
} else if (evt.getPropertyName().equals("model")) { |
262 |
refreshControls(); |
263 |
} |
264 |
} |
265 |
}); |
266 |
|
267 |
refreshControls(); |
268 |
TableColumn tc=getTable().getColumnModel().getColumn(0); |
269 |
tc.setCellRenderer(new MyTableCellRenderer());
|
270 |
|
271 |
|
272 |
} |
273 |
class MyTableCellRenderer extends JLabel implements TableCellRenderer { |
274 |
final Border grayBorder = BorderFactory.createLineBorder(Color.darkGray); |
275 |
|
276 |
MyTableCellRenderer() { |
277 |
|
278 |
} |
279 |
|
280 |
public Component getTableCellRendererComponent(javax.swing.JTable table, |
281 |
Object value, boolean isSelected, boolean hasFocus, int row, |
282 |
int col) {
|
283 |
JLabel label=new JLabel(String.valueOf(row+1)); |
284 |
label.setForeground(Color.blue);
|
285 |
label.setOpaque(true);
|
286 |
if (isSelected)
|
287 |
label.setBackground(Color.yellow);
|
288 |
else
|
289 |
label.setBackground(UIManager.getColor("Button.background")); |
290 |
|
291 |
label.setBorder(grayBorder); |
292 |
return label;
|
293 |
} |
294 |
} |
295 |
|
296 |
/**
|
297 |
*
|
298 |
*/
|
299 |
public void updateSelection() { |
300 |
updating = true;
|
301 |
try {
|
302 |
DefaultListSelectionModel sm = (DefaultListSelectionModel) getTable() |
303 |
.getSelectionModel(); |
304 |
sm.clearSelection(); |
305 |
|
306 |
BitSet bs = (model.getModelo().getRecordset()).getSelection();
|
307 |
sm.setValueIsAdjusting(true);
|
308 |
|
309 |
if (orderIndexes != null) { |
310 |
for (int i = 0; i < orderIndexes.length; i++) { |
311 |
if (bs.get(i)) {
|
312 |
sm.addSelectionInterval((int) orderIndexesInverted[i],
|
313 |
(int) orderIndexesInverted[i]);
|
314 |
if (isEditing())
|
315 |
table.setEditingRow((int)orderIndexesInverted[i]);
|
316 |
} |
317 |
} |
318 |
} else {
|
319 |
for (int i = bs.nextSetBit(0); i >= 0; |
320 |
i = bs.nextSetBit(i + 1)) {
|
321 |
sm.addSelectionInterval(i, i); |
322 |
if (isEditing())
|
323 |
table.setEditingRow(i); |
324 |
} |
325 |
} |
326 |
|
327 |
sm.setValueIsAdjusting(false);
|
328 |
try {
|
329 |
numReg = model.getModelo().getRowCount(); |
330 |
} catch (IOException e) { |
331 |
e.printStackTrace(); |
332 |
} |
333 |
|
334 |
jLabelStatus.setText(" " +
|
335 |
((SelectableDataSource) model.getModelo().getRecordset()).getSelection() |
336 |
.cardinality() + " / " + numReg + " " + |
337 |
PluginServices.getText(this, "registros_seleccionados_total") + |
338 |
".");
|
339 |
PluginServices.getMainFrame().enableControls(); |
340 |
} catch (DriverIOException e) {
|
341 |
e.printStackTrace(); |
342 |
} catch (DriverLoadException e) {
|
343 |
e.printStackTrace(); |
344 |
} |
345 |
updating = false;
|
346 |
} |
347 |
|
348 |
/**
|
349 |
* This method initializes this
|
350 |
*/
|
351 |
protected void initialize() { |
352 |
jLabelStatus = new JLabel(); |
353 |
setLayout(new java.awt.BorderLayout());
|
354 |
jLabelStatus.setText("");
|
355 |
jLabelStatus.setName("");
|
356 |
jLabelStatus.setPreferredSize(new java.awt.Dimension(100, 18)); |
357 |
add(getJScrollPane(), java.awt.BorderLayout.CENTER); |
358 |
this.add(jLabelStatus, java.awt.BorderLayout.SOUTH);
|
359 |
} |
360 |
|
361 |
/**
|
362 |
* This method initializes table
|
363 |
*
|
364 |
* @return javax.swing.JTable
|
365 |
*/
|
366 |
public javax.swing.JTable getTable() {
|
367 |
if (table == null) { |
368 |
table = new javax.swing.JTable();
|
369 |
table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); |
370 |
table.setSelectionModel(new DefaultListSelectionModel()); |
371 |
table.getTableHeader().addMouseListener(new MouseHandler());
|
372 |
table.addKeyListener(new TableKeyListener());
|
373 |
table.addMouseListener(new MouseRow());
|
374 |
table.setSelectionForeground(Color.blue);
|
375 |
table.setSelectionBackground(Color.yellow);
|
376 |
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
377 |
public void valueChanged(ListSelectionEvent e) { |
378 |
|
379 |
if (updating) {
|
380 |
return;
|
381 |
} |
382 |
|
383 |
SelectableDataSource dataModel = null;
|
384 |
|
385 |
try {
|
386 |
dataModel = Table.this.model.getModelo() |
387 |
.getRecordset(); |
388 |
} catch (DriverLoadException e1) {
|
389 |
e1.printStackTrace(); |
390 |
} |
391 |
|
392 |
DefaultListSelectionModel model = (DefaultListSelectionModel) table.getSelectionModel(); |
393 |
BitSet selection = dataModel.getSelection();
|
394 |
|
395 |
for (int i = e.getFirstIndex(); i <= e.getLastIndex(); |
396 |
i++) { |
397 |
if (orderIndexes != null) { |
398 |
selection.set((int) orderIndexes[i],
|
399 |
model.isSelectedIndex(i)); |
400 |
} else {
|
401 |
selection.set(i, model.isSelectedIndex(i)); |
402 |
} |
403 |
} |
404 |
|
405 |
if (e.getValueIsAdjusting() == false) { |
406 |
if (fmap != null) { |
407 |
fmap.endAtomicEvent(); |
408 |
} |
409 |
|
410 |
dataModel.fireSelectionEvents(); |
411 |
} else {
|
412 |
if (fmap != null) { |
413 |
fmap.beginAtomicEvent(); |
414 |
} |
415 |
} |
416 |
|
417 |
jLabelStatus.setText(" " + selection.cardinality() +
|
418 |
" / " + numReg + " " + |
419 |
PluginServices.getText(this,
|
420 |
"registros_seleccionados_total") + "."); |
421 |
|
422 |
} |
423 |
}); |
424 |
|
425 |
JTextField tf=new JTextField(); |
426 |
table.setDefaultEditor(Number.class, new DefaultEditor(tf)); |
427 |
table.setDefaultEditor(String.class, new DefaultEditor(tf)); |
428 |
table.setDefaultEditor(Object.class, new DefaultEditor(tf)); |
429 |
} |
430 |
|
431 |
return table;
|
432 |
} |
433 |
protected class DefaultEditor extends DefaultCellEditor { |
434 |
public Component getTableCellEditorComponent(javax.swing.JTable table, |
435 |
Object value, boolean isSelected, int row, int column) { |
436 |
JTextField tf=(JTextField)super.getTableCellEditorComponent(table, value, isSelected, |
437 |
row, column); |
438 |
if (isSelected){
|
439 |
tf.setBackground(new Color(230,220,220)); |
440 |
tf.selectAll(); |
441 |
|
442 |
} |
443 |
return tf;
|
444 |
} |
445 |
|
446 |
public DefaultEditor(JTextField tf) { |
447 |
super(tf);
|
448 |
//MouseListener[] mouseListeners=getTable().getEditorComponent().getMouseListeners();
|
449 |
// if (!mouseListeners[mouseListeners.length-1].getClass().equals(MouseRow.class)){
|
450 |
tf.addMouseListener(new MouseRow());
|
451 |
// }
|
452 |
getComponent().addKeyListener(new KeyAdapter() { |
453 |
int keyPressed = 0; |
454 |
|
455 |
public void keyPressed(KeyEvent ke) { |
456 |
if (ke.getKeyCode() != KeyEvent.VK_TAB) |
457 |
keyPressed++; |
458 |
JTextField tf = (JTextField) getComponent(); |
459 |
|
460 |
|
461 |
if (ke.getKeyCode() == KeyEvent.VK_RIGHT |
462 |
|| ke.getKeyCode() == KeyEvent.VK_ENTER) {
|
463 |
int caretPosition = tf.getCaretPosition();
|
464 |
if (caretPosition >= tf.getText().length()) {
|
465 |
int x = table.getSelectedColumn();
|
466 |
int y = table.getSelectedRow();
|
467 |
if (x + 1 >= table.getColumnCount()) { |
468 |
x = 0;
|
469 |
y++; |
470 |
} else {
|
471 |
x++; |
472 |
} |
473 |
//table.changeSelection(y, x, false, true);
|
474 |
//initEditField(x,y);
|
475 |
getComponent().setEnabled(false);
|
476 |
} |
477 |
|
478 |
} else if (ke.getKeyCode() == KeyEvent.VK_LEFT) { |
479 |
int caretPosition = tf.getCaretPosition();
|
480 |
if (caretPosition <= 0) { |
481 |
int x = table.getSelectedColumn();
|
482 |
int y = table.getSelectedRow();
|
483 |
if (x == 0) { |
484 |
x = table.getColumnCount() - 1;
|
485 |
if (y - 1 < 0) |
486 |
y = table.getRowCount() - 1;
|
487 |
else
|
488 |
y--; |
489 |
} else {
|
490 |
x--; |
491 |
} |
492 |
//table.changeSelection(y, x, false, true);
|
493 |
//initEditField(x,y);
|
494 |
getComponent().setEnabled(false);
|
495 |
} |
496 |
|
497 |
} |
498 |
} |
499 |
|
500 |
public void keyReleased(KeyEvent ke) { |
501 |
JComponent editor = (JComponent) DefaultEditor.this |
502 |
.getComponent(); |
503 |
if (editor instanceof JTextField) { |
504 |
if (keyPressed == 0) { |
505 |
((JTextField) editor).setText(String.valueOf(ke |
506 |
.getKeyChar())); |
507 |
} |
508 |
} |
509 |
keyPressed--; |
510 |
if (keyPressed < 0) |
511 |
keyPressed = 0;
|
512 |
getComponent().setEnabled(true);
|
513 |
} |
514 |
}); |
515 |
|
516 |
} |
517 |
|
518 |
public Object getCellEditorValue() { |
519 |
String s = ((JTextField) (DefaultEditor.this.getComponent())) |
520 |
.getText(); |
521 |
getComponent().setEnabled(true);
|
522 |
return s;
|
523 |
} |
524 |
|
525 |
public boolean isCellEditable(EventObject event) { |
526 |
// IF NUMBER OF CLICKS IS LESS THAN THE CLICKCOUNTTOSTART RETURN
|
527 |
// FALSE
|
528 |
// FOR CELL EDITING.
|
529 |
if (event instanceof MouseEvent) { |
530 |
return ((MouseEvent) event).getClickCount() >= getClickCountToStart(); |
531 |
} |
532 |
|
533 |
return true; |
534 |
} |
535 |
} |
536 |
|
537 |
|
538 |
|
539 |
|
540 |
/**
|
541 |
* This method initializes jScrollPane
|
542 |
*
|
543 |
* @return javax.swing.JScrollPane
|
544 |
*/
|
545 |
protected javax.swing.JScrollPane getJScrollPane() {
|
546 |
if (jScrollPane == null) { |
547 |
jScrollPane = new javax.swing.JScrollPane();
|
548 |
jScrollPane.setViewportView(getTable()); |
549 |
} |
550 |
|
551 |
return jScrollPane;
|
552 |
} |
553 |
|
554 |
/**
|
555 |
* @see com.iver.mdiApp.ui.MDIManager.SingletonView#getViewModel()
|
556 |
*/
|
557 |
public Object getViewModel() { |
558 |
return model;
|
559 |
} |
560 |
|
561 |
/**
|
562 |
* This method is used to get <strong>an initial</strong> ViewInfo object
|
563 |
* for this Table. It is not intended to retrieve the ViewInfo object in a
|
564 |
* later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
|
565 |
* to retrieve the ViewInfo object at any time after the creation of the
|
566 |
* object.
|
567 |
*
|
568 |
* @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
|
569 |
*/
|
570 |
public ViewInfo getViewInfo() {
|
571 |
if (m_viewInfo==null) { |
572 |
m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
|
573 |
ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE); |
574 |
m_viewInfo.setTitle(PluginServices.getText(this, "Tabla")+ " : " +model.getName()); |
575 |
m_viewInfo.setWidth(300);
|
576 |
m_viewInfo.setHeight(200);
|
577 |
} |
578 |
return m_viewInfo;
|
579 |
} |
580 |
|
581 |
/**
|
582 |
* DOCUMENT ME!
|
583 |
*
|
584 |
* @param indexes
|
585 |
*
|
586 |
* @throws IOException
|
587 |
*/
|
588 |
public void setOrder(long[] indexes) throws IOException { |
589 |
orderIndexes = indexes; |
590 |
orderIndexesInverted = new long[orderIndexes.length]; |
591 |
|
592 |
for (int i = 0; i < indexes.length; i++) { |
593 |
orderIndexesInverted[(int) indexes[i]] = i;
|
594 |
} |
595 |
|
596 |
updating = true;
|
597 |
((DataSourceDataModel) getTable().getModel()).fireTableDataChanged(); |
598 |
updating = false;
|
599 |
|
600 |
updateSelection(); |
601 |
} |
602 |
|
603 |
/* public void startEditing() throws DriverException{
|
604 |
// dw = model.getModelo().getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER);
|
605 |
// dw.beginTrans();
|
606 |
}
|
607 |
*/
|
608 |
|
609 |
/**
|
610 |
* DOCUMENT ME!
|
611 |
*/
|
612 |
|
613 |
/* public void cancelEditing() throws DriverException {
|
614 |
dw.rollBackTrans();
|
615 |
dw = null;
|
616 |
}
|
617 |
*/
|
618 |
|
619 |
/**
|
620 |
* DOCUMENT ME!
|
621 |
*/
|
622 |
|
623 |
/* public void stopEditing() throws DriverException {
|
624 |
// dw.commitTrans();
|
625 |
// dw = null;
|
626 |
// model.getModelo().stop();
|
627 |
// model.getModelo().start();
|
628 |
}
|
629 |
*/
|
630 |
|
631 |
/**
|
632 |
* DOCUMENT ME!
|
633 |
*/
|
634 |
|
635 |
/* public boolean isEditing() {
|
636 |
return dw != null;
|
637 |
}
|
638 |
*/
|
639 |
|
640 |
/**
|
641 |
* Quita los campos seleccionados
|
642 |
*/
|
643 |
public void clearSelectedFields() { |
644 |
headerSelectionSupport.clearSelectedColumns(); |
645 |
} |
646 |
|
647 |
/**
|
648 |
* DOCUMENT ME!
|
649 |
*
|
650 |
* @param index DOCUMENT ME!
|
651 |
*
|
652 |
* @return DOCUMENT ME!
|
653 |
*/
|
654 |
public Value[] getValueRow(int index) { |
655 |
DataSourceDataModel dsdm = (DataSourceDataModel) getTable().getModel(); |
656 |
Value[] values = new Value[dsdm.getColumnCount()]; |
657 |
|
658 |
for (int i = 0; i < dsdm.getColumnCount(); i++) { |
659 |
values[i] = (Value) dsdm.getValueAt(index, i); |
660 |
} |
661 |
|
662 |
return values;
|
663 |
} |
664 |
|
665 |
/* private void refresh() throws DriverException{
|
666 |
//dw.commitTrans();
|
667 |
//model.getModelo().stop();
|
668 |
//dw.beginTrans();
|
669 |
//DataSourceDataModel dsdm=(DataSourceDataModel)getTable().getModel();
|
670 |
//dsdm.fireTableDataChanged();
|
671 |
}*/
|
672 |
/* public void addEmptyRow() throws DriverException{
|
673 |
ValueCollection valuePK=new ValueCollection();
|
674 |
valuePK.setValues(new Value[]{ValueFactory.createValue(dw.getRowCount())});
|
675 |
dw.insertEmptyRow(valuePK);
|
676 |
refresh();
|
677 |
}
|
678 |
*/
|
679 |
/* public void copySelectedRows() throws DriverException{
|
680 |
int[] sel=getSelectedRowIndices();
|
681 |
for(int i=0;i<sel.length;i++){
|
682 |
rowsCopied.add(getValueRow(sel[i]));
|
683 |
}
|
684 |
}
|
685 |
*/
|
686 |
/* public void addSelectionToEnd() throws DriverException {
|
687 |
for (int i=0;i<rowsCopied.size();i++){
|
688 |
dw.insertFilledRow((Value[])rowsCopied.get(i));
|
689 |
}
|
690 |
refresh();
|
691 |
}
|
692 |
*/
|
693 |
/*public void delSelectionRow() throws DriverException{
|
694 |
int[] sel=getSelectedRowIndices();
|
695 |
for(int i=sel.length-1;i>=0;i--){
|
696 |
dw.deleteRow(sel[i]);
|
697 |
}
|
698 |
refresh();
|
699 |
}
|
700 |
*/
|
701 |
/*public boolean isCopy(){
|
702 |
return !rowsCopied.isEmpty();
|
703 |
}
|
704 |
*/
|
705 |
/*
|
706 |
public void addSelectionToRow() throws DriverException {
|
707 |
int[] sel=getSelectedRowIndices();
|
708 |
dw.insertFilledRow((Value[])rowsCopied.get(i),sel[0]);
|
709 |
refresh();
|
710 |
}
|
711 |
*/
|
712 |
public void startEditing() throws EditionException { |
713 |
getModel().getModelo().startEdition(EditionEvent.ALPHANUMERIC); |
714 |
//int row=table.getSelectedRow();
|
715 |
//int column=table.getSelectedColumn();
|
716 |
//if (row!=-1 && column!=-1)
|
717 |
// initEditField(column,row);
|
718 |
|
719 |
// View[] views = (View[]) PluginServices.getMDIManager().getAllViews();
|
720 |
//
|
721 |
// for (int i=0 ; i<views.length ; i++){
|
722 |
// if (views[i] instanceof com.iver.cit.gvsig.gui.View){
|
723 |
// com.iver.cit.gvsig.gui.View view=(com.iver.cit.gvsig.gui.View)views[i];
|
724 |
// FLayers layers =view.getMapControl().getMapContext().getLayers();
|
725 |
// for (int j=0;j<layers.getLayersCount();j++){
|
726 |
// if (layers.getLayer(j).equals(this.getModel().getAssociatedTable())){
|
727 |
// view.showConsole();
|
728 |
// ((FLyrVect)layers.getLayer(j)).setActive(true);
|
729 |
// ((FLyrVect)layers.getLayer(j)).setEditing(true);
|
730 |
// }
|
731 |
// }
|
732 |
// }
|
733 |
// }
|
734 |
} |
735 |
private void initEditField(int[] x,int[] y){ |
736 |
if (getTable().getRowCount()>0){ |
737 |
if (isEditing()) {
|
738 |
|
739 |
if (x.length == 1 && y.length == 1) |
740 |
table.editCellAt(y[0], x[0]); |
741 |
JTextField tf = (JTextField) table.getEditorComponent(); |
742 |
if (tf != null) { |
743 |
tf.selectAll(); |
744 |
tf.requestFocus(); |
745 |
} |
746 |
if (y.length > 0 && x.length > 0) { |
747 |
getTable().setRowSelectionInterval(y[0], y[y.length - 1]); |
748 |
getTable().setColumnSelectionInterval(x[0], x[x.length - 1]); |
749 |
} |
750 |
|
751 |
} |
752 |
} |
753 |
} |
754 |
/**
|
755 |
* DOCUMENT ME!
|
756 |
*/
|
757 |
public void stopEditing() { |
758 |
try {
|
759 |
this.stopEditingCell();
|
760 |
|
761 |
FLyrVect lyr = (FLyrVect) getModel().getAssociatedTable(); |
762 |
|
763 |
if ((lyr != null) && |
764 |
lyr.getSource() instanceof VectorialEditableAdapter) {
|
765 |
VectorialEditableAdapter vea = (VectorialEditableAdapter) lyr.getSource(); |
766 |
ISpatialWriter spatialWriter = (ISpatialWriter) vea.getDriver(); |
767 |
vea.stopEdition(spatialWriter,EditionEvent.ALPHANUMERIC); |
768 |
lyr.setSource(vea.getOriginalAdapter()); |
769 |
lyr.setEditing(false);
|
770 |
refreshControls(); |
771 |
|
772 |
} else {
|
773 |
|
774 |
IEditableSource ies=getModel().getModelo(); |
775 |
if (ies instanceof IWriteable) |
776 |
{ |
777 |
IWriteable w = (IWriteable) ies; |
778 |
IWriter writer = w.getWriter(); |
779 |
if (writer == null) |
780 |
{ |
781 |
NotificationManager.addError("No existe driver de escritura para la tabla"
|
782 |
+ getModel().getName(), new EditionException());
|
783 |
} |
784 |
else
|
785 |
{ |
786 |
ITableDefinition tableDef = ies.getTableDefinition(); |
787 |
writer.initialize(tableDef); |
788 |
|
789 |
ies.stopEdition(writer,EditionEvent.ALPHANUMERIC); |
790 |
|
791 |
// TODO: RELOAD
|
792 |
// EditableAdapter edAdapter = (EditableAdapter) ies;
|
793 |
// // Restaura el datasource a su estado original
|
794 |
// edAdapter.setOriginalDataSource(edAdapter.getRecordset());
|
795 |
ies.getSelection().clear(); |
796 |
refreshControls(); |
797 |
} |
798 |
} |
799 |
|
800 |
/*
|
801 |
GdbmsWriter gdbmswriter = new GdbmsWriter();
|
802 |
gdbmswriter.setDataWare(getModel().getModelo()
|
803 |
.getRecordset()
|
804 |
.getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER));
|
805 |
gdbmswriter.preProcess();
|
806 |
|
807 |
for (int i = 0; i < getModel().getModelo().getRowCount();
|
808 |
i++) {
|
809 |
gdbmswriter.process(getModel().getModelo().getRow(i));
|
810 |
}
|
811 |
|
812 |
gdbmswriter.postProcess();
|
813 |
*/
|
814 |
|
815 |
} |
816 |
|
817 |
} catch (Exception e) { |
818 |
NotificationManager.addError("No se pudo guardar la edici?n", e);
|
819 |
} |
820 |
} |
821 |
|
822 |
/**
|
823 |
* DOCUMENT ME!
|
824 |
*
|
825 |
* @param index DOCUMENT ME!
|
826 |
*/
|
827 |
public void hideColumns(int[] index) { |
828 |
// TODO Auto-generated method stub
|
829 |
} |
830 |
|
831 |
/**
|
832 |
* DOCUMENT ME!
|
833 |
*
|
834 |
* @param index DOCUMENT ME!
|
835 |
*/
|
836 |
public void setUneditableColumns(int[] index) { |
837 |
// TODO Auto-generated method stub
|
838 |
} |
839 |
|
840 |
/**
|
841 |
* DOCUMENT ME!
|
842 |
*
|
843 |
* @param numColumns DOCUMENT ME!
|
844 |
* @param values DOCUMENT ME!
|
845 |
*/
|
846 |
public void setDefaultValues(int[] numColumns, Value[] values) { |
847 |
// TODO Auto-generated method stub
|
848 |
} |
849 |
|
850 |
/**
|
851 |
* DOCUMENT ME!
|
852 |
*
|
853 |
* @return DOCUMENT ME!
|
854 |
*/
|
855 |
public Value getDefaultValue() {
|
856 |
// TODO Auto-generated method stub
|
857 |
return null; |
858 |
} |
859 |
|
860 |
/**
|
861 |
* DOCUMENT ME!
|
862 |
*
|
863 |
* @throws IOException DOCUMENT ME!
|
864 |
*/
|
865 |
public void cancelEditing() throws IOException { |
866 |
this.cancelEditingCell();
|
867 |
getModel().getModelo().cancelEdition(EditionEvent.ALPHANUMERIC); |
868 |
//refresh();
|
869 |
refreshControls(); |
870 |
} |
871 |
|
872 |
/**
|
873 |
* DOCUMENT ME!
|
874 |
*
|
875 |
* @return DOCUMENT ME!
|
876 |
*/
|
877 |
public boolean isEditing() { |
878 |
return getModel().getModelo().isEditing();
|
879 |
} |
880 |
|
881 |
/**
|
882 |
* DOCUMENT ME!
|
883 |
*/
|
884 |
public void refresh() { |
885 |
int row=table.getSelectedRow();
|
886 |
int column=table.getSelectedColumn();
|
887 |
if (row!=-1 && column!=-1 && table.getEditorComponent()!=null){ |
888 |
Value[] values=getValueRow(row);
|
889 |
JTextField jtf=(JTextField)table.getEditorComponent(); |
890 |
jtf.setText(values[column].toString()); |
891 |
} |
892 |
updating = true;
|
893 |
((DataSourceDataModel) getTable().getModel()).fireTableDataChanged(); |
894 |
|
895 |
updating = false;
|
896 |
updateSelection(); |
897 |
PluginServices.getMainFrame().enableControls(); |
898 |
|
899 |
} |
900 |
|
901 |
/**
|
902 |
* Add the rows that are passed like parameter and if parameter is null a
|
903 |
* row is added empties.
|
904 |
*
|
905 |
* @param rows Rows to add or null.
|
906 |
*
|
907 |
* @throws DriverIOException
|
908 |
* @throws IOException
|
909 |
*/
|
910 |
public void addRow(IRow[] rows) throws DriverIOException, IOException { |
911 |
IEditableSource ies=getModel().getModelo(); |
912 |
try {
|
913 |
if (rows == null) { |
914 |
|
915 |
if (getModel().getAssociatedTable()!=null){ |
916 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa."); |
917 |
return;
|
918 |
} |
919 |
IRow row; |
920 |
int numAttr=getModel().getModelo()
|
921 |
.getRecordset() |
922 |
.getFieldCount(); |
923 |
Value[] values=new Value[numAttr]; |
924 |
for (int i=0;i<numAttr;i++){ |
925 |
values[i]=ValueFactory.createNullValue(); |
926 |
} |
927 |
row = new DefaultRow(values);
|
928 |
ies.addRow(row,"Fila en blanco",EditionEvent.ALPHANUMERIC);
|
929 |
} else {
|
930 |
ies.startComplexRow(); |
931 |
for (int i = 0; i < rows.length; i++) { |
932 |
ies.addRow(((IRowEdited) rows[i]).getLinkedRow(),"Pegar filas",EditionEvent.ALPHANUMERIC);
|
933 |
} |
934 |
String description=PluginServices.getText(this,"add_rows"); |
935 |
ies.endComplexRow(description); |
936 |
} |
937 |
} catch (DriverException e) {
|
938 |
// TODO Auto-generated catch block
|
939 |
e.printStackTrace(); |
940 |
} catch (DriverLoadException e) {
|
941 |
// TODO Auto-generated catch block
|
942 |
e.printStackTrace(); |
943 |
} |
944 |
|
945 |
refresh(); |
946 |
|
947 |
} |
948 |
|
949 |
/**
|
950 |
* Copy in the arraylist the rows that be selected just then.
|
951 |
*
|
952 |
* @throws DriverIOException
|
953 |
* @throws IOException
|
954 |
*/
|
955 |
public void copyRow() throws DriverIOException, IOException { |
956 |
int[] index = getSelectedRowIndices(); |
957 |
rowsCopied = new IRow[index.length];
|
958 |
|
959 |
for (int i = 0; i < index.length; i++) { |
960 |
rowsCopied[i] = getModel().getModelo().getRow(index[i]); |
961 |
} |
962 |
} |
963 |
|
964 |
/**
|
965 |
* Cut the rows that be selected just then.
|
966 |
*
|
967 |
* @throws DriverIOException
|
968 |
* @throws IOException
|
969 |
*/
|
970 |
public void cutRow() throws DriverIOException, IOException { |
971 |
int[] index = getSelectedRowIndices(); |
972 |
rowsCopied = new IRow[index.length];
|
973 |
|
974 |
for (int i = 0; i < index.length; i++) { |
975 |
rowsCopied[i] = getModel().getModelo().getRow(index[i]); |
976 |
} |
977 |
|
978 |
removeRow(); |
979 |
} |
980 |
|
981 |
/**
|
982 |
* Remove in the rows that be selected just then.
|
983 |
*
|
984 |
* @throws DriverIOException
|
985 |
* @throws IOException
|
986 |
*/
|
987 |
public void removeRow() throws DriverIOException, IOException { |
988 |
int[] index = getSelectedRowIndices(); |
989 |
getModel().getModelo().startComplexRow(); |
990 |
for (int i = index.length-1; i >=0; i--) { |
991 |
getModel().getModelo().removeRow(index[i],"Eliminar fila", EditionEvent.ALPHANUMERIC);
|
992 |
} |
993 |
// int[] sel={0};
|
994 |
// getTable().setEditingRow(-1);
|
995 |
// getTable().getCellEditor().
|
996 |
// initEditField(sel,sel);
|
997 |
getTable().getCellEditor().cancelCellEditing(); |
998 |
String description=PluginServices.getText(this,"remove_rows"); |
999 |
getModel().getModelo().endComplexRow(description); |
1000 |
getTable().clearSelection(); |
1001 |
|
1002 |
refresh(); |
1003 |
//repaintAssociatedView();
|
1004 |
} |
1005 |
|
1006 |
// private void repaintAssociatedView(){
|
1007 |
// View[] views = (View[]) PluginServices.getMDIManager().getAllViews();
|
1008 |
//
|
1009 |
// for (int i=0 ; i<views.length ; i++){
|
1010 |
// if (views[i] instanceof com.iver.cit.gvsig.gui.View){
|
1011 |
// com.iver.cit.gvsig.gui.View view=(com.iver.cit.gvsig.gui.View)views[i];
|
1012 |
// FLayers layers =view.getMapControl().getMapContext().getLayers();
|
1013 |
// for (int j=0;j<layers.getLayersCount();j++){
|
1014 |
// if (layers.getLayer(j).equals(this.getModel().getAssociatedTable())){
|
1015 |
// view.getMapControl().getMapContext().invalidate();
|
1016 |
// }
|
1017 |
// }
|
1018 |
// }
|
1019 |
// }
|
1020 |
// }
|
1021 |
/**
|
1022 |
* DOCUMENT ME!
|
1023 |
*/
|
1024 |
public void addColumn(FieldDescription newField) { |
1025 |
EditableAdapter edAdapter = (EditableAdapter) getModel().getModelo(); |
1026 |
try {
|
1027 |
edAdapter.addField(newField); |
1028 |
if (getTable().getCellEditor() != null) |
1029 |
getTable().getCellEditor().cancelCellEditing(); |
1030 |
getModel().setModel(edAdapter); // Para que se recalculen los campos. TODO: Limpiear todo esto
|
1031 |
// refresh();
|
1032 |
refreshControls(); |
1033 |
} catch (EditionException e) {
|
1034 |
e.printStackTrace(); |
1035 |
NotificationManager.addError(e); |
1036 |
} |
1037 |
|
1038 |
} |
1039 |
|
1040 |
/**
|
1041 |
* DOCUMENT ME!
|
1042 |
*/
|
1043 |
public void removeColumn() { |
1044 |
EditableAdapter edAdapter = (EditableAdapter) getModel().getModelo(); |
1045 |
try {
|
1046 |
BitSet selectedFields = getSelectedFieldIndices();
|
1047 |
SelectableDataSource sds = edAdapter.getRecordset(); |
1048 |
edAdapter.startComplexRow(); |
1049 |
for(int i=selectedFields.nextSetBit(0); i>=0; i=selectedFields.nextSetBit(i+1)) { |
1050 |
FieldDescription fld = sds.getFieldsDescription()[i]; |
1051 |
edAdapter.removeField(fld.getFieldAlias()); |
1052 |
} |
1053 |
if (getTable().getCellEditor() != null) |
1054 |
getTable().getCellEditor().cancelCellEditing(); |
1055 |
|
1056 |
edAdapter.endComplexRow(PluginServices.getText(this, "remove_fields")); |
1057 |
clearSelectedFields(); |
1058 |
getModel().setModel(edAdapter); // Para que se recalculen los campos. TODO: Limpiear todo esto
|
1059 |
// refresh();
|
1060 |
refreshControls(); |
1061 |
} catch (EditionException e) {
|
1062 |
e.printStackTrace(); |
1063 |
NotificationManager.addError(e); |
1064 |
} catch (DriverException e) {
|
1065 |
e.printStackTrace(); |
1066 |
NotificationManager.addError(e); |
1067 |
} catch (IOException e) { |
1068 |
e.printStackTrace(); |
1069 |
NotificationManager.addError(e); |
1070 |
} catch (DriverIOException e) {
|
1071 |
e.printStackTrace(); |
1072 |
NotificationManager.addError(e); |
1073 |
} |
1074 |
|
1075 |
} |
1076 |
|
1077 |
/**
|
1078 |
* Return if we have rows copied or not.
|
1079 |
*
|
1080 |
* @return True if we have rows copied.
|
1081 |
*/
|
1082 |
public boolean isCopied() { |
1083 |
return (rowsCopied != null); |
1084 |
} |
1085 |
|
1086 |
/**
|
1087 |
* Paste the arraylist rows.
|
1088 |
*
|
1089 |
* @throws DriverIOException
|
1090 |
* @throws IOException
|
1091 |
*/
|
1092 |
public void pasteRow() throws DriverIOException, IOException { |
1093 |
addRow(rowsCopied); |
1094 |
//repaintAssociatedView();
|
1095 |
} |
1096 |
|
1097 |
/**
|
1098 |
* DOCUMENT ME!
|
1099 |
*/
|
1100 |
public void viewActivated() { |
1101 |
//if (isEditing() && getModel().getModelo() instanceof VectorialEditableAdapter)
|
1102 |
updateSelection(); |
1103 |
} |
1104 |
|
1105 |
/**
|
1106 |
* DOCUMENT ME!
|
1107 |
*/
|
1108 |
public void viewClosed() { |
1109 |
// TODO Auto-generated method stub
|
1110 |
} |
1111 |
|
1112 |
/**
|
1113 |
* DOCUMENT ME!
|
1114 |
*
|
1115 |
* @author Fernando Gonz?lez Cort?s
|
1116 |
*/
|
1117 |
public class DataSourceDataModel extends AbstractTableModel { |
1118 |
//private SelectableDataSource dataSource;
|
1119 |
private IEditableSource des;
|
1120 |
private String[] aliases; |
1121 |
private int[] mapping; |
1122 |
|
1123 |
/**
|
1124 |
* Crea un nuevo DataSourceDataModel.
|
1125 |
*
|
1126 |
* @param pt DOCUMENT ME!
|
1127 |
*/
|
1128 |
public DataSourceDataModel(ProjectTable pt) {
|
1129 |
this.des = pt.getModelo();
|
1130 |
|
1131 |
//try {
|
1132 |
//dataSource = pt.getModelo().getRecordset();
|
1133 |
//} catch (DriverLoadException e) {
|
1134 |
// TODO Auto-generated catch block
|
1135 |
// e.printStackTrace();
|
1136 |
//}
|
1137 |
aliases = pt.getAliases(); |
1138 |
mapping = pt.getMapping(); |
1139 |
} |
1140 |
|
1141 |
/**
|
1142 |
* Returns the name of the field.
|
1143 |
*
|
1144 |
* @param col index of field
|
1145 |
*
|
1146 |
* @return Name of field
|
1147 |
*/
|
1148 |
public String getColumnName(int col) { |
1149 |
if (col==0) |
1150 |
return " "; |
1151 |
col--; |
1152 |
int i=mapping[col];
|
1153 |
return aliases[i+1]; |
1154 |
} |
1155 |
|
1156 |
/**
|
1157 |
* Returns the number of fields.
|
1158 |
*
|
1159 |
* @return number of fields
|
1160 |
*/
|
1161 |
public int getColumnCount() { |
1162 |
return mapping.length;
|
1163 |
} |
1164 |
|
1165 |
/**
|
1166 |
* Returns number of rows.
|
1167 |
*
|
1168 |
* @return number of rows.
|
1169 |
*/
|
1170 |
public int getRowCount() { |
1171 |
try {
|
1172 |
return (int) des.getRowCount(); |
1173 |
} catch (DriverIOException e) {
|
1174 |
return 0; |
1175 |
} catch (IOException e) { |
1176 |
return 0; |
1177 |
} |
1178 |
} |
1179 |
|
1180 |
/**
|
1181 |
* DOCUMENT ME!
|
1182 |
*
|
1183 |
* @param row DOCUMENT ME!
|
1184 |
* @param col DOCUMENT ME!
|
1185 |
*
|
1186 |
* @return DOCUMENT ME!
|
1187 |
*/
|
1188 |
public Object getValueAt(int row, int col) { |
1189 |
|
1190 |
if (col==0){ |
1191 |
return null; |
1192 |
} |
1193 |
col--; |
1194 |
int numRow;
|
1195 |
if (orderIndexes != null && row < orderIndexes.length) { |
1196 |
numRow=(int)orderIndexes[row];
|
1197 |
}else {
|
1198 |
numRow=row; |
1199 |
} |
1200 |
try {
|
1201 |
Object obj =null; |
1202 |
IRowEdited rowEdited=des.getRow(numRow); |
1203 |
obj= rowEdited.getAttribute(mapping[col]); |
1204 |
return obj;
|
1205 |
} catch (DriverIOException e1) {
|
1206 |
return ValueFactory.createValue("").toString(); |
1207 |
} catch (IOException e1) { |
1208 |
return ValueFactory.createValue("").toString(); |
1209 |
} |
1210 |
} |
1211 |
|
1212 |
/**
|
1213 |
* DOCUMENT ME!
|
1214 |
*
|
1215 |
* @param rowIndex DOCUMENT ME!
|
1216 |
* @param columnIndex DOCUMENT ME!
|
1217 |
*
|
1218 |
* @return DOCUMENT ME!
|
1219 |
*/
|
1220 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
1221 |
if (columnIndex==0) |
1222 |
return false; |
1223 |
|
1224 |
return isEditing();
|
1225 |
} |
1226 |
|
1227 |
/**
|
1228 |
* DOCUMENT ME!
|
1229 |
*
|
1230 |
* @param aValue DOCUMENT ME!
|
1231 |
* @param rowIndex DOCUMENT ME!
|
1232 |
* @param columnIndex DOCUMENT ME!
|
1233 |
*
|
1234 |
* @throws RuntimeException DOCUMENT ME!
|
1235 |
*/
|
1236 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
1237 |
if (columnIndex==0) |
1238 |
throw new UnsupportedOperationException("Row Number is a read-only column"); |
1239 |
columnIndex--; |
1240 |
Value v; |
1241 |
int numRow;
|
1242 |
if (orderIndexes != null) { |
1243 |
numRow=(int)orderIndexes[rowIndex];
|
1244 |
}else {
|
1245 |
numRow=rowIndex; |
1246 |
} |
1247 |
if (getValueAt(rowIndex,columnIndex+1)==null || getValueAt(rowIndex,columnIndex+1).toString().equals(aValue)) |
1248 |
return;
|
1249 |
try {
|
1250 |
v = ValueFactory.createValueByType(aValue.toString(), |
1251 |
des.getRecordset().getFieldType(columnIndex)); |
1252 |
IRowEdited row = des.getRow(numRow); //.getAttribute(columnIndex);
|
1253 |
Value[] values = row.getAttributes();
|
1254 |
values[columnIndex] = v; |
1255 |
|
1256 |
IRow newRow = null;
|
1257 |
|
1258 |
if (row.getLinkedRow() instanceof IFeature) { |
1259 |
IGeometry geometry = ((DefaultFeature) row.getLinkedRow()).getGeometry(); |
1260 |
newRow = new DefaultFeature(geometry, values,row.getID());
|
1261 |
} else {
|
1262 |
newRow = new DefaultRow(values,row.getID());
|
1263 |
} |
1264 |
|
1265 |
des.modifyRow(numRow, newRow,"Editar valor", EditionEvent.ALPHANUMERIC);
|
1266 |
} catch (DriverException e1) {
|
1267 |
throw new RuntimeException(e1); |
1268 |
} catch (ParseException e) { |
1269 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_del_campo_incorrecto"); |
1270 |
return;
|
1271 |
} catch (DriverLoadException e) {
|
1272 |
throw new RuntimeException(e); |
1273 |
} catch (DriverIOException e) {
|
1274 |
throw new RuntimeException(e); |
1275 |
} catch (IOException e) { |
1276 |
throw new RuntimeException(e); |
1277 |
} catch (NumberFormatException e) { |
1278 |
/* NotificationManager.addError(PluginServices.-getText(this,"numero_incorrecto")+
|
1279 |
"\n"+PluginServices.-getText(this,"fila")+" = "+rowIndex+
|
1280 |
"\n"+PluginServices.-getText(this,"columna")+ " = "+columnIndex,e);
|
1281 |
*/
|
1282 |
} |
1283 |
} |
1284 |
} |
1285 |
public class ColumnModel extends DefaultTableColumnModel{ |
1286 |
private ProjectTable pt;
|
1287 |
private int[] mapping; |
1288 |
public ColumnModel() {
|
1289 |
} |
1290 |
|
1291 |
public void setDataModel(ProjectTable model) { |
1292 |
this.pt=model;
|
1293 |
this.mapping=pt.getMapping();
|
1294 |
} |
1295 |
|
1296 |
public void addColumn(TableColumn tc) { |
1297 |
super.addColumn(tc);
|
1298 |
} |
1299 |
|
1300 |
public TableColumn getColumn(int i) { |
1301 |
TableColumn tc=super.getColumn(i); |
1302 |
return tc;
|
1303 |
} |
1304 |
|
1305 |
public int getColumnCount() { |
1306 |
return super.getColumnCount(); |
1307 |
} |
1308 |
|
1309 |
public void removeColumn(TableColumn arg0) { |
1310 |
super.removeColumn(arg0);
|
1311 |
} |
1312 |
|
1313 |
public int getColumnIndex(Object arg0) { |
1314 |
return super.getColumnIndex(arg0); |
1315 |
} |
1316 |
|
1317 |
public int getColumnIndexAtX(int arg0) { |
1318 |
return super.getColumnIndexAtX(arg0); |
1319 |
} |
1320 |
|
1321 |
public int getColumnMargin() { |
1322 |
return super.getColumnMargin(); |
1323 |
} |
1324 |
|
1325 |
public Enumeration getColumns() { |
1326 |
return super.getColumns(); |
1327 |
} |
1328 |
|
1329 |
public int getTotalColumnWidth() { |
1330 |
int w=0; |
1331 |
for (int i=0;i<getColumnCount();i++){ |
1332 |
w+=pt.getColumn(i).getWidth(); |
1333 |
} |
1334 |
return super.getTotalColumnWidth(); |
1335 |
} |
1336 |
|
1337 |
public void moveColumn(int arg0, int arg1) { |
1338 |
//super.moveColumn(arg0, arg1);
|
1339 |
if (arg0==arg1){
|
1340 |
super.moveColumn(arg0,arg1);
|
1341 |
} |
1342 |
} |
1343 |
|
1344 |
public void addColumnModelListener(TableColumnModelListener arg0) { |
1345 |
super.addColumnModelListener(arg0);
|
1346 |
} |
1347 |
|
1348 |
protected ListSelectionModel createSelectionModel() { |
1349 |
return super.createSelectionModel(); |
1350 |
} |
1351 |
|
1352 |
protected void fireColumnAdded(TableColumnModelEvent arg0) { |
1353 |
super.fireColumnAdded(arg0);
|
1354 |
} |
1355 |
|
1356 |
protected void fireColumnMarginChanged() { |
1357 |
super.fireColumnMarginChanged();
|
1358 |
} |
1359 |
|
1360 |
protected void fireColumnMoved(TableColumnModelEvent arg0) { |
1361 |
super.fireColumnMoved(arg0);
|
1362 |
} |
1363 |
|
1364 |
protected void fireColumnRemoved(TableColumnModelEvent arg0) { |
1365 |
super.fireColumnRemoved(arg0);
|
1366 |
} |
1367 |
|
1368 |
protected void fireColumnSelectionChanged(ListSelectionEvent arg0) { |
1369 |
super.fireColumnSelectionChanged(arg0);
|
1370 |
} |
1371 |
|
1372 |
public TableColumnModelListener[] getColumnModelListeners() { |
1373 |
return super.getColumnModelListeners(); |
1374 |
} |
1375 |
|
1376 |
public boolean getColumnSelectionAllowed() { |
1377 |
return super.getColumnSelectionAllowed(); |
1378 |
} |
1379 |
|
1380 |
public EventListener[] getListeners(Class arg0) { |
1381 |
return super.getListeners(arg0); |
1382 |
} |
1383 |
|
1384 |
public int getSelectedColumnCount() { |
1385 |
return super.getSelectedColumnCount(); |
1386 |
} |
1387 |
|
1388 |
public int[] getSelectedColumns() { |
1389 |
return super.getSelectedColumns(); |
1390 |
} |
1391 |
|
1392 |
public ListSelectionModel getSelectionModel() { |
1393 |
return super.getSelectionModel(); |
1394 |
} |
1395 |
|
1396 |
public void propertyChange(PropertyChangeEvent arg0) { |
1397 |
try{
|
1398 |
super.propertyChange(arg0);
|
1399 |
int w=((Integer)arg0.getNewValue()).intValue(); |
1400 |
if (arg0.getSource() instanceof TableColumn && (w!=75)){ |
1401 |
TableColumn tc=(TableColumn)arg0.getSource(); |
1402 |
Column column=pt.getColumn(tc.getModelIndex()); |
1403 |
column.setWidth(w); |
1404 |
System.out.println("Index guardar = "+tc.getModelIndex()+" , "+"Anchura = "+w); |
1405 |
} |
1406 |
}catch (Exception e) { |
1407 |
System.out.println("PETA"); |
1408 |
} |
1409 |
|
1410 |
} |
1411 |
protected void recalcWidthCache() { |
1412 |
super.recalcWidthCache();
|
1413 |
} |
1414 |
|
1415 |
public void removeColumnModelListener(TableColumnModelListener arg0) { |
1416 |
super.removeColumnModelListener(arg0);
|
1417 |
} |
1418 |
|
1419 |
public void setColumnMargin(int arg0) { |
1420 |
super.setColumnMargin(arg0);
|
1421 |
} |
1422 |
|
1423 |
public void setColumnSelectionAllowed(boolean arg0) { |
1424 |
super.setColumnSelectionAllowed(arg0);
|
1425 |
} |
1426 |
|
1427 |
public void setSelectionModel(ListSelectionModel arg0) { |
1428 |
super.setSelectionModel(arg0);
|
1429 |
} |
1430 |
|
1431 |
public void valueChanged(ListSelectionEvent arg0) { |
1432 |
super.valueChanged(arg0);
|
1433 |
} |
1434 |
|
1435 |
public int hashCode() { |
1436 |
return super.hashCode(); |
1437 |
} |
1438 |
|
1439 |
protected void finalize() throws Throwable { |
1440 |
super.finalize();
|
1441 |
} |
1442 |
|
1443 |
protected Object clone() throws CloneNotSupportedException { |
1444 |
return super.clone(); |
1445 |
} |
1446 |
|
1447 |
public boolean equals(Object arg0) { |
1448 |
return super.equals(arg0); |
1449 |
} |
1450 |
|
1451 |
public String toString() { |
1452 |
return super.toString(); |
1453 |
} |
1454 |
} |
1455 |
/**
|
1456 |
* DOCUMENT ME!
|
1457 |
*
|
1458 |
* @author Vicente Caballero Navarro
|
1459 |
*/
|
1460 |
public class TableSelectionListener implements SelectionListener { |
1461 |
/**
|
1462 |
* @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
1463 |
*/
|
1464 |
public void selectionChanged(SelectionEvent e) { |
1465 |
updateSelection(); |
1466 |
Table.this.repaint(); |
1467 |
} |
1468 |
} |
1469 |
|
1470 |
/**
|
1471 |
* DOCUMENT ME!
|
1472 |
*
|
1473 |
* @author Vicente Caballero Navarro
|
1474 |
*/
|
1475 |
private class MouseHandler extends MouseAdapter { |
1476 |
/**
|
1477 |
* DOCUMENT ME!
|
1478 |
*
|
1479 |
* @param e DOCUMENT ME!
|
1480 |
*/
|
1481 |
public void mouseClicked(MouseEvent e) { |
1482 |
|
1483 |
/* JTableHeader h = (JTableHeader) e.getSource();
|
1484 |
TableColumnModel columnModel = h.getColumnModel();
|
1485 |
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
|
1486 |
int column = columnModel.getColumn(viewColumn).getModelIndex();
|
1487 |
if (column != -1) {
|
1488 |
}*/
|
1489 |
} |
1490 |
} |
1491 |
/**
|
1492 |
* DOCUMENT ME!
|
1493 |
*
|
1494 |
* @author Vicente Caballero Navarro
|
1495 |
*/
|
1496 |
private class TableKeyListener implements KeyListener { |
1497 |
|
1498 |
public void keyPressed(KeyEvent arg0) { |
1499 |
//JTextField tf=(JTextField)table.getEditorComponent();
|
1500 |
//table.setCellSelectionEnabled(true);
|
1501 |
//FocusManager fm = FocusManager.getCurrentManager();
|
1502 |
//fm.focusPreviousComponent(table);
|
1503 |
} |
1504 |
|
1505 |
public void keyReleased(KeyEvent ke) { |
1506 |
int[] row=table.getSelectedRows(); |
1507 |
int[] column=table.getSelectedColumns(); |
1508 |
initEditField(column,row); |
1509 |
} |
1510 |
|
1511 |
public void keyTyped(KeyEvent arg0) { |
1512 |
// TODO Auto-generated method stub
|
1513 |
|
1514 |
} |
1515 |
|
1516 |
} |
1517 |
|
1518 |
/**
|
1519 |
* DOCUMENT ME!
|
1520 |
*
|
1521 |
* @author Vicente Caballero Navarro
|
1522 |
*/
|
1523 |
private class MouseRow extends MouseAdapter { |
1524 |
public void mouseReleased(MouseEvent arg0) { |
1525 |
super.mouseReleased(arg0);
|
1526 |
int[] row=table.getSelectedRows(); |
1527 |
int[] column=table.getSelectedColumns(); |
1528 |
initEditField(column,row); |
1529 |
} |
1530 |
|
1531 |
/**
|
1532 |
* DOCUMENT ME!
|
1533 |
*
|
1534 |
* @param e DOCUMENT ME!
|
1535 |
*/
|
1536 |
/* public void mouseClicked(MouseEvent e) {
|
1537 |
super.mouseClicked(e);
|
1538 |
|
1539 |
|
1540 |
PluginServices.getMainFrame().enableControls();
|
1541 |
|
1542 |
if (e.getButton() == MouseEvent.BUTTON3) {
|
1543 |
new PopupMenu(e.getPoint());
|
1544 |
}
|
1545 |
}
|
1546 |
*/
|
1547 |
} |
1548 |
|
1549 |
public void commandRepaint() { |
1550 |
refresh(); |
1551 |
} |
1552 |
|
1553 |
public void commandRefresh() { |
1554 |
commandRepaint(); |
1555 |
} |
1556 |
|
1557 |
public void toPalette() { |
1558 |
isPalette=true;
|
1559 |
m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
|
1560 |
ViewInfo.MODELESSDIALOG | ViewInfo.RESIZABLE | ViewInfo.PALETTE); |
1561 |
PluginServices.getMDIManager().changeViewInfo(this,getViewInfo());
|
1562 |
} |
1563 |
|
1564 |
public void restore() { |
1565 |
isPalette=false;
|
1566 |
m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
|
1567 |
ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE); |
1568 |
PluginServices.getMDIManager().changeViewInfo(this,getViewInfo());
|
1569 |
} |
1570 |
|
1571 |
public boolean isPalette() { |
1572 |
return isPalette;
|
1573 |
} |
1574 |
|
1575 |
|
1576 |
private void addNumbers(){ |
1577 |
TableColumn tc=new TableColumn(); |
1578 |
tc.setWidth(40);
|
1579 |
tc.setResizable(false);
|
1580 |
getTable().addColumn(tc); |
1581 |
} |
1582 |
public static void main(String[] args) { |
1583 |
JTextField tf=new JTextField("hola"); |
1584 |
tf.selectAll(); |
1585 |
JFrame frame=new JFrame(); |
1586 |
frame.getContentPane().add(tf); |
1587 |
frame.show(true);
|
1588 |
} |
1589 |
|
1590 |
public void stopEditingCell() { |
1591 |
if (table.isEditing()) {
|
1592 |
// TODO: finalizar la edicion de la columna
|
1593 |
this.table.getCellEditor().stopCellEditing();
|
1594 |
this.refresh();
|
1595 |
} |
1596 |
|
1597 |
} |
1598 |
|
1599 |
public void cancelEditingCell() { |
1600 |
if (table.isEditing()) {
|
1601 |
// TODO: finalizar la edicion de la columna
|
1602 |
this.table.getCellEditor().cancelCellEditing();
|
1603 |
this.refresh();
|
1604 |
} |
1605 |
|
1606 |
} |
1607 |
} |