Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / project / document / table / gui / FeatureTypeEditingPanel.java @ 26053

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 org.gvsig.project.document.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.text.ParseException;
49
import java.util.ArrayList;
50

    
51
import javax.swing.DefaultCellEditor;
52
import javax.swing.JComboBox;
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.TableColumn;
63
import javax.swing.table.TableModel;
64

    
65
import org.gvsig.fmap.dal.DataTypes;
66
import org.gvsig.fmap.dal.exception.DataException;
67
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
68
import org.gvsig.fmap.dal.feature.EditableFeatureType;
69
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
70
import org.gvsig.fmap.dal.feature.FeatureStore;
71
import org.gvsig.gui.beans.swing.JButton;
72
import org.gvsig.project.document.table.FeatureTableOperations;
73

    
74
import com.iver.andami.PluginServices;
75
import com.iver.andami.messages.NotificationManager;
76
import com.iver.andami.ui.mdiManager.IWindow;
77
import com.iver.andami.ui.mdiManager.WindowInfo;
78
import com.iver.cit.gvsig.TableEditChangeColumnsExtension;
79

    
80
/**
81
 * To modify FeatureTypes from the interface.
82
 *
83
 * @author Vicente Caballero Navarro
84
 *
85
 */
86
public class FeatureTypeEditingPanel extends JPanel implements IWindow {
87
        private static final long serialVersionUID = -4284879326692474318L;
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 CreateNewAttributePanel panelNewField = new CreateNewAttributePanel();
108

    
109
        private FeatureStore featureStore = null;
110

    
111
        private JPanel jPanelButtons = null;
112

    
113
        private EditableFeatureType editableType = null;
114

    
115
        private class MyTableModel extends AbstractTableModel {
116

    
117
                public MyTableModel(FeatureStore fs) {
118
                        try {
119
                                editableType = (EditableFeatureType) fs.getDefaultFeatureType()
120
                                                .getEditable();
121
                        } catch (DataException e) {
122
                                NotificationManager.addError(PluginServices.getText(this,
123
                                                "create_editabletype"), e);
124
                        }
125

    
126
                }
127

    
128
                public int getColumnCount() {
129
                        return 5;
130
                }
131

    
132
                public int getRowCount() {
133
                        return editableType.size();
134
                }
135

    
136
                public Object getValueAt(int rowIndex, int columnIndex) {
137
                        FeatureAttributeDescriptor myField = null;
138
                        myField = (FeatureAttributeDescriptor) editableType.get(rowIndex);
139

    
140
                        switch (columnIndex) {
141
                        case 0:
142
                                return myField.getName();
143
                        case 1:
144
                                String strType = null;
145
                                int type = myField.getDataType();
146
                                if (type == DataTypes.STRING)
147
                                        strType = "String";
148
                                else if (type == DataTypes.DOUBLE)
149
                                        strType = "Double";
150
                                else if (type == DataTypes.INT)
151
                                        strType = "Integer";
152
                                else if (type == DataTypes.BOOLEAN)
153
                                        strType = "Boolean";
154
                                else if (type == DataTypes.DATE)
155
                                        strType = "Date";
156
                                else if (type == DataTypes.GEOMETRY)
157
                                        strType = "Geometry";
158
                                return strType;
159
                        case 2:
160
                                return new Integer(myField.getSize());
161
                        case 3:
162
                                return new Integer(myField.getPrecision());
163
                        case 4:
164
                                return myField.getDefaultValue();
165

    
166
                        }
167
                        return null;
168
                }
169

    
170
                public Class getColumnClass(int columnIndex) {
171
                        return super.getColumnClass(columnIndex);
172
                }
173

    
174
                public String getColumnName(int column) {
175
                        switch (column) {
176
                        case 0:
177
                                return PluginServices.getText(this, "field_name");
178
                        case 1:
179
                                return PluginServices.getText(this, "field_type");
180
                        case 2:
181
                                return PluginServices.getText(this, "field_length");
182
                        case 3:
183
                                return PluginServices.getText(this, "field_decimal_count");
184
                        case 4:
185
                                return PluginServices.getText(this, "field_default_value");
186

    
187
                        }
188
                        return super.getColumnName(column);
189
                }
190

    
191
                public boolean isCellEditable(int rowIndex, int columnIndex) {
192
                        return false;
193

    
194
                }
195

    
196
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
197
                        if (columnIndex == 0)
198
                                editableType.remove(rowIndex);
199
                        String name = "";
200
                        int type = DataTypes.STRING;
201
                        int size = 0;
202
                        int precision = 0;
203
                        switch (columnIndex) {
204
                        case 0:
205
                                name = (String) aValue;
206
                                break;
207
                        case 1:
208
                                String strType = (String) aValue;
209
                                if (strType.equals("String"))
210
                                        type = DataTypes.STRING;
211
                                if (strType.equals("Double")) {
212
                                        type = DataTypes.DOUBLE;
213
                                        precision = 5;
214
                                }
215
                                if (strType.equals("Integer"))
216
                                        type = DataTypes.INT;
217
                                if (strType.equals("Boolean"))
218
                                        type = DataTypes.BOOLEAN;
219
                                if (strType.equals("Date"))
220
                                        type = DataTypes.DATE;
221
                                break;
222
                        case 2:
223
                                size = ((Integer) aValue).intValue();
224

    
225
                                // TODO: HACERLO BIEN
226
                                // if (ead.getDataType()==DataTypes.STRING) {
227
                                // ead.setPrecision(5);
228
                                // }
229
                        }
230
                        EditableFeatureAttributeDescriptor ead = editableType.add(name,
231
                                        type, size);
232
                        ead.setPrecision(precision);
233
                }
234

    
235
        }
