Statistics
| Revision:

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

History | View | Annotate | Download (47.6 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.EventObject;
58

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

    
77
import org.apache.log4j.Logger;
78

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

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

    
159
    /**
160
     * DOCUMENT ME!
161
     *
162
     * @return DOCUMENT ME!
163
     */
164
    public ProjectTable getModel() {
165
        return model;
166
    }
167

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

    
184
    /**
185
     * DOCUMENT ME!
186
     *
187
     * @return DOCUMENT ME!
188
     */
189
    public int[] getSelectedRowIndices() {
190
        return getTable().getSelectedRows();
191
    }
192

    
193
    /**
194
     * DOCUMENT ME!
195
     */
196
    protected void refreshControls() {
197
        try {
198
            MainFrame mF = PluginServices.getMainFrame();
199

    
200
            if (mF != null) {
201
                PluginServices.getMDIManager().getWindowInfo(Table.this).setTitle(PluginServices.getText(
202
                        this, "Tabla") + ": " + model.getName());
203
            }
204

    
205
            if (model.getAssociatedTable() != null) {
206
                this.fmap = ((FLayer) model.getAssociatedTable()).getMapContext();
207
            } else {
208
                this.fmap = null;
209
            }
210

    
211
            SelectableDataSource dataSource = model.getModelo()
212
                                                                          .getRecordset();
213
            dataSource.mapExternalFields();
214
            logger.debug("dataSource.start()");
215
            dataSource.start();
216

    
217
//            model.createAlias();
218

    
219
            ColumnModel cm=new ColumnModel(model);
220
//            cm.setDataModel(model);
221
            getTable().setColumnModel(cm);
222

    
223
            AbstractTableModel dataModel = new DataSourceDataModel(model);
224

    
225
            getModel().createAlias();
226

    
227
            getTable().setModel(dataModel);
228

    
229
//            TableColumn column = null;
230
//                        for (int i = 0; i < model.getColumnCount(); i++) {
231
//                            column = table.getColumnModel().getColumn(i);
232
//                            int w=model.getColumn(i).getWidth();
233
//                            column.setPreferredWidth(w); //sport column is bigger
234
//                            System.err.println("Table.Dentro de refreshControls. column=" + column.toString());
235
//
236
//                        }
237
//
238
            for (int i=0;i<dataModel.getColumnCount();i++) {
239
                    if (getModel().getModelo().getRecordset().getFieldType(i)==Types.STRUCT) {
240
                            TableColumn tc=getTable().getColumnModel().getColumn(i);
241
                        ValueComplexRenderer vcr=new ValueComplexRenderer();
242
                            tc.setCellRenderer(vcr);
243
                            ValueComplexEditor vce=new ValueComplexEditor();
244
                            tc.setCellEditor(vce);
245
                    }
246
            }
247
//                TableColumn tc=getTable().getColumnModel().getColumn(0);
248
//                tc.setCellRenderer(new MyTableCellRenderer());
249

    
250

    
251
                        headerSelectionSupport.setTableHeader(getTable().getTableHeader());
252
            headerSelectionSupport.addFieldSelectionListener(new FieldSelectionListener() {
253
                    public void fieldSelected(FieldSelectionEvent e) {
254
                        if (PluginServices.getMainFrame() != null) {
255
                            PluginServices.getMainFrame().enableControls();
256
                        }
257
                    }
258
                });
259

    
260
            model.getModelo().getRecordset().addSelectionListener(selectionListener);
261

    
262
            updateSelection();
263
        } catch (DriverException e) {
264
            NotificationManager.addError("No se pudo leer la informaci?n", e);
265
        } catch (DriverLoadException e) {
266
            NotificationManager.addError("No se pudo leer la informaci?n", e);
267
        }
268
    }
269

    
270
    /**
271
     * DOCUMENT ME!
272
     *
273
     * @param table DOCUMENT ME!
274
     */
275
    public void setModel(ProjectTable table) {
276
        model = table;
277

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

    
291
        IEditableSource ies=getModel().getModelo();
292
        ies.addEditionListener(this);
293

    
294
        refreshControls();
295

    
296

    
297
    }
298
    class MyTableCellRenderer extends JLabel implements TableCellRenderer {
299
        final Border grayBorder = BorderFactory.createLineBorder(Color.darkGray);
300

    
301
        MyTableCellRenderer() {
302

    
303
        }
304

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

    
316
                label.setBorder(grayBorder);
317
                return label;
318
        }
319
      }
320

    
321
    /**
322
     *
323
     */
