Statistics
| Revision:

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

History | View | Annotate | Download (36.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui;
42

    
43
import java.awt.Component;
44
import java.awt.event.MouseAdapter;
45
import java.awt.event.MouseEvent;
46
import java.beans.PropertyChangeEvent;
47
import java.beans.PropertyChangeListener;
48
import java.io.File;
49
import java.io.IOException;
50
import java.text.ParseException;
51
import java.util.BitSet;
52
import java.util.Enumeration;
53
import java.util.EventListener;
54

    
55
import javax.swing.DefaultListSelectionModel;
56
import javax.swing.JFileChooser;
57
import javax.swing.JLabel;
58
import javax.swing.JOptionPane;
59
import javax.swing.JPanel;
60
import javax.swing.ListSelectionModel;
61
import javax.swing.event.ListSelectionEvent;
62
import javax.swing.event.ListSelectionListener;
63
import javax.swing.event.TableColumnModelEvent;
64
import javax.swing.event.TableColumnModelListener;
65
import javax.swing.table.AbstractTableModel;
66
import javax.swing.table.DefaultTableColumnModel;
67
import javax.swing.table.TableColumn;
68

    
69
import org.apache.log4j.Logger;
70

    
71
import com.hardcode.driverManager.DriverLoadException;
72
import com.hardcode.gdbms.engine.data.driver.DriverException;
73
import com.hardcode.gdbms.engine.values.Value;
74
import com.hardcode.gdbms.engine.values.ValueFactory;
75
import com.iver.andami.PluginServices;
76
import com.iver.andami.messages.NotificationManager;
77
import com.iver.andami.ui.mdiFrame.MainFrame;
78
import com.iver.andami.ui.mdiManager.SingletonView;
79
import com.iver.andami.ui.mdiManager.ViewInfo;
80
import com.iver.andami.ui.mdiManager.ViewListener;
81
import com.iver.andami.ui.mdiManager.ViewTransform;
82
import com.iver.cit.gvsig.fmap.FMap;
83
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
84
import com.iver.cit.gvsig.fmap.core.DefaultRow;
85
import com.iver.cit.gvsig.fmap.core.IFeature;
86
import com.iver.cit.gvsig.fmap.core.IGeometry;
87
import com.iver.cit.gvsig.fmap.core.IRow;
88
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
89
import com.iver.cit.gvsig.fmap.edition.EditionException;
90
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
91
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
92
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
93
import com.iver.cit.gvsig.fmap.edition.commands.CommandListener;
94
import com.iver.cit.gvsig.fmap.edition.writers.dbf.DbfWriter;
95
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
96
import com.iver.cit.gvsig.fmap.layers.FLayer;
97
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
98
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
99
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
100
import com.iver.cit.gvsig.fmap.layers.SelectionEvent;
101
import com.iver.cit.gvsig.fmap.layers.SelectionListener;
102
import com.iver.cit.gvsig.gui.tables.Column;
103
import com.iver.cit.gvsig.gui.tables.EditionTable;
104
import com.iver.cit.gvsig.gui.tables.PopupMenu;
105
import com.iver.cit.gvsig.project.ProjectTable;
106
import com.iver.utiles.SimpleFileFilter;
107
import com.iver.utiles.swing.jtable.FieldSelectionEvent;
108
import com.iver.utiles.swing.jtable.FieldSelectionListener;
109
import com.iver.utiles.swing.jtable.JTable;
110
import com.iver.utiles.swing.jtable.SelectionHeaderSupport;
111
import com.iver.utiles.swing.jtable.TextFieldCellEditor;
112

    
113

    
114
/**
115
 * DOCUMENT ME!
116
 *
117
 * @author Fernando Gonz?lez Cort?s
118
 */
119
public class Table extends JPanel implements SingletonView, EditionTable,
120
    ViewListener,CommandListener, ViewTransform {
121
    private static Logger logger = Logger.getLogger(Table.class.getName());
122
    private javax.swing.JScrollPane jScrollPane = null;
123
    private JTable table = null;
124
    private ProjectTable model = null;
125
    private JLabel jLabelStatus = null;
126
    private FMap fmap;
127
    private boolean updating = false;
128
    private TableSelectionListener selectionListener = new TableSelectionListener();
129
    private long numReg = 0;
130
    private SelectionHeaderSupport headerSelectionSupport = new SelectionHeaderSupport();
131
    private long[] orderIndexes = null;
132
    private long[] orderIndexesInverted = null;
133
    private IRow[] rowsCopied = null;
134
    private ViewInfo m_viewInfo = null;
135
        private boolean isPalette=false;
136

    
137
    /**
138
     * This is the default constructor
139
     */
140
    public Table() {
141
        super();
142
        initialize();
143
    }
144

    
145
    /**
146
     * DOCUMENT ME!
147
     *
148
     * @return DOCUMENT ME!
149
     */
150
    public ProjectTable getModel() {
151
        return model;
152
    }
153

    
154
    /**
155
     * DOCUMENT ME!
156
     *
157
     * @return DOCUMENT ME!
158
     */
159
    public BitSet getSelectedFieldIndices() {
160
        return headerSelectionSupport.getSelectedColumns();
161
    }
162

    
163
    /**
164
     * DOCUMENT ME!
165
     *
166
     * @return DOCUMENT ME!
167
     */
168
    public int[] getSelectedRowIndices() {
169
        return getTable().getSelectedRows();
170
    }
171

    
172
    /**
173
     * DOCUMENT ME!
174
     */
175
    private void refreshControls() {
176
        try {
177
            MainFrame mF = PluginServices.getMainFrame();
178

    
179
            if (mF != null) {
180
                PluginServices.getMDIManager().getViewInfo(Table.this).setTitle(PluginServices.getText(
181
                        this, "Tabla") + ": " + model.getName());
182
            }
183

    
184
            if (model.getAssociatedTable() != null) {
185
                this.fmap = ((FLayer) model.getAssociatedTable()).getFMap();
186
            } else {
187
                this.fmap = null;
188
            }
189

    
190
            SelectableDataSource dataSource = (SelectableDataSource) model.getModelo()
191
                                                                          .getRecordset();
192
            logger.debug("dataSource.start()");
193
            dataSource.start();
194

    
195

    
196
            ColumnModel cm=new ColumnModel();
197
            cm.setDataModel(model);
198
            getTable().setColumnModel(cm);
199

    
200
            AbstractTableModel dataModel = new DataSourceDataModel(model);
201

    
202
            getTable().setModel(dataModel);
203
            TableColumn column = null;
204
                        for (int i = 0; i < model.getColumnCount(); i++) {
205
                            column = table.getColumnModel().getColumn(i);
206
                            int w=model.getColumn(i).getWidth();
207
                            column.setPreferredWidth(w); //sport column is bigger
208

    
209
                        }
210

    
211
            headerSelectionSupport.setTableHeader(getTable().getTableHeader());
212
            headerSelectionSupport.addFieldSelectionListener(new FieldSelectionListener() {
213
                    public void fieldSelected(FieldSelectionEvent e) {
214
                        if (PluginServices.getMainFrame() != null) {
215
                            PluginServices.getMainFrame().enableControls();
216
                        }
217
                    }
218
                });
219

    
220
            model.getModelo().getRecordset().addSelectionListener(selectionListener);
221

    
222
            updateSelection();
223
        } catch (DriverException e) {
224
            NotificationManager.addError("No se pudo leer la informaci?n", e);
225
        } catch (DriverLoadException e) {
226
            NotificationManager.addError("No se pudo leer la informaci?n", e);
227
        }
228
    }
229

    
230
    /**
231
     * DOCUMENT ME!
232
     *
233
     * @param table DOCUMENT ME!
234
     */
235
    public void setModel(ProjectTable table) {
236
        model = table;
237

    
238
        //Gesti?n del nombre de la ventana
239
        model.addPropertyChangeListener(new PropertyChangeListener() {
240
                public void propertyChange(PropertyChangeEvent evt) {
241
                    if (evt.getPropertyName().equals("name")) {
242
                        PluginServices.getMDIManager().getViewInfo(Table.this)
243
                                      .setTitle(PluginServices.getText(this,
244
                                "Tabla") + ": " + (String) evt.getNewValue());
245
                    } else if (evt.getPropertyName().equals("model")) {
246
                        refreshControls();
247
                    }
248
                }
249
            });
250

    
251
        refreshControls();
252
    }
253

    
254
    /**
255
     *
256
     */
257
    private void updateSelection() {
258
        updating = true;
259

    
260
        try {
261
            DefaultListSelectionModel sm = (DefaultListSelectionModel) getTable()
262
                                                                           .getSelectionModel();
263
            sm.clearSelection();
264

    
265
            BitSet bs = (model.getModelo().getRecordset()).getSelection();
266
            sm.setValueIsAdjusting(true);
267

    
268
            if (orderIndexes != null) {
269
                for (int i = 0; i < orderIndexes.length; i++) {
270
                    if (bs.get(i)) {
271
                        sm.addSelectionInterval((int) orderIndexesInverted[i],
272
                            (int) orderIndexesInverted[i]);
273
                    }
274
                }
275
            } else {
276
                for (int i = bs.nextSetBit(0); i >= 0;
277
                        i = bs.nextSetBit(i + 1)) {
278
                    sm.addSelectionInterval(i, i);
279
                }
280
            }
281

    
282
            sm.setValueIsAdjusting(false);
283

    
284
            try {
285
                numReg = model.getModelo().getRowCount();
286
            } catch (IOException e) {
287
                // TODO Auto-generated catch block
288
                e.printStackTrace();
289
            }
290

    
291
            jLabelStatus.setText(" " +
292
                ((SelectableDataSource) model.getModelo().getRecordset()).getSelection()
293
                 .cardinality() + " / " + numReg + " " +
294
                PluginServices.getText(this, "registros_seleccionados_total") +
295
                ".");
296
        } catch (DriverIOException e) {
297
            // TODO Auto-generated catch block
298
            e.printStackTrace();
299
        } catch (DriverLoadException e) {
300
            // TODO Auto-generated catch block
301
            e.printStackTrace();
302
        }
303

    
304
        updating = false;
305
    }
306

    
307
    /**
308
     * This method initializes this
309
     */
310
    private void initialize() {
311
        jLabelStatus = new JLabel();
312
        setLayout(new java.awt.BorderLayout());
313
        jLabelStatus.setText("");
314
        jLabelStatus.setName("");
315
        jLabelStatus.setPreferredSize(new java.awt.Dimension(100, 18));
316
        add(getJScrollPane(), java.awt.BorderLayout.CENTER);
317
        this.add(jLabelStatus, java.awt.BorderLayout.SOUTH);
318
    }
319

    
320
    /**
321
     * This method initializes table
322
     *
323
     * @return javax.swing.JTable
324
     */
325
    public javax.swing.JTable getTable() {
326
        if (table == null) {
327
            table = new JTable();
328
            table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
329
            table.setSelectionModel(new DefaultListSelectionModel());
330
            table.getTableHeader().addMouseListener(new MouseHandler());
331
            table.addMouseListener(new MouseRow());
332
            table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
333
                    public void valueChanged(ListSelectionEvent e) {
334
                        if (updating) {
335
                            return;
336
                        }
337

    
338
                        SelectableDataSource dataModel = null;
339

    
340
                        try {
341
                            dataModel = Table.this.model.getModelo()
342
                                                        .getRecordset();
343
                        } catch (DriverLoadException e1) {
344
                            // TODO Auto-generated catch block
345
                            e1.printStackTrace();
346
                        }
347

    
348
                        DefaultListSelectionModel model = (DefaultListSelectionModel) table.getSelectionModel();
349
                        BitSet selection = dataModel.getSelection();
350

    
351
                        for (int i = e.getFirstIndex(); i <= e.getLastIndex();
352
                                i++) {
353
                            if (orderIndexes != null) {
354
                                selection.set((int) orderIndexes[i],
355
                                    model.isSelectedIndex(i));
356
                            } else {
357
                                selection.set(i, model.isSelectedIndex(i));
358
                            }
359
                        }
360

    
361
                        if (e.getValueIsAdjusting() == false) {
362
                            if (fmap != null) {
363
                                fmap.endAtomicEvent();
364
                            }
365

    
366
                            dataModel.fireSelectionEvents();
367
                        } else {
368
                            if (fmap != null) {
369
                                fmap.beginAtomicEvent();
370
                            }
371
                        }
372

    
373
                        jLabelStatus.setText(" " + selection.cardinality() +
374
                            " / " + numReg + " " +
375
                            PluginServices.getText(this,
376
                                "registros_seleccionados_total") + ".");
377
                    }
378
                });
379

    
380
            TextFieldCellEditor ce = new TextFieldCellEditor();
381

    
382
            for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
383
                table.getColumnModel().getColumn(i).setCellEditor(ce);
384
            }
385
        }
