Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / Table.java @ 6259

History | View | Annotate | Download (47.3 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
                                 ies.getSelection().clear();
791
                                 refreshControls();
792
                         }
793
                 }
794

    
795
                 /*
796
                GdbmsWriter gdbmswriter = new GdbmsWriter();
797
                gdbmswriter.setDataWare(getModel().getModelo()
798
                                                              .getRecordset()
799
                                                              .getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER));
800
                gdbmswriter.preProcess();
801

802
                for (int i = 0; i < getModel().getModelo().getRowCount();
803
                        i++) {
804
                    gdbmswriter.process(getModel().getModelo().getRow(i));
805
                }
806

807
                gdbmswriter.postProcess();
808
                */
809

    
810
            }
811

    
812
        } catch (Exception e) {
813
            NotificationManager.addError("No se pudo guardar la edici?n", e);
814
        }
815
    }
816

    
817
    /**
818
     * DOCUMENT ME!
819
     *
820
     * @param index DOCUMENT ME!
821
     */
822
    public void hideColumns(int[] index) {
823
        // TODO Auto-generated method stub
824
    }
825

    
826
    /**
827
     * DOCUMENT ME!
828
     *
829
     * @param index DOCUMENT ME!
830
     */
831
    public void setUneditableColumns(int[] index) {
832
        // TODO Auto-generated method stub
833
    }
834

    
835
    /**
836
     * DOCUMENT ME!
837
     *
838
     * @param numColumns DOCUMENT ME!
839
     * @param values DOCUMENT ME!
840
     */
841
    public void setDefaultValues(int[] numColumns, Value[] values) {
842
        // TODO Auto-generated method stub
843
    }
844

    
845
    /**
846
     * DOCUMENT ME!
847
     *
848
     * @return DOCUMENT ME!
849
     */
850
    public Value getDefaultValue() {
851
        // TODO Auto-generated method stub
852
        return null;
853
    }
854

    
855
    /**
856
     * DOCUMENT ME!
857
     *
858
     * @throws IOException DOCUMENT ME!
859
     */
860
    public void cancelEditing() throws IOException {
861
            this.cancelEditingCell();
862
        getModel().getModelo().cancelEdition(EditionEvent.ALPHANUMERIC);
863
        //refresh();
864
        refreshControls();
865
    }
866

    
867
    /**
868
     * DOCUMENT ME!
869
     *
870
     * @return DOCUMENT ME!
871
     */
872
    public boolean isEditing() {
873
        return getModel().getModelo().isEditing();
874
    }
875

    
876
    /**
877
     * DOCUMENT ME!
878
     */
879
    public void refresh() {
880
            int row=table.getSelectedRow();
881
            int column=table.getSelectedColumn();
882
            if (row!=-1 && column!=-1 && table.getEditorComponent()!=null){
883
                    Value[] values=getValueRow(row);
884
                    JTextField jtf=(JTextField)table.getEditorComponent();
885
                    jtf.setText(values[column].toString());
886
            }
887
        updating = true;
888
        ((DataSourceDataModel) getTable().getModel()).fireTableDataChanged();
889

    
890
        updating = false;
891
        updateSelection();
892
        PluginServices.getMainFrame().enableControls();
893

    
894
    }
895

    
896
    /**
897
     * Add the rows that are passed like parameter and if parameter is null a
898
     * row is added empties.
899
     *
900
     * @param rows Rows to add or null.
901
     *
902
     * @throws DriverIOException
903
     * @throws IOException
904
     */
