Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2058 / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / project / documents / table / gui / FeatureTypeEditingPanel.java @ 39255

History | View | Annotate | Download (22 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.app.project.documents.table.gui;
23

    
24
import java.awt.BorderLayout;
25
import java.awt.Component;
26
import java.awt.FlowLayout;
27
import java.awt.GridLayout;
28
import java.awt.event.ActionListener;
29
import java.text.ParseException;
30
import java.util.ArrayList;
31
import java.util.List;
32

    
33
import javax.swing.DefaultCellEditor;
34
import javax.swing.JButton;
35
import javax.swing.JComboBox;
36
import javax.swing.JLabel;
37
import javax.swing.JOptionPane;
38
import javax.swing.JPanel;
39
import javax.swing.JScrollPane;
40
import javax.swing.JTable;
41
import javax.swing.ListSelectionModel;
42
import javax.swing.event.ListSelectionEvent;
43
import javax.swing.event.ListSelectionListener;
44
import javax.swing.table.AbstractTableModel;
45
import javax.swing.table.TableColumn;
46
import javax.swing.table.TableModel;
47

    
48
import org.gvsig.andami.PluginServices;
49
import org.gvsig.andami.messages.NotificationManager;
50
import org.gvsig.andami.ui.mdiManager.IWindow;
51
import org.gvsig.andami.ui.mdiManager.WindowInfo;
52
import org.gvsig.app.project.documents.table.TableOperations;
53
import org.gvsig.fmap.dal.DataTypes;
54
import org.gvsig.fmap.dal.exception.DataException;
55
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
56
import org.gvsig.fmap.dal.feature.EditableFeatureType;
57
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
58
import org.gvsig.fmap.dal.feature.FeatureStore;
59
import org.gvsig.i18n.Messages;
60
import org.gvsig.tools.swing.api.ToolsSwingLocator;
61
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63

    
64
/**
65
 * To modify FeatureTypes from the interface.
66
 * 
67
 * @author Vicente Caballero Navarro
68
 * 
69
 */
70
public class FeatureTypeEditingPanel extends JPanel implements IWindow {
71

    
72
    private static final long serialVersionUID = -4284879326692474318L;
73

    
74
    private static final Logger logger = LoggerFactory
75
        .getLogger(FeatureTypeEditingPanel.class);
76

    
77
    WindowInfo windowInfo = null;
78

    
79
    private JLabel jLabel = null;
80

    
81
    private JScrollPane jScrollPane = null;
82

    
83
    private JTable jTableFields = null;
84

    
85
    private JButton jBtnNewField = null;
86

    
87
    private JButton jBtnDeleteField = null;
88

    
89
    private JButton jBtnRenameField = null;
90

    
91
    private JButton jBtnOK = null;
92

    
93
    private JButton jBtnCancel = null;
94

    
95
    private CreateNewAttributePanel panelNewField =
96
        new CreateNewAttributePanel();
97

    
98
    private FeatureStore featureStore = null;
99

    
100
    private JPanel jPanelButtons = null;
101

    
102
    private EditableFeatureType editableType = null;
103

    
104
    private class MyTableModel extends AbstractTableModel {
105

    
106
        /**
107
                 * 
108
                 */
109
        private static final long serialVersionUID = -2847526298987536118L;
110

    
111
        public MyTableModel() {
112

    
113
        }
114

    
115
        public int getColumnCount() {
116
            return 5;
117
        }
118

    
119
        public int getRowCount() {
120
            return editableType.size();
121
        }
122

    
123
        public Object getValueAt(int rowIndex, int columnIndex) {
124
            FeatureAttributeDescriptor myField = null;
125
            myField = (FeatureAttributeDescriptor) editableType.get(rowIndex);
126

    
127
            switch (columnIndex) {
128
            case 0:
129
                return myField.getName();
130
            case 1:
131
                return myField.getDataType().getName();
132
            case 2:
133
                return new Integer(myField.getSize());
134
            case 3:
135
                return new Integer(myField.getPrecision());
136
            case 4:
137
                return myField.getDefaultValue();
138

    
139
            }
140
            return null;
141
        }
142

    
143
        public Class<?> getColumnClass(int columnIndex) {
144
            return super.getColumnClass(columnIndex);
145
        }
146

    
147
        public String getColumnName(int column) {
148
            switch (column) {
149
            case 0:
150
                return PluginServices.getText(this, "field_name");
151
            case 1:
152
                return PluginServices.getText(this, "field_type");
153
            case 2:
154
                return PluginServices.getText(this, "field_length");
155
            case 3:
156
                return PluginServices.getText(this, "field_decimal_count");
157
            case 4:
158
                return PluginServices.getText(this, "field_default_value");
159

    
160
            }
161
            return super.getColumnName(column);
162
        }
163

    
164
        public boolean isCellEditable(int rowIndex, int columnIndex) {
165
            return false;
166

    
167
        }
168

    
169
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
170
            if (columnIndex == 0) {
171
                editableType.remove(rowIndex);
172
            }
173
            String name = "";
174
            int type = DataTypes.STRING;
175
            int size = 0;
176
            int precision = 0;
177
            switch (columnIndex) {
178
            case 0:
179
                name = (String) aValue;
180
                break;
181
            case 1:
182
                String strType = (String) aValue;
183
                if (strType.equals("String")) {
184
                    type = DataTypes.STRING;
185
                }
186
                if (strType.equals("Double")) {
187
                    type = DataTypes.DOUBLE;
188
                    precision = 5;
189
                }
190
                if (strType.equals("Integer")) {
191
                    type = DataTypes.INT;
192
                }
193
                if (strType.equals("Boolean")) {
194
                    type = DataTypes.BOOLEAN;
195
                }
196
                if (strType.equals("Date")) {
197
                    type = DataTypes.DATE;
198
                }
199
                break;
200
            case 2:
201
                size = ((Integer) aValue).intValue();
202

    
203
                // TODO: HACERLO BIEN
204
                // if (ead.getDataType()==DataTypes.STRING) {
205
                // ead.setPrecision(5);
206
                // }
207
            }
208
            EditableFeatureAttributeDescriptor ead =
209
                editableType.add(name, type, size);
210
            ead.setPrecision(precision);
211
        }
212

    
213
    }
214

    
215
    /**
216
     * This method initializes
217
     * 
218
     * @throws DataException
219
     * 
220
     */
221
    public FeatureTypeEditingPanel(FeatureStore fs) throws DataException {
222
        super();
223
        this.featureStore = fs;
224
        this.editableType = fs.getDefaultFeatureType().getEditable();
225
        initialize();
226
        // Add a new row
227
        TableModel tm;
228
        tm = new MyTableModel();
229
        getJTableFields().setModel(tm);
230
        // Esto lo a?ado aqu? porque si no tiene registros, no hace caso.
231
        // (Por eso no
232
        // lo pongo en getJTable()
233
        TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1);
234
        JComboBox comboBox = new JComboBox();
235
        comboBox.addItem("Boolean");
236
        comboBox.addItem("Date");
237
        comboBox.addItem("Integer");
238
        comboBox.addItem("Double");
239
        comboBox.addItem("String");
240
        typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
241

    
242
    }
