Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / table / gui / FeatureTypeEditingPanel.java @ 29596

History | View | Annotate | Download (17 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.app.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.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.andami.PluginServices;
66
import org.gvsig.andami.messages.NotificationManager;
67
import org.gvsig.andami.ui.mdiManager.IWindow;
68
import org.gvsig.andami.ui.mdiManager.WindowInfo;
69
import org.gvsig.app.project.documents.table.FeatureTableOperations;
70
import org.gvsig.fmap.dal.DataTypes;
71
import org.gvsig.fmap.dal.exception.DataException;
72
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
73
import org.gvsig.fmap.dal.feature.EditableFeatureType;
74
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
75
import org.gvsig.fmap.dal.feature.FeatureStore;
76
import org.gvsig.gui.beans.swing.JButton;
77

    
78

    
79
/**
80
 * To modify FeatureTypes from the interface.
81
 *
82
 * @author Vicente Caballero Navarro
83
 *
84
 */
85
public class FeatureTypeEditingPanel extends JPanel implements IWindow {
86
        private static final long serialVersionUID = -4284879326692474318L;
87

    
88
        WindowInfo windowInfo = 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 CreateNewAttributePanel panelNewField = new CreateNewAttributePanel();
107

    
108
        private FeatureStore featureStore = null;
109

    
110
        private JPanel jPanelButtons = null;
111

    
112
        private EditableFeatureType editableType = null;
113

    
114
        private class MyTableModel extends AbstractTableModel {
115

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

    
125
                }
126

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

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

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

    
139
                        switch (columnIndex) {
140
                        case 0:
141
                                return myField.getName();
142
                        case 1:
143
                                String strType = null;
144
                                int type = myField.getDataType();
145
                                if (type == DataTypes.STRING) {
146
                                        strType = "String";
147
                                } else if (type == DataTypes.DOUBLE) {
148
                                        strType = "Double";
149
                                } else if (type == DataTypes.INT) {
150
                                        strType = "Integer";
151
                                } else if (type == DataTypes.BOOLEAN) {
152
                                        strType = "Boolean";
153
                                } else if (type == DataTypes.DATE) {
154
                                        strType = "Date";
155
                                } else if (type == DataTypes.GEOMETRY) {
156
                                        strType = "Geometry";
157
                                }
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
                        }
200
                        String name = "";
201
                        int type = DataTypes.STRING;
202
                        int size = 0;
203
                        int precision = 0;
204
                        switch (columnIndex) {
205
                        case 0:
206
                                name = (String) aValue;
207
                                break;
208
                        case 1:
209
                                String strType = (String) aValue;
210
                                if (strType.equals("String")) {
211
                                        type = DataTypes.STRING;
212
                                }
213
                                if (strType.equals("Double")) {
214
                                        type = DataTypes.DOUBLE;
215
                                        precision = 5;
216
                                }
217
                                if (strType.equals("Integer")) {
218
                                        type = DataTypes.INT;
219
                                }
220
                                if (strType.equals("Boolean")) {
221
                                        type = DataTypes.BOOLEAN;
222
                                }
223
                                if (strType.equals("Date")) {
224
                                        type = DataTypes.DATE;
225
                                }
226
                                break;
227
                        case 2:
228
                                size = ((Integer) aValue).intValue();
229

    
230
                                // TODO: HACERLO BIEN
231
                                // if (ead.getDataType()==DataTypes.STRING) {
232
                                // ead.setPrecision(5);
233
                                // }
234
                        }
235
                        EditableFeatureAttributeDescriptor ead = editableType.add(name,
236
                                        type, size);
237
                        ead.setPrecision(precision);
238
                }
239

    
240
        }
241

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

    
266
        }
267

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

    
292
                this.add(getJPanelButtons(), BorderLayout.EAST);
293
                JPanel jPanelSouth = new JPanel();
294
                jPanelSouth.setLayout(flowLayout);
295
                jPanelSouth.add(getJBtnOK(), null);
296
                jPanelSouth.add(getJBtnCancel(), null);
297
                this.add(jPanelSouth, BorderLayout.SOUTH);
298

    
299
        }
300

    
301
        public WindowInfo getWindowInfo() {
302
                if (windowInfo == null) {
303
                        windowInfo = new WindowInfo(WindowInfo.MODALDIALOG
304
                                        | WindowInfo.PALETTE | WindowInfo.RESIZABLE);
305
                        windowInfo.setTitle(PluginServices.getText(this, "field_manager"));
306
                        windowInfo.setHeight(this.getHeight());
307
                        windowInfo.setWidth(this.getWidth());
308
                }
309
                return windowInfo;
310
        }
311

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

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

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

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

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

    
396
                                }
397
                        });
398

    
399
                }
400
                return jTableFields;
401
        }
402

    
403
        /**
404
         * This method initializes jBtnNewField
405
         *
406
         * @return javax.swing.JButton
407
         */
