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 / CreateNewAttributePanel.java @ 43975

History | View | Annotate | Download (16.5 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.GridLayout;
28
import java.awt.LayoutManager;
29
import java.awt.event.ActionListener;
30
import java.awt.event.KeyEvent;
31
import java.awt.event.KeyListener;
32
import java.text.ParseException;
33
import javax.swing.JCheckBox;
34

    
35
import javax.swing.JComboBox;
36
import javax.swing.JLabel;
37
import javax.swing.JOptionPane;
38
import javax.swing.JPanel;
39
import javax.swing.JTextField;
40

    
41
import org.gvsig.andami.PluginServices;
42
import org.gvsig.andami.messages.NotificationManager;
43
import org.gvsig.andami.ui.mdiManager.IWindow;
44
import org.gvsig.andami.ui.mdiManager.WindowInfo;
45
import org.gvsig.fmap.dal.DataTypes;
46
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
47
import org.gvsig.fmap.dal.feature.EditableFeatureType;
48
import org.gvsig.gui.beans.AcceptCancelPanel;
49
import org.gvsig.i18n.Messages;
50
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.dataTypes.DataTypesManager;
52

    
53
/**
54
 * To create new FeatureAttributeDescriptor from the interface.
55
 *
56
 * @author Vicente Caballero Navarro
57
 *
58
 */
59
public class CreateNewAttributePanel extends JPanel implements IWindow {
60

    
61
    /**
62
         *
63
         */
64
    private static final long serialVersionUID = 6447641307779709964L;
65
    private static final String DEFAULT_FIELD_LENGTH = "50";
66
    private JLabel jLblFieldName = null;
67
    private JTextField jTxtFieldName = null;
68
    private JLabel jLblFieldType = null;
69
    private JComboBox jCboFieldType = null;
70
    private JLabel jLblFieldLength = null;
71
    private JTextField jTxtFieldLength = null;
72
    private JLabel jLblFieldPrecision = null;
73
    private JTextField jTxtFieldPrecision = null;
74
    private JLabel jLblDefaultValue = null;
75
    private JTextField jTxtDefaultValue = null;
76
    private JLabel jLblEmulated = null;
77
    private JCheckBox jChbEmulated = null;
78
    private WindowInfo viewInfo;
79
    private JPanel jPanel = null;
80
    private AcceptCancelPanel jPanelOkCancel = null;
81
    private JPanel jPnlFields = null;
82
    private int maxAttributeNameSize=-1;
83
    private KeyListener checkInt = new KeyListener() {
84

    
85
        public void keyPressed(KeyEvent e) {
86
            // do nothing
87
        }
88

    
89
        public void keyReleased(KeyEvent e) {
90
            JTextField component = (JTextField) e.getComponent();
91

    
92
            try {
93
                component.setText(String.valueOf(Integer.parseInt(component
94
                    .getText())));
95

    
96
            } catch (Exception ex) {
97
                String text = component.getText();
98
                text =
99
                    (text.length() <= 1) ? "0" : text.substring(0,
100
                        text.length() - 1);
101
                component.setText(text);
102
            }
103
        }
104

    
105
        public void keyTyped(KeyEvent e) {
106
            // do nothing
107
        }
108
    };
109
    private String[] currentFieldNames;
110

    
111
    public CreateNewAttributePanel() {
112
        super();
113
        initialize();
114
    }
115

    
116
    public CreateNewAttributePanel(boolean isDoubleBuffered) {
117
        super(isDoubleBuffered);
118
        initialize();
119
    }
120

    
121
    public CreateNewAttributePanel(LayoutManager layout) {
122
        super(layout);
123
        initialize();
124
    }
125

    
126
    public CreateNewAttributePanel(LayoutManager layout,
127
        boolean isDoubleBuffered) {
128
        super(layout, isDoubleBuffered);
129
        initialize();
130
    }
131

    
132
    public WindowInfo getWindowInfo() {
133
        if (viewInfo == null) {
134
            viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
135
            viewInfo.setWidth(this.getWidth() + 8);
136
            viewInfo.setHeight(this.getHeight());
137
            viewInfo.setTitle(PluginServices.getText(this,
138
                "new_field_properties"));
139
        }
140
        return viewInfo;
141
    }
142

    
143
    /**
144
     * This method initializes this
145
     *
146
     * @return void
147
     */
148
    private void initialize() {
149
        this.setLayout(new BorderLayout());
150
        this.setSize(300, 210);
151
        this.setPreferredSize(new java.awt.Dimension(300, 210));
152
        this.add(getJPanel(), java.awt.BorderLayout.CENTER);
153
        this.add(getJPanelOkCancel(), java.awt.BorderLayout.SOUTH);
154
    }
155

    
156
    /**
157
     * This method initializes jTxtFieldName
158
     *
159
     * @return javax.swing.JTextField
160
     */
161
    private JTextField getJTxtFieldName() {
162
        if (jTxtFieldName == null) {
163
            jTxtFieldName = new JTextField();
164
            jTxtFieldName.setBounds(new java.awt.Rectangle(147, 15, 138, 22));
165
        }
166
        return jTxtFieldName;
167
    }
168

    
169
    /**
170
     * This method initializes jCboFieldType
171
     *
172
     * @return javax.swing.JComboBox
173
     */
174
    private JComboBox getJCboFieldType() {
175
        DataTypesManager manager = ToolsLocator.getDataTypesManager();
176
        if (jCboFieldType == null) {
177
            jCboFieldType = new JComboBox();
178
            jCboFieldType.setBounds(new java.awt.Rectangle(147, 52, 138, 22));
179
            jCboFieldType.addItem(manager.getTypeName(DataTypes.BOOLEAN));
180
            jCboFieldType.addItem(manager.getTypeName(DataTypes.DATE));
181
            jCboFieldType.addItem(manager.getTypeName(DataTypes.INT));
182
            jCboFieldType.addItem(manager.getTypeName(DataTypes.DOUBLE));
183
            jCboFieldType.addItem(manager.getTypeName(DataTypes.STRING));
184

    
185
            jCboFieldType.setSelectedIndex(4);
186
            jCboFieldType
187
                .addActionListener(new java.awt.event.ActionListener() {
188

    
189
                    public void actionPerformed(java.awt.event.ActionEvent e) {
190
                        DataTypesManager manager =
191
                            ToolsLocator.getDataTypesManager();
192
                        String strType =
193
                            (String) getJCboFieldType().getModel()
194
                                .getSelectedItem();
195
                        if (strType == manager.getTypeName(DataTypes.DOUBLE)) {
196
                            getJTxtFieldPrecision().setEnabled(true);
197
                            if (getJTxtFieldPrecision().getText().equals("")) {
198
                                getJTxtFieldPrecision().setText("3");
199
                            } else {
200
                                try {
201
                                    Integer.parseInt(getJTxtFieldPrecision()
202
                                        .getText());
203
                                } catch (NumberFormatException e1) {
204
                                    getJTxtFieldPrecision().setText("3");
205
                                }
206
                            }
207
                        } else {
208
                            getJTxtFieldPrecision().setEnabled(false);
209
                        }
210
                        if (strType == manager.getTypeName(DataTypes.BOOLEAN)) {
211
                            getJTxtFieldLength().setText("0");
212
                            getJTxtFieldLength().setEnabled(false);
213
                        } else {
214
                            getJTxtFieldLength().setEnabled(true);
215
                        }
216

    
217
                    }
218
                });
219

    
220
        }
221
        return jCboFieldType;
222
    }
223

    
224
    /**
225
     * This method initializes jTxtFieldLength
226
     *
227
     * @return javax.swing.JTextField
228
     */
229
    private JTextField getJTxtFieldLength() {
230
        if (jTxtFieldLength == null) {
231
            jTxtFieldLength = new JTextField();
232
            jTxtFieldLength.setBounds(new java.awt.Rectangle(147, 89, 138, 22));
233
            jTxtFieldLength.setText(DEFAULT_FIELD_LENGTH);
234
            jTxtFieldLength.addKeyListener(checkInt);
235
        }
236
        return jTxtFieldLength;
237
    }
238

    
239
    /**
240
     * This method initializes jTxtFieldPrecision
241
     *
242
     * @return javax.swing.JTextField
243
     */
244
    private JTextField getJTxtFieldPrecision() {
245
        if (jTxtFieldPrecision == null) {
246
            jTxtFieldPrecision = new JTextField();
247
            jTxtFieldPrecision.setBounds(new java.awt.Rectangle(147, 126, 138,
248
                22));
249
            jTxtFieldPrecision.setEnabled(false);
250
            jTxtFieldPrecision.addKeyListener(checkInt);
251
        }
252
        return jTxtFieldPrecision;
253
    }
254

    
255
    /**
256
     * This method initializes jTxtDefaultValue
257
     *
258
     * @return javax.swing.JTextField
259
     */
260
    private JTextField getJTxtDefaultValue() {
261
        if (jTxtDefaultValue == null) {
262
            jTxtDefaultValue = new JTextField();
263
            jTxtDefaultValue
264
                .setBounds(new java.awt.Rectangle(147, 163, 138, 22));
265
        }
266
        return jTxtDefaultValue;
267
    }
268

    
269
    public EditableFeatureAttributeDescriptor loadFieldDescription(
270
        EditableFeatureType featureType) throws ParseException {
271
        String nameAttr = "";
272
        int typeAttr = DataTypes.STRING;
273
        int sizeAttr = 0;
274
        int precisionAttr = 0;
275
        Object defaultValueAttr = "";
276

    
277
        nameAttr = getJTxtFieldName().getText();
278

    
279
        if (nameAttr == null || nameAttr.length() == 0) {
280

    
281
            JOptionPane.showMessageDialog(
282
                    this,
283
                    Messages.getText("_No_input_name"),
284
                    Messages.getText("_Rename_column"),
285
                    JOptionPane.ERROR_MESSAGE);
286
            return null;
287
        }
288

    
289
        if (maxAttributeNameSize>0){
290
            if (nameAttr.length()>maxAttributeNameSize){
291
                JOptionPane.showMessageDialog(
292
                    this,
293
                    Messages.getText("_Name_too_long"),
294
                    Messages.getText("_Rename_column"),
295
                    JOptionPane.ERROR_MESSAGE);
296
            return null;
297
            }
298
        }
299

    
300
        String strType =
301
            (String) getJCboFieldType().getModel().getSelectedItem();
302
        typeAttr = ToolsLocator.getDataTypesManager().getType(strType);
303
        try {
304
            int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
305
            sizeAttr = fieldLength;
306
        } catch (Exception e) {
307
            throw new ParseException(e.getMessage(), 0);
308
        }
309

    
310
        if (typeAttr == DataTypes.DOUBLE) {
311
            try {
312
                precisionAttr =
313
                    Integer.parseInt(getJTxtFieldPrecision().getText());
314
            } catch (NumberFormatException e) {
315
                precisionAttr = 3;
316
            }
317
        }
318
        defaultValueAttr = getJTxtDefaultValue().getText();
319
        if (defaultValueAttr.equals("")) {
320
            defaultValueAttr = null;
321
        }
322
        if (featureType.getIndex(nameAttr) != -1) {
323
            NotificationManager.showMessageInfo(
324
                PluginServices.getText(this, "field_already_exists"), null);
325
            return null;
326
        }
327
        EditableFeatureAttributeDescriptor ead =
328
            featureType.add(nameAttr, typeAttr, sizeAttr);
329
        ead.setPrecision(precisionAttr);
330
        ead.setDefaultValue(defaultValueAttr);
331
        return ead;
332
    }
333

    
334
    public void setOkAction(ActionListener okAction) {
335
        getJPanelOkCancel().setOkButtonActionListener(okAction);
336

    
337
    }
338

    
339
    /**
340
     * This method initializes jPanel
341
     *
342
     * @return javax.swing.JPanel
343
     */
344
    private JPanel getJPanel() {
345
        if (jPanel == null) {
346
            jPanel = new JPanel();
347
            jPanel.setLayout(null);
348

    
349
            jPanel.add(getJPnlFields(), null);
350
        }
351
        return jPanel;
352
    }
353

    
354
    /**
355
     * This method initializes jPanelOkCancel
356
     *
357
     * @return javax.swing.JPanel
358
     */
359
    private AcceptCancelPanel getJPanelOkCancel() {
360
        if (jPanelOkCancel == null) {
361
            jPanelOkCancel = new AcceptCancelPanel();
362
            jPanelOkCancel.setCancelButtonActionListener(new ActionListener() {
363

    
364
                public void actionPerformed(java.awt.event.ActionEvent e) {
365
                    PluginServices.getMDIManager().closeWindow(
366
                        CreateNewAttributePanel.this);
367
                }
368
            });
369
            jPanelOkCancel.setPreferredSize(new java.awt.Dimension(10, 50));
370
        }
371
        return jPanelOkCancel;
372
    }
373

    
374
    /**
375
     * This method initializes jPnlFields
376
     *
377
     * @return javax.swing.JPanel
378
     */
379
    private JPanel getJPnlFields() {
380
        if (jPnlFields == null) {
381
            GridLayout gridLayout = new GridLayout();
382
            gridLayout.setRows(6);
383
            gridLayout.setVgap(3);
384
            gridLayout.setHgap(5);
385
            gridLayout.setColumns(2);
386
            jPnlFields = new JPanel();
387
            jPnlFields.setLayout(gridLayout);
388
            jPnlFields.setBounds(new java.awt.Rectangle(5, 12, 290, 142));
389
            jLblEmulated = new JLabel();
390
            jLblEmulated
391
                .setBounds(new java.awt.Rectangle(14, 200, 125, 22));
392
            jLblEmulated.setText(PluginServices.getText(this,
393
                "emulated"));
394
            jLblDefaultValue = new JLabel();
395
            jLblDefaultValue
396
                .setBounds(new java.awt.Rectangle(14, 163, 125, 22));
397
            jLblDefaultValue.setText(PluginServices.getText(this,
398
                "default_value"));
399
            jLblFieldPrecision = new JLabel();
400
            jLblFieldPrecision.setBounds(new java.awt.Rectangle(14, 126, 112,
401
                22));
402
            jLblFieldPrecision.setText(PluginServices
403
                .getText(this, "precision"));
404
            jLblFieldLength = new JLabel();
405
            jLblFieldLength.setBounds(new java.awt.Rectangle(14, 89, 99, 22));
406
            jLblFieldLength.setText(PluginServices
407
                .getText(this, "field_length"));
408
            jLblFieldType = new JLabel();
409
            jLblFieldType.setBounds(new java.awt.Rectangle(14, 52, 94, 22));
410
            jLblFieldType.setText(PluginServices.getText(this, "field_type"));
411
            jLblFieldName = new JLabel();
412
            jLblFieldName.setText(PluginServices.getText(this, "field_name"));
413
            jLblFieldName.setBounds(new java.awt.Rectangle(14, 15, 99, 22));
414
            jPnlFields.add(jLblFieldName, null);
415
            jPnlFields.add(getJTxtFieldName(), null);
416
            jPnlFields.add(jLblFieldType, null);
417
            jPnlFields.add(getJCboFieldType(), null);
418
            jPnlFields.add(jLblFieldLength, null);
419
            jPnlFields.add(getJTxtFieldLength(), null);
420
            jPnlFields.add(jLblFieldPrecision, null);
421
            jPnlFields.add(getJTxtFieldPrecision(), null);
422
            jPnlFields.add(jLblDefaultValue, null);
423
            jPnlFields.add(getJTxtDefaultValue(), null);            
424
            jPnlFields.add(jLblEmulated, null);
425
            jPnlFields.add(getJTxtEmulated(), null);
426
        }
427
        return jPnlFields;
428
    }
429

    
430
    public void setCurrentFieldNames(String[] fieldNames) {
431
        currentFieldNames = fieldNames;
432
        String newField =
433
            PluginServices.getText(this, "field").replaceAll(" +", "_");
434
        int index = 0;
435
        for (int i = 0; i < currentFieldNames.length; i++) {
436
            if (currentFieldNames[i].startsWith(newField)) {
437
                try {
438
                    index =
439
                        Integer.parseInt(currentFieldNames[i].replaceAll(
440
                            newField, ""));
441
                } catch (Exception e) { /* we don't care */
442
                }
443
            }
444
        }
445
        jTxtFieldName.setText(newField + (++index));
446
    }
447

    
448
    public Object getWindowProfile() {
449
        return WindowInfo.DIALOG_PROFILE;
450
    }
451

    
452
    public void setMaxAttributeNameSize(int maxAttributeNameSize) {
453
        this.maxAttributeNameSize = maxAttributeNameSize;
454
    }
455

    
456
    private JCheckBox getJTxtEmulated() {
457
        if (jChbEmulated == null) {
458
            jChbEmulated = new JCheckBox();
459
            jChbEmulated.setBounds(new java.awt.Rectangle(147, 200, 125, 22));
460
        }
461
        return jChbEmulated;
462
    }
463

    
464
}