243

    
244
    /**
245
     * This method initializes this
246
     * 
247
     */
248
    private void initialize() {
249
        FlowLayout flowLayout = new FlowLayout();
250
        flowLayout.setVgap(5);
251
        flowLayout.setHgap(0);
252
        BorderLayout borderLayout = new BorderLayout();
253
        borderLayout.setHgap(15);
254
        borderLayout.setVgap(15);
255
        jLabel = new JLabel();
256
        jLabel.setText(PluginServices.getText(this, "add_delete_edit_fields")
257
            + ": ");
258
        this.setLayout(borderLayout);
259
        this.setSize(new java.awt.Dimension(663, 404));
260
        this.setPreferredSize(new java.awt.Dimension(getWidth(), getHeight()));
261
        JPanel jPanelNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
262
        jPanelNorth.add(jLabel);
263
        this.add(jPanelNorth, BorderLayout.NORTH);
264
        JPanel jPanelCenter =
265
            new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
266
        jPanelCenter.add(getJScrollPane());
267
        this.add(jPanelCenter, BorderLayout.CENTER);
268

    
269
        this.add(getJPanelButtons(), BorderLayout.EAST);
270
        JPanel jPanelSouth = new JPanel();
271
        jPanelSouth.setLayout(flowLayout);
272
        jPanelSouth.add(getJBtnOK(), null);
273
        jPanelSouth.add(getJBtnCancel(), null);
274
        this.add(jPanelSouth, BorderLayout.SOUTH);
275

    
276
    }
