Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / gui / DlgFieldManager.java @ 10661

History | View | Annotate | Download (16.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.project.documents.table.gui;
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.driver.exceptions.ReadDriverException;
71
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
72
import com.iver.andami.PluginServices;
73
import com.iver.andami.messages.NotificationManager;
74
import com.iver.andami.ui.mdiManager.IWindow;
75
import com.iver.andami.ui.mdiManager.WindowInfo;
76
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
77
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
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

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

    
113
        private JPanel jPanelButtons = null;
114

    
115
        private class MyFieldDescription {
116
                boolean isOriginal;
117

    
118
                FieldDescription fieldDesc;
119

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

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

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

    
133
        }
134

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

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

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

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

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

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

    
184
                                }
185
                        return null;
186
                }
187

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

    
193
                public String getColumnName(int column) {
194
                        switch (column) {
195
                        case 0:
196
                                return PluginServices.getText(this, "field_name");
197
                        case 1:
198
                                return PluginServices.getText(this, "field_type");
199
                        case 2:
200
                                return PluginServices.getText(this, "field_length");
201
                        case 3:
202
                                return PluginServices.getText(this, "field_decimal_count");
203
                        case 4:
204
                                return PluginServices.getText(this, "field_default_value");
205

    
206
                        }
207
                        return super.getColumnName(column);
208
                }
209

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

    
213
                }
214

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

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

    
241
                                        fDesc.setFieldLength(fieldLength);
242

    
243
                                        // TODO: HACERLO BIEN
244
                                        if (fDesc.getFieldType() == Types.VARCHAR) {
245
                                                fDesc.setFieldDecimalCount(5);
246
                                        }
247
                                }
248
                        } catch (ReadDriverException 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
                tm = new MyTableModel(ies);
267
                getJTableFields().setModel(tm);
268
                // Esto lo a?ado aqu? porque si no tiene registros, no hace caso.
269
                // (Por eso no
270
                // lo pongo en getJTable()
271
                TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1);
272
                JComboBox comboBox = new JComboBox();
273
                comboBox.addItem("Boolean");
274
                comboBox.addItem("Date");
275
                comboBox.addItem("Integer");
276
                comboBox.addItem("Double");
277
                comboBox.addItem("String");
278
                typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
279

    
280
        }
281

    
282
        /**
283
         * This method initializes this
284
         *
285
         */
286
        private void initialize() {
287
                FlowLayout flowLayout = new FlowLayout();
288
                flowLayout.setVgap(5);
289
                flowLayout.setHgap(0);
290
                BorderLayout borderLayout = new BorderLayout();
291
                borderLayout.setHgap(15);
292
                borderLayout.setVgap(15);
293
                jLabel = new JLabel();
294
                jLabel.setText(PluginServices.getText(this, "add_delete_edit_fields")+": ");
295
                this.setLayout(borderLayout);
296
                this.setSize(new java.awt.Dimension(663,404));
297
                this.setPreferredSize(new java.awt.Dimension(getWidth(), getHeight()));
298
                JPanel jPanelNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
299
                jPanelNorth.add(jLabel);
300
                this.add(jPanelNorth, BorderLayout.NORTH);
301
                JPanel jPanelCenter = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
302
                jPanelCenter.add(getJScrollPane());
303
                this.add(jPanelCenter, BorderLayout.CENTER);
304

    
305
                this.add(getJPanelButtons(), BorderLayout.EAST);
306
                JPanel jPanelSouth = new JPanel();
307
                jPanelSouth.setLayout(flowLayout);
308
                jPanelSouth.add(getJBtnOK(), null);
309
                jPanelSouth.add(getJBtnCancel(), null);
310
                this.add(jPanelSouth, BorderLayout.SOUTH);
311

    
312
        }
