Statistics
| Revision:

svn-gvsig-desktop / 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 / CreateNewAttributePanel.java @ 36443

History | View | Annotate | Download (14.8 KB)

1 36443 cordinyana
/* 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.GridLayout;
26
import java.awt.LayoutManager;
27
import java.awt.event.ActionListener;
28
import java.awt.event.KeyEvent;
29
import java.awt.event.KeyListener;
30
import java.text.ParseException;
31
32
import javax.swing.JComboBox;
33
import javax.swing.JLabel;
34
import javax.swing.JPanel;
35
import javax.swing.JTextField;
36
37
import org.gvsig.andami.PluginServices;
38
import org.gvsig.andami.messages.NotificationManager;
39
import org.gvsig.andami.ui.mdiManager.IWindow;
40
import org.gvsig.andami.ui.mdiManager.WindowInfo;
41
import org.gvsig.fmap.dal.DataTypes;
42
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
43
import org.gvsig.fmap.dal.feature.EditableFeatureType;
44
import org.gvsig.gui.beans.AcceptCancelPanel;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dataTypes.DataTypesManager;
47
48
/**
49
 * To create new FeatureAttributeDescriptor from the interface.
50
 *
51
 * @author Vicente Caballero Navarro
52
 *
53
 */
54
public class CreateNewAttributePanel extends JPanel implements IWindow {
55
56
    /**
57
         *
58
         */
59
    private static final long serialVersionUID = 6447641307779709964L;
60
    private static final String DEFAULT_FIELD_LENGTH = "50";
61
    private JLabel jLblFieldName = null;
62
    private JTextField jTxtFieldName = null;
63
    private JLabel jLblFieldType = null;
64
    private JComboBox jCboFieldType = null;
65
    private JLabel jLblFieldLength = null;
66
    private JTextField jTxtFieldLength = null;
67
    private JLabel jLblFieldPrecision = null;
68
    private JTextField jTxtFieldPrecision = null;
69
    private JLabel jLblDefaultValue = null;
70
    private JTextField jTxtDefaultValue = null;
71
    private WindowInfo viewInfo;
72
    private JPanel jPanel = null;
73
    private AcceptCancelPanel jPanelOkCancel = null;
74
    private JPanel jPnlFields = null;
75
    private KeyListener checkInt = new KeyListener() {
76
77
        public void keyPressed(KeyEvent e) {
78
            // do nothing
79
        }
80
81
        public void keyReleased(KeyEvent e) {
82
            JTextField component = (JTextField) e.getComponent();
83
84
            try {
85
                component.setText(String.valueOf(Integer.parseInt(component
86
                    .getText())));
87
88
            } catch (Exception ex) {
89
                String text = component.getText();
90
                text =
91
                    (text.length() <= 1) ? "0" : text.substring(0,
92
                        text.length() - 1);
93
                component.setText(text);
94
            }
95
        }
96
97
        public void keyTyped(KeyEvent e) {
98
            // do nothing
99
        }
100
    };
101
    private String[] currentFieldNames;
102
103
    public CreateNewAttributePanel() {
104
        super();
105
        initialize();
106
    }
107
108
    public CreateNewAttributePanel(boolean isDoubleBuffered) {
109
        super(isDoubleBuffered);
110
        initialize();
111
    }
112
113
    public CreateNewAttributePanel(LayoutManager layout) {
114
        super(layout);
115
        initialize();
116
    }
117
118
    public CreateNewAttributePanel(LayoutManager layout,
119
        boolean isDoubleBuffered) {
120
        super(layout, isDoubleBuffered);
121
        initialize();
122
    }
123
124
    public WindowInfo getWindowInfo() {
125
        if (viewInfo == null) {
126
            viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
127
            viewInfo.setWidth(this.getWidth() + 8);
128
            viewInfo.setHeight(this.getHeight());
129
            viewInfo.setTitle(PluginServices.getText(this,
130
                "new_field_properties"));
131
        }
132
        return viewInfo;
133
    }
134
135
    /**
136
     * This method initializes this
137
     *
138
     * @return void
139
     */
140
    private void initialize() {
141
        this.setLayout(new BorderLayout());
142
        this.setSize(300, 210);
143
        this.setPreferredSize(new java.awt.Dimension(300, 210));
144
        this.add(getJPanel(), java.awt.BorderLayout.CENTER);
145
        this.add(getJPanelOkCancel(), java.awt.BorderLayout.SOUTH);
146
    }
147
148
    /**
149
     * This method initializes jTxtFieldName
150
     *
151
     * @return javax.swing.JTextField
152
     */
153
    private JTextField getJTxtFieldName() {
154
        if (jTxtFieldName == null) {
155
            jTxtFieldName = new JTextField();
156
            jTxtFieldName.setBounds(new java.awt.Rectangle(147, 15, 138, 22));
157
        }
158
        return jTxtFieldName;
159
    }
160
161
    /**
162
     * This method initializes jCboFieldType
163
     *
164
     * @return javax.swing.JComboBox
165
     */
166
    private JComboBox getJCboFieldType() {
167
        DataTypesManager manager = ToolsLocator.getDataTypesManager();
168
        if (jCboFieldType == null) {
169
            jCboFieldType = new JComboBox();
170
            jCboFieldType.setBounds(new java.awt.Rectangle(147, 52, 138, 22));
171
            jCboFieldType.addItem(manager.getTypeName(DataTypes.BOOLEAN));
172
            jCboFieldType.addItem(manager.getTypeName(DataTypes.DATE));
173
            jCboFieldType.addItem(manager.getTypeName(DataTypes.INT));
174
            jCboFieldType.addItem(manager.getTypeName(DataTypes.DOUBLE));
175
            jCboFieldType.addItem(manager.getTypeName(DataTypes.STRING));
176
177
            jCboFieldType.setSelectedIndex(4);
178
            jCboFieldType
179
                .addActionListener(new java.awt.event.ActionListener() {
180
181
                    public void actionPerformed(java.awt.event.ActionEvent e) {
182
                        DataTypesManager manager =
183
                            ToolsLocator.getDataTypesManager();
184
                        String strType =
185
                            (String) getJCboFieldType().getModel()
186
                                .getSelectedItem();
187
                        if (strType == manager.getTypeName(DataTypes.STRING)) {
188
                            getJTxtFieldPrecision().setEnabled(true);
189
                            if (getJTxtFieldPrecision().getText().equals("")) {
190
                                getJTxtFieldPrecision().setText("3");
191
                            } else {
192
                                try {
193
                                    Integer.parseInt(getJTxtFieldPrecision()
194
                                        .getText());
195
                                } catch (NumberFormatException e1) {
196
                                    getJTxtFieldPrecision().setText("3");
197
                                }
198
                            }
199
                        } else {
200
                            getJTxtFieldPrecision().setEnabled(false);
201
                        }
202
                        if (strType == manager.getTypeName(DataTypes.BOOLEAN)) {
203
                            getJTxtFieldLength().setText("0");
204
                            getJTxtFieldLength().setEnabled(false);
205
                        } else {
206
                            getJTxtFieldLength().setEnabled(true);
207
                        }
208
209
                    }
210
                });
211
212
        }
213
        return jCboFieldType;
214
    }
215
216
    /**
217
     * This method initializes jTxtFieldLength
218
     *
219
     * @return javax.swing.JTextField
220
     */
221
    private JTextField getJTxtFieldLength() {
222
        if (jTxtFieldLength == null) {
223
            jTxtFieldLength = new JTextField();
224
            jTxtFieldLength.setBounds(new java.awt.Rectangle(147, 89, 138, 22));
225
            jTxtFieldLength.setText(DEFAULT_FIELD_LENGTH);
226
            jTxtFieldLength.addKeyListener(checkInt);
227
        }
228
        return jTxtFieldLength;
229
    }
230
231
    /**
232
     * This method initializes jTxtFieldPrecision
233
     *
234
     * @return javax.swing.JTextField
235
     */
236
    private JTextField getJTxtFieldPrecision() {
237
        if (jTxtFieldPrecision == null) {
238
            jTxtFieldPrecision = new JTextField();
239
            jTxtFieldPrecision.setBounds(new java.awt.Rectangle(147, 126, 138,
240
                22));
241
            jTxtFieldPrecision.setEnabled(false);
242
            jTxtFieldPrecision.addKeyListener(checkInt);
243
        }
244
        return jTxtFieldPrecision;
245
    }
246
247
    /**
248
     * This method initializes jTxtDefaultValue
249
     *
250
     * @return javax.swing.JTextField
251
     */
252
    private JTextField getJTxtDefaultValue() {
253
        if (jTxtDefaultValue == null) {
254
            jTxtDefaultValue = new JTextField();
255
            jTxtDefaultValue
256
                .setBounds(new java.awt.Rectangle(147, 163, 138, 22));
257
        }
258
        return jTxtDefaultValue;
259
    }
260
261
    public EditableFeatureAttributeDescriptor loadFieldDescription(
262
        EditableFeatureType featureType) throws ParseException {
263
        String nameAttr = "";
264
        int typeAttr = DataTypes.STRING;
265
        int sizeAttr = 0;
266
        int precisionAttr = 0;
267
        Object defaultValueAttr = "";
268
269
        nameAttr = getJTxtFieldName().getText();
270
        String strType =
271
            (String) getJCboFieldType().getModel().getSelectedItem();
272
        typeAttr = ToolsLocator.getDataTypesManager().getType(strType);
273
        try {
274
            int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
275
            sizeAttr = fieldLength;
276
        } catch (Exception e) {
277
            throw new ParseException(e.getMessage(), 0);
278
        }
279
280
        if (typeAttr == DataTypes.DOUBLE) {
281
            try {
282
                precisionAttr =
283
                    Integer.parseInt(getJTxtFieldPrecision().getText());
284
            } catch (NumberFormatException e) {
285
                precisionAttr = 3;
286
            }
287
        }
288
        defaultValueAttr = getJTxtDefaultValue().getText();
289
        if (defaultValueAttr.equals("")) {
290
            defaultValueAttr = null;
291
        }
292
        if (featureType.getIndex(nameAttr) != -1) {
293
            NotificationManager.showMessageInfo(
294
                PluginServices.getText(this, "field_already_exists"), null);
295
            return null;
296
        }
297
        EditableFeatureAttributeDescriptor ead =
298
            featureType.add(nameAttr, typeAttr, sizeAttr);
299
        ead.setPrecision(precisionAttr);
300
        ead.setDefaultValue(defaultValueAttr);
301
        return ead;
302
    }
303
304
    public void setOkAction(ActionListener okAction) {
305
        getJPanelOkCancel().setOkButtonActionListener(okAction);
306
307
    }
308
309
    /**
310
     * This method initializes jPanel
311
     *
312
     * @return javax.swing.JPanel
313
     */
314
    private JPanel getJPanel() {
315
        if (jPanel == null) {
316
            jPanel = new JPanel();
317
            jPanel.setLayout(null);
318
319
            jPanel.add(getJPnlFields(), null);
320
        }
321
        return jPanel;
322
    }
323
324
    /**
325
     * This method initializes jPanelOkCancel
326
     *
327
     * @return javax.swing.JPanel
328
     */
329
    private AcceptCancelPanel getJPanelOkCancel() {
330
        if (jPanelOkCancel == null) {
331
            jPanelOkCancel = new AcceptCancelPanel();
332
            jPanelOkCancel.setCancelButtonActionListener(new ActionListener() {
333
334
                public void actionPerformed(java.awt.event.ActionEvent e) {
335
                    PluginServices.getMDIManager().closeWindow(
336
                        CreateNewAttributePanel.this);
337
                }
338
            });
339
            jPanelOkCancel.setPreferredSize(new java.awt.Dimension(10, 50));
340
        }
341
        return jPanelOkCancel;
342
    }
343
344
    /**
345
     * This method initializes jPnlFields
346
     *
347
     * @return javax.swing.JPanel
348
     */
349
    private JPanel getJPnlFields() {
350
        if (jPnlFields == null) {
351
            GridLayout gridLayout = new GridLayout();
352
            gridLayout.setRows(6);
353
            gridLayout.setVgap(3);
354
            gridLayout.setHgap(5);
355
            gridLayout.setColumns(2);
356
            jPnlFields = new JPanel();
357
            jPnlFields.setLayout(gridLayout);
358
            jPnlFields.setBounds(new java.awt.Rectangle(5, 12, 290, 142));
359
            jLblDefaultValue = new JLabel();
360
            jLblDefaultValue
361
                .setBounds(new java.awt.Rectangle(14, 163, 125, 22));
362
            jLblDefaultValue.setText(PluginServices.getText(this,
363
                "default_value"));
364
            jLblFieldPrecision = new JLabel();
365
            jLblFieldPrecision.setBounds(new java.awt.Rectangle(14, 126, 112,
366
                22));
367
            jLblFieldPrecision.setText(PluginServices
368
                .getText(this, "precision"));
369
            jLblFieldLength = new JLabel();
370
            jLblFieldLength.setBounds(new java.awt.Rectangle(14, 89, 99, 22));
371
            jLblFieldLength.setText(PluginServices
372
                .getText(this, "field_length"));
373
            jLblFieldType = new JLabel();
374
            jLblFieldType.setBounds(new java.awt.Rectangle(14, 52, 94, 22));
375
            jLblFieldType.setText(PluginServices.getText(this, "field_type"));
376
            jLblFieldName = new JLabel();
377
            jLblFieldName.setText(PluginServices.getText(this, "field_name"));
378
            jLblFieldName.setBounds(new java.awt.Rectangle(14, 15, 99, 22));
379
            jPnlFields.add(jLblFieldName, null);
380
            jPnlFields.add(getJTxtFieldName(), null);
381
            jPnlFields.add(jLblFieldType, null);
382
            jPnlFields.add(getJCboFieldType(), null);
383
            jPnlFields.add(jLblFieldLength, null);
384
            jPnlFields.add(getJTxtFieldLength(), null);
385
            jPnlFields.add(jLblFieldPrecision, null);
386
            jPnlFields.add(getJTxtFieldPrecision(), null);
387
            jPnlFields.add(jLblDefaultValue, null);
388
            jPnlFields.add(getJTxtDefaultValue(), null);
389
        }
390
        return jPnlFields;
391
    }
392
393
    public void setCurrentFieldNames(String[] fieldNames) {
394
        currentFieldNames = fieldNames;
395
        String newField =
396
            PluginServices.getText(this, "field").replaceAll(" +", "_");
397
        int index = 0;
398
        for (int i = 0; i < currentFieldNames.length; i++) {
399
            if (currentFieldNames[i].startsWith(newField)) {
400
                try {
401
                    index =
402
                        Integer.parseInt(currentFieldNames[i].replaceAll(
403
                            newField, ""));
404
                } catch (Exception e) { /* we don't care */
405
                }
406
            }
407
        }
408
        jTxtFieldName.setText(newField + (++index));
409
    }
410
411
    public Object getWindowProfile() {
412
        return WindowInfo.DIALOG_PROFILE;
413
    }
414
415
}