Statistics
| Revision:

svn-gvsig-desktop / tags / v10_RC2c / applications / appgvSIG / src / com / iver / cit / gvsig / gui / dialogs / DlgFieldManager.java @ 8745

History | View | Annotate | Download (17.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.gui.dialogs;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.FlowLayout;
46
import java.awt.GridLayout;
47
import java.awt.event.ActionListener;
48
import java.sql.Types;
49
import java.text.ParseException;
50
import java.util.ArrayList;
51

    
52
import javax.swing.DefaultCellEditor;
53
import javax.swing.JComboBox;
54
import javax.swing.JLabel;
55
import javax.swing.JOptionPane;
56
import javax.swing.JPanel;
57
import javax.swing.JScrollPane;
58
import javax.swing.JTable;
59
import javax.swing.ListSelectionModel;
60
import javax.swing.event.ListSelectionEvent;
61
import javax.swing.event.ListSelectionListener;
62
import javax.swing.table.AbstractTableModel;
63
import javax.swing.table.DefaultTableModel;
64
import javax.swing.table.TableColumn;
65
import javax.swing.table.TableModel;
66

    
67
import org.gvsig.gui.beans.swing.JButton;
68

    
69
import com.hardcode.driverManager.DriverLoadException;
70
import com.hardcode.gdbms.engine.data.driver.DriverException;
71
import com.iver.andami.PluginServices;
72
import com.iver.andami.messages.NotificationManager;
73
import com.iver.andami.ui.mdiManager.IWindow;
74
import com.iver.andami.ui.mdiManager.WindowInfo;
75
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
76
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
77
import com.iver.cit.gvsig.fmap.edition.EditionException;
78
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
79
import com.iver.cit.gvsig.gui.panels.FPanelCreateField;
80

    
81
public class DlgFieldManager extends JPanel implements IWindow {
82

    
83
        /**
84
         *
85
         */
86
        private static final long serialVersionUID = -4284879326692474318L;
87
        private int MAX_FIELD_LENGTH = 254;
88

    
89
        WindowInfo windowInfo = null;
90

    
91
        private JLabel jLabel = null;
92

    
93
        private JScrollPane jScrollPane = null;
94

    
95
        private JTable jTableFields = null;
96

    
97
        private JButton jBtnNewField = null;
98

    
99
        private JButton jBtnDeleteField = null;
100

    
101
        private JButton jBtnRenameField = null;
102

    
103
        private JButton jBtnOK = null;
104

    
105
        private JButton jBtnCancel = null;
106

    
107
        private FPanelCreateField panelNewField = new FPanelCreateField();
108

    
109
        // private IFieldManager fieldManager;;
110
        private EditableAdapter edAdapter = null;
111

    
112
        private JPanel jPanelButtons = null;
113

    
114
        private class MyFieldDescription {
115
                boolean isOriginal;
116

    
117
                FieldDescription fieldDesc;
118

    
119
                MyFieldDescription(FieldDescription fieldDesc, boolean isOriginal) {
120
                        this.fieldDesc = fieldDesc;
121
                        this.isOriginal = isOriginal;
122
                }
123

    
124
                FieldDescription getFieldDescription() {
125
                        return fieldDesc;
126
                }
127

    
128
                boolean isOriginal() {
129
                        return isOriginal;
130
                }
131

    
132
        }
133

    
134
        private class MyTableModel extends AbstractTableModel {
135
                IEditableSource myIes;
136

    
137
                public MyTableModel(IEditableSource ies) {
138
                        myIes = ies;
139
                }
140

    
141
                public int getColumnCount() {
142
                        return 5;
143
                }
144

    
145
                public int getRowCount() {
146
                        try {
147
                                return myIes.getRecordset().getFieldCount();
148
                        } catch (DriverLoadException e) {
149
                                e.printStackTrace();
150
                        } catch (DriverException e) {
151
                                e.printStackTrace();
152
                        }
153
                        return 0;
154
                }
155

    
156
                public Object getValueAt(int rowIndex, int columnIndex) {
157
                        FieldDescription[] myFields;
158
                        try {
159
                                myFields = myIes.getFieldsDescription();
160
                                FieldDescription aux = myFields[rowIndex];
161
                                switch (columnIndex) {
162
                                case 0:
163
                                        return aux.getFieldAlias();
164
                                case 1:
165
                                        String strType = null;
166
                                        int type = aux.getFieldType();
167
                                        if (type == Types.VARCHAR)
168
                                                strType = "String";
169
                                        if (type == Types.DOUBLE)
170
                                                strType = "Double";
171
                                        if (type == Types.INTEGER)
172
                                                strType = "Integer";
173
                                        if (type == Types.BOOLEAN)
174
                                                strType = "Boolean";
175
                                        if (type == Types.DATE)
176
                                                strType = "Date";
177

    
178
                                        return strType;
179
                                case 2:
180
                                        return new Integer(aux.getFieldLength());
181
                                case 3:
182
                                        return new Integer(aux.getFieldDecimalCount());
183
                                case 4:
184
                                        return aux.getDefaultValue();
185

    
186
                                }
187
                        } catch (DriverLoadException e) {
188
                                e.printStackTrace();
189
                        }
190

    
191
                        return null;
192
                }
193

    
194
                public Class getColumnClass(int columnIndex) {
195
                        // TODO Auto-generated method stub
196
                        return super.getColumnClass(columnIndex);
197
                }
198

    
199
                public String getColumnName(int column) {
200
                        switch (column) {
201
                        case 0:
202
                                return PluginServices.getText(this, "field_name");
203
                        case 1:
204
                                return PluginServices.getText(this, "field_type");
205
                        case 2:
206
                                return PluginServices.getText(this, "field_length");
207
                        case 3:
208
                                return PluginServices.getText(this, "field_decimal_count");
209
                        case 4:
210
                                return PluginServices.getText(this, "field_default_value");
211

    
212
                        }
213
                        return super.getColumnName(column);
214
                }
215

    
216
                public boolean isCellEditable(int rowIndex, int columnIndex) {
217
                        return false;
218

    
219
                }
220

    
221
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
222
                        FieldDescription[] myFields;
223
                        try {
224
                                myFields = myIes.getRecordset().getFieldsDescription();
225
                                FieldDescription fDesc = myFields[rowIndex];
226

    
227
                                switch (columnIndex) {
228
                                case 0:
229
                                        fDesc.setFieldAlias((String) aValue);
230
                                        break;
231
                                case 1:
232
                                        String strType = (String) aValue;
233
                                        if (strType.equals("String"))
234
                                                fDesc.setFieldType(Types.VARCHAR);
235
                                        if (strType.equals("Double"))
236
                                                fDesc.setFieldType(Types.DOUBLE);
237
                                        if (strType.equals("Integer"))
238
                                                fDesc.setFieldType(Types.INTEGER);
239
                                        if (strType.equals("Boolean"))
240
                                                fDesc.setFieldType(Types.BOOLEAN);
241
                                        if (strType.equals("Date"))
242
                                                fDesc.setFieldType(Types.DATE);
243
                                        break;
244
                                case 2:
245
                                        int fieldLength = ((Integer) aValue).intValue();
246
                                        fDesc.setFieldLength(fieldLength);
247

    
248
                                        // TODO: HACERLO BIEN
249
                                        if (fDesc.getFieldType() == Types.VARCHAR)
250
                                                fDesc.setFieldDecimalCount(5);
251

    
252
                                }
253
                        } catch (DriverLoadException e) {
254
                                e.printStackTrace();
255
                        } catch (DriverException e) {
256
                                e.printStackTrace();
257
                        }
258

    
259
                }
260

    
261
        }
262

    
263
        /**
264
         * This method initializes
265
         *
266
         */
267
        public DlgFieldManager(IEditableSource ies) {
268
                super();
269
                this.edAdapter = (EditableAdapter) ies;
270
                initialize();
271
                // Add a new row
272
                TableModel tm;
273
                try {
274
                        tm = new MyTableModel(ies);
275
                        getJTableFields().setModel(tm);
276
                        // Esto lo a?ado aqu? porque si no tiene registros, no hace caso.
277
                        // (Por eso no
278
                        // lo pongo en getJTable()
279
                        TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1);
280
                        JComboBox comboBox = new JComboBox();
281
                        comboBox.addItem("Boolean");
282
                        comboBox.addItem("Date");
283
                        comboBox.addItem("Integer");
284
                        comboBox.addItem("Double");
285
                        comboBox.addItem("String");
286
                        typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
287

    
288

    
289
                } catch (DriverLoadException e) {
290
                        // TODO Auto-generated catch block
291
                        e.printStackTrace();
292
                }
293

    
294
        }
