Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / gui / Table.java @ 7304

History | View | Annotate | Download (47 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.project.documents.table.gui;
42

    
43
import java.awt.Color;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.event.KeyAdapter;
47
import java.awt.event.KeyEvent;
48
import java.awt.event.KeyListener;
49
import java.awt.event.MouseAdapter;
50
import java.awt.event.MouseEvent;
51
import java.beans.PropertyChangeEvent;
52
import java.beans.PropertyChangeListener;
53
import java.io.IOException;
54
import java.sql.Types;
55
import java.text.ParseException;
56
import java.util.BitSet;
57
import java.util.Enumeration;
58
import java.util.EventListener;
59
import java.util.EventObject;
60

    
61
import javax.swing.BorderFactory;
62
import javax.swing.DefaultCellEditor;
63
import javax.swing.DefaultListSelectionModel;
64
import javax.swing.JComponent;
65
import javax.swing.JFrame;
66
import javax.swing.JLabel;
67
import javax.swing.JOptionPane;
68
import javax.swing.JPanel;
69
import javax.swing.JTextField;
70
import javax.swing.ListSelectionModel;
71
import javax.swing.UIManager;
72
import javax.swing.border.Border;
73
import javax.swing.event.ListSelectionEvent;
74
import javax.swing.event.ListSelectionListener;
75
import javax.swing.event.TableColumnModelEvent;
76
import javax.swing.event.TableColumnModelListener;
77
import javax.swing.table.AbstractTableModel;
78
import javax.swing.table.DefaultTableColumnModel;
79
import javax.swing.table.TableCellRenderer;
80
import javax.swing.table.TableColumn;
81

    
82
import org.apache.log4j.Logger;
83

    
84
import com.hardcode.driverManager.DriverLoadException;
85
import com.hardcode.gdbms.engine.data.driver.DriverException;
86
import com.hardcode.gdbms.engine.values.Value;
87
import com.hardcode.gdbms.engine.values.ValueFactory;
88
import com.iver.andami.PluginServices;
89
import com.iver.andami.messages.NotificationManager;
90
import com.iver.andami.ui.mdiFrame.MainFrame;
91
import com.iver.andami.ui.mdiManager.SingletonWindow;
92
import com.iver.andami.ui.mdiManager.WindowInfo;
93
import com.iver.andami.ui.mdiManager.IWindowListener;
94
import com.iver.andami.ui.mdiManager.IWindowTransform;
95
import com.iver.cit.gvsig.fmap.MapContext;
96
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
97
import com.iver.cit.gvsig.fmap.core.DefaultRow;
98
import com.iver.cit.gvsig.fmap.core.IFeature;
99
import com.iver.cit.gvsig.fmap.core.IGeometry;
100
import com.iver.cit.gvsig.fmap.core.IRow;
101
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
102
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
103
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
104
import com.iver.cit.gvsig.fmap.edition.AfterFieldEditEvent;
105
import com.iver.cit.gvsig.fmap.edition.AfterRowEditEvent;
106
import com.iver.cit.gvsig.fmap.edition.BeforeFieldEditEvent;
107
import com.iver.cit.gvsig.fmap.edition.BeforeRowEditEvent;
108
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
109
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
110
import com.iver.cit.gvsig.fmap.edition.EditionException;
111
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
112
import com.iver.cit.gvsig.fmap.edition.IEditionListener;
113
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
114
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
115
import com.iver.cit.gvsig.fmap.edition.IWriteable;
116
import com.iver.cit.gvsig.fmap.edition.IWriter;
117
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
118
import com.iver.cit.gvsig.fmap.edition.commands.CommandListener;
119
import com.iver.cit.gvsig.fmap.layers.FLayer;
120
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
121
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
122
import com.iver.cit.gvsig.fmap.layers.SelectionEvent;
123
import com.iver.cit.gvsig.fmap.layers.SelectionListener;
124
import com.iver.cit.gvsig.gui.tables.Column;
125
import com.iver.cit.gvsig.project.documents.table.EditionTable;
126
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
127
import com.iver.utiles.swing.jtable.FieldSelectionEvent;
128
import com.iver.utiles.swing.jtable.FieldSelectionListener;
129
import com.iver.utiles.swing.jtable.SelectionHeaderSupport;
130

    
131
/**
132
 * DOCUMENT ME!
133
 *
134
 * @author Fernando Gonz?lez Cort?s
135
 */
136
public class Table extends JPanel implements SingletonWindow, EditionTable,
137
    IWindowListener,CommandListener, IWindowTransform, IEditionListener {
138
    private static Logger logger = Logger.getLogger(Table.class.getName());
139
    private javax.swing.JScrollPane jScrollPane = null;
140
    protected javax.swing.JTable table = null;
141
    protected ProjectTable model = null;
142
    protected JLabel jLabelStatus = null;
143
    protected MapContext fmap;
144
    protected boolean updating = false;
145
    private TableSelectionListener selectionListener = new TableSelectionListener();
146
    private long numReg = 0;
147
    protected SelectionHeaderSupport headerSelectionSupport = new SelectionHeaderSupport();
148
    private long[] orderIndexes = null;
149
    private long[] orderIndexesInverted = null;
150
    private IRow[] rowsCopied = null;
151
    private WindowInfo m_viewInfo = null;
152
        private boolean isPalette=false;
153
        private String[] antAliases;
154
        private int[] antMapping;
155
        /**
156
     * This is the default constructor
157
     */
158
    public Table() {
159
        super();
160
        initialize();
161
    }
162

    
163
    /**
164
     * DOCUMENT ME!
165
     *
166
     * @return DOCUMENT ME!
167
     */
168
    public ProjectTable getModel() {
169
        return model;
170
    }
171

    
172
    /**
173
     * DOCUMENT ME!
174
     *
175
     * @return DOCUMENT ME!
176
     */
177
    public BitSet getSelectedFieldIndices() {
178
            BitSet bs=headerSelectionSupport.getSelectedColumns();
179
//            BitSet newbs=new BitSet();
180
//            for (int i =1;i<bs.length();i++){
181
//                    if (bs.get(i))
182
//                            newbs.set(i-1);
183
//            }
184
//        return newbs;
185
            return bs;
186
    }
187

    
188
    /**
189
     * DOCUMENT ME!
190
     *
191
     * @return DOCUMENT ME!
192
     */
193
    public int[] getSelectedRowIndices() {
194
        return getTable().getSelectedRows();
195
    }
196

    
197
    /**
198
     * DOCUMENT ME!
199
     */
200
    protected void refreshControls() {
201
        try {
202
            MainFrame mF = PluginServices.getMainFrame();
203

    
204
            if (mF != null) {
205
                PluginServices.getMDIManager().getWindowInfo(Table.this).setTitle(PluginServices.getText(
206
                        this, "Tabla") + ": " + model.getName());
207
            }
208

    
209
            if (model.getAssociatedTable() != null) {
210
                this.fmap = ((FLayer) model.getAssociatedTable()).getMapContext();
211
            } else {
212
                this.fmap = null;
213
            }
214

    
215
            SelectableDataSource dataSource = (SelectableDataSource) model.getModelo()
216
                                                                          .getRecordset();
217
            dataSource.mapExternalFields();
218
            logger.debug("dataSource.start()");
219
            dataSource.start();
220

    
221
//            model.createAlias();
222

    
223
            ColumnModel cm=new ColumnModel(model);
224
//            cm.setDataModel(model);
225
            getTable().setColumnModel(cm);
226

    
227
            AbstractTableModel dataModel = new DataSourceDataModel(model);
228

    
229
            getModel().createAlias();
230

    
231
            getTable().setModel(dataModel);
232
//            TableColumn column = null;
233
//                        for (int i = 0; i < model.getColumnCount(); i++) {
234
//                            column = table.getColumnModel().getColumn(i);
235
//                            int w=model.getColumn(i).getWidth();
236
//                            column.setPreferredWidth(w); //sport column is bigger
237
//                            System.err.println("Table.Dentro de refreshControls. column=" + column.toString());
238
//
239
//                        }
240
//
241
//                TableColumn tc=getTable().getColumnModel().getColumn(0);
242
//                tc.setCellRenderer(new MyTableCellRenderer());
243

    
244

    
245
                        headerSelectionSupport.setTableHeader(getTable().getTableHeader());
246
            headerSelectionSupport.addFieldSelectionListener(new FieldSelectionListener() {
247
                    public void fieldSelected(FieldSelectionEvent e) {
248
                        if (PluginServices.getMainFrame() != null) {
249
                            PluginServices.getMainFrame().enableControls();
250
                        }
251
                    }
252
                });
253

    
254
            model.getModelo().getRecordset().addSelectionListener(selectionListener);
255

    
256
            updateSelection();
257
        } catch (DriverException e) {
258
            NotificationManager.addError("No se pudo leer la informaci?n", e);
259
        } catch (DriverLoadException e) {
260
            NotificationManager.addError("No se pudo leer la informaci?n", e);
261
        }
262
    }
263

    
264
    /**
265
     * DOCUMENT ME!
266
     *
267
     * @param table DOCUMENT ME!
268
     */
269
    public void setModel(ProjectTable table) {
270
        model = table;
271

    
272
        //Gesti?n del nombre de la ventana
273
        model.addPropertyChangeListener(new PropertyChangeListener() {
274
                public void propertyChange(PropertyChangeEvent evt) {
275
                    if (evt.getPropertyName().equals("name")) {
276
                        PluginServices.getMDIManager().getWindowInfo(Table.this)
277
                                      .setTitle(PluginServices.getText(this,
278
                                "Tabla") + ": " + (String) evt.getNewValue());
279
                    } else if (evt.getPropertyName().equals("model")) {
280
                        refreshControls();
281
                    }
282
                }
283
            });
284

    
285
        IEditableSource ies=getModel().getModelo();
286
        ies.addEditionListener(this);
287

    
288
        refreshControls();
289

    
290

    
291
    }
292
    class MyTableCellRenderer extends JLabel implements TableCellRenderer {
293
        final Border grayBorder = BorderFactory.createLineBorder(Color.darkGray);
294

    
295
        MyTableCellRenderer() {
296

    
297
        }
298

    
299
        public Component getTableCellRendererComponent(javax.swing.JTable table,
300
            Object value, boolean isSelected, boolean hasFocus, int row,
301
            int col) {
302
                JLabel label=new JLabel(String.valueOf(row+1));
303
                 label.setForeground(Color.blue);
304
                label.setOpaque(true);
305
                 if (isSelected)
306
                        label.setBackground(Color.yellow);
307
                else
308
                        label.setBackground(UIManager.getColor("Button.background"));
309

    
310
                label.setBorder(grayBorder);
311
                return label;
312
        }
313
      }
314

    
315
    /**
316
     *
317
     */
318
    public void updateSelection() {
319
        updating = true;
320
        try {
321
            DefaultListSelectionModel sm = (DefaultListSelectionModel) getTable()
322
                                                                           .getSelectionModel();
323
            sm.clearSelection();
324

    
325
            BitSet bs = (model.getModelo().getRecordset()).getSelection();
326
            sm.setValueIsAdjusting(true);
327

    
328
            if (orderIndexes != null) {
329
                for (int i = 0; i < orderIndexes.length; i++) {
330
                    if (bs.get(i)) {
331
                        sm.addSelectionInterval((int) orderIndexesInverted[i],
332
                            (int) orderIndexesInverted[i]);
333
                        if (isEditing())
334
                                table.setEditingRow((int)orderIndexesInverted[i]);
335
                    }
336
                }
337
            } else {
338
                for (int i = bs.nextSetBit(0); i >= 0;
339
                        i = bs.nextSetBit(i + 1)) {
340
                    sm.addSelectionInterval(i, i);
341
                    if (isEditing())
342
                            table.setEditingRow(i);
343
                }
344
            }
345

    
346
            sm.setValueIsAdjusting(false);
347
            try {
348
                numReg = model.getModelo().getRowCount();
349
            } catch (IOException e) {
350
                e.printStackTrace();
351
            }
352

    
353
            jLabelStatus.setText(" " +
354
                ((SelectableDataSource) model.getModelo().getRecordset()).getSelection()
355
                 .cardinality() + " / " + numReg + " " +
356
                PluginServices.getText(this, "registros_seleccionados_total") +
357
                ".");
358
            PluginServices.getMainFrame().enableControls();
359
        } catch (DriverIOException e) {
360
            e.printStackTrace();
361
        } catch (DriverLoadException e) {
362
            e.printStackTrace();
363
        }
364
        updating = false;
365
    }
366

    
367
    /**
368
     * This method initializes this
369
     */
370
    protected void initialize() {
371
        jLabelStatus = new JLabel();
372
        setLayout(new java.awt.BorderLayout());
373
        jLabelStatus.setText("");
374
        jLabelStatus.setName("");
375
        jLabelStatus.setPreferredSize(new java.awt.Dimension(100, 18));
376
        add(getJScrollPane(), java.awt.BorderLayout.CENTER);
377
        this.add(jLabelStatus, java.awt.BorderLayout.SOUTH);
378
        this.setPreferredSize(new Dimension(300,200));
379
    }
380

    
381
    /**
382
     * This method initializes table
383
     *
384
     * @return javax.swing.JTable
385
     */
386
    public javax.swing.JTable getTable() {
387
        if (table == null) {
388
            table = new javax.swing.JTable();
389
            table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
390
            table.setSelectionModel(new DefaultListSelectionModel());
391
            table.getTableHeader().addMouseListener(new MouseHandler());
392
            table.addKeyListener(new TableKeyListener());
393
            table.addMouseListener(new MouseRow());
394
            table.setSelectionForeground(Color.blue);
395
                    table.setSelectionBackground(Color.yellow);
396
                    table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
397
                    public void valueChanged(ListSelectionEvent e) {
398

    
399
                            if (updating) {
400
                                 return;
401
                        }
402

    
403
                        SelectableDataSource dataModel = null;
404

    
405
                        try {
406
                            dataModel = Table.this.model.getModelo()
407
                                                        .getRecordset();
408
                        } catch (DriverLoadException e1) {
409
                            e1.printStackTrace();
410
                        }
411

    
412
                        DefaultListSelectionModel model = (DefaultListSelectionModel) table.getSelectionModel();
413
                        BitSet selection = dataModel.getSelection();
414

    
415
                        for (int i = e.getFirstIndex(); i <= e.getLastIndex();
416
                                i++) {
417
                            if (orderIndexes != null) {
418
                                selection.set((int) orderIndexes[i],
419
                                    model.isSelectedIndex(i));
420
                            } else {
421
                                selection.set(i, model.isSelectedIndex(i));
422
                            }
423
                        }
424

    
425
                        if (e.getValueIsAdjusting() == false) {
426
                            if (fmap != null) {
427
                                fmap.endAtomicEvent();
428
                            }
429

    
430
                            dataModel.fireSelectionEvents();
431
                        } else {
432
                            if (fmap != null) {
433
                                fmap.beginAtomicEvent();
434
                            }
435
                        }
436

    
437
                        jLabelStatus.setText(" " + selection.cardinality() +
438
                            " / " + numReg + " " +
439
                            PluginServices.getText(this,
440
                                "registros_seleccionados_total") + ".");
441

    
442
                    }
443
                });
444

    
445
                       JTextField tf=new JTextField();
446
                    table.setDefaultEditor(Number.class, new DefaultEditor(tf));
447
            table.setDefaultEditor(String.class, new DefaultEditor(tf));
448
            table.setDefaultEditor(Object.class, new DefaultEditor(tf));
449
        }
450

    
451
        return table;
452
    }