236

    
237
        /**
238
         * This method initializes
239
         *
240
         */
241
        public FeatureTypeEditingPanel(FeatureStore fs) {
242
                super();
243
                this.featureStore = fs;
244
                initialize();
245
                // Add a new row
246
                TableModel tm;
247
                tm = new MyTableModel(fs);
248
                getJTableFields().setModel(tm);
249
                // Esto lo a?ado aqu? porque si no tiene registros, no hace caso.
250
                // (Por eso no
251
                // lo pongo en getJTable()
252
                TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1);
253
                JComboBox comboBox = new JComboBox();
254
                comboBox.addItem("Boolean");
255
                comboBox.addItem("Date");
256
                comboBox.addItem("Integer");
257
                comboBox.addItem("Double");
258
                comboBox.addItem("String");
259
                typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
260

    
261
        }
262

    
263
        /**
264
         * This method initializes this
265
         *
266
         */
267
        private void initialize() {
268
                FlowLayout flowLayout = new FlowLayout();
269
                flowLayout.setVgap(5);
270
                flowLayout.setHgap(0);
271
                BorderLayout borderLayout = new BorderLayout();
272
                borderLayout.setHgap(15);
273
                borderLayout.setVgap(15);
274
                jLabel = new JLabel();
275
                jLabel.setText(PluginServices.getText(this, "add_delete_edit_fields")
276
                                + ": ");
277
                this.setLayout(borderLayout);
278
                this.setSize(new java.awt.Dimension(663, 404));
279
                this.setPreferredSize(new java.awt.Dimension(getWidth(), getHeight()));
280
                JPanel jPanelNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
281
                jPanelNorth.add(jLabel);
282
                this.add(jPanelNorth, BorderLayout.NORTH);
283
                JPanel jPanelCenter = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
284
                jPanelCenter.add(getJScrollPane());
285
                this.add(jPanelCenter, BorderLayout.CENTER);
286

    
287
                this.add(getJPanelButtons(), BorderLayout.EAST);
288
                JPanel jPanelSouth = new JPanel();
289
                jPanelSouth.setLayout(flowLayout);
290
                jPanelSouth.add(getJBtnOK(), null);
291
                jPanelSouth.add(getJBtnCancel(), null);
292
                this.add(jPanelSouth, BorderLayout.SOUTH);
293

    
294
        }
295

    
296
        public WindowInfo getWindowInfo() {
297
                if (windowInfo == null) {
298
                        windowInfo = new WindowInfo(WindowInfo.MODALDIALOG
299
                                        | WindowInfo.PALETTE | WindowInfo.RESIZABLE);
300
                        windowInfo.setTitle(PluginServices.getText(this, "field_manager"));
301
                        windowInfo.setHeight(this.getHeight());
302
                        windowInfo.setWidth(this.getWidth());
303
                }
304
                return windowInfo;
305
        }
306

    
307
        // /**
308
        // * Convierte lo que hay en la tabla en una definici?n de campos adecuada
309
        // * para crear un LayerDefinition
310
        // *
311
        // * @return
312
        // */
313
        // public FieldDescription[] getFieldsDescription() {
314
        // DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
315
        // FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()];
316
        //
317
        // for (int i = 0; i < tm.getRowCount(); i++) {
318
        // fieldsDesc[i] = new FieldDescription();
319
        // fieldsDesc[i].setFieldName((String) tm.getValueAt(i, 0));
320
        // String strType = (String) tm.getValueAt(i, 1);
321
        // if (strType.equals("String"))
322
        // fieldsDesc[i].setFieldType(Types.VARCHAR);
323
        // if (strType.equals("Double"))
324
        // fieldsDesc[i].setFieldType(Types.DOUBLE);
325
        // if (strType.equals("Integer"))