295

    
296
        /**
297
         * This method initializes this
298
         *
299
         */
300
        private void initialize() {
301
                FlowLayout flowLayout = new FlowLayout();
302
                flowLayout.setVgap(5);
303
                flowLayout.setHgap(0);
304
                BorderLayout borderLayout = new BorderLayout();
305
                borderLayout.setHgap(15);
306
                borderLayout.setVgap(15);
307
                jLabel = new JLabel();
308
                jLabel.setText(PluginServices.getText(this, "add_delete_edit_fields")+": ");
309
                this.setLayout(borderLayout);
310
                this.setSize(new java.awt.Dimension(663,404));
311
                this.setPreferredSize(new java.awt.Dimension(getWidth(), getHeight()));
312
                JPanel jPanelNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
313
                jPanelNorth.add(jLabel);
314
                this.add(jPanelNorth, BorderLayout.NORTH);
315
                JPanel jPanelCenter = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
316
                jPanelCenter.add(getJScrollPane());
317
                this.add(jPanelCenter, BorderLayout.CENTER);
318

    
319
                this.add(getJPanelButtons(), BorderLayout.EAST);
320
                JPanel jPanelSouth = new JPanel();
321
                jPanelSouth.setLayout(flowLayout);
322
                jPanelSouth.add(getJBtnOK(), null);
323
                jPanelSouth.add(getJBtnCancel(), null);
324
                this.add(jPanelSouth, BorderLayout.SOUTH);
325

    
326
        }