313

    
314
        public WindowInfo getWindowInfo() {
315
                if (windowInfo == null) {
316
                        windowInfo = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.PALETTE
317
                                        | WindowInfo.RESIZABLE);
318
                        windowInfo.setTitle(PluginServices.getText(this, "field_manager"));
319
                        windowInfo.setHeight(this.getHeight());
320
                        windowInfo.setWidth(this.getWidth());
321
                }
322
                return windowInfo;
323
        }
324

    
325
        /**
326
         * Convierte lo que hay en la tabla en una definici?n de campos adecuada
327
         * para crear un LayerDefinition
328
         *
329
         * @return
330
         */
331
        public FieldDescription[] getFieldsDescription() {
332
                DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
333
                FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()];
334

    
335
                for (int i = 0; i < tm.getRowCount(); i++) {
336
                        fieldsDesc[i] = new FieldDescription();
337
                        fieldsDesc[i].setFieldName((String) tm.getValueAt(i, 0));
338
                        String strType = (String) tm.getValueAt(i, 1);
339
                        if (strType.equals("String"))
340
                                fieldsDesc[i].setFieldType(Types.VARCHAR);
341
                        if (strType.equals("Double"))
342
                                fieldsDesc[i].setFieldType(Types.DOUBLE);
343
                        if (strType.equals("Integer"))
344
                                fieldsDesc[i].setFieldType(Types.INTEGER);
345
                        if (strType.equals("Boolean"))
346
                                fieldsDesc[i].setFieldType(Types.BOOLEAN);
347
                        if (strType.equals("Date"))
348
                                fieldsDesc[i].setFieldType(Types.DATE);
349
                        int fieldLength = Integer.parseInt((String) tm.getValueAt(i, 2));
350
                        fieldsDesc[i].setFieldLength(fieldLength);
351

    
352
                        // TODO: HACERLO BIEN
353
                        if (strType.equals("Double"))
354
                                fieldsDesc[i].setFieldDecimalCount(5);
355

    
356
                }
357

    
358
                return fieldsDesc;
359
        }
360

    
361
        /**
362
         * This method initializes jScrollPane
363
         *
364
         * @return javax.swing.JScrollPane
365
         */
366
        private JScrollPane getJScrollPane() {
367
                if (jScrollPane == null) {
368
                        jScrollPane = new JScrollPane();
369
                        jScrollPane.setPreferredSize(new java.awt.Dimension(482,350));
370
                        jScrollPane.setViewportView(getJTableFields());
371
                }
372
                return jScrollPane;
373
        }
374

    
375
        /**
376
         * This method initializes jTableFields
377
         *
378
         * @return javax.swing.JTable
379
         */
380
        private JTable getJTableFields() {
381
                if (jTableFields == null) {
382
                        jTableFields = new JTable();
383
                        jTableFields
384
                                        .setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
385

    
386
                        jTableFields.setColumnSelectionAllowed(false);
387
                        // Ask to be notified of selection changes.
388
                        ListSelectionModel rowSM = jTableFields.getSelectionModel();
389
                        rowSM.addListSelectionListener(new ListSelectionListener() {
390
                                public void valueChanged(ListSelectionEvent e) {
391
                                        // Ignore extra messages.
392
                                        if (e.getValueIsAdjusting())
393
                                                return;
394

    
395
                                        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
396
                                        if (lsm.isSelectionEmpty()) {
397
                                                // no rows are selected
398
                                                jBtnDeleteField.setEnabled(false);
399
                                        } else {
400
                                                jBtnDeleteField.setEnabled(true);
401
                                        }
402
                                        if (jTableFields.getSelectedRows().length != 1)
403
                                        {
404
                                                getJBtnRenameField().setEnabled(false);
405
                                        }
406
                                        else
407
                                        {
408
                                                getJBtnRenameField().setEnabled(true);
409
                                        }
410

    
411
                                }
412
                        });
413

    
414
                }
415
                return jTableFields;
416
        }
417

    
418
        /**
419
         * This method initializes jBtnNewField
420
         *
421
         * @return javax.swing.JButton
422
         */