386

    
387
        return table;
388
    }
389

    
390
    /**
391
     * This method initializes jScrollPane
392
     *
393
     * @return javax.swing.JScrollPane
394
     */
395
    private javax.swing.JScrollPane getJScrollPane() {
396
        if (jScrollPane == null) {
397
            jScrollPane = new javax.swing.JScrollPane();
398
            jScrollPane.setViewportView(getTable());
399
        }
400

    
401
        return jScrollPane;
402
    }
403

    
404
    /**
405
     * @see com.iver.mdiApp.ui.MDIManager.SingletonView#getViewModel()
406
     */
407
    public Object getViewModel() {
408
        return model;
409
    }
410

    
411
    /**
412
     * This method is used to get <strong>an initial</strong> ViewInfo object
413
     * for this Table. It is not intended to retrieve the ViewInfo object in a
414
     * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
415
     * to retrieve the ViewInfo object at any time after the creation of the
416
     * object.
417
     * 
418
     * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
419
     */
420
    public ViewInfo getViewInfo() {
421
            if (m_viewInfo==null) {
422
                    m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
423
                        ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE);
424
                    m_viewInfo.setTitle(PluginServices.getText(this, "Tabla"));
425
                    m_viewInfo.setWidth(300);
426
                    m_viewInfo.setHeight(200);
427
            }
