Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / table / gui / CreateNewAttributePanel.java @ 31496

History | View | Annotate | Download (12.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.app.project.documents.table.gui;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.GridLayout;
45
import java.awt.LayoutManager;
46
import java.awt.event.ActionListener;
47
import java.awt.event.KeyEvent;
48
import java.awt.event.KeyListener;
49
import java.text.ParseException;
50

    
51
import javax.swing.JComboBox;
52
import javax.swing.JLabel;
53
import javax.swing.JPanel;
54
import javax.swing.JTextField;
55

    
56
import org.gvsig.andami.PluginServices;
57
import org.gvsig.andami.messages.NotificationManager;
58
import org.gvsig.andami.ui.mdiManager.IWindow;
59
import org.gvsig.andami.ui.mdiManager.WindowInfo;
60
import org.gvsig.fmap.dal.DataTypes;
61
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
62
import org.gvsig.fmap.dal.feature.EditableFeatureType;
63
import org.gvsig.gui.beans.AcceptCancelPanel;
64

    
65

    
66
/**
67
 * To create new FeatureAttributeDescriptor from the interface.
68
 *
69
 * @author Vicente Caballero Navarro
70
 *
71
 */
72
public class CreateNewAttributePanel extends JPanel implements IWindow {
73

    
74
        /**
75
         * 
76
         */
77
        private static final long serialVersionUID = 6447641307779709964L;
78
        private static final String DEFAULT_FIELD_LENGTH = "50";
79
        private JLabel jLblFieldName = null;
80
        private JTextField jTxtFieldName = null;
81
        private JLabel jLblFieldType = null;
82
        private JComboBox jCboFieldType = null;
83
        private JLabel jLblFieldLength = null;
84
        private JTextField jTxtFieldLength = null;
85
        private JLabel jLblFieldPrecision = null;
86
        private JTextField jTxtFieldPrecision = null;
87
        private JLabel jLblDefaultValue = null;
88
        private JTextField jTxtDefaultValue = null;
89
        private WindowInfo viewInfo;
90
        private JPanel jPanel = null;
91
        private AcceptCancelPanel jPanelOkCancel = null;
92
        private JPanel jPnlFields = null;
93
        private KeyListener checkInt = new KeyListener() {
94
                public void keyPressed(KeyEvent e) {
95
                        // do nothing
96
                }
97

    
98
                public void keyReleased(KeyEvent e) {
99
                        JTextField component = (JTextField) e.getComponent();
100

    
101
                        try {
102
                                component.setText(String.valueOf(Integer.parseInt(component
103
                                                .getText())));
104

    
105
                        } catch (Exception ex) {
106
                                String text = component.getText();
107
                                text = (text.length() <= 1) ? "0" : text.substring(0, text
108
                                                .length() - 1);
109
                                component.setText(text);
110
                        }
111
                }
112

    
113
                public void keyTyped(KeyEvent e) {
114
                        // do nothing
115
                }
116
        };
117
        private String[] currentFieldNames;
118

    
119
        public CreateNewAttributePanel() {
120
                super();
121
                initialize();
122
        }
123

    
124
        public CreateNewAttributePanel(boolean isDoubleBuffered) {
125
                super(isDoubleBuffered);
126
                initialize();
127
        }
128

    
129
        public CreateNewAttributePanel(LayoutManager layout) {
130
                super(layout);
131
                initialize();
132
        }
133

    
134
        public CreateNewAttributePanel(LayoutManager layout,
135
                        boolean isDoubleBuffered) {
136
                super(layout, isDoubleBuffered);
137
                initialize();
138
        }
139

    
140
        public WindowInfo getWindowInfo() {
141
                if (viewInfo == null) {
142
                        viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
143
                        viewInfo.setWidth(this.getWidth() + 8);
144
                        viewInfo.setHeight(this.getHeight());
145
                        viewInfo.setTitle(PluginServices.getText(this,
146
                                        "new_field_properties"));
147
                }
148
                return viewInfo;
149
        }
150

    
151
        /**
152
         * This method initializes this
153
         *
154
         * @return void
155
         */
156
        private void initialize() {
157
                this.setLayout(new BorderLayout());
158
                this.setSize(300, 210);
159
                this.setPreferredSize(new java.awt.Dimension(300, 210));
160
                this.add(getJPanel(), java.awt.BorderLayout.CENTER);
161
                this.add(getJPanelOkCancel(), java.awt.BorderLayout.SOUTH);
162
        }
163

    
164
        /**
165
         * This method initializes jTxtFieldName
166
         *
167
         * @return javax.swing.JTextField
168
         */
169
        private JTextField getJTxtFieldName() {
170
                if (jTxtFieldName == null) {
171
                        jTxtFieldName = new JTextField();
172
                        jTxtFieldName.setBounds(new java.awt.Rectangle(147, 15, 138, 22));
173
                }
174
                return jTxtFieldName;
175
        }
176

    
177
        /**
178
         * This method initializes jCboFieldType
179
         *
180
         * @return javax.swing.JComboBox
181
         */
182
        private JComboBox getJCboFieldType() {
183
                if (jCboFieldType == null) {
184
                        jCboFieldType = new JComboBox();
185
                        jCboFieldType.setBounds(new java.awt.Rectangle(147, 52, 138, 22));
186
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.BOOLEAN]);
187
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.DATE]);
188
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.INT]);
189
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.DOUBLE]);
190
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.STRING]);
191

    
192
                        jCboFieldType.setSelectedIndex(4);