324
    public void updateSelection() {
325
        updating = true;
326
        try {
327
            DefaultListSelectionModel sm = (DefaultListSelectionModel) getTable()
328
                                                                           .getSelectionModel();
329
            sm.clearSelection();
330

    
331
            BitSet bs = (model.getModelo().getRecordset()).getSelection();
332
            sm.setValueIsAdjusting(true);
333

    
334
            if (orderIndexes != null) {
335
                for (int i = 0; i < orderIndexes.length; i++) {
336
                    if (bs.get(i)) {
337
                        sm.addSelectionInterval((int) orderIndexesInverted[i],
338
                            (int) orderIndexesInverted[i]);
339
                        if (isEditing())
340
                                table.setEditingRow((int)orderIndexesInverted[i]);
341
                    }
342
                }
343
            } else {
344
                for (int i = bs.nextSetBit(0); i >= 0;
345
                        i = bs.nextSetBit(i + 1)) {
346
                    sm.addSelectionInterval(i, i);
347
                    if (isEditing())
348
                            table.setEditingRow(i);
349
                }
350
            }
351

    
352
            sm.setValueIsAdjusting(false);
353
            try {
354
                numReg = model.getModelo().getRowCount();
355
            } catch (IOException e) {
356
                e.printStackTrace();
357
            }
358

    
359
            jLabelStatus.setText(" " +
360
                (model.getModelo().getRecordset()).getSelection()
361
                 .cardinality() + " / " + numReg + " " +
362
                PluginServices.getText(this, "registros_seleccionados_total") +
363
                ".");
364
            if (PluginServices.getMainFrame() != null)
365
                    PluginServices.getMainFrame().enableControls();
366
        } catch (DriverIOException e) {
367
            e.printStackTrace();
368
        } catch (DriverLoadException e) {
369
            e.printStackTrace();
370
        }
371
        updating = false;
372
    }
373

    
374
    /**
375
     * This method initializes this
376
     */
377
    protected void initialize() {
378
        jLabelStatus = new JLabel();
379
        setLayout(new java.awt.BorderLayout());
380
        jLabelStatus.setText("");
381
        jLabelStatus.setName("");
382
        jLabelStatus.setPreferredSize(new java.awt.Dimension(100, 18));
383
        add(getJScrollPane(), java.awt.BorderLayout.CENTER);
384
        this.add(jLabelStatus, java.awt.BorderLayout.SOUTH);
385
        this.setPreferredSize(new Dimension(300,200));
386
    }
387

    
388
    /**
389
     * This method initializes table
390
     *
391
     * @return javax.swing.JTable
392
     */
393
    public javax.swing.JTable getTable() {
394
        if (table == null) {
395
            table = new javax.swing.JTable();
396
            table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
397
            table.setSelectionModel(new DefaultListSelectionModel());
398
            table.getTableHeader().addMouseListener(new MouseHandler());
399
            table.addKeyListener(new TableKeyListener());
400
            table.addMouseListener(new MouseRow());
401
            table.setSelectionForeground(Color.blue);
402
                    table.setSelectionBackground(Color.yellow);
403
                    table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
404
                    public void valueChanged(ListSelectionEvent e) {
405

    
406
                            if (updating) {
407
                                 return;
408
                        }
409

    
410
                        SelectableDataSource dataModel = null;
411

    
412
                        try {
413
                            dataModel = Table.this.model.getModelo()
414
                                                        .getRecordset();
415
                        } catch (DriverLoadException e1) {
416
                            e1.printStackTrace();
417
                        }
418

    
419
                        DefaultListSelectionModel model = (DefaultListSelectionModel) table.getSelectionModel();
420
                        BitSet selection = dataModel.getSelection();
421

    
422
                        for (int i = e.getFirstIndex(); i <= e.getLastIndex();
423
                                i++) {
424
                            if (orderIndexes != null) {
425
                                selection.set((int) orderIndexes[i],
426
                                    model.isSelectedIndex(i));
427
                            } else {
428
                                selection.set(i, model.isSelectedIndex(i));
429
                            }
430
                        }
431

    
432
                        if (e.getValueIsAdjusting() == false) {
433
                            if (fmap != null) {
434
                                fmap.endAtomicEvent();
435
                            }
436

    
437
                            dataModel.fireSelectionEvents();
438
                        } else {
439
                            if (fmap != null) {
440
                                fmap.beginAtomicEvent();
441
                            }
442
                        }
443

    
444
                        jLabelStatus.setText(" " + selection.cardinality() +
445
                            " / " + numReg + " " +
446
                            PluginServices.getText(this,
447
                                "registros_seleccionados_total") + ".");
448

    
449
                    }
450
                });
451

    
452
                       JTextField tf=new JTextField();
453
                    table.setDefaultEditor(Number.class, new DefaultEditor(tf));
454
            table.setDefaultEditor(String.class, new DefaultEditor(tf));
455
            table.setDefaultEditor(Object.class, new DefaultEditor(tf));
456
        }
457

    
458
        return table;
459
    }