277

    
278
    public WindowInfo getWindowInfo() {
279
        if (windowInfo == null) {
280
            windowInfo =
281
                new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.PALETTE
282
                    | WindowInfo.RESIZABLE);
283
            windowInfo.setTitle(PluginServices.getText(this, "field_manager"));
284
            windowInfo.setHeight(this.getHeight());
285
            windowInfo.setWidth(this.getWidth());
286
        }
287
        return windowInfo;
288
    }
289

    
290
    // /**
291
    // * Convierte lo que hay en la tabla en una definici?n de campos adecuada
292
    // * para crear un LayerDefinition
293
    // *
294
    // * @return
295
    // */
296
    // public FieldDescription[] getFieldsDescription() {
297
    // DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
298
    // FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()];
299
    //
300
    // for (int i = 0; i < tm.getRowCount(); i++) {
301
    // fieldsDesc[i] = new FieldDescription();
302
    // fieldsDesc[i].setFieldName((String) tm.getValueAt(i, 0));
303
    // String strType = (String) tm.getValueAt(i, 1);
304
    // if (strType.equals("String"))
305
    // fieldsDesc[i].setFieldType(Types.VARCHAR);
306
    // if (strType.equals("Double"))
307
    // fieldsDesc[i].setFieldType(Types.DOUBLE);
308
    // if (strType.equals("Integer"))
309
    // fieldsDesc[i].setFieldType(Types.INTEGER);
310
    // if (strType.equals("Boolean"))
311
    // fieldsDesc[i].setFieldType(Types.BOOLEAN);
312
    // if (strType.equals("Date"))
313
    // fieldsDesc[i].setFieldType(Types.DATE);
314
    // int fieldLength = Integer.parseInt((String) tm.getValueAt(i, 2));
315
    // fieldsDesc[i].setFieldLength(fieldLength);
316
    //
317
    // // TODO: HACERLO BIEN
318
    // if (strType.equals("Double"))
319
    // fieldsDesc[i].setFieldDecimalCount(5);
320
    //
321
    // }
322
    //
323
    // return fieldsDesc;
324
    // }
325

    
326
    /**
327
     * This method initializes jScrollPane
328
     * 
329
     * @return javax.swing.JScrollPane
330
     */
331
    private JScrollPane getJScrollPane() {
332
        if (jScrollPane == null) {
333
            jScrollPane = new JScrollPane();
334
            jScrollPane.setPreferredSize(new java.awt.Dimension(482, 350));
335
            jScrollPane.setViewportView(getJTableFields());
336
        }
337
        return jScrollPane;
338
    }
339

    
340
    /**
341
     * This method initializes jTableFields
342
     * 
343
     * @return javax.swing.JTable
344
     */
345
    private JTable getJTableFields() {
346
        if (jTableFields == null) {
347
            jTableFields = new JTable();
348
            jTableFields
349
                .setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
350

    
351
            jTableFields.setColumnSelectionAllowed(false);
352
            // Ask to be notified of selection changes.
353
            ListSelectionModel rowSM = jTableFields.getSelectionModel();
354
            rowSM.addListSelectionListener(new ListSelectionListener() {
355

    
356
                public void valueChanged(ListSelectionEvent e) {
357
                    // Ignore extra messages.
358
                    if (e.getValueIsAdjusting()) {
359
                        return;
360
                    }
361

    
362
                    ListSelectionModel lsm = (ListSelectionModel) e.getSource();
363
                    if (lsm.isSelectionEmpty()) {
364
                        // no rows are selected
365
                        jBtnDeleteField.setEnabled(false);
366
                    } else {
367
                        jBtnDeleteField.setEnabled(true);
368
                    }
369
                    if (jTableFields.getSelectedRows().length != 1) {
370
                        getJBtnRenameField().setEnabled(false);
371
                    } else {
372
                        getJBtnRenameField().setEnabled(true);
373
                    }
374

    
375
                }
376
            });
377

    
378
        }
379
        return jTableFields;
380
    }