453
    protected class DefaultEditor extends DefaultCellEditor {
454
                public Component getTableCellEditorComponent(javax.swing.JTable table,
455
                                Object value, boolean isSelected, int row, int column) {
456
                        JTextField tf=(JTextField)super.getTableCellEditorComponent(table, value, isSelected,
457
                                        row, column);
458
                        if (isSelected){
459
                                tf.setBackground(new Color(230,220,220));
460
                                tf.selectAll();
461

    
462
                        }
463
                        return tf;
464
                }
465

    
466
                public DefaultEditor(JTextField tf) {
467
                        super(tf);
468
                         //MouseListener[] mouseListeners=getTable().getEditorComponent().getMouseListeners();
469
                   // if (!mouseListeners[mouseListeners.length-1].getClass().equals(MouseRow.class)){
470
                   tf.addMouseListener(new MouseRow());
471
                   // }
472
                        getComponent().addKeyListener(new KeyAdapter() {
473
                                int keyPressed = 0;
474

    
475
                                public void keyPressed(KeyEvent ke) {
476
                                        if (ke.getKeyCode() != KeyEvent.VK_TAB)
477
                                                keyPressed++;
478
                                        JTextField tf = (JTextField) getComponent();
479

    
480

    
481
                                        if (ke.getKeyCode() == KeyEvent.VK_RIGHT
482
                                                        || ke.getKeyCode() == KeyEvent.VK_ENTER) {
483
                                                int caretPosition = tf.getCaretPosition();
484
                                                if (caretPosition >= tf.getText().length()) {
485
                                                        int x = table.getSelectedColumn();
486
                                                        int y = table.getSelectedRow();
487
                                                        if (x + 1 >= table.getColumnCount()) {
488
                                                                x = 0;
489
                                                                y++;
490
                                                        } else {
491
                                                                x++;
492
                                                        }
493
                                                        //table.changeSelection(y, x, false, true);
494
                                                        //initEditField(x,y);
495
                                                        getComponent().setEnabled(false);
496
                                                }
497

    
498
                                        } else if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
499
                                                int caretPosition = tf.getCaretPosition();
500
                                                if (caretPosition <= 0) {
501
                                                        int x = table.getSelectedColumn();
502
                                                        int y = table.getSelectedRow();
503
                                                        if (x == 0) {
504
                                                                x = table.getColumnCount() - 1;
505
                                                                if (y - 1 < 0)
506
                                                                        y = table.getRowCount() - 1;
507
                                                                else
508
                                                                        y--;
509
                                                        } else {
510
                                                                x--;
511
                                                        }
512
                                                        //table.changeSelection(y, x, false, true);
513
                                                        //initEditField(x,y);
514
                                                        getComponent().setEnabled(false);
515
                                                }
516

    
517
                                        }
518
                                }
519

    
520
                                public void keyReleased(KeyEvent ke) {
521
                                        JComponent editor = (JComponent) DefaultEditor.this
522
                                                        .getComponent();
523
                                        if (editor instanceof JTextField) {
524
                                                if (keyPressed == 0) {
525
                                                        ((JTextField) editor).setText(String.valueOf(ke
526
                                                                        .getKeyChar()));
527
                                                }
528
                                        }
529
                                        keyPressed--;
530
                                        if (keyPressed < 0)
531
                                                keyPressed = 0;
532
                                        getComponent().setEnabled(true);
533
                                }
534
                        });
535

    
536
                }