460
    protected class DefaultEditor extends DefaultCellEditor {
461
                public Component getTableCellEditorComponent(javax.swing.JTable table,
462
                                Object value, boolean isSelected, int row, int column) {
463
                        JTextField tf=(JTextField)super.getTableCellEditorComponent(table, value, isSelected,
464
                                        row, column);
465
                        if (isSelected){
466
                                tf.setBackground(new Color(230,220,220));
467
                                tf.selectAll();
468

    
469
                        }
470
                        return tf;
471
                }
472

    
473
                public DefaultEditor(JTextField tf) {
474
                        super(tf);
475
                         //MouseListener[] mouseListeners=getTable().getEditorComponent().getMouseListeners();
476
                   // if (!mouseListeners[mouseListeners.length-1].getClass().equals(MouseRow.class)){
477
                   tf.addMouseListener(new MouseRow());
478
                   // }
479
                        getComponent().addKeyListener(new KeyAdapter() {
480
                                int keyPressed = 0;
481

    
482
                                public void keyPressed(KeyEvent ke) {
483
                                        if (ke.getKeyCode() != KeyEvent.VK_TAB)
484
                                                keyPressed++;
485
                                        JTextField tf = (JTextField) getComponent();
486

    
487

    
488
                                        if (ke.getKeyCode() == KeyEvent.VK_RIGHT
489
                                                        || ke.getKeyCode() == KeyEvent.VK_ENTER) {
490
                                                int caretPosition = tf.getCaretPosition();
491
                                                if (caretPosition >= tf.getText().length()) {
492
                                                        int x = table.getSelectedColumn();
493
                                                        int y = table.getSelectedRow();
494
                                                        if (x + 1 >= table.getColumnCount()) {
495
                                                                x = 0;
496
                                                                y++;
497
                                                        } else {
498
                                                                x++;
499
                                                        }
500
                                                        //table.changeSelection(y, x, false, true);
501
                                                        //initEditField(x,y);
502
                                                        getComponent().setEnabled(false);
503
                                                }
504

    
505
                                        } else if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
506
                                                int caretPosition = tf.getCaretPosition();
507
                                                if (caretPosition <= 0) {
508
                                                        int x = table.getSelectedColumn();
509
                                                        int y = table.getSelectedRow();
510
                                                        if (x == 0) {
511
                                                                x = table.getColumnCount() - 1;
512
                                                                if (y - 1 < 0)
513
                                                                        y = table.getRowCount() - 1;
514
                                                                else
515
                                                                        y--;
516
                                                        } else {
517
                                                                x--;
518
                                                        }
519
                                                        //table.changeSelection(y, x, false, true);
520
                                                        //initEditField(x,y);
521
                                                        getComponent().setEnabled(false);
522
                                                }
523

    
524
                                        }
525
                                }
526

    
527
                                public void keyReleased(KeyEvent ke) {
528
                                        JComponent editor = (JComponent) DefaultEditor.this
529
                                                        .getComponent();
530
                                        if (editor instanceof JTextField) {
531
                                                if (keyPressed == 0) {
532
                                                        ((JTextField) editor).setText(String.valueOf(ke
533
                                                                        .getKeyChar()));
534
                                                }
535
                                        }
536
                                        keyPressed--;
537
                                        if (keyPressed < 0)
538
                                                keyPressed = 0;
539
                                        getComponent().setEnabled(true);
540
                                }
541
                        });
542

    
543
                }
544

    
545
                public Object getCellEditorValue() {
546
                        String s = ((JTextField) (DefaultEditor.this.getComponent()))
547
                                        .getText();
548
                        getComponent().setEnabled(true);
549
                        return s;
550
                }
551

    
552
                public boolean isCellEditable(EventObject event) {
553
                        // IF NUMBER OF CLICKS IS LESS THAN THE CLICKCOUNTTOSTART RETURN
554
                        // FALSE
555
                        // FOR CELL EDITING.
556
                        if (event instanceof MouseEvent) {
557
                                return ((MouseEvent) event).getClickCount() >= getClickCountToStart();
558
                        }
559

    
560
                        return true;
561
                }
562
        }
563

    
564

    
565

    
566

    
567
    /**
568
         * This method initializes jScrollPane
569
         *
570
         * @return javax.swing.JScrollPane
571
         */
572
    protected javax.swing.JScrollPane getJScrollPane() {
573
        if (jScrollPane == null) {
574
            jScrollPane = new javax.swing.JScrollPane();
575
            jScrollPane.setViewportView(getTable());
576
        }
577

    
578
        return jScrollPane;
579
    }
580

    
581
    /**
582
         * @see com.iver.mdiApp.ui.MDIManager.SingletonWindow#getWindowModel()
583
         */
584
    public Object getWindowModel() {
585
        return model;
586
    }
587

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

    
608
    /**
609
     * DOCUMENT ME!
610
     *
611
     * @param indexes
612
     *
613
     * @throws IOException
614
     */
