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 @ 40558

History | View | Annotate | Download (14.9 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

    
34
import javax.swing.JComboBox;
35
import javax.swing.JLabel;
36
import javax.swing.JPanel;
37
import javax.swing.JTextField;
38

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

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

    
58
    /**
59
         * 
60
         */
61
    private static final long serialVersionUID = 6447641307779709964L;
62
    private static final String DEFAULT_FIELD_LENGTH = "50";
63
    private JLabel jLblFieldName = null;
64
    private JTextField jTxtFieldName = null;
65
    private JLabel jLblFieldType = null;
66
    private JComboBox jCboFieldType = null;
67
    private JLabel jLblFieldLength = null;
68
    private JTextField jTxtFieldLength = null;
69
    private JLabel jLblFieldPrecision = null;
70
    private JTextField jTxtFieldPrecision = null;
71
    private JLabel jLblDefaultValue = null;
72
    private JTextField jTxtDefaultValue = null;
73
    private WindowInfo viewInfo;
74
    private JPanel jPanel = null;
75
    private AcceptCancelPanel jPanelOkCancel = null;
76
    private JPanel jPnlFields = null;
77
    private KeyListener checkInt = new KeyListener() {
78

    
79
        public void keyPressed(KeyEvent e) {
80
            // do nothing
81
        }
82

    
83
        public void keyReleased(KeyEvent e) {
84
            JTextField component = (JTextField) e.getComponent();
85

    
86
            try {
87
                component.setText(String.valueOf(Integer.parseInt(component
88
                    .getText())));
89

    
90
            } catch (Exception ex) {
91
                String text = component.getText();
92
                text =
93
                    (text.length() <= 1) ? "0" : text.substring(0,
94
                        text.length() - 1);
95
                component.setText(text);
96
            }
97
        }
98

    
99
        public void keyTyped(KeyEvent e) {
100
            // do nothing
101
        }
102
    };
103
    private String[] currentFieldNames;
104

    
105
    public CreateNewAttributePanel() {
106
        super();
107
        initialize();
108
    }
109

    
110
    public CreateNewAttributePanel(boolean isDoubleBuffered) {
111
        super(isDoubleBuffered);
112
        initialize();
113
    }
114

    
115
    public CreateNewAttributePanel(LayoutManager layout) {
116
        super(layout);
117
        initialize();
118
    }
119

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

    
126
    public WindowInfo getWindowInfo() {
127
        if (viewInfo == null) {
128
            viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
129
            viewInfo.setWidth(this.getWidth() + 8);
130
            viewInfo.setHeight(this.getHeight());
131
            viewInfo.setTitle(PluginServices.getText(this,
132
                "new_field_properties"));
133
        }
134
        return viewInfo;
135
    }
136

    
137
    /**
138
     * This method initializes this
139
     * 
140
     * @return void
141
     */
142
    private void initialize() {
143
        this.setLayout(new BorderLayout());
144
        this.setSize(300, 210);
145
        this.setPreferredSize(new java.awt.Dimension(300, 210));
146
        this.add(getJPanel(), java.awt.BorderLayout.CENTER);
147
        this.add(getJPanelOkCancel(), java.awt.BorderLayout.SOUTH);
148
    }
149

    
150
    /**
151
     * This method initializes jTxtFieldName
152
     * 
153
     * @return javax.swing.JTextField
154
     */
155
    private JTextField getJTxtFieldName() {
156
        if (jTxtFieldName == null) {
157
            jTxtFieldName = new JTextField();
158
            jTxtFieldName.setBounds(new java.awt.Rectangle(147, 15, 138, 22));
159
        }
160
        return jTxtFieldName;
161
    }
162

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

    
179
            jCboFieldType.setSelectedIndex(4);
180
            jCboFieldType
181
                .addActionListener(new java.awt.event.ActionListener() {
182

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

    
211
                    }
212
                });
213

    
214
        }
215
        return jCboFieldType;
216
    }
217

    
218
    /**
219
     * This method initializes jTxtFieldLength
220
     * 
221
     * @return javax.swing.JTextField
222
     */