428
        return m_viewInfo;
429
    }
430

    
431
    /**
432
     * DOCUMENT ME!
433
     *
434
     * @param indexes
435
     *
436
     * @throws IOException
437
     */
438
    public void setOrder(long[] indexes) throws IOException {
439
        orderIndexes = indexes;
440
        orderIndexesInverted = new long[orderIndexes.length];
441

    
442
        for (int i = 0; i < indexes.length; i++) {
443
            orderIndexesInverted[(int) indexes[i]] = i;
444
        }
445

    
446
        updating = true;
447
        ((DataSourceDataModel) getTable().getModel()).fireTableDataChanged();
448
        updating = false;
449
        updateSelection();
450
    }
451

    
452
    /*  public void startEditing() throws DriverException{
453
       // dw = model.getModelo().getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER);
454
       // dw.beginTrans();
455
       }
456
     */
457

    
458
    /**
459
     * DOCUMENT ME!
460
     */
461

    
462
    /*  public void cancelEditing() throws DriverException {
463
       dw.rollBackTrans();
464
       dw = null;
465
       }
466
     */
467

    
468
    /**
469
     * DOCUMENT ME!
470
     */
471

    
472
    /*  public void stopEditing() throws DriverException {
473
       // dw.commitTrans();
474
       // dw = null;
475
       // model.getModelo().stop();
476
       // model.getModelo().start();
477
       }
478
     */