615
    public void setOrder(long[] indexes){
616
        orderIndexes = indexes;
617
        orderIndexesInverted = new long[orderIndexes.length];
618

    
619
        for (int i = 0; i < indexes.length; i++) {
620
            orderIndexesInverted[(int) indexes[i]] = i;
621
        }
622

    
623
        updating = true;
624
        ((DataSourceDataModel) getTable().getModel()).fireTableDataChanged();
625
        updating = false;
626

    
627
        updateSelection();
628
    }
629

    
630
    /*  public void startEditing() throws DriverException{
631
       // dw = model.getModelo().getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER);
632
       // dw.beginTrans();
633
       }
634
     */
635

    
636
    /**
637
     * DOCUMENT ME!
638
     */
639

    
640
    /*  public void cancelEditing() throws DriverException {
641
       dw.rollBackTrans();
642
       dw = null;
643
       }
644
     */
645

    
646
    /**
647
     * DOCUMENT ME!
648
     */
649

    
650
    /*  public void stopEditing() throws DriverException {
651
       // dw.commitTrans();
652
       // dw = null;
653
       // model.getModelo().stop();
654
       // model.getModelo().start();
655
       }
656
     */
657

    
658
    /**
659
     * DOCUMENT ME!
660
     */
661

    
662
    /*  public boolean isEditing() {
663
       return dw != null;
664
       }
665
     */
666

    
667
    /**
668
     * Quita los campos seleccionados
669
     */
670
    public void clearSelectedFields() {
671
        headerSelectionSupport.clearSelectedColumns();
672
    }
673

    
674
    /**
675
     * DOCUMENT ME!
676
     *
677
     * @param index DOCUMENT ME!
678
     *
679
     * @return DOCUMENT ME!
680
     */
681
    public Value[] getValueRow(int index) {
682
        DataSourceDataModel dsdm = (DataSourceDataModel) getTable().getModel();
683
        Value[] values = new Value[dsdm.getColumnCount()];
684

    
685
        for (int i = 0; i < dsdm.getColumnCount(); i++) {
686
            values[i] = (Value) dsdm.getValueAt(index, i);
687
        }
688

    
689
        return values;
690
    }
691

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

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

    
769
                        if (x.length == 1 && y.length == 1)
770
                                table.editCellAt(y[0], x[0]);
771
                        JTextField tf = (JTextField) table.getEditorComponent();
772
                        if (tf != null) {
773
                                tf.selectAll();
774
                                tf.requestFocus();
775
                        }
776
//                        if (y.length > 0 && x.length > 0) {
777
//                                getTable().setRowSelectionInterval(y[0], y[y.length - 1]);
778
//                                getTable().setColumnSelectionInterval(x[0], x[x.length - 1]);
779
//                        }
780

    
781
                }
782
            }
783
    }
784
    /**
785
         * DOCUMENT ME!
786
         */
787
    public void stopEditing() {
788
        try {
789
                this.stopEditingCell();
790

    
791
            FLyrVect lyr = (FLyrVect) getModel().getAssociatedTable();
792

    
793
            if ((lyr != null) &&
794
                    lyr.getSource() instanceof VectorialEditableAdapter) {
795
                    VectorialEditableAdapter vea = (VectorialEditableAdapter) lyr.getSource();
796
                    ISpatialWriter spatialWriter = (ISpatialWriter) vea.getOriginalDriver();
797
                    vea.stopEdition(spatialWriter,EditionEvent.ALPHANUMERIC);
798
                    lyr.setSource(vea.getOriginalAdapter());
799
                    lyr.setEditing(false);
800
                    refreshControls();
801

    
802
            } else {
803

    
804
                 IEditableSource ies=getModel().getModelo();
805
                 if (ies instanceof IWriteable)
806
                 {
807
                         IWriteable w = (IWriteable) ies;
808
                         IWriter writer = w.getWriter();
809
                         if (writer == null)
810
                         {
811
                                 NotificationManager.addError("No existe driver de escritura para la tabla"
812
                                                 + getModel().getName(), new EditionException());
813
                         }
814
                         else
815
                         {
816
                                             ITableDefinition tableDef = ies.getTableDefinition();
817
                                            writer.initialize(tableDef);
818

    
819
                                 ies.stopEdition(writer,EditionEvent.ALPHANUMERIC);
820

    
821
                                 // TODO: RELOAD
822
//                                 EditableAdapter edAdapter = (EditableAdapter) ies;
823
//                                 // Restaura el datasource a su estado original
824
//                                 edAdapter.setOriginalDataSource(edAdapter.getRecordset());
825
                                 ies.getSelection().clear();
826
                                 refreshControls();
827
                         }
828
                 }
829

    
830
                 /*
831
                GdbmsWriter gdbmswriter = new GdbmsWriter();
832
                gdbmswriter.setDataWare(getModel().getModelo()
833
                                                              .getRecordset()
834
                                                              .getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER));
835
                gdbmswriter.preProcess();
836

837
                for (int i = 0; i < getModel().getModelo().getRowCount();
838
                        i++) {
839
                    gdbmswriter.process(getModel().getModelo().getRow(i));
840
                }
841

842
                gdbmswriter.postProcess();
843
                */
844

    
845
            }
846

    
847
        } catch (Exception e) {
848
            NotificationManager.addError("No se pudo guardar la edici?n", e);
849
        }
850
    }