326
        // fieldsDesc[i].setFieldType(Types.INTEGER);
327
        // if (strType.equals("Boolean"))
328
        // fieldsDesc[i].setFieldType(Types.BOOLEAN);
329
        // if (strType.equals("Date"))
330
        // fieldsDesc[i].setFieldType(Types.DATE);
331
        // int fieldLength = Integer.parseInt((String) tm.getValueAt(i, 2));
332
        // fieldsDesc[i].setFieldLength(fieldLength);
333
        //
334
        // // TODO: HACERLO BIEN
335
        // if (strType.equals("Double"))
336
        // fieldsDesc[i].setFieldDecimalCount(5);
337
        //
338
        // }
339
        //
340
        // return fieldsDesc;
341
        // }
342

    
343
        /**
344
         * This method initializes jScrollPane
345
         *
346
         * @return javax.swing.JScrollPane
347
         */
348
        private JScrollPane getJScrollPane() {
349
                if (jScrollPane == null) {
350
                        jScrollPane = new JScrollPane();
351
                        jScrollPane.setPreferredSize(new java.awt.Dimension(482, 350));
352
                        jScrollPane.setViewportView(getJTableFields());
353
                }
354
                return jScrollPane;
355
        }
356

    
357
        /**
358
         * This method initializes jTableFields
359
         *
360
         * @return javax.swing.JTable
361
         */
362
        private JTable getJTableFields() {
363
                if (jTableFields == null) {
364
                        jTableFields = new JTable();
365
                        jTableFields
366
                                        .setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
367

    
368
                        jTableFields.setColumnSelectionAllowed(false);
369
                        // Ask to be notified of selection changes.
370
                        ListSelectionModel rowSM = jTableFields.getSelectionModel();
371
                        rowSM.addListSelectionListener(new ListSelectionListener() {
372
                                public void valueChanged(ListSelectionEvent e) {
373
                                        // Ignore extra messages.
374
                                        if (e.getValueIsAdjusting())
375
                                                return;
376

    
377
                                        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
378
                                        if (lsm.isSelectionEmpty()) {
379
                                                // no rows are selected
380
                                                jBtnDeleteField.setEnabled(false);
381
                                        } else {
382
                                                jBtnDeleteField.setEnabled(true);
383
                                        }
384
                                        if (jTableFields.getSelectedRows().length != 1) {
385
                                                getJBtnRenameField().setEnabled(false);
386
                                        } else {
387
                                                getJBtnRenameField().setEnabled(true);
388
                                        }
389

    
390
                                }
391
                        });
392

    
393
                }
394
                return jTableFields;
395
        }
396

    
397
        /**
398
         * This method initializes jBtnNewField
399
         *
400
         * @return javax.swing.JButton
401
         */