479

    
480
    /**
481
     * DOCUMENT ME!
482
     */
483

    
484
    /*  public boolean isEditing() {
485
       return dw != null;
486
       }
487
     */
488

    
489
    /**
490
     * Quita los campos seleccionados
491
     */
492
    public void clearSelectedFields() {
493
        headerSelectionSupport.clearSelectedColumns();
494
    }
495

    
496
    /**
497
     * DOCUMENT ME!
498
     *
499
     * @param index DOCUMENT ME!
500
     *
501
     * @return DOCUMENT ME!
502
     */
503
    public Value[] getValueRow(int index) {
504
        DataSourceDataModel dsdm = (DataSourceDataModel) getTable().getModel();
505
        Value[] values = new Value[dsdm.getColumnCount()];
506

    
507
        for (int i = 0; i < dsdm.getColumnCount(); i++) {
508
            values[i] = (Value) dsdm.getValueAt(index, i);
509
        }
510

    
511
        return values;
512
    }
513

    
514
    /* private void refresh() throws DriverException{
515
       //dw.commitTrans();
516
       //model.getModelo().stop();
517
       //dw.beginTrans();
518
           //DataSourceDataModel dsdm=(DataSourceDataModel)getTable().getModel();
519
           //dsdm.fireTableDataChanged();
520
       }*/
521
    /*  public void addEmptyRow() throws DriverException{
522
       ValueCollection valuePK=new ValueCollection();
523
           valuePK.setValues(new Value[]{ValueFactory.createValue(dw.getRowCount())});
524
           dw.insertEmptyRow(valuePK);
525
           refresh();
526
       }
527
     */
528
    /*        public void copySelectedRows() throws DriverException{
529
       int[] sel=getSelectedRowIndices();
530
       for(int i=0;i<sel.length;i++){
531
               rowsCopied.add(getValueRow(sel[i]));
532
       }
533
       }
534
     */
535
    /*        public void addSelectionToEnd() throws DriverException {
536
       for (int i=0;i<rowsCopied.size();i++){
537
               dw.insertFilledRow((Value[])rowsCopied.get(i));
538
       }
539
       refresh();
540
       }
541
     */
542
    /*public void delSelectionRow() throws DriverException{
543
       int[] sel=getSelectedRowIndices();
544
       for(int i=sel.length-1;i>=0;i--){
545
               dw.deleteRow(sel[i]);
546
       }
547
       refresh();
548
       }
549
     */
550
    /*public boolean isCopy(){
551
       return !rowsCopied.isEmpty();
552
       }
553
     */
554
    /*
555
       public void addSelectionToRow() throws DriverException {
556
                   int[] sel=getSelectedRowIndices();
557
                           dw.insertFilledRow((Value[])rowsCopied.get(i),sel[0]);
558
                   refresh();
559
           }
560
     */
561
    public void startEditing() throws EditionException {
562
        getModel().getModelo().startEdition();
563
    }
564

    
565
    /**
566
     * DOCUMENT ME!
567
     */