193
                        jCboFieldType
194
                                        .addActionListener(new java.awt.event.ActionListener() {
195
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
196
                                                        String strType = (String) getJCboFieldType()
197
                                                                        .getModel().getSelectedItem();
198
                                                        if (strType == DataTypes.TYPE_NAMES[DataTypes.STRING]) {
199
                                                                getJTxtFieldPrecision().setEnabled(true);
200
                                                                if (getJTxtFieldPrecision().getText()
201
                                                                                .equals("")) {
202
                                                                        getJTxtFieldPrecision().setText("3");
203
                                                                } else {
204
                                                                        try {
205
                                                                                Integer
206
                                                                                                .parseInt(getJTxtFieldPrecision()
207
                                                                                                                .getText());
208
                                                                        } catch (NumberFormatException e1) {
209
                                                                                getJTxtFieldPrecision().setText("3");
210
                                                                        }
211
                                                                }
212
                                                        } else {
213
                                                                getJTxtFieldPrecision().setEnabled(false);
214
                                                        }
215
                                                        if (strType == DataTypes.TYPE_NAMES[DataTypes.BOOLEAN]) {
216
                                                                getJTxtFieldLength().setText("0");
217
                                                                getJTxtFieldLength().setEnabled(false);
218
                                                        } else {
219
                                                                getJTxtFieldLength().setEnabled(true);
220
                                                        }
221

    
222
                                                }
223
                                        });
224

    
225
                }
226
                return jCboFieldType;
227
        }
228

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

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

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

    
274
        public EditableFeatureAttributeDescriptor loadFieldDescription(
275
                        EditableFeatureType featureType) throws ParseException {
276
                String nameAttr = "";
277
                int typeAttr = DataTypes.STRING;
278
                int sizeAttr = 0;
279
                int precisionAttr = 0;
280
                Object defaultValueAttr = "";
281

    
282
                nameAttr = getJTxtFieldName().getText();
283
                String strType = (String) getJCboFieldType().getModel()
284
                                .getSelectedItem();
285
                for (int type = 0; type < DataTypes.TYPE_NAMES.length; type++) {
286
                        if (DataTypes.TYPE_NAMES[type] == strType) {
287
                                typeAttr = type;
288
                                break;
289
                        }
290
                }
291
                try {
292
                        int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
293
                        sizeAttr = fieldLength;
294
                } catch (Exception e) {
295
                        throw new ParseException(e.getMessage(), 0);
296
                }
297

    
298
                if (strType == DataTypes.TYPE_NAMES[DataTypes.DOUBLE]) {
299
                        try {
300
                                precisionAttr = Integer.parseInt(getJTxtFieldPrecision()
301
                                                .getText());
302
                        } catch (NumberFormatException e) {
303
                                precisionAttr = 3;
304
                        }
305
                }
306
                defaultValueAttr = getJTxtDefaultValue().getText();
307
                if (defaultValueAttr.equals("")){
308
                        defaultValueAttr=null;
309
                }
310
                if (featureType.getIndex(nameAttr) != -1) {
311
                        NotificationManager.showMessageInfo(PluginServices.getText(this,
312
                                        "field_already_exists"), null);
313
                        return null;
314
                }
315
                EditableFeatureAttributeDescriptor ead = featureType.add(nameAttr,
316
                                typeAttr, sizeAttr);
317
                ead.setPrecision(precisionAttr);
318
                ead.setDefaultValue(defaultValueAttr);
319
                return ead;
320
        }
321

    
322
        public void setOkAction(ActionListener okAction) {
323
                getJPanelOkCancel().setOkButtonActionListener(okAction);
324

    
325
        }
326

    
327
        /**
328
         * This method initializes jPanel
329
         *
330
         * @return javax.swing.JPanel
331
         */
