Statistics
| Revision:

root / branches / v2_0_0_prep / 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 @ 38564

History | View | Annotate | Download (21.4 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.tools.swing.api.ToolsSwingLocator;
60
import org.slf4j.Logger;
61
import org.slf4j.LoggerFactory;
62

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

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

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

    
76
    WindowInfo windowInfo = null;
77

    
78
    private JLabel jLabel = null;
79

    
80
    private JScrollPane jScrollPane = null;
81

    
82
    private JTable jTableFields = null;
83

    
84
    private JButton jBtnNewField = null;
85

    
86
    private JButton jBtnDeleteField = null;
87

    
88
    private JButton jBtnRenameField = null;
89

    
90
    private JButton jBtnOK = null;
91

    
92
    private JButton jBtnCancel = null;
93

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

    
97
    private FeatureStore featureStore = null;
98

    
99
    private JPanel jPanelButtons = null;
100

    
101
    private EditableFeatureType editableType = null;
102

    
103
    private class MyTableModel extends AbstractTableModel {
104

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

    
110
        public MyTableModel() {
111

    
112
        }
113

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

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

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

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

    
138
            }
139
            return null;
140
        }
141

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

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

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

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

    
166
        }
167

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

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

    
212
    }
213

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

    
241
    }
242

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

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

    
275
    }
276

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

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

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

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

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

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

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

    
374
                }
375
            });
376

    
377
        }
378
        return jTableFields;
379
    }
380

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

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

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

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

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

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

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

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

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

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

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

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

    
537
    /**
538
     * This method initializes jBtnOK
539
     * 
540
     * @return javax.swing.JButton
541
     */
542
    private JButton getJBtnOK() {
543
        if (jBtnOK == null) {
544
            jBtnOK =
545
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
546
            jBtnOK.setText(PluginServices.getText(this, "aceptar"));
547
            jBtnOK.addActionListener(new java.awt.event.ActionListener() {
548

    
549
                public void actionPerformed(java.awt.event.ActionEvent e) {
550
                    try {
551
                        featureStore.update(editableType);
552
                    } catch (DataException e1) {
553
                        NotificationManager.showMessageError(PluginServices
554
                            .getText(this, "update_featuretype_error"), e1);
555
                    }
556
                    PluginServices.getMDIManager().closeWindow(
557
                        FeatureTypeEditingPanel.this);
558
                }
559
            });
560
        }
561
        return jBtnOK;
562
    }
563

    
564
    /**
565
     * This method initializes jButton
566
     * 
567
     * @return javax.swing.JButton
568
     */
569
    private JButton getJBtnCancel() {
570
        if (jBtnCancel == null) {
571
            jBtnCancel =
572
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
573
            jBtnCancel.setText(PluginServices.getText(this, "cancelar"));
574
            jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
575

    
576
                public void actionPerformed(java.awt.event.ActionEvent e) {
577
                    PluginServices.getMDIManager().closeWindow(
578
                        FeatureTypeEditingPanel.this);
579
                }
580
            });
581
            jBtnCancel.setVisible(false);
582
        }
583
        return jBtnCancel;
584
    }
585

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

    
603
    public Object getWindowProfile() {
604
        return WindowInfo.DIALOG_PROFILE;
605
    }
606

    
607
}