327

    
328
        public WindowInfo getWindowInfo() {
329
                if (windowInfo == null) {
330
                        windowInfo = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.PALETTE
331
                                        | WindowInfo.RESIZABLE);
332
                        windowInfo.setTitle(PluginServices.getText(this, "field_manager"));
333
                        windowInfo.setHeight(this.getHeight());
334
                        windowInfo.setWidth(this.getWidth());
335
                }
336
                return windowInfo;
337
        }
338

    
339
        /**
340
         * Convierte lo que hay en la tabla en una definici?n de campos adecuada
341
         * para crear un LayerDefinition
342
         *
343
         * @return
344
         */
345
        public FieldDescription[] getFieldsDescription() {
346
                DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
347
                FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()];
348

    
349
                for (int i = 0; i < tm.getRowCount(); i++) {
350
                        fieldsDesc[i] = new FieldDescription();
351
                        fieldsDesc[i].setFieldName((String) tm.getValueAt(i, 0));
352
                        String strType = (String) tm.getValueAt(i, 1);
353
                        if (strType.equals("String"))
354
                                fieldsDesc[i].setFieldType(Types.VARCHAR);
355
                        if (strType.equals("Double"))
356
                                fieldsDesc[i].setFieldType(Types.DOUBLE);
357
                        if (strType.equals("Integer"))
358
                                fieldsDesc[i].setFieldType(Types.INTEGER);
359
                        if (strType.equals("Boolean"))
360
                                fieldsDesc[i].setFieldType(Types.BOOLEAN);
361
                        if (strType.equals("Date"))
362
                                fieldsDesc[i].setFieldType(Types.DATE);
363
                        int fieldLength = Integer.parseInt((String) tm.getValueAt(i, 2));
364
                        fieldsDesc[i].setFieldLength(fieldLength);
365

    
366
                        // TODO: HACERLO BIEN
367
                        if (strType.equals("Double"))
368
                                fieldsDesc[i].setFieldDecimalCount(5);
369

    
370
                }
371

    
372
                return fieldsDesc;
373
        }
374

    
375
        /**
376
         * This method initializes jScrollPane
377
         *
378
         * @return javax.swing.JScrollPane
379
         */
380
        private JScrollPane getJScrollPane() {
381
                if (jScrollPane == null) {
382
                        jScrollPane = new JScrollPane();
383
                        jScrollPane.setPreferredSize(new java.awt.Dimension(482,350));
384
                        jScrollPane.setViewportView(getJTableFields());
385
                }
386
                return jScrollPane;
387
        }
388

    
389
        /**
390
         * This method initializes jTableFields
391
         *
392
         * @return javax.swing.JTable
393
         */
394
        private JTable getJTableFields() {
395
                if (jTableFields == null) {
396
                        jTableFields = new JTable();
397
                        jTableFields
398
                                        .setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
399

    
400
                        jTableFields.setColumnSelectionAllowed(false);
401
                        // Ask to be notified of selection changes.
402
                        ListSelectionModel rowSM = jTableFields.getSelectionModel();
403
                        rowSM.addListSelectionListener(new ListSelectionListener() {
404
                                public void valueChanged(ListSelectionEvent e) {
405
                                        // Ignore extra messages.
406
                                        if (e.getValueIsAdjusting())
407
                                                return;
408

    
409
                                        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
410
                                        if (lsm.isSelectionEmpty()) {
411
                                                // no rows are selected
412
                                                jBtnDeleteField.setEnabled(false);
413
                                        } else {
414
                                                jBtnDeleteField.setEnabled(true);
415
                                        }
416
                                        if (jTableFields.getSelectedRows().length != 1)
417
                                        {
418
                                                getJBtnRenameField().setEnabled(false);
419
                                        }
420
                                        else
421
                                        {
422
                                                getJBtnRenameField().setEnabled(true);
423
                                        }
424

    
425
                                }
426
                        });
427

    
428
                }
429
                return jTableFields;
430
        }