408
        private JButton getJBtnNewField() {
409
                if (jBtnNewField == null) {
410
                        jBtnNewField = new JButton();
411
                        jBtnNewField.setText(PluginServices.getText(this, "new_field"));
412
                        jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
413
                                private ArrayList tempFieldNames = new ArrayList();
414

    
415
                                {
416
                                        try {
417
                                                int size = editableType.size();
418
                                                for (int i = 0; i < size; i++) {
419
                                                        FeatureAttributeDescriptor ad = (FeatureAttributeDescriptor) editableType
420
                                                                        .get(i);
421
                                                        tempFieldNames.add(ad.getName());
422
                                                }
423
                                        } catch (Exception ex) {
424
                                        }
425
                                }
426

    
427
                                public void actionPerformed(java.awt.event.ActionEvent e) {
428
                                        ActionListener okAction;
429
                                        okAction = new java.awt.event.ActionListener() {
430

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

    
453
                                                }
454
                                        };
455
                                        panelNewField.setOkAction(okAction);
456
                                        String[] names = (String[]) tempFieldNames
457
                                                        .toArray(new String[0]);
458
                                        panelNewField.setCurrentFieldNames(names);
459
                                        panelNewField = (CreateNewAttributePanel) PluginServices
460
                                                        .getMDIManager().addWindow(panelNewField);
461
                                }
462
                        });
463
                }
464
                return jBtnNewField;
465
        }
466

    
467
        /**
468
         * This method initializes jButton
469
         *
470
         * @return javax.swing.JButton
471
         */
472
        private JButton getJBtnDelete() {
473
                if (jBtnDeleteField == null) {
474
                        jBtnDeleteField = new JButton();
475
                        jBtnDeleteField.setText(PluginServices
476
                                        .getText(this, "delete_field"));
477
                        jBtnDeleteField
478
                                        .addActionListener(new java.awt.event.ActionListener() {
479
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
480
                                                        int[] selecteds = jTableFields.getSelectedRows();
481
                                                        TableModel tm = jTableFields.getModel();
482

    
483
                                                        for (int i = selecteds.length - 1; i >= 0; i--) {
484
                                                                String fieldName = (String) tm.getValueAt(
485
                                                                                selecteds[i], 0);
486
                                                                editableType.remove(fieldName);
487
                                                        }
488
                                                        jTableFields.revalidate();
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.setText(PluginServices
504
                                        .getText(this, "rename_field"));
505
                        jBtnRenameField
506
                                        .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
                                                        for (int i = selecteds.length - 1; i >= 0; i--) {
512
                                                                String fieldName = (String) tm.getValueAt(
513
                                                                                selecteds[i], 0);
514
                                                                String newName = JOptionPane
515
                                                                                .showInputDialog(
516
                                                                                                (Component) PluginServices
517
                                                                                                                .getMDIManager()
518
                                                                                                                .getActiveWindow(),
519
                                                                                                PluginServices
520
                                                                                                                .getText(this,
521
                                                                                                                                "please_insert_new_field_name"),
522
                                                                                                fieldName);
523
                                                                if (newName == null) {
524
                                                                        return;
525
                                                                }
526
                                                                if (editableType.getIndex(newName) != -1) {
527
                                                                        NotificationManager.showMessageInfo(
528
                                                                                        PluginServices.getText(this,
529
                                                                                                        "field_already_exists"),
530
                                                                                        null);
531
                                                                        return;
532
                                                                }
533
                                                                FeatureAttributeDescriptor ad = (FeatureAttributeDescriptor) editableType
534
                                                                                .get(fieldName);
535
                                                                editableType.remove(ad.getName());
536
                                                                EditableFeatureAttributeDescriptor ead = editableType
537
                                                                                .add(newName, ad.getDataType(), ad
538
                                                                                                .getSize());
539
                                                                ead.setPrecision(ad.getPrecision());
540
                                                        }
541
                                                        jTableFields.repaint();
542
                                                }
543
                                        });
544
                }
545
                return jBtnRenameField;
546
        }
547

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

    
573
        /**
574
         * This method initializes jButton
575
         *
576
         * @return javax.swing.JButton
577
         */
578
        private JButton getJBtnCancel() {
579
                if (jBtnCancel == null) {
580
                        jBtnCancel = new JButton();
581
                        jBtnCancel.setText(PluginServices.getText(this, "cancelar"));
582
                        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
583
                                public void actionPerformed(java.awt.event.ActionEvent e) {
584
                                        PluginServices.getMDIManager().closeWindow(
585
                                                        FeatureTypeEditingPanel.this);
586
                                }
587
                        });
588
                        jBtnCancel.setVisible(false);
589
                }
590
                return jBtnCancel;
591
        }
592

    
593
        /**
594
         * This method initializes jPanelButtons
595
         *
596
         * @return javax.swing.JPanel
597
         */
598
        private JPanel getJPanelButtons() {
599
                if (jPanelButtons == null) {
600
                        jPanelButtons = new JPanel();
601
                        JPanel aux = new JPanel(new GridLayout(3, 1));
602
                        aux.add(getJBtnNewField());
603
                        aux.add(getJBtnRenameField());
604
                        aux.add(getJBtnDelete());
605
                        jPanelButtons.add(aux, BorderLayout.NORTH);
606
                }
607
                return jPanelButtons;
608
        }
609

    
610
        public Object getWindowProfile() {
611
                return WindowInfo.DIALOG_PROFILE;
612
        }
613

    
614
}