905
    public void addRow(IRow[] rows) throws DriverIOException, IOException {
906
            IEditableSource ies=getModel().getModelo();
907
            try {
908
            if (rows == null) {
909

    
910
                    if (getModel().getAssociatedTable()!=null){
911
                            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa.");
912
                            return;
913
                    }
914
                IRow row;
915
                int numAttr=getModel().getModelo()
916
                        .getRecordset()
917
                        .getFieldCount();
918
                    Value[] values=new Value[numAttr];
919
                    for (int i=0;i<numAttr;i++){
920
                            values[i]=ValueFactory.createNullValue();
921
                    }
922
                row = new DefaultRow(values);
923
                ies.addRow(row,"Fila en blanco",EditionEvent.ALPHANUMERIC);
924
            } else {
925
                    ies.startComplexRow();
926
                for (int i = 0; i < rows.length; i++) {
927
                   ies.addRow(((IRowEdited) rows[i]).getLinkedRow(),"Pegar filas",EditionEvent.ALPHANUMERIC);
928
                }
929
                String description=PluginServices.getText(this,"add_rows");
930
                ies.endComplexRow(description);
931
            }
932
        } catch (DriverException e) {
933
            // TODO Auto-generated catch block
934
            e.printStackTrace();
935
        } catch (DriverLoadException e) {
936
            // TODO Auto-generated catch block
937
            e.printStackTrace();
938
        }
939

    
940
        refresh();
941

    
942
    }
943

    
944
    /**
945
     * Copy in the arraylist the rows that be selected just then.
946
     *
947
     * @throws DriverIOException
948
     * @throws IOException
949
     */
950
    public void copyRow() throws DriverIOException, IOException {
951
        int[] index = getSelectedRowIndices();
952
        rowsCopied = new IRow[index.length];
953

    
954
        for (int i = 0; i < index.length; i++) {
955
            rowsCopied[i] = getModel().getModelo().getRow(index[i]);
956
        }
957
    }
958

    
959
    /**
960
     * Cut the rows that be selected just then.
961
     *
962
     * @throws DriverIOException
963
     * @throws IOException
964
     */
965
    public void cutRow() throws DriverIOException, IOException {
966
        int[] index = getSelectedRowIndices();
967
        rowsCopied = new IRow[index.length];
968

    
969
        for (int i = 0; i < index.length; i++) {
970
            rowsCopied[i] = getModel().getModelo().getRow(index[i]);
971
        }
972

    
973
        removeRow();
974
    }
975

    
976
    /**
977
     * Remove in the rows that be selected just then.
978
     *
979
     * @throws DriverIOException
980
     * @throws IOException
981
     */
982
    public void removeRow() throws DriverIOException, IOException {
983
            int[] index = getSelectedRowIndices();
984
        getModel().getModelo().startComplexRow();
985
        for (int i = index.length-1; i >=0; i--) {
986
            getModel().getModelo().removeRow(index[i],"Eliminar fila", EditionEvent.ALPHANUMERIC);
987
        }
988
//        int[] sel={0};
989
//        getTable().setEditingRow(-1);
990
//        getTable().getCellEditor().
991
//        initEditField(sel,sel);
992
        getTable().getCellEditor().cancelCellEditing();
993
        String description=PluginServices.getText(this,"remove_rows");
994
        getModel().getModelo().endComplexRow(description);
995
        getTable().clearSelection();
996

    
997
        refresh();
998
        //repaintAssociatedView();
999
    }
1000

    
1001
//    private void repaintAssociatedView(){
1002
//             View[] views = (View[]) PluginServices.getMDIManager().getAllViews();
1003
//
1004
//                 for (int i=0 ; i<views.length ; i++){
1005
//                         if (views[i] instanceof com.iver.cit.gvsig.gui.View){
1006
//                                 com.iver.cit.gvsig.gui.View view=(com.iver.cit.gvsig.gui.View)views[i];
1007
//                                 FLayers layers =view.getMapControl().getMapContext().getLayers();
1008
//                                 for (int j=0;j<layers.getLayersCount();j++){
1009
//                                         if (layers.getLayer(j).equals(this.getModel().getAssociatedTable())){
1010
//                                                 view.getMapControl().getMapContext().invalidate();
1011
//                                         }
1012
//                                 }
1013
//                         }
1014
//                 }
1015
//    }
1016
    /**
1017
     * DOCUMENT ME!
1018
     */
