Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / dialogs / DlgFieldManager.java @ 6802

History | View | Annotate | Download (15.9 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.Component;
44
import java.awt.event.ActionListener;
45
import java.sql.Types;
46
import java.text.ParseException;
47

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

    
65
import com.hardcode.driverManager.DriverLoadException;
66
import com.hardcode.gdbms.engine.data.driver.DriverException;
67
import com.iver.andami.PluginServices;
68
import com.iver.andami.messages.NotificationManager;
69
import com.iver.andami.ui.mdiManager.View;
70
import com.iver.andami.ui.mdiManager.ViewInfo;
71
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
72
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
73
import com.iver.cit.gvsig.fmap.edition.EditionException;
74
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
75
import com.iver.cit.gvsig.gui.panels.FPanelCreateField;
76

    
77
public class DlgFieldManager extends JPanel implements View {
78

    
79
        /**
80
         * 
81
         */
82
        private static final long serialVersionUID = -4284879326692474318L;
83

    
84
        ViewInfo viewInfo = null;
85

    
86
        private JLabel jLabel = null;
87

    
88
        private JScrollPane jScrollPane = null;
89

    
90
        private JTable jTableFields = null;
91

    
92
        private JButton jBtnNewField = null;
93

    
94
        private JButton jBtnDeleteField = null;
95

    
96
        private JButton jBtnRenameField = null;
97

    
98
        private JButton jBtnOK = null;
99

    
100
        private JButton jBtnCancel = null;
101
        
102
        private FPanelCreateField panelNewField = new FPanelCreateField();
103

    
104
        // private IFieldManager fieldManager;;
105
        private EditableAdapter edAdapter = null;
106

    
107
        private class MyFieldDescription {
108
                boolean isOriginal;
109

    
110
                FieldDescription fieldDesc;
111

    
112
                MyFieldDescription(FieldDescription fieldDesc, boolean isOriginal) {
113
                        this.fieldDesc = fieldDesc;
114
                        this.isOriginal = isOriginal;
115
                }
116

    
117
                FieldDescription getFieldDescription() {
118
                        return fieldDesc;
119
                }
120

    
121
                boolean isOriginal() {
122
                        return isOriginal;
123
                }
124

    
125
        }
126

    
127
        private class MyTableModel extends AbstractTableModel {
128
                IEditableSource myIes;
129

    
130
                public MyTableModel(IEditableSource ies) {
131
                        myIes = ies;
132
                }
133

    
134
                public int getColumnCount() {
135
                        return 5;
136
                }
137

    
138
                public int getRowCount() {
139
                        try {
140
                                return myIes.getRecordset().getFieldCount();
141
                        } catch (DriverLoadException e) {
142
                                e.printStackTrace();
143
                        } catch (DriverException e) {
144
                                e.printStackTrace();
145
                        }
146
                        return 0;
147
                }
148

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

    
171
                                        return strType;
172
                                case 2:
173
                                        return new Integer(aux.getFieldLength());
174
                                case 3:
175
                                        return new Integer(aux.getFieldDecimalCount());
176
                                case 4:
177
                                        return aux.getDefaultValue();
178
                                        
179
                                }
180
                        } catch (DriverLoadException e) {
181
                                e.printStackTrace();
182
                        }
183

    
184
                        return null;
185
                }
186

    
187
                public Class getColumnClass(int columnIndex) {
188
                        // TODO Auto-generated method stub
189
                        return super.getColumnClass(columnIndex);
190
                }
191

    
192
                public String getColumnName(int column) {
193
                        switch (column) {
194
                        case 0:
195
                                return PluginServices.getText(this, "FieldName");
196
                        case 1:
197
                                return PluginServices.getText(this, "FieldType");
198
                        case 2:
199
                                return PluginServices.getText(this, "FieldLength");
200
                        case 3:
201
                                return PluginServices.getText(this, "FieldDecimalCount");
202
                        case 4:
203
                                return PluginServices.getText(this, "FieldDefaultValue");
204
                                
205
                        }
206
                        return super.getColumnName(column);
207
                }
208

    
209
                public boolean isCellEditable(int rowIndex, int columnIndex) {
210
                        return false;
211

    
212
                }
213

    
214
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
215
                        FieldDescription[] myFields;
