Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / 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 @ 40558

History | View | Annotate | Download (23 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.table.gui;
25

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

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

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

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

    
74
    private static final long serialVersionUID = -4284879326692474318L;
75

    
76
    private static final Logger logger = LoggerFactory
77
        .getLogger(FeatureTypeEditingPanel.class);
78

    
79
    WindowInfo windowInfo = null;
80

    
81
    private JLabel jLabel = null;
82

    
83
    private JScrollPane jScrollPane = null;
84

    
85
    private JTable jTableFields = null;
86

    
87
    private JButton jBtnNewField = null;
88

    
89
    private JButton jBtnDeleteField = null;
90

    
91
    private JButton jBtnRenameField = null;
92

    
93
    private JButton jBtnOK = null;
94

    
95
    private JButton jBtnCancel = null;
96

    
97
    private CreateNewAttributePanel panelNewField =
98
        new CreateNewAttributePanel();
99

    
100
    private FeatureStore featureStore = null;
101

    
102
    private JPanel jPanelButtons = null;
103

    
104
    private EditableFeatureType editableType = null;
105

    
106
    private class MyTableModel extends AbstractTableModel {
107

    
108
        /**
109
                 * 
110
                 */
111
        private static final long serialVersionUID = -2847526298987536118L;
112

    
113
        public MyTableModel() {
114

    
115
        }
116

    
117
        public int getColumnCount() {
118
            return 5;
119
        }
120

    
121
        public int getRowCount() {
122
            return editableType.size();
123
        }
124

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

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

    
141
            }
142
            return null;
143
        }
144

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

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

    
162
            }
163
            return super.getColumnName(column);
164
        }
165

    
166
        public boolean isCellEditable(int rowIndex, int columnIndex) {
167
            return false;
168

    
169
        }
170

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

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

    
215
    }
216

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

    
244
    }
245

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

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

    
278
    }
279

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

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

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

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

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

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

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

    
377
                }
378
            });
379

    
380
        }
381
        return jTableFields;
382
    }
383

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

    
396
                private List<String> tempFieldNames = new ArrayList<String>();
397

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

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

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

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

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

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

    
475
                        for (int i = selecteds.length - 1; i >= 0; i--) {
476
                            String fieldName =
477
                                (String) tm.getValueAt(selecteds[i], 0);
478
                            
479
                            String old_geom_fld = editableType.getDefaultGeometryAttributeName();
480
                            if (old_geom_fld.compareTo(fieldName) == 0) {
481
                                JOptionPane.showMessageDialog(
482
                                    jTableFields,
483
                                    Messages.getText("_Cannot_delete_geometry_field"),
484
                                    Messages.getText("_Remove_column"),
485
                                    JOptionPane.WARNING_MESSAGE);
486
                                continue;
487
                            }
488

    
489
                            editableType.remove(fieldName);
490
                        }
491
                        jTableFields.getSelectionModel().clearSelection();
492
                        jTableFields.revalidate();
493
                    }
494
                });
495
        }
496
        return jBtnDeleteField;
497
    }
498

    
499
    /**
500
     * This method initializes jBtnRenameField
501
     * 
502
     * @return javax.swing.JButton
503
     */