423
        private JButton getJBtnNewField() {
424
                if (jBtnNewField == null) {
425
                        jBtnNewField = new JButton();
426
                        jBtnNewField.setText(PluginServices.getText(this, "new_field"));
427
                        jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
428
                                private ArrayList tempFieldNames = new ArrayList();
429

    
430
                                {
431
                                        try{
432
                                                String[] f = edAdapter.getRecordset().getFieldNames();
433
                                                for (int i = 0; i < f.length; i++) {
434
                                                        tempFieldNames.add(f[i]);
435
                                                }
436
                                        } catch (Exception ex) {}
437
                                }
438

    
439
                                public void actionPerformed(java.awt.event.ActionEvent e) {
440
                                        ActionListener okAction;
441
                                        okAction = new java.awt.event.ActionListener() {
442

    
443
                                                public void actionPerformed(java.awt.event.ActionEvent e){
444
                                                        try {
445
                                                                FieldDescription fld = panelNewField.getFieldDescription();
446
                                                                if (fld.getFieldType() == Types.VARCHAR && fld.getFieldLength()>MAX_FIELD_LENGTH) {
447
                                                                        JOptionPane.showMessageDialog(DlgFieldManager.this, PluginServices.getText(this, "max_length_is") + ":" + MAX_FIELD_LENGTH);
448
                                                                        fld.setFieldLength(MAX_FIELD_LENGTH);
449
                                                                }
450

    
451
                                                                if (edAdapter.getRecordset().getFieldIndexByName(fld.getFieldAlias()) != -1)
452
                                                                {
453
                                                                        JOptionPane.showMessageDialog(
454
                                                                                        null,
455
                                                                                        PluginServices.getText(this, "field_already_exists"));
456
                                                                        return;
457
                                                                }
458
                                                                tempFieldNames.add(fld.getFieldAlias());
459
                                                                edAdapter.addField(fld);
460
                                                                jTableFields.revalidate();
461
                                                                PluginServices.getMDIManager().closeWindow(panelNewField);
462
                                                        } catch (ParseException e2) {
463
                                                                NotificationManager.addError(e2);
464
                                                        } catch (ReadDriverException e3) {
465
                                                                NotificationManager.addError(e3);
466
                                                        }
467

    
468

    
469
                                                }
470
                                        };
471
                                        panelNewField.setOkAction(okAction);
472
                                        String[] names = (String[]) tempFieldNames.toArray(new String[0]);
473
                                        panelNewField.setCurrentFieldNames(names);
474
                                        panelNewField = (FPanelCreateField) PluginServices.getMDIManager().addWindow(panelNewField);
475
                                }
476
                        });
477
                }
478
                return jBtnNewField;
479
        }
480

    
481
        /**
482
         * This method initializes jButton
483
         *
484
         * @return javax.swing.JButton
485
         */
486
        private JButton getJBtnDelete() {
487
                if (jBtnDeleteField == null) {
488
                        jBtnDeleteField = new JButton();
489
                        jBtnDeleteField.setText(PluginServices.getText(this, "delete_field"));
490
                        jBtnDeleteField
491
                                        .addActionListener(new java.awt.event.ActionListener() {
492
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
493
                                                        int[] selecteds = jTableFields.getSelectedRows();
494
                                                        TableModel tm = jTableFields.getModel();
495

    
496

    
497
                                                        for (int i = selecteds.length - 1; i >= 0; i--)
498
                                                        {
499
                                                                String fieldName = (String) tm.getValueAt(selecteds[i],0);
500
                                                                try {
501
                                                                        edAdapter.removeField(fieldName);
502
                                                                } catch (WriteDriverException e1) {
503
                                                                        e1.printStackTrace();
504
                                                                } catch (ReadDriverException e1) {
505
                                                                        e1.printStackTrace();
506
                                                                }
507

    
508
                                                        }
509
                                                        jTableFields.revalidate();
510

    
511
                                                }
512
                                        });
513
                }