216
                        try {
217
                                myFields = myIes.getRecordset().getFieldsDescription();
218
                                FieldDescription fDesc = myFields[rowIndex];
219

    
220
                                switch (columnIndex) {
221
                                case 0:
222
                                        fDesc.setFieldAlias((String) aValue);
223
                                        break;
224
                                case 1:
225
                                        String strType = (String) aValue;
226
                                        if (strType.equals("String"))
227
                                                fDesc.setFieldType(Types.VARCHAR);
228
                                        if (strType.equals("Double"))
229
                                                fDesc.setFieldType(Types.DOUBLE);
230
                                        if (strType.equals("Integer"))
231
                                                fDesc.setFieldType(Types.INTEGER);
232
                                        if (strType.equals("Boolean"))
233
                                                fDesc.setFieldType(Types.BOOLEAN);
234
                                        if (strType.equals("Date"))
235
                                                fDesc.setFieldType(Types.DATE);
236
                                        break;
237
                                case 2:
238
                                        int fieldLength = ((Integer) aValue).intValue();
239
                                        fDesc.setFieldLength(fieldLength);
240

    
241
                                        // TODO: HACERLO BIEN
242
                                        if (fDesc.getFieldType() == Types.VARCHAR)
243
                                                fDesc.setFieldDecimalCount(5);
244

    
245
                                }
246
                        } catch (DriverLoadException e) {
247
                                e.printStackTrace();
248
                        } catch (DriverException e) {
249
                                e.printStackTrace();
250
                        }
251

    
252
                }
253

    
254
        }
255

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

    
281
                        TableColumn widthColumn = jTableFields.getColumnModel()
282
                                        .getColumn(2);
283
                } catch (DriverLoadException e) {
284
                        // TODO Auto-generated catch block
285
                        e.printStackTrace();
286
                }
287

    
288
        }
289

    
290
        /**
291
         * This method initializes this
292
         * 
293
         */
294
        private void initialize() {
295
                jLabel = new JLabel();
296
                jLabel.setBounds(new java.awt.Rectangle(14, 9, 361, 34));
297
                jLabel.setText("Puede a?adir, borrar o renombrar los campos:");
298
                this.setLayout(null);
299
                this.setSize(new java.awt.Dimension(462,327));
300
                this.add(jLabel, null);
301
                this.add(getJScrollPane(), null);
302
                this.add(getJBtnNewField(), null);
303
                this.add(getJButton(), null);
304
                this.add(getJBtnRenameField(), null);
305
                this.add(getJBtnOK(), null);
306
                this.add(getJButton2(), null);
307

    
308
        }
309

    
310
        public ViewInfo getViewInfo() {
311
                if (viewInfo == null) {
312
                        viewInfo = new ViewInfo(ViewInfo.MODALDIALOG | ViewInfo.PALETTE
313
                                        | ViewInfo.RESIZABLE);
314
                }
315
                return viewInfo;
316
        }
317

    
318
        /**
319
         * Convierte lo que hay en la tabla en una definici?n de campos adecuada
320
         * para crear un LayerDefinition
321
         * 
322
         * @return
323
         */
324
        public FieldDescription[] getFieldsDescription() {
325
                DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
326
                FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()];
327

    
328
                for (int i = 0; i < tm.getRowCount(); i++) {
329
                        fieldsDesc[i] = new FieldDescription();
330
                        fieldsDesc[i].setFieldName((String) tm.getValueAt(i, 0));
331
                        String strType = (String) tm.getValueAt(i, 1);
332
                        if (strType.equals("String"))
333
                                fieldsDesc[i].setFieldType(Types.VARCHAR);
334
                        if (strType.equals("Double"))
335
                                fieldsDesc[i].setFieldType(Types.DOUBLE);
336
                        if (strType.equals("Integer"))
337
                                fieldsDesc[i].setFieldType(Types.INTEGER);
338
                        if (strType.equals("Boolean"))
339
                                fieldsDesc[i].setFieldType(Types.BOOLEAN);
340
                        if (strType.equals("Date"))
341
                                fieldsDesc[i].setFieldType(Types.DATE);
342
                        int fieldLength = Integer.parseInt((String) tm.getValueAt(i, 2));
343
                        fieldsDesc[i].setFieldLength(fieldLength);
344

    
345
                        // TODO: HACERLO BIEN
346
                        if (strType.equals("Double"))
347
                                fieldsDesc[i].setFieldDecimalCount(5);
348

    
349
                }
350

    
351
                return fieldsDesc;
352
        }
353

    
354
        /**
355
         * This method initializes jScrollPane
356
         * 
357
         * @return javax.swing.JScrollPane
358
         */