1019
    public void addColumn(FieldDescription newField) {
1020
            EditableAdapter edAdapter = (EditableAdapter) getModel().getModelo();
1021
            try {
1022
                        edAdapter.addField(newField);
1023
                        if (getTable().getCellEditor() != null)
1024
                                getTable().getCellEditor().cancelCellEditing();
1025
        
1026
                refresh();
1027
                refreshControls();
1028
                } catch (EditionException e) {
1029
                        e.printStackTrace();
1030
                        NotificationManager.addError(e);
1031
                }
1032

    
1033
    }
1034

    
1035
    /**
1036
     * DOCUMENT ME!
1037
     */
1038
    public void removeColumn() {
1039
            EditableAdapter edAdapter = (EditableAdapter) getModel().getModelo();
1040
            try {
1041
                    BitSet selectedFields = getSelectedFieldIndices();
1042
                    SelectableDataSource sds = edAdapter.getRecordset();
1043
                    edAdapter.startComplexRow();
1044
                    for(int i=selectedFields.nextSetBit(0); i>=0; i=selectedFields.nextSetBit(i+1)) {
1045
                            FieldDescription fld = sds.getFieldsDescription()[i];
1046
                            edAdapter.removeField(fld.getFieldAlias());
1047
                    }
1048
                    if (getTable().getCellEditor() != null)
1049
                                getTable().getCellEditor().cancelCellEditing();
1050
        
1051
                edAdapter.endComplexRow(PluginServices.getText(this, "remove_fields"));
1052
        
1053
                getModel().setModel(edAdapter); // Para que se recalculen los campos. TODO: Limpiear todo esto 
1054
                refresh();
1055
                refreshControls();
1056
                } catch (EditionException e) {
1057
                        e.printStackTrace();
1058
                        NotificationManager.addError(e);
1059
                } catch (DriverException e) {
1060
                        e.printStackTrace();
1061
                        NotificationManager.addError(e);
1062
                } catch (IOException e) {
1063
                        e.printStackTrace();
1064
                        NotificationManager.addError(e);
1065
                } catch (DriverIOException e) {
1066
                        e.printStackTrace();
1067
                        NotificationManager.addError(e);
1068
                }
1069

    
1070
    }
1071

    
1072
    /**
1073
     * Return if we have rows copied or not.
1074
     *
1075
     * @return True if we have rows copied.
1076
     */
1077
    public boolean isCopied() {
1078
        return (rowsCopied != null);
1079
    }
1080

    
1081
    /**
1082
     * Paste the arraylist rows.
1083
     *
1084
     * @throws DriverIOException
1085
     * @throws IOException
1086
     */
1087
    public void pasteRow() throws DriverIOException, IOException {
1088
        addRow(rowsCopied);
1089
        //repaintAssociatedView();
1090
    }
1091

    
1092
    /**
1093
     * DOCUMENT ME!
1094
     */
1095
    public void viewActivated() {
1096
        //if (isEditing() && getModel().getModelo() instanceof VectorialEditableAdapter)
1097
        updateSelection();
1098
    }
1099

    
1100
    /**
1101
     * DOCUMENT ME!
1102
     */
1103
    public void viewClosed() {
1104
        // TODO Auto-generated method stub
1105
    }
1106

    
1107
    /**
1108
     * DOCUMENT ME!
1109
     *
1110
     * @author Fernando Gonz?lez Cort?s
1111
     */