381

    
382
    /**
383
     * This method initializes jBtnNewField
384
     * 
385
     * @return javax.swing.JButton
386
     */
387
    private JButton getJBtnNewField() {
388
        if (jBtnNewField == null) {
389
            jBtnNewField =
390
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
391
            jBtnNewField.setText(PluginServices.getText(this, "new_field"));
392
            jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
393

    
394
                private List<String> tempFieldNames = new ArrayList<String>();
395

    
396
                {
397
                    try {
398
                        int size = editableType.size();
399
                        for (int i = 0; i < size; i++) {
400
                            FeatureAttributeDescriptor ad =
401
                                (FeatureAttributeDescriptor) editableType
402
                                    .get(i);
403
                            tempFieldNames.add(ad.getName());
404
                        }
405
                    } catch (Exception ex) {
406
                        logger.warn("Can't initialize tempFieldNames", ex);
407
                    }
408
                }
409

    
410
                public void actionPerformed(java.awt.event.ActionEvent e) {
411
                    ActionListener okAction;
412
                    okAction = new java.awt.event.ActionListener() {
413

    
414
                        public void actionPerformed(java.awt.event.ActionEvent e) {
415
                            try {
416
                                EditableFeatureAttributeDescriptor ead =
417
                                    panelNewField
418
                                        .loadFieldDescription(editableType);
419
                                if (ead == null) {
420
                                    return;
421
                                }
422
                                if (ead.getType() == DataTypes.STRING
423
                                    && ead.getSize() > TableOperations.MAX_FIELD_LENGTH) {
424
                                    NotificationManager.showMessageInfo(
425
                                        PluginServices.getText(this,
426
                                            "max_length_is")
427
                                            + ":"
428
                                            + TableOperations.MAX_FIELD_LENGTH,
429
                                        null);
430
                                    ead.setSize(TableOperations.MAX_FIELD_LENGTH);
431
                                }
432
                                tempFieldNames.add(ead.getName());
433
                                jTableFields.revalidate();
434
                                PluginServices.getMDIManager().closeWindow(
435
                                    panelNewField);
436
                            } catch (ParseException e2) {
437
                                NotificationManager.addError(e2);
438
                            }
439

    
440
                        }
441
                    };
442
                    panelNewField.setOkAction(okAction);
443
                    String[] names =
444
                        (String[]) tempFieldNames.toArray(new String[0]);
445
                    panelNewField.setCurrentFieldNames(names);
446
                    panelNewField =
447
                        (CreateNewAttributePanel) PluginServices
448
                            .getMDIManager().addWindow(panelNewField);
449
                }
450
            });
451
        }
452
        return jBtnNewField;
453
    }
454

    
455
    /**
456
     * This method initializes jButton
457
     * 
458
     * @return javax.swing.JButton
459
     */
460
    private JButton getJBtnDelete() {
461
        if (jBtnDeleteField == null) {
462
            jBtnDeleteField =
463
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
464
            jBtnDeleteField.setText(PluginServices
465
                .getText(this, "delete_field"));
466
            jBtnDeleteField
467
                .addActionListener(new java.awt.event.ActionListener() {
468

    
469
                    public void actionPerformed(java.awt.event.ActionEvent e) {
470
                        int[] selecteds = jTableFields.getSelectedRows();
471
                        TableModel tm = jTableFields.getModel();
472

    
473
                        for (int i = selecteds.length - 1; i >= 0; i--) {
474
                            String fieldName =
475
                                (String) tm.getValueAt(selecteds[i], 0);
476
                            editableType.remove(fieldName);
477
                        }
478
                        jTableFields.revalidate();
479
                    }
480
                });
481
        }
482
        return jBtnDeleteField;
483
    }