504
    private JButton getJBtnRenameField() {
505
        if (jBtnRenameField == null) {
506
            jBtnRenameField =
507
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
508
            jBtnRenameField.setText(PluginServices
509
                .getText(this, "rename_field"));
510
            jBtnRenameField
511
                .addActionListener(new java.awt.event.ActionListener() {
512

    
513
                    public void actionPerformed(java.awt.event.ActionEvent e) {
514
                        int[] selecteds = jTableFields.getSelectedRows();
515
                        TableModel tm = jTableFields.getModel();
516

    
517
                        for (int i = selecteds.length - 1; i >= 0; i--) {
518
                            String fieldName =
519
                                (String) tm.getValueAt(selecteds[i], 0);
520
                            
521
                            String old_geom_fld = editableType.getDefaultGeometryAttributeName();
522
                            if (old_geom_fld.compareTo(fieldName) == 0) {
523
                                JOptionPane.showMessageDialog(
524
                                    jTableFields,
525
                                    Messages.getText("_Cannot_rename_geometry_field"),
526
                                    Messages.getText("_Rename_column"),
527
                                    JOptionPane.WARNING_MESSAGE);
528
                                continue;
529
                            }
530

    
531
                            String newName =
532
                                JOptionPane.showInputDialog(
533
                                    (Component) PluginServices.getMDIManager()
534
                                        .getActiveWindow(), PluginServices
535
                                        .getText(this, "_Please_insert_new_field_name"),
536
                                    fieldName);
537
                            if (newName == null) {
538
                                return;
539
                            }
540
                            if (editableType.getIndex(newName) != -1) {
541
                                NotificationManager.showMessageInfo(
542
                                    PluginServices.getText(this,
543
                                        "field_already_exists"), null);
544
                                return;
545
                            }
546

    
547
                            try {
548
                                EditableFeatureAttributeDescriptor efad =
549
                                    (EditableFeatureAttributeDescriptor)
550
                                    editableType.getAttributeDescriptor(fieldName);
551
                                efad.setName(newName);
552
                                
553
                            } catch (Exception de) {
554
                                JOptionPane.showMessageDialog(
555
                                    jTableFields,
556
                                    Messages.getText("_Unable_to_rename_field")
557
                                    + ": " + de.getMessage(),
558
                                    Messages.getText("_Rename_column"),
559
                                    JOptionPane.ERROR_MESSAGE);
560
                            }
561
                        }
562
                        jTableFields.repaint();
563
                    }
564
                });
565
        }
566
        return jBtnRenameField;
567
    }
568

    
569
    /**
570
     * This method initializes jBtnOK
571
     * 
572
     * @return javax.swing.JButton
573
     */
574
    private JButton getJBtnOK() {
575
        if (jBtnOK == null) {
576
            jBtnOK =
577
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
578
            jBtnOK.setText(PluginServices.getText(this, "aceptar"));
579
            jBtnOK.addActionListener(new java.awt.event.ActionListener() {
580

    
581
                public void actionPerformed(java.awt.event.ActionEvent e) {
582
                    try {
583
                        featureStore.update(editableType);
584
                    } catch (DataException e1) {
585
                        NotificationManager.showMessageError(PluginServices
586
                            .getText(this, "update_featuretype_error"), e1);
587
                    }
588
                    PluginServices.getMDIManager().closeWindow(
589
                        FeatureTypeEditingPanel.this);
590
                }
591
            });
592
        }
593
        return jBtnOK;
594
    }
595

    
596
    /**
597
     * This method initializes jButton
598
     * 
599
     * @return javax.swing.JButton
600
     */
601
    private JButton getJBtnCancel() {
602
        if (jBtnCancel == null) {
603
            jBtnCancel =
604
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
605
            jBtnCancel.setText(PluginServices.getText(this, "cancelar"));
606
            jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
607

    
608
                public void actionPerformed(java.awt.event.ActionEvent e) {
609
                    PluginServices.getMDIManager().closeWindow(
610
                        FeatureTypeEditingPanel.this);
611
                }
612
            });
613
            // jBtnCancel.setVisible(false);
614
        }
615
        return jBtnCancel;
616
    }
617

    
618
    /**
619
     * This method initializes jPanelButtons
620
     * 
621
     * @return javax.swing.JPanel
622
     */
623
    private JPanel getJPanelButtons() {
624
        if (jPanelButtons == null) {
625
            jPanelButtons = new JPanel();
626
            JPanel aux = new JPanel(new GridLayout(3, 1));
627
            aux.add(getJBtnNewField());
628
            aux.add(getJBtnRenameField());
629
            aux.add(getJBtnDelete());
630
            jPanelButtons.add(aux, BorderLayout.NORTH);
631
        }
632
        return jPanelButtons;
633
    }
634

    
635
    public Object getWindowProfile() {
636
        return WindowInfo.DIALOG_PROFILE;
637
    }
638

    
639
}