1112
    public class DataSourceDataModel extends AbstractTableModel {
1113
        //private SelectableDataSource dataSource;
1114
        private IEditableSource des;
1115
        private String[] aliases;
1116
        private int[] mapping;
1117

    
1118
        /**
1119
         * Crea un nuevo DataSourceDataModel.
1120
         *
1121
         * @param pt DOCUMENT ME!
1122
         */
1123
        public DataSourceDataModel(ProjectTable pt) {
1124
            this.des = pt.getModelo();
1125

    
1126
            //try {
1127
            //dataSource = pt.getModelo().getRecordset();
1128
            //} catch (DriverLoadException e) {
1129
            // TODO Auto-generated catch block
1130
            //        e.printStackTrace();
1131
            //}
1132
            aliases = pt.getAliases();
1133
            mapping = pt.getMapping();
1134
        }
1135

    
1136
        /**
1137
         * Returns the name of the field.
1138
         *
1139
         * @param col index of field
1140
         *
1141
         * @return Name of field
1142
         */
1143
        public String getColumnName(int col) {
1144
                if (col==0)
1145
                        return " ";
1146
                col--;
1147
                int i=mapping[col];
1148
                return aliases[i+1];
1149
        }
1150

    
1151
        /**
1152
         * Returns the number of fields.
1153
         *
1154
         * @return number of fields
1155
         */
1156
        public int getColumnCount() {
1157
            return mapping.length;
1158
        }
1159

    
1160
        /**
1161
         * Returns number of rows.
1162
         *
1163
         * @return number of rows.
1164
         */
1165
        public int getRowCount() {
1166
            try {
1167
                return (int) des.getRowCount();
1168
            } catch (DriverIOException e) {
1169
                return 0;
1170
            } catch (IOException e) {
1171
                return 0;
1172
            }
1173
        }
1174

    
1175
        /**
1176
         * DOCUMENT ME!
1177
         *
1178
         * @param row DOCUMENT ME!
1179
         * @param col DOCUMENT ME!
1180
         *
1181
         * @return DOCUMENT ME!
1182
         */
1183
        public Object getValueAt(int row, int col) {
1184

    
1185
           if (col==0){
1186
                   return null;
1187
           }
1188
           col--;
1189
           int numRow;
1190
           if (orderIndexes != null && row < orderIndexes.length) {
1191
                   numRow=(int)orderIndexes[row];
1192
           }else {
1193
                   numRow=row;
1194
           }
1195
                try {
1196
                        Object obj =null;
1197
                                IRowEdited rowEdited=des.getRow(numRow);
1198
                                obj= rowEdited.getAttribute(mapping[col]);
1199
                                        return obj;
1200
                        } catch (DriverIOException e1) {
1201
                                return ValueFactory.createValue("").toString();
1202
                        } catch (IOException e1) {
1203
                                return ValueFactory.createValue("").toString();
1204
                        }
1205
            }
1206

    
1207
        /**
1208
         * DOCUMENT ME!
1209
         *
1210
         * @param rowIndex DOCUMENT ME!
1211
         * @param columnIndex DOCUMENT ME!
1212
         *
1213
         * @return DOCUMENT ME!
1214
         */
1215
        public boolean isCellEditable(int rowIndex, int columnIndex) {
1216
                if (columnIndex==0)
1217
                return false;
1218

    
1219
                return isEditing();
1220
        }
1221

    
1222
        /**
1223
         * DOCUMENT ME!
1224
         *
1225
         * @param aValue DOCUMENT ME!
1226
         * @param rowIndex DOCUMENT ME!
1227
         * @param columnIndex DOCUMENT ME!
1228
         *
1229
         * @throws RuntimeException DOCUMENT ME!
1230
         */
1231
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
1232
                 if (columnIndex==0)
1233
                 throw new UnsupportedOperationException("Row Number is a read-only column");
1234
                 columnIndex--;
1235
                Value v;
1236
                 int numRow;
1237
             if (orderIndexes != null) {
1238
                     numRow=(int)orderIndexes[rowIndex];
1239
             }else {
1240
                     numRow=rowIndex;
1241
             }
1242
                if (getValueAt(rowIndex,columnIndex+1)==null || getValueAt(rowIndex,columnIndex+1).toString().equals(aValue))
1243
                    return;
1244
            try {
1245
                    v = ValueFactory.createValueByType(aValue.toString(),
1246
                        des.getRecordset().getFieldType(columnIndex));
1247
                IRowEdited row = des.getRow(numRow); //.getAttribute(columnIndex);
1248
                Value[] values = row.getAttributes();
1249
                values[columnIndex] = v;
1250

    
1251
                IRow newRow = null;
1252

    
1253
                if (row.getLinkedRow() instanceof IFeature) {
1254
                    IGeometry geometry = ((DefaultFeature) row.getLinkedRow()).getGeometry();
1255
                    newRow = new DefaultFeature(geometry, values,row.getID());
1256
                } else {
1257
                    newRow = new DefaultRow(values,row.getID());
1258
                }
1259

    
1260
                des.modifyRow(numRow, newRow,"Editar valor", EditionEvent.ALPHANUMERIC);
1261
            } catch (DriverException e1) {
1262
                throw new RuntimeException(e1);
1263
            } catch (ParseException e) {
1264
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_del_campo_incorrecto");
1265
                        return;
1266
            } catch (DriverLoadException e) {
1267
                throw new RuntimeException(e);
1268
            } catch (DriverIOException e) {
1269
                throw new RuntimeException(e);
1270
            } catch (IOException e) {
1271
                throw new RuntimeException(e);
1272
            } catch (NumberFormatException e) {
1273
                        /*        NotificationManager.addError(PluginServices.-getText(this,"numero_incorrecto")+
1274
                                                "\n"+PluginServices.-getText(this,"fila")+" = "+rowIndex+
1275
                                                "\n"+PluginServices.-getText(this,"columna")+ " = "+columnIndex,e);
1276
                        */
1277
                        }
1278
        }