537

    
538
                public Object getCellEditorValue() {
539
                        String s = ((JTextField) (DefaultEditor.this.getComponent()))
540
                                        .getText();
541
                        getComponent().setEnabled(true);
542
                        return s;
543
                }
544

    
545
                public boolean isCellEditable(EventObject event) {
546
                        // IF NUMBER OF CLICKS IS LESS THAN THE CLICKCOUNTTOSTART RETURN
547
                        // FALSE
548
                        // FOR CELL EDITING.
549
                        if (event instanceof MouseEvent) {
550
                                return ((MouseEvent) event).getClickCount() >= getClickCountToStart();
551
                        }
552

    
553
                        return true;
554
                }
555
        }
556

    
557

    
558

    
559

    
560
    /**
561
         * This method initializes jScrollPane
562
         *
563
         * @return javax.swing.JScrollPane
564
         */
565
    protected javax.swing.JScrollPane getJScrollPane() {
566
        if (jScrollPane == null) {
567
            jScrollPane = new javax.swing.JScrollPane();
568
            jScrollPane.setViewportView(getTable());
569
        }
570

    
571
        return jScrollPane;
572
    }
573

    
574
    /**
575
         * @see com.iver.mdiApp.ui.MDIManager.SingletonWindow#getWindowModel()
576
         */
577
    public Object getWindowModel() {
578
        return model;
579
    }
580

    
581
    /**
582
         * This method is used to get <strong>an initial</strong> ViewInfo object
583
         * for this Table. It is not intended to retrieve the ViewInfo object in a
584
         * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
585
         * to retrieve the ViewInfo object at any time after the creation of the
586
         * object.
587
         *
588
         * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
589
         */