851

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

    
861
    /**
862
     * DOCUMENT ME!
863
     *
864
     * @param index DOCUMENT ME!
865
     */
866
    public void setUneditableColumns(int[] index) {
867
        // TODO Auto-generated method stub
868
    }
869

    
870
    /**
871
     * DOCUMENT ME!
872
     *
873
     * @param numColumns DOCUMENT ME!
874
     * @param values DOCUMENT ME!
875
     */
876
    public void setDefaultValues(int[] numColumns, Value[] values) {
877
        // TODO Auto-generated method stub
878
    }
879

    
880
    /**
881
     * DOCUMENT ME!
882
     *
883
     * @return DOCUMENT ME!
884
     */
885
    public Value getDefaultValue() {
886
        // TODO Auto-generated method stub
887
        return null;
888
    }
889

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

    
912
    /**
913
     * DOCUMENT ME!
914
     *
915
     * @return DOCUMENT ME!
916
     */
917
    public boolean isEditing() {
918
        return getModel().getModelo().isEditing();
919
    }
920

    
921
    /**
922
     * DOCUMENT ME!
923
     */
924
    public void refresh() {
925
            int row=table.getSelectedRow();
926
            int column=table.getSelectedColumn();
927
            if (row!=-1 && column!=-1 && table.getEditorComponent()!=null){
928
                    Value[] values=getValueRow(row);
929
                    JTextField jtf=(JTextField)table.getEditorComponent();
930
                    jtf.setText(values[column].toString());
931
            }
932
        updating = true;
933
        ((DataSourceDataModel) getTable().getModel()).fireTableDataChanged();
934

    
935
        updating = false;
936
        updateSelection();
937
        PluginServices.getMainFrame().enableControls();
938

    
939
    }
940

    
941
    /**
942
     * Add the rows that are passed like parameter and if parameter is null a
943
     * row is added empties.
944
     *
945
     * @param rows Rows to add or null.
946
     *
947
     * @throws DriverIOException
948
     * @throws IOException
949
     */
950
    public void addRow(IRow[] rows) throws DriverIOException, IOException {
951
            IEditableSource ies=getModel().getModelo();
952
            try {
953
            if (rows == null) {
954

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

    
985
        refresh();
986

    
987
    }
988

    
989
    /**
990
     * Copy in the arraylist the rows that be selected just then.
991
     *
992
     * @throws DriverIOException
993
     * @throws IOException
994
     */
995
    public void copyRow() throws DriverIOException, IOException {
996
        int[] index = getSelectedRowIndices();
997
        rowsCopied = new IRow[index.length];
998

    
999
        for (int i = 0; i < index.length; i++) {
1000
            rowsCopied[i] = getModel().getModelo().getRow(index[i]);
1001
        }
1002
    }
1003

    
1004
    /**
1005
     * Cut the rows that be selected just then.
1006
     *
1007
     * @throws DriverIOException
1008
     * @throws IOException
1009
     */
1010
    public void cutRow() throws DriverIOException, IOException {
1011
        int[] index = getSelectedRowIndices();
1012
        rowsCopied = new IRow[index.length];
1013

    
1014
        for (int i = 0; i < index.length; i++) {
1015
            rowsCopied[i] = getModel().getModelo().getRow(index[i]);
1016
        }
1017

    
1018
        removeRow();
1019
    }
1020

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

    
1043
        refresh();
1044
        //repaintAssociatedView();
1045
    }
1046

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

    
1079
    }
1080

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

    
1098
                edAdapter.endComplexRow(PluginServices.getText(this, "remove_fields"));
1099
                clearSelectedFields();
1100
                getModel().setModel(edAdapter); // Para que se recalculen los campos. TODO: Limpiear todo esto
1101
                // refresh();
1102
                refreshControls();
1103
                } catch (EditionException e) {
1104
                        e.printStackTrace();
1105
                        NotificationManager.addError(e);
1106
                } catch (DriverException e) {
1107
                        e.printStackTrace();
1108
                        NotificationManager.addError(e);
1109
                } catch (IOException e) {
1110
                        e.printStackTrace();
1111
                        NotificationManager.addError(e);
1112
                } catch (DriverIOException e) {
1113
                        e.printStackTrace();
1114
                        NotificationManager.addError(e);
1115
                }
1116

    
1117
    }