1279
    }
1280
    public class ColumnModel extends DefaultTableColumnModel{
1281
            private ProjectTable pt;
1282
            private int[] mapping;
1283
                public ColumnModel() {
1284
                }
1285

    
1286
                public void setDataModel(ProjectTable model) {
1287
                        this.pt=model;
1288
                        this.mapping=pt.getMapping();
1289
                }
1290

    
1291
                public void addColumn(TableColumn tc) {
1292
                        super.addColumn(tc);
1293
                }
1294

    
1295
                public TableColumn getColumn(int i) {
1296
                        TableColumn tc=super.getColumn(i);
1297
                        return tc;
1298
                }
1299

    
1300
                public int getColumnCount() {
1301
                        return super.getColumnCount();
1302
                }
1303

    
1304
                public void removeColumn(TableColumn arg0) {
1305
                        super.removeColumn(arg0);
1306
                }
1307

    
1308
                public int getColumnIndex(Object arg0) {
1309
                        return super.getColumnIndex(arg0);
1310
                }
1311

    
1312
                public int getColumnIndexAtX(int arg0) {
1313
                        return super.getColumnIndexAtX(arg0);
1314
                }
1315

    
1316
                public int getColumnMargin() {
1317
                        return super.getColumnMargin();
1318
                }
1319

    
1320
                public Enumeration getColumns() {
1321
                        return super.getColumns();
1322
                }
1323

    
1324
                public int getTotalColumnWidth() {
1325
                        int w=0;
1326
                        for (int i=0;i<getColumnCount();i++){
1327
                                w+=pt.getColumn(i).getWidth();
1328
                        }
1329
                        return super.getTotalColumnWidth();
1330
                }
1331

    
1332
                public void moveColumn(int arg0, int arg1) {
1333
                        //super.moveColumn(arg0, arg1);
1334
                        if (arg0==arg1){
1335
                                super.moveColumn(arg0,arg1);
1336
                        }
1337
                }
1338

    
1339
                public void addColumnModelListener(TableColumnModelListener arg0) {
1340
                        super.addColumnModelListener(arg0);
1341
                }
1342

    
1343
                protected ListSelectionModel createSelectionModel() {
1344
                        return super.createSelectionModel();
1345
                }
1346

    
1347
                protected void fireColumnAdded(TableColumnModelEvent arg0) {
1348
                        super.fireColumnAdded(arg0);
1349
                }
1350

    
1351
                protected void fireColumnMarginChanged() {
1352
                        super.fireColumnMarginChanged();
1353
                }
1354

    
1355
                protected void fireColumnMoved(TableColumnModelEvent arg0) {
1356
                        super.fireColumnMoved(arg0);
1357
                }
1358

    
1359
                protected void fireColumnRemoved(TableColumnModelEvent arg0) {
1360
                        super.fireColumnRemoved(arg0);
1361
                }
1362

    
1363
                protected void fireColumnSelectionChanged(ListSelectionEvent arg0) {
1364
                        super.fireColumnSelectionChanged(arg0);
1365
                }
1366

    
1367
                public TableColumnModelListener[] getColumnModelListeners() {
1368
                        return super.getColumnModelListeners();
1369
                }
1370

    
1371
                public boolean getColumnSelectionAllowed() {
1372
                        return super.getColumnSelectionAllowed();
1373
                }
1374

    
1375
                public EventListener[] getListeners(Class arg0) {
1376
                        return super.getListeners(arg0);
1377
                }
1378

    
1379
                public int getSelectedColumnCount() {
1380
                        return super.getSelectedColumnCount();
1381
                }
1382

    
1383
                public int[] getSelectedColumns() {
1384
                        return super.getSelectedColumns();
1385
                }
1386

    
1387
                public ListSelectionModel getSelectionModel() {
1388
                        return super.getSelectionModel();
1389
                }
1390

    
1391
                public void propertyChange(PropertyChangeEvent arg0) {
1392
                        try{
1393
                        super.propertyChange(arg0);
1394
                        int w=((Integer)arg0.getNewValue()).intValue();
1395
                        if (arg0.getSource() instanceof TableColumn && (w!=75)){
1396
                                TableColumn tc=(TableColumn)arg0.getSource();
1397
                                Column column=pt.getColumn(tc.getModelIndex());
1398
                                column.setWidth(w);
1399
                                System.out.println("Index guardar = "+tc.getModelIndex()+" , "+"Anchura = "+w);
1400
                        }
1401
                        }catch (Exception e) {
1402
                                System.out.println("PETA");
1403
                        }
1404

    
1405
                }
1406
                protected void recalcWidthCache() {
1407
                        super.recalcWidthCache();
1408
                }
1409

    
1410
                public void removeColumnModelListener(TableColumnModelListener arg0) {
1411
                        super.removeColumnModelListener(arg0);
1412
                }
1413

    
1414
                public void setColumnMargin(int arg0) {
1415
                        super.setColumnMargin(arg0);
1416
                }
1417

    
1418
                public void setColumnSelectionAllowed(boolean arg0) {
1419
                        super.setColumnSelectionAllowed(arg0);
1420
                }
1421

    
1422
                public void setSelectionModel(ListSelectionModel arg0) {
1423
                        super.setSelectionModel(arg0);
1424
                }
1425

    
1426
                public void valueChanged(ListSelectionEvent arg0) {
1427
                        super.valueChanged(arg0);
1428
                }
1429

    
1430
                public int hashCode() {
1431
                        return super.hashCode();
1432
                }
1433

    
1434
                protected void finalize() throws Throwable {
1435
                        super.finalize();
1436
                }
1437

    
1438
                protected Object clone() throws CloneNotSupportedException {
1439
                        return super.clone();
1440
                }
1441

    
1442
                public boolean equals(Object arg0) {
1443
                        return super.equals(arg0);
1444
                }
1445

    
1446
                public String toString() {
1447
                        return super.toString();
1448
                }
1449
        }