402
        private JButton getJBtnNewField() {
403
                if (jBtnNewField == null) {
404
                        jBtnNewField = new JButton();
405
                        jBtnNewField.setText(PluginServices.getText(this, "new_field"));
406
                        jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
407
                                private ArrayList tempFieldNames = new ArrayList();
408

    
409
                                {
410
                                        try {
411
                                                int size = editableType.size();
412
                                                for (int i = 0; i < size; i++) {
413
                                                        FeatureAttributeDescriptor ad = (FeatureAttributeDescriptor) editableType
414
                                                                        .get(i);
415
                                                        tempFieldNames.add(ad.getName());
416
                                                }
417
                                        } catch (Exception ex) {
418
                                        }
419
                                }
420

    
421
                                public void actionPerformed(java.awt.event.ActionEvent e) {
422
                                        ActionListener okAction;
423
                                        okAction = new java.awt.event.ActionListener() {
424

    
425
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
426
                                                        try {
427
                                                                EditableFeatureAttributeDescriptor ead = panelNewField
428
                                                                                .loadFieldDescription(editableType);
429
                                                                if (ead == null)
430
                                                                        return;
431
                                                                if (ead.getDataType() == DataTypes.STRING
432
                                                                                && ead.getSize() > FeatureTableOperations.MAX_FIELD_LENGTH) {
433
                                                                        NotificationManager.showMessageInfo( PluginServices.getText(this,
434
                                                                                                        "max_length_is")
435
                                                                                                        + ":" + FeatureTableOperations.MAX_FIELD_LENGTH, null);
436
                                                                        ead.setSize(FeatureTableOperations.MAX_FIELD_LENGTH);
437
                                                                }
438
                                                                tempFieldNames.add(ead.getName());
439
                                                                jTableFields.revalidate();
440
                                                                PluginServices.getMDIManager().closeWindow(
441
                                                                                panelNewField);
442
                                                        } catch (ParseException e2) {
443
                                                                NotificationManager.addError(e2);
444
                                                        }
445

    
446
                                                }
447
                                        };
448
                                        panelNewField.setOkAction(okAction);
449
                                        String[] names = (String[]) tempFieldNames
450
                                                        .toArray(new String[0]);
451
                                        panelNewField.setCurrentFieldNames(names);
452
                                        panelNewField = (CreateNewAttributePanel) PluginServices
453
                                                        .getMDIManager().addWindow(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 getJBtnDelete() {
466
                if (jBtnDeleteField == null) {
467
                        jBtnDeleteField = new JButton();
468
                        jBtnDeleteField.setText(PluginServices
469
                                        .getText(this, "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
                                                        for (int i = selecteds.length - 1; i >= 0; i--) {
477
                                                                String fieldName = (String) tm.getValueAt(
478
                                                                                selecteds[i], 0);
479
                                                                editableType.remove(fieldName);
480
                                                        }
481
                                                        jTableFields.revalidate();
482
                                                }
483
                                        });
484
                }
485
                return jBtnDeleteField;
486
        }
487

    
488
        /**
489
         * This method initializes jBtnRenameField
490
         *
491
         * @return javax.swing.JButton
492
         */
493
        private JButton getJBtnRenameField() {
494
                if (jBtnRenameField == null) {
495
                        jBtnRenameField = new JButton();
496
                        jBtnRenameField.setText(PluginServices
497
                                        .getText(this, "rename_field"));
498
                        jBtnRenameField
499
                                        .addActionListener(new java.awt.event.ActionListener() {
500
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
501
                                                        int[] selecteds = jTableFields.getSelectedRows();
502
                                                        TableModel tm = jTableFields.getModel();
503

    
504
                                                        for (int i = selecteds.length - 1; i >= 0; i--) {
505
                                                                String fieldName = (String) tm.getValueAt(
506
                                                                                selecteds[i], 0);
507
                                                                String newName = JOptionPane
508
                                                                                .showInputDialog(
509
                                                                                                (Component) PluginServices
510
                                                                                                                .getMDIManager()
511
                                                                                                                .getActiveWindow(),
512
                                                                                                PluginServices
513
                                                                                                                .getText(this,
514
                                                                                                                                "please_insert_new_field_name"),
515
                                                                                                fieldName);
516
                                                                if (newName == null)
517
                                                                        return;
518
                                                                if (editableType.getIndex(newName) != -1) {
519
                                                                        NotificationManager.showMessageInfo(
520
                                                                                        PluginServices.getText(this,
521
                                                                                                        "field_already_exists"),
522
                                                                                        null);
523
                                                                        return;
524
                                                                }
525
                                                                FeatureAttributeDescriptor ad = (FeatureAttributeDescriptor) editableType
526
                                                                                .get(fieldName);
527
                                                                editableType.remove(ad.getName());
528
                                                                EditableFeatureAttributeDescriptor ead = editableType
529
                                                                                .add(newName, ad.getDataType(), ad
530
                                                                                                .getSize());
531
                                                                ead.setPrecision(ad.getPrecision());
532
                                                        }
533
                                                        jTableFields.repaint();
534
                                                }
535
                                        });
536
                }
537
                return jBtnRenameField;
538
        }
539

    
540
        /**
541
         * This method initializes jBtnOK
542
         *
543
         * @return javax.swing.JButton
544
         */
545
        private JButton getJBtnOK() {
546
                if (jBtnOK == null) {
547
                        jBtnOK = new JButton();
548
                        jBtnOK.setText(PluginServices.getText(this, "aceptar"));
549
                        jBtnOK.addActionListener(new java.awt.event.ActionListener() {
550
                                public void actionPerformed(java.awt.event.ActionEvent e) {
551
                                        try {
552
                                                featureStore.update(editableType);
553
                                        } catch (DataException e1) {
554
                                                NotificationManager.showMessageError(PluginServices
555
                                                                .getText(this, "update_featuretype_error"), e1);
556
                                        }
557
                                        PluginServices.getMDIManager().closeWindow(
558
                                                        FeatureTypeEditingPanel.this);
559
                                }
560
                        });
561
                }
562
                return jBtnOK;
563
        }
564

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

    
585
        /**
586
         * This method initializes jPanelButtons
587
         *
588
         * @return javax.swing.JPanel
589
         */
590
        private JPanel getJPanelButtons() {
591
                if (jPanelButtons == null) {
592
                        jPanelButtons = new JPanel();
593
                        JPanel aux = new JPanel(new GridLayout(3, 1));
594
                        aux.add(getJBtnNewField());
595
                        aux.add(getJBtnRenameField());
596
                        aux.add(getJBtnDelete());
597
                        jPanelButtons.add(aux, BorderLayout.NORTH);
598
                }
599
                return jPanelButtons;
600
        }
601

    
602
}