590
    public WindowInfo getWindowInfo() {
591
            if (m_viewInfo==null) {
592
                    m_viewInfo = new WindowInfo(WindowInfo.ICONIFIABLE |
593
                        WindowInfo.MAXIMIZABLE | WindowInfo.RESIZABLE);
594
                    m_viewInfo.setTitle(PluginServices.getText(this, "Tabla")+ " : " +model.getName());
595
                    m_viewInfo.setWidth(300);
596
                    m_viewInfo.setHeight(200);
597
            }
598
        return m_viewInfo;
599
    }
600

    
601
    /**
602
     * DOCUMENT ME!
603
     *
604
     * @param indexes
605
     *
606
     * @throws IOException
607
     */
608
    public void setOrder(long[] indexes) throws IOException {
609
        orderIndexes = indexes;
610
        orderIndexesInverted = new long[orderIndexes.length];
611

    
612
        for (int i = 0; i < indexes.length; i++) {
613
            orderIndexesInverted[(int) indexes[i]] = i;
614
        }
615

    
616
        updating = true;
617
        ((DataSourceDataModel) getTable().getModel()).fireTableDataChanged();
618
        updating = false;
619

    
620
        updateSelection();
621
    }
622

    
623
    /*  public void startEditing() throws DriverException{
624
       // dw = model.getModelo().getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER);
625
       // dw.beginTrans();
626
       }
627
     */
628

    
629
    /**
630
     * DOCUMENT ME!
631
     */
632

    
633
    /*  public void cancelEditing() throws DriverException {
634
       dw.rollBackTrans();
635
       dw = null;
636
       }
637
     */
638

    
639
    /**
640
     * DOCUMENT ME!
641
     */
642

    
643
    /*  public void stopEditing() throws DriverException {
644
       // dw.commitTrans();
645
       // dw = null;
646
       // model.getModelo().stop();
647
       // model.getModelo().start();
648
       }
649
     */
650

    
651
    /**
652
     * DOCUMENT ME!
653
     */
654

    
655
    /*  public boolean isEditing() {
656
       return dw != null;
657
       }
658
     */
659

    
660
    /**
661
     * Quita los campos seleccionados
662
     */
663
    public void clearSelectedFields() {
664
        headerSelectionSupport.clearSelectedColumns();
665
    }
666

    
667
    /**
668
     * DOCUMENT ME!
669
     *
670
     * @param index DOCUMENT ME!
671
     *
672
     * @return DOCUMENT ME!
673
     */
674
    public Value[] getValueRow(int index) {
675
        DataSourceDataModel dsdm = (DataSourceDataModel) getTable().getModel();
676
        Value[] values = new Value[dsdm.getColumnCount()];
677

    
678
        for (int i = 0; i < dsdm.getColumnCount(); i++) {
679
            values[i] = (Value) dsdm.getValueAt(index, i);
680
        }
681

    
682
        return values;
683
    }
684

    
685
    /* private void refresh() throws DriverException{
686
       //dw.commitTrans();
687
       //model.getModelo().stop();
688
       //dw.beginTrans();
689
           //DataSourceDataModel dsdm=(DataSourceDataModel)getTable().getModel();
690
           //dsdm.fireTableDataChanged();
691
       }*/
692
    /*  public void addEmptyRow() throws DriverException{
693
       ValueCollection valuePK=new ValueCollection();
694
           valuePK.setValues(new Value[]{ValueFactory.createValue(dw.getRowCount())});
695
           dw.insertEmptyRow(valuePK);
696
           refresh();
697
       }
698
     */
699
    /*        public void copySelectedRows() throws DriverException{
700
       int[] sel=getSelectedRowIndices();
701
       for(int i=0;i<sel.length;i++){
702
               rowsCopied.add(getValueRow(sel[i]));
703
       }
704
       }
705
     */
706
    /*        public void addSelectionToEnd() throws DriverException {
707
       for (int i=0;i<rowsCopied.size();i++){
708
               dw.insertFilledRow((Value[])rowsCopied.get(i));
709
       }
710
       refresh();
711
       }
712
     */
713
    /*public void delSelectionRow() throws DriverException{
714
       int[] sel=getSelectedRowIndices();
715
       for(int i=sel.length-1;i>=0;i--){
716
               dw.deleteRow(sel[i]);
717
       }
718
       refresh();
719
       }
720
     */
721
    /*public boolean isCopy(){
722
       return !rowsCopied.isEmpty();
723
       }
724
     */
725
    /*
726
       public void addSelectionToRow() throws DriverException {
727
                   int[] sel=getSelectedRowIndices();
728
                           dw.insertFilledRow((Value[])rowsCopied.get(i),sel[0]);
729
                   refresh();
730
           }
731
     */
732
    public void startEditing() throws EditionException {
733
       getModel().getModelo().startEdition(EditionEvent.ALPHANUMERIC);
734
       // Guardamos el estado actual para recuperarlo si cancelamos la edici?n.
735
       antAliases = getModel().getAliases();
736
       antMapping = getModel().getMapping();
737
       //int row=table.getSelectedRow();
738
       //int column=table.getSelectedColumn();
739
       //if (row!=-1 && column!=-1)
740
       //   initEditField(column,row);
741

    
742
//       View[] views = (View[]) PluginServices.getMDIManager().getAllViews();
743
//
744
//                for (int i=0 ; i<views.length ; i++){
745
//                        if (views[i] instanceof com.iver.cit.gvsig.gui.View){
746
//                                com.iver.cit.gvsig.gui.View view=(com.iver.cit.gvsig.gui.View)views[i];
747
//                                FLayers layers =view.getMapControl().getMapContext().getLayers();
748
//                                for (int j=0;j<layers.getLayersCount();j++){
749
//                                        if (layers.getLayer(j).equals(this.getModel().getAssociatedTable())){
750
//                                                view.showConsole();
751
//                                                ((FLyrVect)layers.getLayer(j)).setActive(true);
752
//                                                ((FLyrVect)layers.getLayer(j)).setEditing(true);
753
//                                        }
754
//                                }
755
//                        }
756
//                }
757
    }
758
    private void initEditField(int[] x,int[] y){
759
            if (getTable().getRowCount()>0){
760
            if (isEditing()) {
761

    
762
                        if (x.length == 1 && y.length == 1)
763
                                table.editCellAt(y[0], x[0]);
764
                        JTextField tf = (JTextField) table.getEditorComponent();
765
                        if (tf != null) {
766
                                tf.selectAll();
767
                                tf.requestFocus();
768
                        }
769
                        if (y.length > 0 && x.length > 0) {
770
                                getTable().setRowSelectionInterval(y[0], y[y.length - 1]);
771
                                getTable().setColumnSelectionInterval(x[0], x[x.length - 1]);
772
                        }
773

    
774
                }
775
            }
776
    }