1450
    /**
1451
     * DOCUMENT ME!
1452
     *
1453
     * @author Vicente Caballero Navarro
1454
     */
1455
    public class TableSelectionListener implements SelectionListener {
1456
        /**
1457
         * @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
1458
         */
1459
        public void selectionChanged(SelectionEvent e) {
1460
            updateSelection();
1461
            Table.this.repaint();
1462
        }
1463
    }
1464

    
1465
    /**
1466
     * DOCUMENT ME!
1467
     *
1468
     * @author Vicente Caballero Navarro
1469
     */
1470
    private class MouseHandler extends MouseAdapter {
1471
        /**
1472
         * DOCUMENT ME!
1473
         *
1474
         * @param e DOCUMENT ME!
1475
         */
1476
        public void mouseClicked(MouseEvent e) {
1477

    
1478
            /* JTableHeader h = (JTableHeader) e.getSource();
1479
               TableColumnModel columnModel = h.getColumnModel();
1480
               int viewColumn = columnModel.getColumnIndexAtX(e.getX());
1481
               int column = columnModel.getColumn(viewColumn).getModelIndex();
1482
               if (column != -1) {
1483
               }*/
1484
        }
1485
    }
1486
    /**
1487
     * DOCUMENT ME!
1488
     *
1489
     * @author Vicente Caballero Navarro
1490
     */