431

    
432
        /**
433
         * This method initializes jBtnNewField
434
         *
435
         * @return javax.swing.JButton
436
         */
437
        private JButton getJBtnNewField() {
438
                if (jBtnNewField == null) {
439
                        jBtnNewField = new JButton();
440
                        jBtnNewField.setText(PluginServices.getText(this, "new_field"));
441
                        jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
442
                                private ArrayList tempFieldNames = new ArrayList();
443

    
444
                                {
445
                                        try{
446
                                                String[] f = edAdapter.getRecordset().getFieldNames();
447
                                                for (int i = 0; i < f.length; i++) {
448
                                                        tempFieldNames.add(f[i]);
449
                                                }
450
                                        } catch (Exception ex) {}
451
                                }
452

    
453
                                public void actionPerformed(java.awt.event.ActionEvent e) {
454

    
455
                                        ActionListener okAction;
456
                                        okAction = new java.awt.event.ActionListener() {
457
                                                public void actionPerformed(java.awt.event.ActionEvent e){
458
                                                        try {
459
                                                                FieldDescription fld = panelNewField.getFieldDescription();
460
                                                                if (fld.getFieldType() == Types.VARCHAR && fld.getFieldLength()>MAX_FIELD_LENGTH) {
461
                                                                        JOptionPane.showMessageDialog(DlgFieldManager.this, PluginServices.getText(this, "max_length_is") + ":" + MAX_FIELD_LENGTH);
462
                                                                        fld.setFieldLength(MAX_FIELD_LENGTH);
463
                                                                }
464
                                                                if (edAdapter.getRecordset().getFieldIndexByName(fld.getFieldAlias()) != -1)
465
                                                                {
466
                                                                        JOptionPane.showMessageDialog(
467
                                                                                        null,
468
                                                                                        PluginServices.getText(this, "field_already_exists"));
469
                                                                        return;
470
                                                                }
471
                                                                tempFieldNames.add(fld.getFieldAlias());
472

    
473
                                                                edAdapter.addField(fld);
474
                                                                jTableFields.revalidate();
475
                                                                PluginServices.getMDIManager().closeWindow(panelNewField);
476
                                                        } catch (EditionException e1) {
477
                                                                NotificationManager.addError(e1);
478
                                                        } catch (ParseException e2) {
479
                                                                NotificationManager.addError(e2);
480
                                                        } catch (DriverLoadException e3) {
481
                                                                NotificationManager.addError(e3);
482
                                                        } catch (DriverException e3) {
483
                                                                NotificationManager.addError(e3);
484
                                                        }
485

    
486

    
487
                                                }
488
                                        };
489
                                        panelNewField.setOkAction(okAction);
490
                                        String[] names = (String[]) tempFieldNames.toArray(new String[0]);
491
                                        panelNewField.setCurrentFieldNames(names);
492
                                        panelNewField = (FPanelCreateField) PluginServices.getMDIManager().addWindow(panelNewField);
493
                                }
494
                        });
495
                }
496
                return jBtnNewField;
497
        }
498

    
499
        /**
500
         * This method initializes jButton
501
         *
502
         * @return javax.swing.JButton
503
         */
504
        private JButton getJBtnDelete() {
505
                if (jBtnDeleteField == null) {
506
                        jBtnDeleteField = new JButton();
507
                        jBtnDeleteField.setText(PluginServices.getText(this, "delete_field"));
508
                        jBtnDeleteField
509
                                        .addActionListener(new java.awt.event.ActionListener() {
510
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
511
                                                        int[] selecteds = jTableFields.getSelectedRows();
512
                                                        TableModel tm = jTableFields.getModel();
513

    
514

    
515
                                                        for (int i = selecteds.length - 1; i >= 0; i--)
516
                                                        {
517
                                                                String fieldName = (String) tm.getValueAt(selecteds[i],0);
518
                                                                try {
519
                                                                        edAdapter.removeField(fieldName);
520
                                                                } catch (EditionException e1) {
521
                                                                        // TODO Auto-generated catch block
522
                                                                        e1.printStackTrace();
523
                                                                }
524
                                                        }
525
                                                        jTableFields.revalidate();
526

    
527
                                                }
528
                                        });
529
                }