359
        private JScrollPane getJScrollPane() {
360
                if (jScrollPane == null) {
361
                        jScrollPane = new JScrollPane();
362
                        jScrollPane.setBounds(new java.awt.Rectangle(13,53,309,203));
363
                        jScrollPane.setViewportView(getJTableFields());
364
                }
365
                return jScrollPane;
366
        }
367

    
368
        /**
369
         * This method initializes jTableFields
370
         * 
371
         * @return javax.swing.JTable
372
         */
373
        private JTable getJTableFields() {
374
                if (jTableFields == null) {
375
                        jTableFields = new JTable();
376
                        jTableFields
377
                                        .setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
378

    
379
                        jTableFields.setColumnSelectionAllowed(false);
380
                        // Ask to be notified of selection changes.
381
                        ListSelectionModel rowSM = jTableFields.getSelectionModel();
382
                        rowSM.addListSelectionListener(new ListSelectionListener() {
383
                                public void valueChanged(ListSelectionEvent e) {
384
                                        // Ignore extra messages.
385
                                        if (e.getValueIsAdjusting())
386
                                                return;
387

    
388
                                        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
389
                                        if (lsm.isSelectionEmpty()) {
390
                                                // no rows are selected
391
                                                jBtnDeleteField.setEnabled(false);
392
                                        } else {
393
                                                jBtnDeleteField.setEnabled(true);
394
                                        }
395
                                        if (jTableFields.getSelectedRows().length != 1)
396
                                        {
397
                                                getJBtnRenameField().setEnabled(false);
398
                                        }
399
                                        else
400
                                        {
401
                                                getJBtnRenameField().setEnabled(true);
402
                                        }
403
                                                
404
                                }
405
                        });
406

    
407
                }
408
                return jTableFields;
409
        }
410

    
411
        /**
412
         * This method initializes jBtnNewField
413
         * 
414
         * @return javax.swing.JButton
415
         */
416
        private JButton getJBtnNewField() {
417
                if (jBtnNewField == null) {
418
                        jBtnNewField = new JButton();
419
                        jBtnNewField.setBounds(new java.awt.Rectangle(332,60,118,25));
420
                        jBtnNewField.setText(PluginServices.getText(this, "New Field"));
421
                        jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
422
                                public void actionPerformed(java.awt.event.ActionEvent e) {                                        
423
                                                                                
424
                                        ActionListener okAction;
425
                                        okAction = new java.awt.event.ActionListener() {
426
                                                public void actionPerformed(java.awt.event.ActionEvent e){
427
                                                        try {
428
                                                                FieldDescription fld = panelNewField.getFieldDescription();
429
                                                                if (edAdapter.getRecordset().getFieldIndexByName(fld.getFieldAlias()) != -1)
430
                                                                {
431
                                                                        JOptionPane.showMessageDialog(
432
                                                                                        null,
433
                                                                                        PluginServices.getText(this, "field_already_exists"));
434
                                                                        return;
435
                                                                }
436
                                                                edAdapter.addField(fld);
437
                                                                jTableFields.revalidate();
438
                                                                PluginServices.getMDIManager().closeView(panelNewField);                                                                
439
                                                        } catch (EditionException e1) {
440
                                                                NotificationManager.addError(e1);
441
                                                        } catch (ParseException e2) {
442
                                                                NotificationManager.addError(e2);
443
                                                        } catch (DriverLoadException e3) {
444
                                                                NotificationManager.addError(e3);
445
                                                        } catch (DriverException e3) {
446
                                                                NotificationManager.addError(e3);
447
                                                        }
448

    
449
                                                        
450
                                                }
451
                                        };
452
                                        panelNewField.setOkAction(okAction);
453
                                        panelNewField = (FPanelCreateField) PluginServices.getMDIManager().addView(panelNewField);
454
                                }
455
                        });
456
                }
457
                return jBtnNewField;
458
        }
459

    
460
        /**
461
         * This method initializes jButton
462
         * 
463
         * @return javax.swing.JButton
464
         */