1118

    
1119
    /**
1120
     * Return if we have rows copied or not.
1121
     *
1122
     * @return True if we have rows copied.
1123
     */
1124
    public boolean isCopied() {
1125
        return (rowsCopied != null);
1126
    }
1127

    
1128
    /**
1129
     * Paste the arraylist rows.
1130
     *
1131
     * @throws DriverIOException
1132
     * @throws IOException
1133
     */
1134
    public void pasteRow() throws DriverIOException, IOException {
1135
        addRow(rowsCopied);
1136
        //repaintAssociatedView();
1137
    }
1138

    
1139
    /**
1140
     * DOCUMENT ME!
1141
     */
1142
    public void windowActivated() {
1143
        //if (isEditing() && getModel().getModelo() instanceof VectorialEditableAdapter)
1144
        updateSelection();
1145
    }
1146

    
1147
    /**
1148
     * DOCUMENT ME!
1149
     */
1150
    public void windowClosed() {
1151
        // TODO Auto-generated method stub
1152
    }
1153

    
1154
    /**
1155
     * DOCUMENT ME!
1156
     *
1157
     * @author Fernando Gonz?lez Cort?s
1158
     */
1159
    public class DataSourceDataModel extends AbstractTableModel {
1160
        //private SelectableDataSource dataSource;
1161
        ProjectTable pt;
1162

    
1163
        /**
1164
         * Crea un nuevo DataSourceDataModel.
1165
         *
1166
         * @param pt DOCUMENT ME!
1167
         */
1168
        public DataSourceDataModel(ProjectTable pt) {
1169
            this.pt = pt;
1170

    
1171
            //try {
1172
            //dataSource = pt.getModelo().getRecordset();
1173
            //} catch (DriverLoadException e) {
1174
            // TODO Auto-generated catch block
1175
            //        e.printStackTrace();
1176
            //}
1177
        }
1178

    
1179
        /**
1180
         * Returns the name of the field.
1181
         *
1182
         * @param col index of field
1183
         *
1184
         * @return Name of field
1185
         */
1186
        public String getColumnName(int col) {
1187
//                if (col==0)
1188
//                        return " ";
1189
//                col--;
1190
                int i=pt.getMapping()[col];
1191
                return pt.getAliases()[i];
1192
        }
1193

    
1194
        /**
1195
         * Returns the number of fields.
1196
         *
1197
         * @return number of fields
1198
         */
1199
        public int getColumnCount() {
1200
            return pt.getColumnCount();
1201
        }
1202

    
1203
        /**
1204
         * Returns number of rows.
1205
         *
1206
         * @return number of rows.
1207
         */
1208
        public int getRowCount() {
1209
            try {
1210
                    IEditableSource des = pt.getModelo();
1211
                return des.getRowCount();
1212
            } catch (DriverIOException e) {
1213
                return 0;
1214
            } catch (IOException e) {
1215
                return 0;
1216
            }
1217
        }
1218

    
1219
        /**
1220
         * DOCUMENT ME!
1221
         *
1222
         * @param row DOCUMENT ME!
1223
         * @param col DOCUMENT ME!
1224
         *
1225
         * @return DOCUMENT ME!
1226
         */
1227
        public Object getValueAt(int row, int col) {
1228

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

    
1254
        /**
1255
         * DOCUMENT ME!
1256
         *
1257
         * @param rowIndex DOCUMENT ME!
1258
         * @param columnIndex DOCUMENT ME!
1259
         *
1260
         * @return DOCUMENT ME!
1261
         */
1262
        public boolean isCellEditable(int rowIndex, int columnIndex) {
1263
//                if (columnIndex==0)
1264
//                return false;
1265
                try {
1266
                                if (getModel().getModelo().getRecordset().getFieldType(columnIndex)==Types.STRUCT)
1267
                                        return true;
1268
                        } catch (DriverLoadException e) {
1269
                                e.printStackTrace();
1270
                        } catch (DriverException e) {
1271
                                e.printStackTrace();
1272
                        }
1273
                return isEditing();
1274
        }
1275

    
1276
        /**
1277
         * DOCUMENT ME!
1278
         *
1279
         * @param aValue DOCUMENT ME!
1280
         * @param rowIndex DOCUMENT ME!
1281
         * @param columnIndex DOCUMENT ME!
1282
         *
1283
         * @throws RuntimeException DOCUMENT ME!
1284
         */
1285
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
1286
//                 if (columnIndex==0)
1287
//                 throw new UnsupportedOperationException("Row Number is a read-only column");
1288
//                 columnIndex--;
1289
                Value v;
1290
                 int numRow;
1291
             if (orderIndexes != null) {
1292
                     numRow=(int)orderIndexes[rowIndex];
1293
             }else {
1294
                     numRow=rowIndex;
1295
             }
1296
                if (getValueAt(rowIndex,columnIndex)==null || getValueAt(rowIndex,columnIndex).toString().equals(aValue))
1297
                    return;
1298
            try {
1299
                    IEditableSource des = pt.getModelo();
1300
                    v = ValueFactory.createValueByType(aValue.toString(),
1301
                        des.getRecordset().getFieldType(columnIndex));
1302
                IRowEdited row = des.getRow(numRow); //.getAttribute(columnIndex);
1303
                Value[] values = row.getAttributes();
1304
                values[columnIndex] = v;
1305

    
1306
                IRow newRow = null;
1307

    
1308
                if (row.getLinkedRow() instanceof IFeature) {
1309
                    IGeometry geometry = ((DefaultFeature) row.getLinkedRow()).getGeometry();
1310
                    newRow = new DefaultFeature(geometry, values,row.getID());
1311
                } else {
1312
                    newRow = new DefaultRow(values,row.getID());
1313
                }
1314

    
1315
                des.modifyRow(numRow, newRow,"Editar valor", EditionEvent.ALPHANUMERIC);
1316
            } catch (DriverException e1) {
1317
                throw new RuntimeException(e1);
1318
            } catch (ParseException e) {
1319
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_del_campo_incorrecto");
1320
                        return;
1321
            } catch (DriverLoadException e) {
1322
                throw new RuntimeException(e);
1323
            } catch (DriverIOException e) {
1324
                throw new RuntimeException(e);
1325
            } catch (IOException e) {
1326
                throw new RuntimeException(e);
1327
            } catch (NumberFormatException e) {
1328
                        /*        NotificationManager.addError(PluginServices.-getText(this,"numero_incorrecto")+
1329
                                                "\n"+PluginServices.-getText(this,"fila")+" = "+rowIndex+
1330
                                                "\n"+PluginServices.-getText(this,"columna")+ " = "+columnIndex,e);
1331
                        */
1332
                        }
1333
        }
1334
    }