777
    /**
778
         * DOCUMENT ME!
779
         */
780
    public void stopEditing() {
781
        try {
782
                this.stopEditingCell();
783

    
784
            FLyrVect lyr = (FLyrVect) getModel().getAssociatedTable();
785

    
786
            if ((lyr != null) &&
787
                    lyr.getSource() instanceof VectorialEditableAdapter) {
788
                    VectorialEditableAdapter vea = (VectorialEditableAdapter) lyr.getSource();
789
                    ISpatialWriter spatialWriter = (ISpatialWriter) vea.getOriginalDriver();
790
                    vea.stopEdition(spatialWriter,EditionEvent.ALPHANUMERIC);
791
                    lyr.setSource(vea.getOriginalAdapter());
792
                    lyr.setEditing(false);
793
                    refreshControls();
794

    
795
            } else {
796

    
797
                 IEditableSource ies=getModel().getModelo();
798
                 if (ies instanceof IWriteable)
799
                 {
800
                         IWriteable w = (IWriteable) ies;
801
                         IWriter writer = w.getWriter();
802
                         if (writer == null)
803
                         {
804
                                 NotificationManager.addError("No existe driver de escritura para la tabla"
805
                                                 + getModel().getName(), new EditionException());
806
                         }
807
                         else
808
                         {
809
                                             ITableDefinition tableDef = ies.getTableDefinition();
810
                                            writer.initialize(tableDef);
811

    
812
                                 ies.stopEdition(writer,EditionEvent.ALPHANUMERIC);
813

    
814
                                 // TODO: RELOAD
815
//                                 EditableAdapter edAdapter = (EditableAdapter) ies;
816
//                                 // Restaura el datasource a su estado original
817
//                                 edAdapter.setOriginalDataSource(edAdapter.getRecordset());
818
                                 ies.getSelection().clear();
819
                                 refreshControls();
820
                         }
821
                 }
822

    
823
                 /*
824
                GdbmsWriter gdbmswriter = new GdbmsWriter();
825
                gdbmswriter.setDataWare(getModel().getModelo()
826
                                                              .getRecordset()
827
                                                              .getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER));
828
                gdbmswriter.preProcess();
829

830
                for (int i = 0; i < getModel().getModelo().getRowCount();
831
                        i++) {
832
                    gdbmswriter.process(getModel().getModelo().getRow(i));
833
                }
834

835
                gdbmswriter.postProcess();
836
                */
837

    
838
            }
839

    
840
        } catch (Exception e) {
841
            NotificationManager.addError("No se pudo guardar la edici?n", e);
842
        }
843
    }
844

    
845
    /**
846
     * DOCUMENT ME!
847
     *
848
     * @param index DOCUMENT ME!
849
     */
850
    public void hideColumns(int[] index) {
851
        // TODO Auto-generated method stub
852
    }
853

    
854
    /**
855
     * DOCUMENT ME!
856
     *
857
     * @param index DOCUMENT ME!
858
     */
859
    public void setUneditableColumns(int[] index) {
860
        // TODO Auto-generated method stub
861
    }
862

    
863
    /**
864
     * DOCUMENT ME!
865
     *
866
     * @param numColumns DOCUMENT ME!
867
     * @param values DOCUMENT ME!
868
     */
869
    public void setDefaultValues(int[] numColumns, Value[] values) {
870
        // TODO Auto-generated method stub
871
    }
872

    
873
    /**
874
     * DOCUMENT ME!
875
     *
876
     * @return DOCUMENT ME!
877
     */
878
    public Value getDefaultValue() {
879
        // TODO Auto-generated method stub
880
        return null;
881
    }
882

    
883
    /**
884
     * DOCUMENT ME!
885
     *
886
     * @throws IOException DOCUMENT ME!
887
     */
888
    public void cancelEditing() throws IOException {
889
            this.cancelEditingCell();
890
        getModel().getModelo().cancelEdition(EditionEvent.ALPHANUMERIC);
891
        if (antAliases != null)
892
        {
893
                getModel().setAliases(antAliases);
894
                getModel().setMapping(antMapping);
895
                getModel().recalculateColumnsFromAliases();
896
        }
897
        else
898
        {
899
                System.err.println("Algo ha ido mal con antAliases");
900
        }
901
        //refresh();
902
        refreshControls();
903
    }
904

    
905
    /**
906
     * DOCUMENT ME!
907
     *
908
     * @return DOCUMENT ME!
909
     */
910
    public boolean isEditing() {
911
        return getModel().getModelo().isEditing();
912
    }
913

    
914
    /**
915
     * DOCUMENT ME!
916
     */
917
    public void refresh() {
918
            int row=table.getSelectedRow();
919
            int column=table.getSelectedColumn();
920
            if (row!=-1 && column!=-1 && table.getEditorComponent()!=null){
921
                    Value[] values=getValueRow(row);
922
                    JTextField jtf=(JTextField)table.getEditorComponent();
923
                    jtf.setText(values[column].toString());
924
            }
925
        updating = true;
926
        ((DataSourceDataModel) getTable().getModel()).fireTableDataChanged();
927

    
928
        updating = false;
929
        updateSelection();
930
        PluginServices.getMainFrame().enableControls();
931

    
932
    }
933

    
934
    /**
935
     * Add the rows that are passed like parameter and if parameter is null a
936
     * row is added empties.
937
     *
938
     * @param rows Rows to add or null.
939
     *
940
     * @throws DriverIOException
941
     * @throws IOException
942
     */
943
    public void addRow(IRow[] rows) throws DriverIOException, IOException {
944
            IEditableSource ies=getModel().getModelo();
945
            try {
946
            if (rows == null) {
947

    
948
                    if (getModel().getAssociatedTable()!=null){
949
                            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa.");
950
                            return;
951
                    }
952
                IRow row;
953
                int numAttr=getModel().getModelo()
954
                        .getRecordset()
955
                        .getFieldCount();
956
                    Value[] values=new Value[numAttr];
957
                    for (int i=0;i<numAttr;i++){
958
                            values[i]=ValueFactory.createNullValue();
959
                    }
960
                row = new DefaultRow(values);
961
                ies.addRow(row,"Fila en blanco",EditionEvent.ALPHANUMERIC);
962
            } else {
963
                    ies.startComplexRow();
964
                for (int i = 0; i < rows.length; i++) {
965
                   ies.addRow(((IRowEdited) rows[i]).getLinkedRow(),"Pegar filas",EditionEvent.ALPHANUMERIC);
966
                }
967
                String description=PluginServices.getText(this,"add_rows");
968
                ies.endComplexRow(description);
969
            }
970
        } catch (DriverException e) {
971
            // TODO Auto-generated catch block
972
            e.printStackTrace();
973
        } catch (DriverLoadException e) {
974
            // TODO Auto-generated catch block
975
            e.printStackTrace();
976
        }
977

    
978
        refresh();
979

    
980
    }