484

    
485
    /**
486
     * This method initializes jBtnRenameField
487
     * 
488
     * @return javax.swing.JButton
489
     */
490
    private JButton getJBtnRenameField() {
491
        if (jBtnRenameField == null) {
492
            jBtnRenameField =
493
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
494
            jBtnRenameField.setText(PluginServices
495
                .getText(this, "rename_field"));
496
            jBtnRenameField
497
                .addActionListener(new java.awt.event.ActionListener() {
498

    
499
                    public void actionPerformed(java.awt.event.ActionEvent e) {
500
                        int[] selecteds = jTableFields.getSelectedRows();
501
                        TableModel tm = jTableFields.getModel();
502

    
503
                        for (int i = selecteds.length - 1; i >= 0; i--) {
504
                            String fieldName =
505
                                (String) tm.getValueAt(selecteds[i], 0);
506
                            String newName =
507
                                JOptionPane.showInputDialog(
508
                                    (Component) PluginServices.getMDIManager()
509
                                        .getActiveWindow(), PluginServices
510
                                        .getText(this,
511
                                            "_Please_insert_new_field_name_Cannot_be_undone"),
512
                                    fieldName);
513
                            if (newName == null) {
514
                                return;
515
                            }
516
                            if (editableType.getIndex(newName) != -1) {
517
                                NotificationManager.showMessageInfo(
518
                                    PluginServices.getText(this,
519
                                        "field_already_exists"), null);
520
                                return;
521
                            }
522
                            
523

    
524
                            try {
525
                                TableOperations.renameColumn(
526
                                    featureStore, fieldName, newName);
527
                            } catch (DataException de) {
528
                                JOptionPane.showMessageDialog(
529
                                    jTableFields,
530
                                    Messages.getText("_Unable_to_rename_field")
531
                                    + ": " + de.getMessage(),
532
                                    Messages.getText("_Rename_column"),
533
                                    JOptionPane.ERROR_MESSAGE);
534
                            }
535
                            
536
                            /*
537
                             * We need to close the dialog
538
                             * to avoid another update of the
539
                             * feature type
540
                             */
541
                            PluginServices.getMDIManager().closeWindow(
542
                                FeatureTypeEditingPanel.this);
543

    
544
                            
545
                        }
546
                        jTableFields.repaint();
547
                    }
548
                });
549
        }
550
        return jBtnRenameField;
551
    }
552

    
553
    /**
554
     * This method initializes jBtnOK
555
     * 
556
     * @return javax.swing.JButton
557
     */
558
    private JButton getJBtnOK() {
559
        if (jBtnOK == null) {
560
            jBtnOK =
561
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
562
            jBtnOK.setText(PluginServices.getText(this, "aceptar"));
563
            jBtnOK.addActionListener(new java.awt.event.ActionListener() {
564

    
565
                public void actionPerformed(java.awt.event.ActionEvent e) {
566
                    try {
567
                        featureStore.update(editableType);
568
                    } catch (DataException e1) {
569
                        NotificationManager.showMessageError(PluginServices
570
                            .getText(this, "update_featuretype_error"), e1);
571
                    }
572
                    PluginServices.getMDIManager().closeWindow(
573
                        FeatureTypeEditingPanel.this);
574
                }
575
            });
576
        }
577
        return jBtnOK;
578
    }
579

    
580
    /**
581
     * This method initializes jButton
582
     * 
583
     * @return javax.swing.JButton
584
     */
585
    private JButton getJBtnCancel() {
586
        if (jBtnCancel == null) {
587
            jBtnCancel =
588
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
589
            jBtnCancel.setText(PluginServices.getText(this, "cancelar"));
590
            jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
591

    
592
                public void actionPerformed(java.awt.event.ActionEvent e) {
593
                    PluginServices.getMDIManager().closeWindow(
594
                        FeatureTypeEditingPanel.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
        return jPanelButtons;
617
    }
618

    
619
    public Object getWindowProfile() {
620
        return WindowInfo.DIALOG_PROFILE;
621
    }
622

    
623
}