1335
    public class ColumnModel extends DefaultTableColumnModel{
1336
            private ProjectTable pt;
1337
                public ColumnModel(ProjectTable pt) {
1338
                        this.pt = pt;
1339
                }
1340

    
1341

    
1342
                public int getTotalColumnWidth() {
1343
//                        int w=0;
1344
//                        for (int i=0;i<getColumnCount();i++){
1345
//                                w+=pt.getColumn(i).getWidth();
1346
//                        }
1347
                        return super.getTotalColumnWidth();
1348
                }
1349

    
1350
                public void moveColumn(int arg0, int arg1) {
1351
                        //super.moveColumn(arg0, arg1);
1352
                        if (arg0==arg1){
1353
                                super.moveColumn(arg0,arg1);
1354
                        }
1355
                }
1356

    
1357

    
1358
                public void propertyChange(PropertyChangeEvent arg0) {
1359
                        try{
1360
                        super.propertyChange(arg0);
1361
                        int w=((Integer)arg0.getNewValue()).intValue();
1362
                        if (arg0.getSource() instanceof TableColumn && (w!=75)){
1363
                                TableColumn tc=(TableColumn)arg0.getSource();
1364
                                Column column=pt.getColumn(tc.getModelIndex());
1365
                                column.setWidth(w);
1366
                                System.out.println("Index guardar = "+tc.getModelIndex()+" , "+"Anchura = "+w);
1367
                        }
1368
                        }catch (Exception e) {
1369
                                System.out.println("PETA");
1370
                        }
1371

    
1372
                }
1373
        }
1374
    /**
1375
     * DOCUMENT ME!
1376
     *
1377
     * @author Vicente Caballero Navarro
1378
     */
1379
    public class TableSelectionListener implements SelectionListener {
1380
        /**
1381
         * @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
1382
         */
1383
        public void selectionChanged(SelectionEvent e) {
1384
            updateSelection();
1385
            Table.this.repaint();
1386
            //((ValueComplexRenderer)Table.this.getTable().getColumnModel().getColumn(getSelectedFieldIndices().nextSetBit(0)).getCellRenderer()).getValue();
1387
        }
1388
    }
1389

    
1390
    /**
1391
     * DOCUMENT ME!
1392
     *
1393
     * @author Vicente Caballero Navarro
1394
     */
1395
    private class MouseHandler extends MouseAdapter {
1396
        /**
1397
         * DOCUMENT ME!
1398
         *
1399
         * @param e DOCUMENT ME!
1400
         */
1401
        public void mouseClicked(MouseEvent e) {
1402

    
1403
            /* JTableHeader h = (JTableHeader) e.getSource();
1404
               TableColumnModel columnModel = h.getColumnModel();
1405
               int viewColumn = columnModel.getColumnIndexAtX(e.getX());
1406
               int column = columnModel.getColumn(viewColumn).getModelIndex();
1407
               if (column != -1) {
1408
               }*/
1409
        }
1410
    }