223
    private JTextField getJTxtFieldLength() {
224
        if (jTxtFieldLength == null) {
225
            jTxtFieldLength = new JTextField();
226
            jTxtFieldLength.setBounds(new java.awt.Rectangle(147, 89, 138, 22));
227
            jTxtFieldLength.setText(DEFAULT_FIELD_LENGTH);
228
            jTxtFieldLength.addKeyListener(checkInt);
229
        }
230
        return jTxtFieldLength;
231
    }
232

    
233
    /**
234
     * This method initializes jTxtFieldPrecision
235
     * 
236
     * @return javax.swing.JTextField
237
     */
238
    private JTextField getJTxtFieldPrecision() {
239
        if (jTxtFieldPrecision == null) {
240
            jTxtFieldPrecision = new JTextField();
241
            jTxtFieldPrecision.setBounds(new java.awt.Rectangle(147, 126, 138,
242
                22));
243
            jTxtFieldPrecision.setEnabled(false);
244
            jTxtFieldPrecision.addKeyListener(checkInt);
245
        }
246
        return jTxtFieldPrecision;
247
    }
248

    
249
    /**
250
     * This method initializes jTxtDefaultValue
251
     * 
252
     * @return javax.swing.JTextField
253
     */
254
    private JTextField getJTxtDefaultValue() {
255
        if (jTxtDefaultValue == null) {
256
            jTxtDefaultValue = new JTextField();
257
            jTxtDefaultValue
258
                .setBounds(new java.awt.Rectangle(147, 163, 138, 22));
259
        }
260
        return jTxtDefaultValue;
261
    }
262

    
263
    public EditableFeatureAttributeDescriptor loadFieldDescription(
264
        EditableFeatureType featureType) throws ParseException {
265
        String nameAttr = "";
266
        int typeAttr = DataTypes.STRING;
267
        int sizeAttr = 0;
268
        int precisionAttr = 0;
269
        Object defaultValueAttr = "";
270

    
271
        nameAttr = getJTxtFieldName().getText();
272
        String strType =
273
            (String) getJCboFieldType().getModel().getSelectedItem();
274
        typeAttr = ToolsLocator.getDataTypesManager().getType(strType);
275
        try {
276
            int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
277
            sizeAttr = fieldLength;
278
        } catch (Exception e) {
279
            throw new ParseException(e.getMessage(), 0);
280
        }
281

    
282
        if (typeAttr == DataTypes.DOUBLE) {
283
            try {
284
                precisionAttr =
285
                    Integer.parseInt(getJTxtFieldPrecision().getText());
286
            } catch (NumberFormatException e) {
287
                precisionAttr = 3;
288
            }
289
        }
290
        defaultValueAttr = getJTxtDefaultValue().getText();
291
        if (defaultValueAttr.equals("")) {
292
            defaultValueAttr = null;
293
        }
294
        if (featureType.getIndex(nameAttr) != -1) {
295
            NotificationManager.showMessageInfo(
296
                PluginServices.getText(this, "field_already_exists"), null);
297
            return null;
298
        }
299
        EditableFeatureAttributeDescriptor ead =
300
            featureType.add(nameAttr, typeAttr, sizeAttr);
301
        ead.setPrecision(precisionAttr);
302
        ead.setDefaultValue(defaultValueAttr);
303
        return ead;
304
    }
305

    
306
    public void setOkAction(ActionListener okAction) {
307
        getJPanelOkCancel().setOkButtonActionListener(okAction);
308

    
309
    }
310

    
311
    /**
312
     * This method initializes jPanel
313
     * 
314
     * @return javax.swing.JPanel
315
     */
316
    private JPanel getJPanel() {
317
        if (jPanel == null) {
318
            jPanel = new JPanel();
319
            jPanel.setLayout(null);
320

    
321
            jPanel.add(getJPnlFields(), null);
322
        }
323
        return jPanel;
324
    }
325

    
326
    /**
327
     * This method initializes jPanelOkCancel
328
     * 
329
     * @return javax.swing.JPanel
330
     */