1491
    private class TableKeyListener implements KeyListener {
1492

    
1493
                public void keyPressed(KeyEvent arg0) {
1494
                        //JTextField tf=(JTextField)table.getEditorComponent();
1495
                        //table.setCellSelectionEnabled(true);
1496
                        //FocusManager fm = FocusManager.getCurrentManager();
1497
                        //fm.focusPreviousComponent(table);
1498
                }
1499

    
1500
                public void keyReleased(KeyEvent ke) {
1501
                        int[] row=table.getSelectedRows();
1502
                    int[] column=table.getSelectedColumns();
1503
                    initEditField(column,row);
1504
                }
1505

    
1506
                public void keyTyped(KeyEvent arg0) {
1507
                        // TODO Auto-generated method stub
1508

    
1509
                }
1510

    
1511
    }
1512

    
1513
    /**
1514
     * DOCUMENT ME!
1515
     *
1516
     * @author Vicente Caballero Navarro
1517
     */
1518
    private class MouseRow extends MouseAdapter {
1519
               public void mouseReleased(MouseEvent arg0) {
1520
                        super.mouseReleased(arg0);
1521
                        int[] row=table.getSelectedRows();
1522
                    int[] column=table.getSelectedColumns();
1523
                    initEditField(column,row);
1524
                }
1525

    
1526
                /**
1527
         * DOCUMENT ME!
1528
         *
1529
         * @param e DOCUMENT ME!
1530
         */
1531
    /*    public void mouseClicked(MouseEvent e) {
1532
            super.mouseClicked(e);
1533

1534

1535
                PluginServices.getMainFrame().enableControls();
1536

1537
            if (e.getButton() == MouseEvent.BUTTON3) {
1538
                new PopupMenu(e.getPoint());
1539
            }
1540
        }
1541
*/
1542
    }
1543

    
1544
        public void commandRepaint() {
1545
                refresh();
1546
        }
1547

    
1548
        public void commandRefresh() {
1549
                commandRepaint();
1550
        }
1551

    
1552
        public void toPalette() {
1553
                isPalette=true;
1554
                m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
1555
                                ViewInfo.MODELESSDIALOG | ViewInfo.RESIZABLE | ViewInfo.PALETTE);
1556
        PluginServices.getMDIManager().changeViewInfo(this,getViewInfo());
1557
        }
1558

    
1559
        public void restore() {
1560
                isPalette=false;
1561
                m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
1562
                    ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE);
1563
                PluginServices.getMDIManager().changeViewInfo(this,getViewInfo());
1564
        }
1565

    
1566
        public boolean isPalette() {
1567
                return isPalette;
1568
        }
1569

    
1570

    
1571
        private void addNumbers(){
1572
                TableColumn tc=new TableColumn();
1573
                tc.setWidth(40);
1574
                tc.setResizable(false);
1575
                getTable().addColumn(tc);
1576
        }
1577
        public static void main(String[] args) {
1578
                JTextField tf=new JTextField("hola");
1579
                tf.selectAll();
1580
                JFrame frame=new JFrame();
1581
                frame.getContentPane().add(tf);
1582
                frame.show(true);
1583
        }
1584

    
1585
        public void stopEditingCell() {
1586
            if (table.isEditing()) {
1587
                    // TODO: finalizar la edicion de la columna
1588
                    this.table.getCellEditor().stopCellEditing();
1589
                    this.refresh();
1590
            }
1591

    
1592
        }
1593

    
1594
        public void cancelEditingCell() {
1595
            if (table.isEditing()) {
1596
                    // TODO: finalizar la edicion de la columna
1597
                    this.table.getCellEditor().cancelCellEditing();
1598
                    this.refresh();
1599
            }
1600

    
1601
        }
1602
}