530
                return jBtnDeleteField;
531
        }
532

    
533
        /**
534
         * This method initializes jBtnRenameField
535
         *
536
         * @return javax.swing.JButton
537
         */
538
        private JButton getJBtnRenameField() {
539
                if (jBtnRenameField == null) {
540
                        jBtnRenameField = new JButton();
541
                        jBtnRenameField.setText(PluginServices.getText(this, "rename_field"));
542
                        jBtnRenameField.addActionListener(new java.awt.event.ActionListener() {
543
                                public void actionPerformed(java.awt.event.ActionEvent e) {
544
                                        int[] selecteds = jTableFields.getSelectedRows();
545
                                        TableModel tm = jTableFields.getModel();
546

    
547

    
548
                                        for (int i = selecteds.length - 1; i >= 0; i--)
549
                                        {
550
                                                String fieldName = (String) tm.getValueAt(selecteds[i],0);
551
                                                try {
552
                                                        String newName = JOptionPane.showInputDialog(
553
                                                                        (Component) PluginServices.getMDIManager().getActiveWindow(),
554
                                                                        PluginServices.getText(this, "please_insert_new_field_name"),
555
                                                                        fieldName
556
                                                                        );
557
                                                        if (newName == null) return;
558
                                                        if (edAdapter.getRecordset().getFieldIndexByName(newName) != -1)
559
                                                        {
560
                                                                JOptionPane.showMessageDialog(
561
                                                                                null,
562
                                                                                PluginServices.getText(this, "field_already_exists"));
563
                                                                return;
564
                                                        }
565

    
566

    
567
                                                        edAdapter.renameField(fieldName, newName);
568
                                                } catch (EditionException e1) {
569
                                                        // TODO Auto-generated catch block
570
                                                        e1.printStackTrace();
571
                                                } catch (DriverLoadException e1) {
572
                                                        // TODO Auto-generated catch block
573
                                                        e1.printStackTrace();
574
                                                } catch (DriverException e1) {
575
                                                        // TODO Auto-generated catch block
576
                                                        e1.printStackTrace();
577
                                                }
578
                                        }
579
                                        jTableFields.repaint();
580

    
581
                                }
582
                        });
583
                }
584
                return jBtnRenameField;
585
        }
586

    
587
        /**
588
         * This method initializes jBtnOK
589
         *
590
         * @return javax.swing.JButton
591
         */
592
        private JButton getJBtnOK() {
593
                if (jBtnOK == null) {
594
                        jBtnOK = new JButton();
595
                        jBtnOK.setText(PluginServices.getText(this, "aceptar"));
596
                        jBtnOK.addActionListener(new java.awt.event.ActionListener() {
597
                                public void actionPerformed(java.awt.event.ActionEvent e) {
598
                                        PluginServices.getMDIManager().closeWindow(DlgFieldManager.this);
599
                                }
600
                        });
601
                }
602
                return jBtnOK;
603
        }
604

    
605
        /**
606
         * This method initializes jButton
607
         *
608
         * @return javax.swing.JButton
609
         */
610
        private JButton getJBtnCancel() {
611
                if (jBtnCancel == null) {
612
                        jBtnCancel = new JButton();
613
                        jBtnCancel.setText(PluginServices.getText(this, "cancelar"));
614
                        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
615
                                public void actionPerformed(java.awt.event.ActionEvent e) {
616
                                        PluginServices.getMDIManager().closeWindow(DlgFieldManager.this);
617
                                }
618
                        });
619
                        jBtnCancel.setVisible(false);
620
                }
621
                return jBtnCancel;
622
        }
623

    
624
        /**
625
         * This method initializes jPanelButtons
626
         *
627
         * @return javax.swing.JPanel
628
         */
629
        private JPanel getJPanelButtons() {
630
                if (jPanelButtons == null) {
631
                        jPanelButtons = new JPanel();
632
                        JPanel aux = new JPanel(new GridLayout(3, 1));
633
                        aux.add(getJBtnNewField());
634
                        aux.add(getJBtnRenameField());
635
                        aux.add(getJBtnDelete());
636
                        jPanelButtons.add(aux, BorderLayout.NORTH);
637

    
638
                }
639
                return jPanelButtons;
640
        }
641

    
642
} // @jve:decl-index=0:visual-constraint="12,23"
643