514
                return jBtnDeleteField;
515
        }
516

    
517
        /**
518
         * This method initializes jBtnRenameField
519
         *
520
         * @return javax.swing.JButton
521
         */
522
        private JButton getJBtnRenameField() {
523
                if (jBtnRenameField == null) {
524
                        jBtnRenameField = new JButton();
525
                        jBtnRenameField.setText(PluginServices.getText(this, "rename_field"));
526
                        jBtnRenameField.addActionListener(new java.awt.event.ActionListener() {
527
                                public void actionPerformed(java.awt.event.ActionEvent e) {
528
                                        int[] selecteds = jTableFields.getSelectedRows();
529
                                        TableModel tm = jTableFields.getModel();
530

    
531

    
532
                                        for (int i = selecteds.length - 1; i >= 0; i--)
533
                                        {
534
                                                String fieldName = (String) tm.getValueAt(selecteds[i],0);
535
                                                try {
536
                                                        String newName = JOptionPane.showInputDialog(
537
                                                                        (Component) PluginServices.getMDIManager().getActiveWindow(),
538
                                                                        PluginServices.getText(this, "please_insert_new_field_name"),
539
                                                                        fieldName
540
                                                                        );
541
                                                        if (newName == null) return;
542
                                                        if (edAdapter.getRecordset().getFieldIndexByName(newName) != -1)
543
                                                        {
544
                                                                JOptionPane.showMessageDialog(
545
                                                                                null,
546
                                                                                PluginServices.getText(this, "field_already_exists"));
547
                                                                return;
548
                                                        }
549

    
550

    
551
                                                        edAdapter.renameField(fieldName, newName);
552
                                                } catch (ReadDriverException e1) {
553
                                                        // TODO Auto-generated catch block
554
                                                        e1.printStackTrace();
555
                                                }
556
                                        }
557
                                        jTableFields.repaint();
558

    
559
                                }
560
                        });
561
                }
562
                return jBtnRenameField;
563
        }
564

    
565
        /**
566
         * This method initializes jBtnOK
567
         *
568
         * @return javax.swing.JButton
569
         */
570
        private JButton getJBtnOK() {
571
                if (jBtnOK == null) {
572
                        jBtnOK = new JButton();
573
                        jBtnOK.setText(PluginServices.getText(this, "aceptar"));
574
                        jBtnOK.addActionListener(new java.awt.event.ActionListener() {
575
                                public void actionPerformed(java.awt.event.ActionEvent e) {
576
                                        PluginServices.getMDIManager().closeWindow(DlgFieldManager.this);
577
                                }
578
                        });
579
                }
580
                return jBtnOK;
581
        }
582

    
583
        /**
584
         * This method initializes jButton
585
         *
586
         * @return javax.swing.JButton
587
         */
588
        private JButton getJBtnCancel() {
589
                if (jBtnCancel == null) {
590
                        jBtnCancel = new JButton();
591
                        jBtnCancel.setText(PluginServices.getText(this, "cancelar"));
592
                        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
593
                                public void actionPerformed(java.awt.event.ActionEvent e) {
594
                                        PluginServices.getMDIManager().closeWindow(DlgFieldManager.this);
595
                                }
596
                        });
597
                        jBtnCancel.setVisible(false);
598
                }
599
                return jBtnCancel;
600
        }
601

    
602
        /**
603
         * This method initializes jPanelButtons
604
         *
605
         * @return javax.swing.JPanel
606
         */
607
        private JPanel getJPanelButtons() {
608
                if (jPanelButtons == null) {
609
                        jPanelButtons = new JPanel();
610
                        JPanel aux = new JPanel(new GridLayout(3, 1));
611
                        aux.add(getJBtnNewField());
612
                        aux.add(getJBtnRenameField());
613
                        aux.add(getJBtnDelete());
614
                        jPanelButtons.add(aux, BorderLayout.NORTH);
615

    
616
                }
617
                return jPanelButtons;
618
        }
619

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