332
        private JPanel getJPanel() {
333
                if (jPanel == null) {
334
                        jPanel = new JPanel();
335
                        jPanel.setLayout(null);
336

    
337
                        jPanel.add(getJPnlFields(), null);
338
                }
339
                return jPanel;
340
        }
341

    
342
        /**
343
         * This method initializes jPanelOkCancel
344
         *
345
         * @return javax.swing.JPanel
346
         */
347
        private AcceptCancelPanel getJPanelOkCancel() {
348
                if (jPanelOkCancel == null) {
349
                        jPanelOkCancel = new AcceptCancelPanel();
350
                        jPanelOkCancel.setCancelButtonActionListener(new ActionListener() {
351
                                public void actionPerformed(java.awt.event.ActionEvent e) {
352
                                        PluginServices.getMDIManager().closeWindow(
353
                                                        CreateNewAttributePanel.this);
354
                                }
355
                        });
356
                        jPanelOkCancel.setPreferredSize(new java.awt.Dimension(10, 50));
357
                }
358
                return jPanelOkCancel;
359
        }
360

    
361
        /**
362
         * This method initializes jPnlFields
363
         *
364
         * @return javax.swing.JPanel
365
         */
366
        private JPanel getJPnlFields() {
367
                if (jPnlFields == null) {
368
                        GridLayout gridLayout = new GridLayout();
369
                        gridLayout.setRows(6);
370
                        gridLayout.setVgap(3);
371
                        gridLayout.setHgap(5);
372
                        gridLayout.setColumns(2);
373
                        jPnlFields = new JPanel();
374
                        jPnlFields.setLayout(gridLayout);
375
                        jPnlFields.setBounds(new java.awt.Rectangle(5, 12, 290, 142));
376
                        jLblDefaultValue = new JLabel();
377
                        jLblDefaultValue
378
                                        .setBounds(new java.awt.Rectangle(14, 163, 125, 22));
379
                        jLblDefaultValue.setText(PluginServices.getText(this,
380
                                        "default_value"));
381
                        jLblFieldPrecision = new JLabel();
382
                        jLblFieldPrecision.setBounds(new java.awt.Rectangle(14, 126, 112,
383
                                        22));
384
                        jLblFieldPrecision.setText(PluginServices
385
                                        .getText(this, "precision"));
386
                        jLblFieldLength = new JLabel();
387
                        jLblFieldLength.setBounds(new java.awt.Rectangle(14, 89, 99, 22));
388
                        jLblFieldLength.setText(PluginServices
389
                                        .getText(this, "field_length"));
390
                        jLblFieldType = new JLabel();
391
                        jLblFieldType.setBounds(new java.awt.Rectangle(14, 52, 94, 22));
392
                        jLblFieldType.setText(PluginServices.getText(this, "field_type"));
393
                        jLblFieldName = new JLabel();
394
                        jLblFieldName.setText(PluginServices.getText(this, "field_name"));
395
                        jLblFieldName.setBounds(new java.awt.Rectangle(14, 15, 99, 22));
396
                        jPnlFields.add(jLblFieldName, null);
397
                        jPnlFields.add(getJTxtFieldName(), null);
398
                        jPnlFields.add(jLblFieldType, null);
399
                        jPnlFields.add(getJCboFieldType(), null);
400
                        jPnlFields.add(jLblFieldLength, null);
401
                        jPnlFields.add(getJTxtFieldLength(), null);
402
                        jPnlFields.add(jLblFieldPrecision, null);
403
                        jPnlFields.add(getJTxtFieldPrecision(), null);
404
                        jPnlFields.add(jLblDefaultValue, null);
405
                        jPnlFields.add(getJTxtDefaultValue(), null);
406
                }
407
                return jPnlFields;
408
        }
409

    
410
        public void setCurrentFieldNames(String[] fieldNames) {
411
                currentFieldNames = fieldNames;
412
                String newField = PluginServices.getText(this, "field").replaceAll(
413
                                " +", "_");
414
                int index = 0;
415
                for (int i = 0; i < currentFieldNames.length; i++) {
416
                        if (currentFieldNames[i].startsWith(newField)) {
417
                                try {
418
                                        index = Integer.parseInt(currentFieldNames[i].replaceAll(
419
                                                        newField, ""));
420
                                } catch (Exception e) { /* we don't care */
421
                                }
422
                        }
423
                }
424
                jTxtFieldName.setText(newField + (++index));
425
        }
426

    
427
        public Object getWindowProfile() {
428
                return WindowInfo.DIALOG_PROFILE;
429
        }
430

    
431
}