981

    
982
    /**
983
     * Copy in the arraylist the rows that be selected just then.
984
     *
985
     * @throws DriverIOException
986
     * @throws IOException
987
     */
988
    public void copyRow() throws DriverIOException, IOException {
989
        int[] index = getSelectedRowIndices();
990
        rowsCopied = new IRow[index.length];
991

    
992
        for (int i = 0; i < index.length; i++) {
993
            rowsCopied[i] = getModel().getModelo().getRow(index[i]);
994
        }
995
    }
996

    
997
    /**
998
     * Cut the rows that be selected just then.
999
     *
1000
     * @throws DriverIOException
1001
     * @throws IOException
1002
     */
1003
    public void cutRow() throws DriverIOException, IOException {
1004
        int[] index = getSelectedRowIndices();
1005
        rowsCopied = new IRow[index.length];
1006

    
1007
        for (int i = 0; i < index.length; i++) {
1008
            rowsCopied[i] = getModel().getModelo().getRow(index[i]);
1009
        }
1010

    
1011
        removeRow();
1012
    }
1013

    
1014
    /**
1015
     * Remove in the rows that be selected just then.
1016
     *
1017
     * @throws DriverIOException
1018
     * @throws IOException
1019
     */
1020
    public void removeRow() throws DriverIOException, IOException {
1021
            int[] index = getSelectedRowIndices();
1022
        getModel().getModelo().startComplexRow();
1023
        for (int i = index.length-1; i >=0; i--) {
1024
            getModel().getModelo().removeRow(index[i],"Eliminar fila", EditionEvent.ALPHANUMERIC);
1025
        }
1026
//        int[] sel={0};
1027
//        getTable().setEditingRow(-1);
1028
//        getTable().getCellEditor().
1029
//        initEditField(sel,sel);
1030
        if (getTable().getCellEditor() != null)
1031
                getTable().getCellEditor().cancelCellEditing();
1032
        String description=PluginServices.getText(this,"remove_rows");
1033
        getModel().getModelo().endComplexRow(description);
1034
        getTable().clearSelection();
1035

    
1036
        refresh();
1037
        //repaintAssociatedView();
1038
    }
1039

    
1040
//    private void repaintAssociatedView(){
1041
//             View[] views = (View[]) PluginServices.getMDIManager().getAllViews();
1042
//
1043
//                 for (int i=0 ; i<views.length ; i++){
1044
//                         if (views[i] instanceof com.iver.cit.gvsig.gui.View){
1045
//                                 com.iver.cit.gvsig.gui.View view=(com.iver.cit.gvsig.gui.View)views[i];
1046
//                                 FLayers layers =view.getMapControl().getMapContext().getLayers();
1047
//                                 for (int j=0;j<layers.getLayersCount();j++){
1048
//                                         if (layers.getLayer(j).equals(this.getModel().getAssociatedTable())){
1049
//                                                 view.getMapControl().getMapContext().invalidate();
1050
//                                         }
1051
//                                 }
1052
//                         }
1053
//                 }
1054
//    }
1055
    /**
1056
     * DOCUMENT ME!
1057
     */
1058
    public void addColumn(FieldDescription newField) {
1059
            EditableAdapter edAdapter = (EditableAdapter) getModel().getModelo();
1060
            try {
1061
                        edAdapter.addField(newField);
1062
                        if (getTable().getCellEditor() != null)
1063
                                getTable().getCellEditor().cancelCellEditing();
1064
                getModel().setModel(edAdapter); // Para que se recalculen los campos. TODO: Limpiear todo esto
1065
                // refresh();
1066
                refreshControls();
1067
                } catch (EditionException e) {
1068
                        e.printStackTrace();
1069
                        NotificationManager.addError(e);
1070
                }
1071

    
1072
    }
1073

    
1074
    /**
1075
     * DOCUMENT ME!
1076
     */
1077
    public void removeColumn() {
1078
            EditableAdapter edAdapter = (EditableAdapter) getModel().getModelo();
1079
            try {
1080
                    BitSet selectedFields = getSelectedFieldIndices();
1081
                    SelectableDataSource sds = edAdapter.getRecordset();
1082
                    edAdapter.startComplexRow();
1083
                    FieldDescription[] auxFlds = sds.getFieldsDescription();
1084
                    for(int i=selectedFields.nextSetBit(0); i>=0; i=selectedFields.nextSetBit(i+1)) {
1085
                            FieldDescription fld = auxFlds[i];
1086
                            edAdapter.removeField(fld.getFieldAlias());
1087
                    }
1088
                    if (getTable().getCellEditor() != null)
1089
                                getTable().getCellEditor().cancelCellEditing();
1090

    
1091
                edAdapter.endComplexRow(PluginServices.getText(this, "remove_fields"));
1092
                clearSelectedFields();
1093
                getModel().setModel(edAdapter); // Para que se recalculen los campos. TODO: Limpiear todo esto
1094
                // refresh();
1095
                refreshControls();
1096
                } catch (EditionException e) {
1097
                        e.printStackTrace();
1098
                        NotificationManager.addError(e);
1099
                } catch (DriverException e) {
1100
                        e.printStackTrace();
1101
                        NotificationManager.addError(e);
1102
                } catch (IOException e) {
1103
                        e.printStackTrace();
1104
                        NotificationManager.addError(e);
1105
                } catch (DriverIOException e) {
1106
                        e.printStackTrace();
1107
                        NotificationManager.addError(e);
1108
                }
1109

    
1110
    }
1111

    
1112
    /**
1113
     * Return if we have rows copied or not.
1114
     *
1115
     * @return True if we have rows copied.
1116
     */
1117
    public boolean isCopied() {
1118
        return (rowsCopied != null);
1119
    }
1120

    
1121
    /**
1122
     * Paste the arraylist rows.
1123
     *
1124
     * @throws DriverIOException
1125
     * @throws IOException
1126
     */
1127
    public void pasteRow() throws DriverIOException, IOException {
1128
        addRow(rowsCopied);
1129
        //repaintAssociatedView();
1130
    }
1131

    
1132
    /**
1133
     * DOCUMENT ME!
1134
     */
1135
    public void windowActivated() {
1136
        //if (isEditing() && getModel().getModelo() instanceof VectorialEditableAdapter)
1137
        updateSelection();
1138
    }
1139

    
1140
    /**
1141
     * DOCUMENT ME!
1142
     */
1143
    public void windowClosed() {
1144
        // TODO Auto-generated method stub
1145
    }
1146

    
1147
    /**
1148
     * DOCUMENT ME!
1149
     *
1150
     * @author Fernando Gonz?lez Cort?s
1151
     */