331
    private AcceptCancelPanel getJPanelOkCancel() {
332
        if (jPanelOkCancel == null) {
333
            jPanelOkCancel = new AcceptCancelPanel();
334
            jPanelOkCancel.setCancelButtonActionListener(new ActionListener() {
335

    
336
                public void actionPerformed(java.awt.event.ActionEvent e) {
337
                    PluginServices.getMDIManager().closeWindow(
338
                        CreateNewAttributePanel.this);
339
                }
340
            });
341
            jPanelOkCancel.setPreferredSize(new java.awt.Dimension(10, 50));
342
        }
343
        return jPanelOkCancel;
344
    }
345

    
346
    /**
347
     * This method initializes jPnlFields
348
     * 
349
     * @return javax.swing.JPanel
350
     */
351
    private JPanel getJPnlFields() {
352
        if (jPnlFields == null) {
353
            GridLayout gridLayout = new GridLayout();
354
            gridLayout.setRows(6);
355
            gridLayout.setVgap(3);
356
            gridLayout.setHgap(5);
357
            gridLayout.setColumns(2);
358
            jPnlFields = new JPanel();
359
            jPnlFields.setLayout(gridLayout);
360
            jPnlFields.setBounds(new java.awt.Rectangle(5, 12, 290, 142));
361
            jLblDefaultValue = new JLabel();
362
            jLblDefaultValue
363
                .setBounds(new java.awt.Rectangle(14, 163, 125, 22));
364
            jLblDefaultValue.setText(PluginServices.getText(this,
365
                "default_value"));
366
            jLblFieldPrecision = new JLabel();
367
            jLblFieldPrecision.setBounds(new java.awt.Rectangle(14, 126, 112,
368
                22));
369
            jLblFieldPrecision.setText(PluginServices
370
                .getText(this, "precision"));
371
            jLblFieldLength = new JLabel();
372
            jLblFieldLength.setBounds(new java.awt.Rectangle(14, 89, 99, 22));
373
            jLblFieldLength.setText(PluginServices
374
                .getText(this, "field_length"));
375
            jLblFieldType = new JLabel();
376
            jLblFieldType.setBounds(new java.awt.Rectangle(14, 52, 94, 22));
377
            jLblFieldType.setText(PluginServices.getText(this, "field_type"));
378
            jLblFieldName = new JLabel();
379
            jLblFieldName.setText(PluginServices.getText(this, "field_name"));
380
            jLblFieldName.setBounds(new java.awt.Rectangle(14, 15, 99, 22));
381
            jPnlFields.add(jLblFieldName, null);
382
            jPnlFields.add(getJTxtFieldName(), null);
383
            jPnlFields.add(jLblFieldType, null);
384
            jPnlFields.add(getJCboFieldType(), null);
385
            jPnlFields.add(jLblFieldLength, null);
386
            jPnlFields.add(getJTxtFieldLength(), null);
387
            jPnlFields.add(jLblFieldPrecision, null);
388
            jPnlFields.add(getJTxtFieldPrecision(), null);
389
            jPnlFields.add(jLblDefaultValue, null);
390
            jPnlFields.add(getJTxtDefaultValue(), null);
391
        }
392
        return jPnlFields;
393
    }
394

    
395
    public void setCurrentFieldNames(String[] fieldNames) {
396
        currentFieldNames = fieldNames;
397
        String newField =
398
            PluginServices.getText(this, "field").replaceAll(" +", "_");
399
        int index = 0;
400
        for (int i = 0; i < currentFieldNames.length; i++) {
401
            if (currentFieldNames[i].startsWith(newField)) {
402
                try {
403
                    index =
404
                        Integer.parseInt(currentFieldNames[i].replaceAll(
405
                            newField, ""));
406
                } catch (Exception e) { /* we don't care */
407
                }
408
            }
409
        }
410
        jTxtFieldName.setText(newField + (++index));
411
    }
412

    
413
    public Object getWindowProfile() {
414
        return WindowInfo.DIALOG_PROFILE;
415
    }
416

    
417
}