Statistics
| Revision:

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

History | View | Annotate | Download (12.7 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
import org.gvsig.tools.ToolsLocator;
65
import org.gvsig.tools.dataTypes.DataTypesManager;
66

    
67

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

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

    
100
                public void keyReleased(KeyEvent e) {
101
                        JTextField component = (JTextField) e.getComponent();
102

    
103
                        try {
104
                                component.setText(String.valueOf(Integer.parseInt(component
105
                                                .getText())));
106

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

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

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

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

    
131
        public CreateNewAttributePanel(LayoutManager layout) {
132
                super(layout);
133
                initialize();
134
        }
135

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

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

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

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

    
179
        /**
180
         * This method initializes jCboFieldType
181
         *
182
         * @return javax.swing.JComboBox
183
         */
184
        private JComboBox getJCboFieldType() {
185
                DataTypesManager manager = ToolsLocator.getDataTypesManager();
186
                if (jCboFieldType == null) {
187
                        jCboFieldType = new JComboBox();
188
                        jCboFieldType.setBounds(new java.awt.Rectangle(147, 52, 138, 22));
189
                        jCboFieldType.addItem(manager.getTypeName(DataTypes.BOOLEAN));
190
                        jCboFieldType.addItem(manager.getTypeName(DataTypes.DATE));
191
                        jCboFieldType.addItem(manager.getTypeName(DataTypes.INT));
192
                        jCboFieldType.addItem(manager.getTypeName(DataTypes.DOUBLE));
193
                        jCboFieldType.addItem(manager.getTypeName(DataTypes.STRING));
194

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

    
226
                                                }
227
                                        });
228

    
229
                }
230
                return jCboFieldType;
231
        }
232

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

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

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

    
278
        public EditableFeatureAttributeDescriptor loadFieldDescription(
279
                        EditableFeatureType featureType) throws ParseException {
280
                String nameAttr = "";
281
                int typeAttr = DataTypes.STRING;
282
                int sizeAttr = 0;
283
                int precisionAttr = 0;
284
                Object defaultValueAttr = "";
285

    
286
                nameAttr = getJTxtFieldName().getText();
287
                String strType = (String) getJCboFieldType().getModel().getSelectedItem();
288
                typeAttr = ToolsLocator.getDataTypesManager().getType(strType);
289
                try {
290
                        int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
291
                        sizeAttr = fieldLength;
292
                } catch (Exception e) {
293
                        throw new ParseException(e.getMessage(), 0);
294
                }
295

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

    
320
        public void setOkAction(ActionListener okAction) {
321
                getJPanelOkCancel().setOkButtonActionListener(okAction);
322

    
323
        }
324

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

    
335
                        jPanel.add(getJPnlFields(), null);
336
                }
337
                return jPanel;
338
        }
339

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

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

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

    
425
        public Object getWindowProfile() {
426
                return WindowInfo.DIALOG_PROFILE;
427
        }
428

    
429
}