1152
    public class DataSourceDataModel extends AbstractTableModel {
1153
        //private SelectableDataSource dataSource;
1154
        ProjectTable pt;
1155

    
1156
        /**
1157
         * Crea un nuevo DataSourceDataModel.
1158
         *
1159
         * @param pt DOCUMENT ME!
1160
         */
1161
        public DataSourceDataModel(ProjectTable pt) {
1162
            this.pt = pt;
1163

    
1164
            //try {
1165
            //dataSource = pt.getModelo().getRecordset();
1166
            //} catch (DriverLoadException e) {
1167
            // TODO Auto-generated catch block
1168
            //        e.printStackTrace();
1169
            //}
1170
        }
1171

    
1172
        /**
1173
         * Returns the name of the field.
1174
         *
1175
         * @param col index of field
1176
         *
1177
         * @return Name of field
1178
         */
1179
        public String getColumnName(int col) {
1180
//                if (col==0)
1181
//                        return " ";
1182
//                col--;
1183
                int i=pt.getMapping()[col];
1184
                return pt.getAliases()[i];
1185
        }
1186

    
1187
        /**
1188
         * Returns the number of fields.
1189
         *
1190
         * @return number of fields
1191
         */
1192
        public int getColumnCount() {
1193
            return pt.getColumnCount();
1194
        }
1195

    
1196
        /**
1197
         * Returns number of rows.
1198
         *
1199
         * @return number of rows.
1200
         */
1201
        public int getRowCount() {
1202
            try {
1203
                    IEditableSource des = pt.getModelo();
1204
                return (int) des.getRowCount();
1205
            } catch (DriverIOException e) {
1206
                return 0;
1207
            } catch (IOException e) {
1208
                return 0;
1209
            }
1210
        }
1211

    
1212
        /**
1213
         * DOCUMENT ME!
1214
         *
1215
         * @param row DOCUMENT ME!
1216
         * @param col DOCUMENT ME!
1217
         *
1218
         * @return DOCUMENT ME!
1219
         */
1220
        public Object getValueAt(int row, int col) {
1221

    
1222
//           if (col==0){
1223
//                   return new Integer(row);
1224
//           }
1225
//           col--;
1226
           int numRow;
1227
           if (orderIndexes != null && row < orderIndexes.length) {
1228
                   numRow=(int)orderIndexes[row];
1229
           }else {
1230
                   numRow=row;
1231
           }
1232
                try {
1233
                        IEditableSource des = pt.getModelo();
1234
                        Object obj =null;
1235
                                IRowEdited rowEdited=des.getRow(numRow);
1236
                                obj= rowEdited.getAttribute(pt.getMapping()[col]);
1237
                                        return obj;
1238
                        } catch (DriverIOException e1) {
1239
                                e1.printStackTrace();
1240
                                return ValueFactory.createValue("").toString();
1241
                        } catch (IOException e1) {
1242
                                e1.printStackTrace();
1243
                                return ValueFactory.createValue("").toString();
1244
                        }
1245
            }
1246

    
1247
        /**
1248
         * DOCUMENT ME!
1249
         *
1250
         * @param rowIndex DOCUMENT ME!
1251
         * @param columnIndex DOCUMENT ME!
1252
         *
1253
         * @return DOCUMENT ME!
1254
         */
1255
        public boolean isCellEditable(int rowIndex, int columnIndex) {
1256
//                if (columnIndex==0)
1257
//                return false;
1258

    
1259
                return isEditing();
1260
        }
1261

    
1262
        /**
1263
         * DOCUMENT ME!
1264
         *
1265
         * @param aValue DOCUMENT ME!
1266
         * @param rowIndex DOCUMENT ME!
1267
         * @param columnIndex DOCUMENT ME!
1268
         *
1269
         * @throws RuntimeException DOCUMENT ME!
1270
         */
1271
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
1272
//                 if (columnIndex==0)
1273
//                 throw new UnsupportedOperationException("Row Number is a read-only column");
1274
//                 columnIndex--;
1275
                Value v;
1276
                 int numRow;
1277
             if (orderIndexes != null) {
1278
                     numRow=(int)orderIndexes[rowIndex];
1279
             }else {
1280
                     numRow=rowIndex;
1281
             }
1282
                if (getValueAt(rowIndex,columnIndex)==null || getValueAt(rowIndex,columnIndex).toString().equals(aValue))
1283
                    return;
1284
            try {
1285
                    IEditableSource des = pt.getModelo();
1286
                    v = ValueFactory.createValueByType(aValue.toString(),
1287
                        des.getRecordset().getFieldType(columnIndex));
1288
                IRowEdited row = des.getRow(numRow); //.getAttribute(columnIndex);
1289
                Value[] values = row.getAttributes();
1290
                values[columnIndex] = v;
1291

    
1292
                IRow newRow = null;
1293

    
1294
                if (row.getLinkedRow() instanceof IFeature) {
1295
                    IGeometry geometry = ((DefaultFeature) row.getLinkedRow()).getGeometry();
1296
                    newRow = new DefaultFeature(geometry, values,row.getID());
1297
                } else {
1298
                    newRow = new DefaultRow(values,row.getID());
1299
                }
1300

    
1301
                des.modifyRow(numRow, newRow,"Editar valor", EditionEvent.ALPHANUMERIC);
1302
            } catch (DriverException e1) {
1303
                throw new RuntimeException(e1);
1304
            } catch (ParseException e) {
1305
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_del_campo_incorrecto");
1306
                        return;
1307
            } catch (DriverLoadException e) {
1308
                throw new RuntimeException(e);
1309
            } catch (DriverIOException e) {
1310
                throw new RuntimeException(e);
1311
            } catch (IOException e) {
1312
                throw new RuntimeException(e);
1313
            } catch (NumberFormatException e) {
1314
                        /*        NotificationManager.addError(PluginServices.-getText(this,"numero_incorrecto")+
1315
                                                "\n"+PluginServices.-getText(this,"fila")+" = "+rowIndex+
1316
                                                "\n"+PluginServices.-getText(this,"columna")+ " = "+columnIndex,e);
1317
                        */
1318
                        }
1319
        }
1320
    }
1321
    public class ColumnModel extends DefaultTableColumnModel{
1322
            private ProjectTable pt;
1323
                public ColumnModel(ProjectTable pt) {
1324
                        this.pt = pt;
1325
                }
1326

    
1327

    
1328
                public int getTotalColumnWidth() {
1329
//                        int w=0;
1330
//                        for (int i=0;i<getColumnCount();i++){
1331
//                                w+=pt.getColumn(i).getWidth();
1332
//                        }
1333
                        return super.getTotalColumnWidth();
1334
                }
1335

    
1336
                public void moveColumn(int arg0, int arg1) {
1337
                        //super.moveColumn(arg0, arg1);
1338
                        if (arg0==arg1){
1339
                                super.moveColumn(arg0,arg1);
1340
                        }
1341
                }
1342

    
1343

    
1344
                public void propertyChange(PropertyChangeEvent arg0) {
1345
                        try{
1346
                        super.propertyChange(arg0);
1347
                        int w=((Integer)arg0.getNewValue()).intValue();
1348
                        if (arg0.getSource() instanceof TableColumn && (w!=75)){
1349
                                TableColumn tc=(TableColumn)arg0.getSource();
1350
                                Column column=pt.getColumn(tc.getModelIndex());
1351
                                column.setWidth(w);
1352
                                System.out.println("Index guardar = "+tc.getModelIndex()+" , "+"Anchura = "+w);
1353
                        }
1354
                        }catch (Exception e) {
1355
                                System.out.println("PETA");
1356
                        }
1357

    
1358
                }
1359
        }