1411
    /**
1412
     * DOCUMENT ME!
1413
     *
1414
     * @author Vicente Caballero Navarro
1415
     */
1416
    private class TableKeyListener implements KeyListener {
1417

    
1418
                public void keyPressed(KeyEvent arg0) {
1419
                        //JTextField tf=(JTextField)table.getEditorComponent();
1420
                        //table.setCellSelectionEnabled(true);
1421
                        //FocusManager fm = FocusManager.getCurrentManager();
1422
                        //fm.focusPreviousComponent(table);
1423
                }
1424

    
1425
                public void keyReleased(KeyEvent ke) {
1426
                        int[] row=table.getSelectedRows();
1427
                    int[] column=table.getSelectedColumns();
1428
                    initEditField(column,row);
1429
                }
1430

    
1431
                public void keyTyped(KeyEvent arg0) {
1432
                        // TODO Auto-generated method stub
1433

    
1434
                }
1435

    
1436
    }
1437

    
1438
    /**
1439
     * DOCUMENT ME!
1440
     *
1441
     * @author Vicente Caballero Navarro
1442
     */
1443
    private class MouseRow extends MouseAdapter {
1444
               public void mouseReleased(MouseEvent arg0) {
1445
                        super.mouseReleased(arg0);
1446
                        int[] row=table.getSelectedRows();
1447
                    int[] column=table.getSelectedColumns();
1448
                    initEditField(column,row);
1449
                }
1450

    
1451
                /**
1452
         * DOCUMENT ME!
1453
         *
1454
         * @param e DOCUMENT ME!
1455
         */
1456
    /*    public void mouseClicked(MouseEvent e) {
1457
            super.mouseClicked(e);
1458

1459

1460
                PluginServices.getMainFrame().enableControls();
1461

1462
            if (e.getButton() == MouseEvent.BUTTON3) {
1463
                new PopupMenu(e.getPoint());
1464
            }
1465
        }
1466
*/
1467
    }
1468

    
1469
        public void commandRepaint() {
1470
                refresh();
1471
        }
1472

    
1473
        public void commandRefresh() {
1474
                commandRepaint();
1475
        }
1476

    
1477
        public void toPalette() {
1478
                isPalette=true;
1479
                m_viewInfo.toPalette(true);
1480
                m_viewInfo.setClosed(false);
1481
                PluginServices.getMDIManager().changeWindowInfo(this,getWindowInfo());
1482
        }
1483

    
1484
        public void restore() {
1485
                isPalette=false;
1486
                m_viewInfo.toPalette(false);
1487
                m_viewInfo.setClosed(false);
1488
                PluginServices.getMDIManager().changeWindowInfo(this,getWindowInfo());
1489
        }
1490

    
1491
        public boolean isPalette() {
1492
                return isPalette;
1493
        }
1494

    
1495
        public static void main(String[] args) {
1496
                JTextField tf=new JTextField("hola");
1497
                tf.selectAll();
1498
                JFrame frame=new JFrame();
1499
                frame.getContentPane().add(tf);
1500
                frame.setVisible(true);
1501
        }
1502

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

    
1510
        }
1511

    
1512
        public void cancelEditingCell() {
1513
            if (table.isEditing()) {
1514
                    // TODO: finalizar la edicion de la columna
1515
                    this.table.getCellEditor().cancelCellEditing();
1516
                    this.refresh();
1517
            }
1518

    
1519
        }
1520

    
1521
        public void processEvent(EditionEvent e) {
1522
                if (e.getChangeType() == EditionEvent.STOP_EDITION)
1523
                {
1524
                        refreshControls();
1525
                }
1526

    
1527
        }
1528

    
1529
        public void beforeRowEditEvent(IRow feat, BeforeRowEditEvent e) {
1530
                // TODO Auto-generated method stub
1531

    
1532
        }
1533

    
1534
        public void afterRowEditEvent(IRow feat, AfterRowEditEvent e) {
1535
                // TODO Auto-generated method stub
1536

    
1537
        }
1538

    
1539
        public void beforeFieldEditEvent(BeforeFieldEditEvent e) {
1540
                // TODO Auto-generated method stub
1541

    
1542
        }
1543

    
1544
        public void afterFieldEditEvent(AfterFieldEditEvent e) {
1545
                try {
1546
//                        getModel().createAlias();
1547
                        clearSelectedFields();
1548
//                        refresh();
1549
                        refreshControls();
1550
                // ((DataSourceDataModel) getTable().getModel()).fireTableDataChanged();
1551
                } catch (DriverLoadException e1) {
1552
                        // TODO Auto-generated catch block
1553
                        e1.printStackTrace();
1554
                /* } catch (DriverException e1) {
1555
                        // TODO Auto-generated catch block
1556
                        e1.printStackTrace(); */
1557
                }
1558

    
1559
        }
1560
}