568
    public void stopEditing() {
569
        try {
570
            FLyrVect lyr = (FLyrVect) getModel().getAssociatedTable();
571

    
572
            if ((lyr != null) &&
573
                    lyr.getSource() instanceof VectorialEditableAdapter) {
574
                JFileChooser jfc = new JFileChooser();
575
                SimpleFileFilter filterShp = new SimpleFileFilter("shp",
576
                        "Ficheros .shp");
577
                jfc.setFileFilter(filterShp);
578

    
579
                if (jfc.showSaveDialog(
580
                            (Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
581
                    File newFile = jfc.getSelectedFile();
582

    
583
                    //VectorialEditableAdapter vea = (VectorialEditableAdapter) layer.getSource();
584
                    // File newFile = vea.getDriver().
585
                    VectorialEditableAdapter vea = (VectorialEditableAdapter) lyr.getSource();
586
                    ShpWriter writer = (ShpWriter)LayerFactory.getWM().getWriter("Shape Writer");
587
                    writer.setFile(newFile);
588
                    writer.initialize(lyr);
589
                    vea.stopEdition(writer);
590
                    lyr.setSource(vea.getOriginalAdapter());
591
                    lyr.setEditing(false);
592

    
593
                    ///vista.hideConsole();
594
                }
595
            } else {
596

    
597
                     JFileChooser jfc = new JFileChooser();
598
                 SimpleFileFilter filterShp = new SimpleFileFilter("dbf",
599
                         "Ficheros .dbf");
600
                 jfc.setFileFilter(filterShp);
601

    
602
                 if (jfc.showSaveDialog(
603
                             (Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
604
                     File newFile = jfc.getSelectedFile();
605

    
606
                     IEditableSource ies=getModel().getModelo();
607
                     DbfWriter writer = (DbfWriter)LayerFactory.getWM().getWriter("DBF Writer");
608
                     writer.initialize(newFile,getModel().getModelo());
609
                     ies.stopEdition(writer);
610

    
611
                     ///vista.hideConsole();
612
                 }
613

    
614
/*
615
                GdbmsWriter gdbmswriter = new GdbmsWriter(getModel().getModelo()
616
                                                              .getRecordset()
617
                                                              .getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER));
618
                gdbmswriter.preProcess();
619

620
                for (int i = 0; i < getModel().getModelo().getRowCount();
621
                        i++) {
622
                    gdbmswriter.process(getModel().getModelo().getRow(i));
623
                }
624

625
                gdbmswriter.postProcess();
626
  */
627
            }
628

    
629
            //layer.setSource(getModel().getModelo().getOriginalAdapter());
630
            //layer.setEditing(false);
631
            //vista.hideConsole();
632
            //}
633
        } catch (Exception e) {
634
            NotificationManager.addError("No se pudo guardar la edici?n", e);
635
        }
636
    }
637

    
638
    /**
639
     * DOCUMENT ME!
640
     *
641
     * @param index DOCUMENT ME!
642
     */
643
    public void hideColumns(int[] index) {
644
        // TODO Auto-generated method stub
645
    }
646

    
647
    /**
648
     * DOCUMENT ME!
649
     *
650
     * @param index DOCUMENT ME!
651
     */
652
    public void setUneditableColumns(int[] index) {
653
        // TODO Auto-generated method stub
654
    }
655

    
656
    /**
657
     * DOCUMENT ME!
658
     *
659
     * @param numColumns DOCUMENT ME!
660
     * @param values DOCUMENT ME!
661
     */
662
    public void setDefaultValues(int[] numColumns, Value[] values) {
663
        // TODO Auto-generated method stub
664
    }
665

    
666
    /**
667
     * DOCUMENT ME!
668
     *
669
     * @return DOCUMENT ME!
670
     */
671
    public Value getDefaultValue() {
672
        // TODO Auto-generated method stub
673
        return null;
674
    }
675

    
676
    /**
677
     * DOCUMENT ME!
678
     *
679
     * @return DOCUMENT ME!
680
     */
681
    public int[] getSelectedColumns() {
682
        return null;
683
    }
684

    
685
    /**
686
     * DOCUMENT ME!
687
     *
688
     * @throws IOException DOCUMENT ME!
689
     */
690
    public void cancelEditing() throws IOException {
691
        getModel().getModelo().cancelEdition();
692
    }
693

    
694
    /**
695
     * DOCUMENT ME!
696
     *
697
     * @return DOCUMENT ME!
698
     */
699
    public boolean isEditing() {
700
        return getModel().getModelo().isEditing();
701
    }
702

    
703
    /**
704
     * DOCUMENT ME!
705
     */
706
    public void refresh() {
707
        updating = true;
708
        ((DataSourceDataModel) getTable().getModel()).fireTableDataChanged();
709
        updating = false;
710
        updateSelection();
711
    }
712

    
713
    /**
714
     * Add the rows that are passed like parameter and if parameter is null a
715
     * row is added empties.
716
     *
717
     * @param rows Rows to add or null.
718
     *
719
     * @throws DriverIOException
720
     * @throws IOException
721
     */
722
    public void addRow(IRow[] rows) throws DriverIOException, IOException {
723
        try {
724
            if (rows == null) {
725
                    if (getModel().getAssociatedTable()!=null){
726
                            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa.");
727
                            return;
728
                    }
729
                IRow row;
730
                int numAttr=getModel().getModelo()
731
                        .getRecordset()
732
                        .getFieldCount();
733
                    Value[] values=new Value[numAttr];
734
                    for (int i=0;i<numAttr;i++){
735
                            values[i]=ValueFactory.createNullValue();
736
                    }
737
                row = new DefaultRow(values);
738
                getModel().getModelo().addRow(row,"Fila en blanco");
739
            } else {
740
                    getModel().getModelo().startComplexRow();
741
                for (int i = 0; i < rows.length; i++) {
742
                    getModel().getModelo().addRow(((IRowEdited) rows[i]).getLinkedRow(),"Pegar filas");
743
                }
744
                getModel().getModelo().endComplexRow();
745
            }
746
        } catch (DriverException e) {
747
            // TODO Auto-generated catch block
748
            e.printStackTrace();
749
        } catch (DriverLoadException e) {
750
            // TODO Auto-generated catch block
751
            e.printStackTrace();
752
        }
753

    
754
        refresh();
755
    }
756

    
757
    /**
758
     * Copy in the arraylist the rows that be selected just then.
759
     *
760
     * @throws DriverIOException
761
     * @throws IOException
762
     */
763
    public void copyRow() throws DriverIOException, IOException {
764
        int[] index = getSelectedRowIndices();
765
        rowsCopied = new IRow[index.length];
766

    
767
        for (int i = 0; i < index.length; i++) {
768
            rowsCopied[i] = getModel().getModelo().getRow(index[i]);
769
        }
770
    }
771

    
772
    /**
773
     * Cut the rows that be selected just then.
774
     *
775
     * @throws DriverIOException
776
     * @throws IOException
777
     */
778
    public void cutRow() throws DriverIOException, IOException {
779
        int[] index = getSelectedRowIndices();
780
        rowsCopied = new IRow[index.length];
781

    
782
        for (int i = 0; i < index.length; i++) {
783
            rowsCopied[i] = getModel().getModelo().getRow(index[i]);
784
        }
785

    
786
        removeRow();
787
    }
788

    
789
    /**
790
     * Remove in the rows that be selected just then.
791
     *
792
     * @throws DriverIOException
793
     * @throws IOException
794
     */
795
    public void removeRow() throws DriverIOException, IOException {
796
        int[] index = getSelectedRowIndices();
797
        getModel().getModelo().startComplexRow();
798
        for (int i = 0; i < index.length; i++) {
799
            getModel().getModelo().removeRow(index[i],"Eliminar fila");
800
        }
801
        getModel().getModelo().endComplexRow();
802
        refresh();
803
    }
804

    
805
    /**
806
     * DOCUMENT ME!
807
     */
808
    public void addColumn() {
809
        // TODO Auto-generated method stub
810
        refresh();
811
    }
812

    
813
    /**
814
     * DOCUMENT ME!
815
     */
816
    public void removeColumn() {
817
        // TODO Auto-generated method stub
818
        refresh();
819
    }
820

    
821
    /**
822
     * Return if we have rows copied or not.
823
     *
824
     * @return True if we have rows copied.
825
     */
826
    public boolean isCopied() {
827
        return (rowsCopied != null);
828
    }
829

    
830
    /**
831
     * Paste the arraylist rows.
832
     *
833
     * @throws DriverIOException
834
     * @throws IOException
835
     */
836
    public void pasteRow() throws DriverIOException, IOException {
837
        addRow(rowsCopied);
838
    }
839

    
840
    /**
841
     * DOCUMENT ME!
842
     */
843
    public void viewActivated() {
844
        //if (isEditing() && getModel().getModelo() instanceof VectorialEditableAdapter)
845
        updateSelection();
846
    }
847

    
848
    /**
849
     * DOCUMENT ME!
850
     */
851
    public void viewClosed() {
852
        // TODO Auto-generated method stub
853
    }
854

    
855
    /**
856
     * DOCUMENT ME!
857
     *
858
     * @author Fernando Gonz?lez Cort?s
859
     */
860
    public class DataSourceDataModel extends AbstractTableModel {
861
        //private SelectableDataSource dataSource;
862
        private IEditableSource des;
863
        private String[] aliases;
864
        private int[] mapping;
865

    
866
        /**
867
         * Crea un nuevo DataSourceDataModel.
868
         *
869
         * @param pt DOCUMENT ME!
870
         */
871
        public DataSourceDataModel(ProjectTable pt) {
872
            this.des = pt.getModelo();
873

    
874
            //try {
875
            //dataSource = pt.getModelo().getRecordset();
876
            //} catch (DriverLoadException e) {
877
            // TODO Auto-generated catch block
878
            //        e.printStackTrace();
879
            //}
880
            aliases = pt.getAliases();
881
            mapping = pt.getMapping();
882
        }
883

    
884
        /**
885
         * Returns the name of the field.
886
         *
887
         * @param col index of field
888
         *
889
         * @return Name of field
890
         */
891
        public String getColumnName(int col) {
892
            return aliases[mapping[col]];
893
        }
894

    
895
        /**
896
         * Returns the number of fields.
897
         *
898
         * @return number of fields
899
         */
900
        public int getColumnCount() {
901
            return mapping.length;
902
        }
903

    
904
        /**
905
         * Returns number of rows.
906
         *
907
         * @return number of rows.
908
         */
909
        public int getRowCount() {
910
            try {
911
                return (int) des.getRowCount();
912
            } catch (DriverIOException e) {
913
                return 0;
914
            } catch (IOException e) {
915
                return 0;
916
            }
917
        }
918

    
919
        /**
920
         * DOCUMENT ME!
921
         *
922
         * @param row DOCUMENT ME!
923
         * @param col DOCUMENT ME!
924
         *
925
         * @return DOCUMENT ME!
926
         */
927
        public Object getValueAt(int row, int col) {
928
            SelectableDataSource sds=des.getRecordset();
929

    
930
                try {
931
                                if (orderIndexes != null) {
932
                                        return sds.getFieldValue(row, col);
933
                                } else {
934
                                        Object obj = sds.getFieldValue(row, col);
935
                                        return obj;
936
                                }
937
                        } catch (DriverException e) {
938
                                return ValueFactory.createValue("").toString();
939
                        }
940
            }
941

    
942
        /**
943
         * DOCUMENT ME!
944
         *
945
         * @param rowIndex DOCUMENT ME!
946
         * @param columnIndex DOCUMENT ME!
947
         *
948
         * @return DOCUMENT ME!
949
         */
950
        public boolean isCellEditable(int rowIndex, int columnIndex) {
951
            return isEditing();
952
        }
953

    
954
        /**
955
         * DOCUMENT ME!
956
         *
957
         * @param aValue DOCUMENT ME!
958
         * @param rowIndex DOCUMENT ME!
959
         * @param columnIndex DOCUMENT ME!
960
         *
961
         * @throws RuntimeException DOCUMENT ME!
962
         */
963
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
964
            Value v;
965

    
966
            try {
967
                v = ValueFactory.createValueByType(aValue.toString(),
968
                        des.getRecordset().getFieldType(columnIndex));
969

    
970
                IRowEdited row = des.getRow(rowIndex); //.getAttribute(columnIndex);
971
                Value[] values = row.getAttributes();
972
                values[columnIndex] = v;
973

    
974
                IRow newRow = null;
975

    
976
                if (row.getLinkedRow() instanceof IFeature) {
977
                    IGeometry geometry = ((DefaultFeature) row.getLinkedRow()).getGeometry();
978
                    newRow = new DefaultFeature(geometry, values);
979
                } else {
980
                    newRow = new DefaultRow(values);
981
                }
982

    
983
                des.modifyRow(rowIndex, newRow,"Editar valor");
984
            } catch (DriverException e1) {
985
                throw new RuntimeException(e1);
986
            } catch (ParseException e) {
987
                throw new RuntimeException(e);
988
            } catch (DriverLoadException e) {
989
                throw new RuntimeException(e);
990
            } catch (DriverIOException e) {
991
                throw new RuntimeException(e);
992
            } catch (IOException e) {
993
                throw new RuntimeException(e);
994
            }
995
        }
996
    }
997
    public class ColumnModel extends DefaultTableColumnModel{
998
            private ProjectTable pt;
999
                public ColumnModel() {
1000
                }
1001

    
1002
                public void setDataModel(ProjectTable model) {
1003
                        this.pt=model;
1004
                }
1005

    
1006
                public void addColumn(TableColumn tc) {
1007
                        super.addColumn(tc);
1008
                }
1009

    
1010
                public TableColumn getColumn(int i) {
1011
                        TableColumn tc=super.getColumn(i);
1012
                        return tc;
1013
                }
1014

    
1015
                public int getColumnCount() {
1016
                        return super.getColumnCount();
1017
                }
1018

    
1019
                public void removeColumn(TableColumn arg0) {
1020
                        super.removeColumn(arg0);
1021
                }
1022

    
1023
                public int getColumnIndex(Object arg0) {
1024
                        return super.getColumnIndex(arg0);
1025
                }
1026

    
1027
                public int getColumnIndexAtX(int arg0) {
1028
                        return super.getColumnIndexAtX(arg0);
1029
                }
1030

    
1031
                public int getColumnMargin() {
1032
                        return super.getColumnMargin();
1033
                }
1034

    
1035
                public Enumeration getColumns() {
1036
                        return super.getColumns();
1037
                }
1038

    
1039
                public int getTotalColumnWidth() {
1040
                        int w=0;
1041
                        for (int i=0;i<getColumnCount();i++){
1042
                                w+=pt.getColumn(i).getWidth();
1043
                        }
1044
                        return super.getTotalColumnWidth();
1045
                }
1046

    
1047
                public void moveColumn(int arg0, int arg1) {
1048
                        super.moveColumn(arg0, arg1);
1049
                }
1050

    
1051
                public void addColumnModelListener(TableColumnModelListener arg0) {
1052
                        super.addColumnModelListener(arg0);
1053
                }
1054

    
1055
                protected ListSelectionModel createSelectionModel() {
1056
                        return super.createSelectionModel();
1057
                }
1058

    
1059
                protected void fireColumnAdded(TableColumnModelEvent arg0) {
1060
                        super.fireColumnAdded(arg0);
1061
                }
1062

    
1063
                protected void fireColumnMarginChanged() {
1064
                        super.fireColumnMarginChanged();
1065
                }
1066

    
1067
                protected void fireColumnMoved(TableColumnModelEvent arg0) {
1068
                        super.fireColumnMoved(arg0);
1069
                }
1070

    
1071
                protected void fireColumnRemoved(TableColumnModelEvent arg0) {
1072
                        super.fireColumnRemoved(arg0);
1073
                }
1074

    
1075
                protected void fireColumnSelectionChanged(ListSelectionEvent arg0) {
1076
                        super.fireColumnSelectionChanged(arg0);
1077
                }
1078

    
1079
                public TableColumnModelListener[] getColumnModelListeners() {
1080
                        return super.getColumnModelListeners();
1081
                }
1082

    
1083
                public boolean getColumnSelectionAllowed() {
1084
                        return super.getColumnSelectionAllowed();
1085
                }
1086

    
1087
                public EventListener[] getListeners(Class arg0) {
1088
                        return super.getListeners(arg0);
1089
                }
1090

    
1091
                public int getSelectedColumnCount() {
1092
                        return super.getSelectedColumnCount();
1093
                }
1094

    
1095
                public int[] getSelectedColumns() {
1096
                        return super.getSelectedColumns();
1097
                }
1098

    
1099
                public ListSelectionModel getSelectionModel() {
1100
                        return super.getSelectionModel();
1101
                }
1102

    
1103
                public void propertyChange(PropertyChangeEvent arg0) {
1104
                        super.propertyChange(arg0);
1105
                        int w=((Integer)arg0.getNewValue()).intValue();
1106
                        if (arg0.getSource() instanceof TableColumn && (w!=75)){
1107
                                TableColumn tc=(TableColumn)arg0.getSource();
1108
                                Column column=pt.getColumn(tc.getModelIndex());
1109
                                column.setWidth(w);
1110
                                System.out.println("Index guardar = "+tc.getModelIndex()+" , "+"Anchura = "+w);
1111
                        }
1112

    
1113
                }
1114
                protected void recalcWidthCache() {
1115
                        super.recalcWidthCache();
1116
                }
1117

    
1118
                public void removeColumnModelListener(TableColumnModelListener arg0) {
1119
                        super.removeColumnModelListener(arg0);
1120
                }
1121

    
1122
                public void setColumnMargin(int arg0) {
1123
                        super.setColumnMargin(arg0);
1124
                }
1125

    
1126
                public void setColumnSelectionAllowed(boolean arg0) {
1127
                        super.setColumnSelectionAllowed(arg0);
1128
                }
1129

    
1130
                public void setSelectionModel(ListSelectionModel arg0) {
1131
                        super.setSelectionModel(arg0);
1132
                }
1133

    
1134
                public void valueChanged(ListSelectionEvent arg0) {
1135
                        super.valueChanged(arg0);
1136
                }
1137

    
1138
                public int hashCode() {
1139
                        return super.hashCode();
1140
                }
1141

    
1142
                protected void finalize() throws Throwable {
1143
                        super.finalize();
1144
                }
1145

    
1146
                protected Object clone() throws CloneNotSupportedException {
1147
                        return super.clone();
1148
                }
1149

    
1150
                public boolean equals(Object arg0) {
1151
                        return super.equals(arg0);
1152
                }
1153

    
1154
                public String toString() {
1155
                        return super.toString();
1156
                }
1157
        }
1158
    /**
1159
     * DOCUMENT ME!
1160
     *
1161
     * @author Vicente Caballero Navarro
1162
     */
1163
    public class TableSelectionListener implements SelectionListener {
1164
        /**
1165
         * @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
1166
         */
1167
        public void selectionChanged(SelectionEvent e) {
1168
            updateSelection();
1169
            Table.this.repaint();
1170
        }
1171
    }
1172

    
1173
    /**
1174
     * DOCUMENT ME!
1175
     *
1176
     * @author Vicente Caballero Navarro
1177
     */
1178
    private class MouseHandler extends MouseAdapter {
1179
        /**
1180
         * DOCUMENT ME!
1181
         *
1182
         * @param e DOCUMENT ME!
1183
         */
1184
        public void mouseClicked(MouseEvent e) {
1185
            /* JTableHeader h = (JTableHeader) e.getSource();
1186
               TableColumnModel columnModel = h.getColumnModel();
1187
               int viewColumn = columnModel.getColumnIndexAtX(e.getX());
1188
               int column = columnModel.getColumn(viewColumn).getModelIndex();
1189
               if (column != -1) {
1190
               }*/
1191
        }
1192
    }
1193

    
1194
    /**
1195
     * DOCUMENT ME!
1196
     *
1197
     * @author Vicente Caballero Navarro
1198
     */
1199
    private class MouseRow extends MouseAdapter {
1200
        /**
1201
         * DOCUMENT ME!
1202
         *
1203
         * @param e DOCUMENT ME!
1204
         */
1205
        public void mouseClicked(MouseEvent e) {
1206
            PluginServices.getMainFrame().enableControls();
1207

    
1208
            if (e.getButton() == MouseEvent.BUTTON3) {
1209
                new PopupMenu(e.getPoint());
1210
            }
1211
        }
1212
    }
1213

    
1214
        public void commandRepaint() {
1215
                refresh();
1216
        }
1217

    
1218
        public void commandRefresh() {
1219
                commandRepaint();
1220
        }
1221

    
1222
        public void toPalette() {
1223
                isPalette=true;
1224
                m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
1225
                                ViewInfo.MODELESSDIALOG | ViewInfo.RESIZABLE | ViewInfo.PALETTE);
1226
        PluginServices.getMDIManager().changeViewInfo(this,getViewInfo());
1227
        }
1228

    
1229
        public void restore() {
1230
                isPalette=false;
1231
                m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
1232
                    ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE);
1233
                PluginServices.getMDIManager().changeViewInfo(this,getViewInfo());
1234
        }
1235

    
1236
        public boolean isPalette() {
1237
                return isPalette;
1238
        }
1239
}