1360
    /**
1361
     * DOCUMENT ME!
1362
     *
1363
     * @author Vicente Caballero Navarro
1364
     */
1365
    public class TableSelectionListener implements SelectionListener {
1366
        /**
1367
         * @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
1368
         */
1369
        public void selectionChanged(SelectionEvent e) {
1370
            updateSelection();
1371
            Table.this.repaint();
1372
        }
1373
    }
1374

    
1375
    /**
1376
     * DOCUMENT ME!
1377
     *
1378
     * @author Vicente Caballero Navarro
1379
     */
1380
    private class MouseHandler extends MouseAdapter {
1381
        /**
1382
         * DOCUMENT ME!
1383
         *
1384
         * @param e DOCUMENT ME!
1385
         */
1386
        public void mouseClicked(MouseEvent e) {
1387

    
1388
            /* JTableHeader h = (JTableHeader) e.getSource();
1389
               TableColumnModel columnModel = h.getColumnModel();
1390
               int viewColumn = columnModel.getColumnIndexAtX(e.getX());
1391
               int column = columnModel.getColumn(viewColumn).getModelIndex();
1392
               if (column != -1) {
1393
               }*/
1394
        }
1395
    }
1396
    /**
1397
     * DOCUMENT ME!
1398
     *
1399
     * @author Vicente Caballero Navarro
1400
     */
1401
    private class TableKeyListener implements KeyListener {
1402

    
1403
                public void keyPressed(KeyEvent arg0) {
1404
                        //JTextField tf=(JTextField)table.getEditorComponent();
1405
                        //table.setCellSelectionEnabled(true);
1406
                        //FocusManager fm = FocusManager.getCurrentManager();
1407
                        //fm.focusPreviousComponent(table);
1408
                }
1409

    
1410
                public void keyReleased(KeyEvent ke) {
1411
                        int[] row=table.getSelectedRows();
1412
                    int[] column=table.getSelectedColumns();
1413
                    initEditField(column,row);
1414
                }
1415

    
1416
                public void keyTyped(KeyEvent arg0) {
1417
                        // TODO Auto-generated method stub
1418

    
1419
                }
1420

    
1421
    }
1422

    
1423
    /**
1424
     * DOCUMENT ME!
1425
     *
1426
     * @author Vicente Caballero Navarro
1427
     */
1428
    private class MouseRow extends MouseAdapter {
1429
               public void mouseReleased(MouseEvent arg0) {
1430
                        super.mouseReleased(arg0);
1431
                        int[] row=table.getSelectedRows();
1432
                    int[] column=table.getSelectedColumns();
1433
                    initEditField(column,row);
1434
                }
1435

    
1436
                /**
1437
         * DOCUMENT ME!
1438
         *
1439
         * @param e DOCUMENT ME!
1440
         */
1441
    /*    public void mouseClicked(MouseEvent e) {
1442
            super.mouseClicked(e);
1443

1444

1445
                PluginServices.getMainFrame().enableControls();
1446

1447
            if (e.getButton() == MouseEvent.BUTTON3) {
1448
                new PopupMenu(e.getPoint());
1449
            }
1450
        }
1451
*/
1452
    }
1453

    
1454
        public void commandRepaint() {
1455
                refresh();
1456
        }
1457

    
1458
        public void commandRefresh() {
1459
                commandRepaint();
1460
        }
1461

    
1462
        public void toPalette() {
1463
                isPalette=true;
1464
                m_viewInfo.toPalette(true);
1465
                m_viewInfo.setClosed(false);
1466
                PluginServices.getMDIManager().changeWindowInfo(this,getWindowInfo());
1467
        }
1468

    
1469
        public void restore() {
1470
                isPalette=false;
1471
                m_viewInfo.toPalette(false);
1472
                m_viewInfo.setClosed(false);
1473
                PluginServices.getMDIManager().changeWindowInfo(this,getWindowInfo());
1474
        }
1475

    
1476
        public boolean isPalette() {
1477
                return isPalette;
1478
        }
1479

    
1480

    
1481
        private void addNumbers(){
1482
                TableColumn tc=new TableColumn();
1483
                tc.setWidth(40);
1484
                tc.setResizable(false);
1485
                getTable().addColumn(tc);
1486
        }
1487
        public static void main(String[] args) {
1488
                JTextField tf=new JTextField("hola");
1489
                tf.selectAll();
1490
                JFrame frame=new JFrame();
1491
                frame.getContentPane().add(tf);
1492
                frame.show(true);
1493
        }
1494

    
1495
        public void stopEditingCell() {
1496
            if (table.isEditing()) {
1497
                    // TODO: finalizar la edicion de la columna
1498
                    this.table.getCellEditor().stopCellEditing();
1499
                    this.refresh();
1500
            }
1501

    
1502
        }
1503

    
1504
        public void cancelEditingCell() {
1505
            if (table.isEditing()) {
1506
                    // TODO: finalizar la edicion de la columna
1507
                    this.table.getCellEditor().cancelCellEditing();
1508
                    this.refresh();
1509
            }
1510

    
1511
        }
1512

    
1513
        public void processEvent(EditionEvent e) {
1514
                if (e.getChangeType() == EditionEvent.STOP_EDITION)
1515
                {
1516
                        refreshControls();
1517
                }
1518

    
1519
        }
1520

    
1521
        public void beforeRowEditEvent(IRow feat, BeforeRowEditEvent e) {
1522
                // TODO Auto-generated method stub
1523

    
1524
        }
1525

    
1526
        public void afterRowEditEvent(IRow feat, AfterRowEditEvent e) {
1527
                // TODO Auto-generated method stub
1528

    
1529
        }
1530

    
1531
        public void beforeFieldEditEvent(BeforeFieldEditEvent e) {
1532
                // TODO Auto-generated method stub
1533

    
1534
        }
1535

    
1536
        public void afterFieldEditEvent(AfterFieldEditEvent e) {
1537
                try {
1538
//                        getModel().createAlias();
1539
                        clearSelectedFields();
1540
//                        refresh();
1541
                        refreshControls();
1542
                // ((DataSourceDataModel) getTable().getModel()).fireTableDataChanged();
1543
                } catch (DriverLoadException e1) {
1544
                        // TODO Auto-generated catch block
1545
                        e1.printStackTrace();
1546
                /* } catch (DriverException e1) {
1547
                        // TODO Auto-generated catch block
1548
                        e1.printStackTrace(); */
1549
                }
1550

    
1551
        }
1552
}