Statistics
| Revision:

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

History | View | Annotate | Download (14.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.BorderLayout;
44
import java.awt.event.ActionListener;
45
import java.sql.Types;
46

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

    
66
import org.gvsig.gui.beans.AcceptCancelPanel;
67

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

    
81
public class DlgFieldManager extends JPanel implements View {
82

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

    
88
        ViewInfo viewInfo = null;
89

    
90
        private JLabel jLabel = null;
91

    
92
        private JScrollPane jScrollPane = null;
93

    
94
        private JTable jTableFields = null;
95

    
96
        private JButton jBtnNewField = null;
97

    
98
        private JButton jBtnDeleteField = null;
99

    
100
        private JButton jBtnRenameField = null;
101

    
102
        private JButton jBtnOK = null;
103

    
104
        private JButton jBtnCancel = null;
105
        
106
        private FPanelCreateField panelNewField = new FPanelCreateField();
107

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

    
111
        private class MyFieldDescription {
112
                boolean isOriginal;
113

    
114
                FieldDescription fieldDesc;
115

    
116
                MyFieldDescription(FieldDescription fieldDesc, boolean isOriginal) {
117
                        this.fieldDesc = fieldDesc;
118
                        this.isOriginal = isOriginal;
119
                }
120

    
121
                FieldDescription getFieldDescription() {
122
                        return fieldDesc;
123
                }
124

    
125
                boolean isOriginal() {
126
                        return isOriginal;
127
                }
128

    
129
        }
130

    
131
        private class MyTableModel extends AbstractTableModel {
132
                IEditableSource myIes;
133

    
134
                public MyTableModel(IEditableSource ies) {
135
                        myIes = ies;
136
                }
137

    
138
                public int getColumnCount() {
139
                        return 3;
140
                }
141

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

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

    
175
                                        return strType;
176
                                case 2:
177
                                        return new Integer(aux.getFieldLength());
178
                                }
179
                        } catch (DriverLoadException e) {
180
                                e.printStackTrace();
181
                        } catch (DriverException e) {
182
                                e.printStackTrace();
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, "FieldName");
197
                        case 1:
198
                                return PluginServices.getText(this, "FieldType");
199
                        case 2:
200
                                return PluginServices.getText(this, "FieldLength");
201
                        }
202
                        return super.getColumnName(column);
203
                }
204

    
205
                public boolean isCellEditable(int rowIndex, int columnIndex) {
206
                        return false;
207

    
208
                }
209

    
210
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
211
                        FieldDescription[] myFields;
212
                        try {
213
                                myFields = myIes.getRecordset().getFieldsDescription();
214
                                FieldDescription fDesc = myFields[rowIndex];
215

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

    
237
                                        // TODO: HACERLO BIEN
238
                                        if (fDesc.getFieldType() == Types.VARCHAR)
239
                                                fDesc.setFieldDecimalCount(5);
240

    
241
                                }
242
                        } catch (DriverLoadException e) {
243
                                e.printStackTrace();
244
                        } catch (DriverException e) {
245
                                e.printStackTrace();
246
                        }
247

    
248
                }
249

    
250
        }
251

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

    
277
                        TableColumn widthColumn = jTableFields.getColumnModel()
278
                                        .getColumn(2);
279
                } catch (DriverLoadException e) {
280
                        // TODO Auto-generated catch block
281
                        e.printStackTrace();
282
                }
283

    
284
        }
285

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

    
304
        }
305

    
306
        public ViewInfo getViewInfo() {
307
                if (viewInfo == null) {
308
                        viewInfo = new ViewInfo(ViewInfo.MODALDIALOG | ViewInfo.PALETTE
309
                                        | ViewInfo.RESIZABLE);
310
                }
311
                return viewInfo;
312
        }
313

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

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

    
341
                        // TODO: HACERLO BIEN
342
                        if (strType.equals("Double"))
343
                                fieldsDesc[i].setFieldDecimalCount(5);
344

    
345
                }
346

    
347
                return fieldsDesc;
348
        }
349

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

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

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

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

    
403
                }
404
                return jTableFields;
405
        }
406

    
407
        /**
408
         * This method initializes jBtnNewField
409
         * 
410
         * @return javax.swing.JButton
411
         */
412
        private JButton getJBtnNewField() {
413
                if (jBtnNewField == null) {
414
                        jBtnNewField = new JButton();
415
                        jBtnNewField.setBounds(new java.awt.Rectangle(255, 60, 118, 25));
416
                        jBtnNewField.setText(PluginServices.getText(this, "New Field"));
417
                        jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
418
                                public void actionPerformed(java.awt.event.ActionEvent e) {                                        
419
                                                                                
420
                                        ActionListener okAction;
421
                                        okAction = new java.awt.event.ActionListener() {
422
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
423
                                                        FieldDescription fld = panelNewField.getFieldDescription();
424
                                                        try {
425
                                                                edAdapter.addField(fld);
426
                                                                jTableFields.revalidate();
427
                                                        } catch (EditionException e1) {
428
                                                                // TODO Auto-generated catch block
429
                                                                e1.printStackTrace();
430
                                                        }
431

    
432
                                                        
433
                                                }
434
                                        };
435
                                        panelNewField.setOkAction(okAction);
436
                                        panelNewField = (FPanelCreateField) PluginServices.getMDIManager().addView(panelNewField);
437
                                }
438
                        });