465
        private JButton getJButton() {
466
                if (jBtnDeleteField == null) {
467
                        jBtnDeleteField = new JButton();
468
                        jBtnDeleteField.setBounds(new java.awt.Rectangle(332,90,118,25));
469
                        jBtnDeleteField.setText("Delete Field");
470
                        jBtnDeleteField
471
                                        .addActionListener(new java.awt.event.ActionListener() {
472
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
473
                                                        int[] selecteds = jTableFields.getSelectedRows();
474
                                                        TableModel tm = jTableFields.getModel();
475
                                                        
476

    
477
                                                        for (int i = selecteds.length - 1; i >= 0; i--)
478
                                                        {
479
                                                                String fieldName = (String) tm.getValueAt(selecteds[i],0);
480
                                                                try {
481
                                                                        edAdapter.removeField(fieldName);
482
                                                                } catch (EditionException e1) {
483
                                                                        // TODO Auto-generated catch block
484
                                                                        e1.printStackTrace();
485
                                                                }
486
                                                        }
487
                                                        jTableFields.revalidate();
488

    
489
                                                }
490
                                        });
491
                }
492
                return jBtnDeleteField;
493
        }
494

    
495
        /**
496
         * This method initializes jBtnRenameField
497
         * 
498
         * @return javax.swing.JButton
499
         */
500
        private JButton getJBtnRenameField() {
501
                if (jBtnRenameField == null) {
502
                        jBtnRenameField = new JButton();
503
                        jBtnRenameField
504
                                        .setBounds(new java.awt.Rectangle(332,120,118,25));
505
                        jBtnRenameField.setText("Rename Field...");
506
                        jBtnRenameField.addActionListener(new java.awt.event.ActionListener() {
507
                                public void actionPerformed(java.awt.event.ActionEvent e) {
508
                                        int[] selecteds = jTableFields.getSelectedRows();
509
                                        TableModel tm = jTableFields.getModel();
510
                                        
511

    
512
                                        for (int i = selecteds.length - 1; i >= 0; i--)
513
                                        {
514
                                                String fieldName = (String) tm.getValueAt(selecteds[i],0);
515
                                                try {
516
                                                        String newName = JOptionPane.showInputDialog(
517
                                                                        (Component) PluginServices.getMDIManager().getActiveView(), 
518
                                                                        PluginServices.getText(this, "please_insert_new_field_name"),
519
                                                                        fieldName
520
                                                                        );
521
                                                        if (newName == null) return;
522
                                                        if (edAdapter.getRecordset().getFieldIndexByName(newName) != -1)
523
                                                        {
524
                                                                JOptionPane.showMessageDialog(
525
                                                                                null,
526
                                                                                PluginServices.getText(this, "field_already_exists"));
527
                                                                return;
528
                                                        }
529
                                                        
530

    
531
                                                        edAdapter.renameField(fieldName, newName);
532
                                                } catch (EditionException e1) {
533
                                                        // TODO Auto-generated catch block
534
                                                        e1.printStackTrace();
535
                                                } catch (DriverLoadException e1) {
536
                                                        // TODO Auto-generated catch block
537
                                                        e1.printStackTrace();
538
                                                } catch (DriverException e1) {
539
                                                        // TODO Auto-generated catch block
540
                                                        e1.printStackTrace();
541
                                                }
542
                                        }
543
                                        jTableFields.repaint();
544
                                        
545
                                }
546
                        });
547
                }
548
                return jBtnRenameField;
549
        }
550

    
551
        /**
552
         * This method initializes jBtnOK
553
         * 
554
         * @return javax.swing.JButton
555
         */
556
        private JButton getJBtnOK() {
557
                if (jBtnOK == null) {
558
                        jBtnOK = new JButton();
559
                        jBtnOK.setBounds(new java.awt.Rectangle(134,278,91,23));
560
                        jBtnOK.setText("Aceptar");
561
                        jBtnOK.addActionListener(new java.awt.event.ActionListener() {
562
                                public void actionPerformed(java.awt.event.ActionEvent e) {
563
                                        PluginServices.getMDIManager().closeView(DlgFieldManager.this);
564
                                }
565
                        });
566
                }
567
                return jBtnOK;
568
        }
569

    
570
        /**
571
         * This method initializes jButton
572
         * 
573
         * @return javax.swing.JButton
574
         */
575
        private JButton getJButton2() {
576
                if (jBtnCancel == null) {
577
                        jBtnCancel = new JButton();
578
                        jBtnCancel.setBounds(new java.awt.Rectangle(239,278,91,23));
579
                        jBtnCancel.setText("Cancelar");
580
                        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
581
                                public void actionPerformed(java.awt.event.ActionEvent e) {
582
                                        PluginServices.getMDIManager().closeView(DlgFieldManager.this);
583
                                }
584
                        });
585
                        jBtnCancel.setVisible(false);
586
                }
587
                return jBtnCancel;
588
        }
589

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