439
                }
440
                return jBtnNewField;
441
        }
442

    
443
        /**
444
         * This method initializes jButton
445
         * 
446
         * @return javax.swing.JButton
447
         */
448
        private JButton getJButton() {
449
                if (jBtnDeleteField == null) {
450
                        jBtnDeleteField = new JButton();
451
                        jBtnDeleteField.setBounds(new java.awt.Rectangle(255, 90, 118, 25));
452
                        jBtnDeleteField.setText("Delete Field");
453
                        jBtnDeleteField
454
                                        .addActionListener(new java.awt.event.ActionListener() {
455
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
456
                                                        int[] selecteds = jTableFields.getSelectedRows();
457
                                                        TableModel tm = jTableFields.getModel();
458
                                                        
459

    
460
                                                        for (int i = selecteds.length - 1; i >= 0; i--)
461
                                                        {
462
                                                                String fieldName = (String) tm.getValueAt(selecteds[i],0);
463
                                                                try {
464
                                                                        edAdapter.removeField(fieldName);
465
                                                                } catch (EditionException e1) {
466
                                                                        // TODO Auto-generated catch block
467
                                                                        e1.printStackTrace();
468
                                                                }
469
                                                        }
470
                                                        jTableFields.revalidate();
471

    
472
                                                }
473
                                        });
474
                }
475
                return jBtnDeleteField;
476
        }
477

    
478
        /**
479
         * This method initializes jBtnRenameField
480
         * 
481
         * @return javax.swing.JButton
482
         */
483
        private JButton getJBtnRenameField() {
484
                if (jBtnRenameField == null) {
485
                        jBtnRenameField = new JButton();
486
                        jBtnRenameField
487
                                        .setBounds(new java.awt.Rectangle(255, 120, 118, 25));
488
                        jBtnRenameField.setText("Rename Field...");
489
                        jBtnRenameField.addActionListener(new java.awt.event.ActionListener() {
490
                                public void actionPerformed(java.awt.event.ActionEvent e) {
491
                                        int[] selecteds = jTableFields.getSelectedRows();
492
                                        TableModel tm = jTableFields.getModel();
493
                                        
494

    
495
                                        for (int i = selecteds.length - 1; i >= 0; i--)
496
                                        {
497
                                                String fieldName = (String) tm.getValueAt(selecteds[i],0);
498
                                                try {
499
                                                        String newName = JOptionPane.showInputDialog(
500
                                                                        (JComponent) PluginServices.getMDIManager().getActiveView(), 
501
                                                                        PluginServices.getText(this, "please_insert_new_field_name"),
502
                                                                        fieldName
503
                                                                        );
504
                                                        if (newName == null) return;
505

    
506
                                                        edAdapter.renameField(fieldName, newName);
507
                                                } catch (EditionException e1) {
508
                                                        // TODO Auto-generated catch block
509
                                                        e1.printStackTrace();
510
                                                }
511
                                        }
512
                                        jTableFields.repaint();
513
                                        
514
                                }
515
                        });
516
                }
517
                return jBtnRenameField;
518
        }
519

    
520
        /**
521
         * This method initializes jBtnOK
522
         * 
523
         * @return javax.swing.JButton
524
         */
525
        private JButton getJBtnOK() {
526
                if (jBtnOK == null) {
527
                        jBtnOK = new JButton();
528
                        jBtnOK.setBounds(new java.awt.Rectangle(180, 285, 91, 23));
529
                        jBtnOK.setText("Aceptar");
530
                        jBtnOK.addActionListener(new java.awt.event.ActionListener() {
531
                                public void actionPerformed(java.awt.event.ActionEvent e) {
532
                                        PluginServices.getMDIManager().closeView(DlgFieldManager.this);
533
                                }
534
                        });
535
                }
536
                return jBtnOK;
537
        }
538

    
539
        /**
540
         * This method initializes jButton
541
         * 
542
         * @return javax.swing.JButton
543
         */
544
        private JButton getJButton2() {
545
                if (jBtnCancel == null) {
546
                        jBtnCancel = new JButton();
547
                        jBtnCancel.setBounds(new java.awt.Rectangle(285, 285, 91, 23));
548
                        jBtnCancel.setText("Cancelar");
549
                        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
550
                                public void actionPerformed(java.awt.event.ActionEvent e) {
551
                                        PluginServices.getMDIManager().closeView(DlgFieldManager.this);
552
                                }
553
                        });
554
                }
555
                return jBtnCancel;
556
        }
557

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