Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / gui / panels / FPanelCreateField.java @ 24759

History | View | Annotate | Download (11.4 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 com.iver.cit.gvsig.gui.panels;
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.fmap.dal.DataTypes;
57
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
58
import org.gvsig.gui.beans.AcceptCancelPanel;
59

    
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.ui.mdiManager.IWindow;
62
import com.iver.andami.ui.mdiManager.WindowInfo;
63

    
64
public class FPanelCreateField extends JPanel implements IWindow {
65

    
66
        private static final String DEFAULT_FIELD_LENGTH = "50";
67
        private JLabel jLblFieldName = null;
68
        private JTextField jTxtFieldName = null;
69
        private JLabel jLblFieldType = null;
70
        private JComboBox jCboFieldType = null;
71
        private JLabel jLblFieldLength = null;
72
        private JTextField jTxtFieldLength = null;
73
        private JLabel jLblFieldPrecision = null;
74
        private JTextField jTxtFieldPrecision = null;
75
        private JLabel jLblDefaultValue = null;
76
        private JTextField jTxtDefaultValue = null;
77
        private WindowInfo viewInfo;
78
        private JPanel jPanel = null;  //  @jve:decl-index=0:visual-constraint="299,27"
79
        private AcceptCancelPanel jPanelOkCancel = null;
80
        private JPanel jPnlFields = null;
81
        private KeyListener checkInt = new KeyListener() {
82
                public void keyPressed(KeyEvent e)  { }
83
                public void keyReleased(KeyEvent e) {
84
                        JTextField component = (JTextField) e.getComponent();
85

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

    
93
                        } catch (Exception ex) {
94
                                String text = component.getText();
95
                                text = (text.length() <= 1)? "0" : text.substring(0, text.length()-1);
96
                                component.setText(text);
97
                        }}
98
                public void keyTyped(KeyEvent e)    { }
99
        };
100
        private String[] currentFieldNames;
101

    
102
        public FPanelCreateField() {
103
                super();
104
                // TODO Auto-generated constructor stub
105
                initialize();
106
        }
107

    
108
        public FPanelCreateField(boolean isDoubleBuffered) {
109
                super(isDoubleBuffered);
110
                // TODO Auto-generated constructor stub
111
                initialize();
112
        }
113

    
114
        public FPanelCreateField(LayoutManager layout) {
115
                super(layout);
116
                // TODO Auto-generated constructor stub
117
                initialize();
118
        }
119

    
120
        public FPanelCreateField(LayoutManager layout, boolean isDoubleBuffered) {
121
                super(layout, isDoubleBuffered);
122
                // TODO Auto-generated constructor stub
123
                initialize();
124
        }
125

    
126
        public WindowInfo getWindowInfo() {
127
                if (viewInfo == null)
128
                {
129
                        viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
130
                        viewInfo.setWidth(this.getWidth()+8);
131
                        viewInfo.setHeight(this.getHeight());
132
                        viewInfo.setTitle(PluginServices.getText(this, "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

    
144
                this.setLayout(new BorderLayout());
145
                this.setSize(300, 210);
146
                this.setPreferredSize(new java.awt.Dimension(300,210));
147
                this.add(getJPanel(), java.awt.BorderLayout.CENTER);
148
                this.add(getJPanelOkCancel(), java.awt.BorderLayout.SOUTH);
149

    
150
        }
151

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

    
162
                }
163
                return jTxtFieldName;
164
        }
165

    
166
        /**
167
         * This method initializes jCboFieldType
168
         *
169
         * @return javax.swing.JComboBox
170
         */
171
        private JComboBox getJCboFieldType() {
172
                if (jCboFieldType == null) {
173
                        jCboFieldType = new JComboBox();
174
                        jCboFieldType.setBounds(new java.awt.Rectangle(147,52,138,22));
175
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.BOOLEAN]);
176
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.DATE]);
177
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.INT]);
178
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.DOUBLE]);
179
                        jCboFieldType.addItem(DataTypes.TYPE_NAMES[DataTypes.STRING]);
180

    
181
                        jCboFieldType.setSelectedIndex(4);
182
                        jCboFieldType.addActionListener(new java.awt.event.ActionListener() {
183
                                public void actionPerformed(java.awt.event.ActionEvent e) {
184
                                        // System.out.println("actionPerformed()" + e.getActionCommand()); // TODO Auto-generated Event stub actionPerformed()
185
                                        String strType = (String) getJCboFieldType().getModel().getSelectedItem();
186
                                        if (strType == DataTypes.TYPE_NAMES[DataTypes.STRING]) {
187
                                                getJTxtFieldPrecision().setEnabled(true);
188
                                                if (getJTxtFieldPrecision().getText().equals("")){
189
                                                        getJTxtFieldPrecision().setText("3");
190
                                                } else {
191
                                                        try {
192
                                                                Integer.parseInt(getJTxtFieldPrecision().getText());
193
                                                        } catch (NumberFormatException e1){
194
                                                                getJTxtFieldPrecision().setText("3");
195
                                                        }
196
                                                }
197
                                        }else{
198
                                                getJTxtFieldPrecision().setEnabled(false);
199
                                        }
200
                                        if (strType == DataTypes.TYPE_NAMES[DataTypes.BOOLEAN])
201
                                        {
202
                                                getJTxtFieldLength().setText("0");
203
                                                getJTxtFieldLength().setEnabled(false);
204
                                        } else {
205
                                                getJTxtFieldLength().setEnabled(true);
206
                                        }
207

    
208
                                }
209
                        });
210

    
211
                }
212
                return jCboFieldType;
213
        }
214

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

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

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

    
258
        public void loadFieldDescription(
259
                        EditableFeatureAttributeDescriptor attribute)
260
                        throws ParseException {
261
                attribute.setName(getJTxtFieldName().getText());
262
                String strType = (String) getJCboFieldType().getModel()
263
                                .getSelectedItem();
264
                for (int type = 0; type < DataTypes.TYPE_NAMES.length; type++) {
265
                        if (DataTypes.TYPE_NAMES[type] == strType) {
266
                                attribute.setDataType(type);
267
                                break;
268
                        }
269
                }
270

    
271
                try {
272
                        int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
273
                        attribute.setSize(fieldLength);
274
                } catch (Exception e) {
275
                        throw new ParseException(e.getMessage(), 0);
276
                }
277

    
278
                if (strType == DataTypes.TYPE_NAMES[DataTypes.DOUBLE]) {
279
                        try {
280
                                attribute.setPrecision(Integer.parseInt(getJTxtFieldPrecision()
281
                                                .getText()));
282
                        } catch (NumberFormatException e) {
283
                                attribute.setPrecision(3);
284
                        }
285
                }
286
                attribute.setDefaultValue(getJTxtDefaultValue().getText());
287
        }
288

    
289
        public void setOkAction(ActionListener okAction) {
290
                getJPanelOkCancel().setOkButtonActionListener(okAction);
291

    
292
        }
293

    
294
        /**
295
         * This method initializes jPanel
296
         *
297
         * @return javax.swing.JPanel
298
         */
299
        private JPanel getJPanel() {
300
                if (jPanel == null) {
301
                        jPanel = new JPanel();
302
                        jPanel.setLayout(null);
303

    
304
                        jPanel.add(getJPnlFields(), null);
305
                }
306
                return jPanel;
307
        }
308

    
309
        /**
310
         * This method initializes jPanelOkCancel
311
         *
312
         * @return javax.swing.JPanel
313
         */
314
        private AcceptCancelPanel getJPanelOkCancel() {
315
                if (jPanelOkCancel == null) {
316
                        jPanelOkCancel = new AcceptCancelPanel();
317
                        jPanelOkCancel.setCancelButtonActionListener(new ActionListener(){
318
                                public void actionPerformed(java.awt.event.ActionEvent e) {
319
                                        PluginServices.getMDIManager().closeWindow(FPanelCreateField.this);
320
                                };
321
                        });
322
                        jPanelOkCancel.setPreferredSize(new java.awt.Dimension(10,50));
323
                }
324
                return jPanelOkCancel;
325
        }
326

    
327
        /**
328
         * This method initializes jPnlFields
329
         *
330
         * @return javax.swing.JPanel
331
         */
332
        private JPanel getJPnlFields() {
333
                if (jPnlFields == null) {
334
                        GridLayout gridLayout = new GridLayout();
335
                        gridLayout.setRows(6);
336
                        gridLayout.setVgap(3);
337
                        gridLayout.setHgap(5);
338
                        gridLayout.setColumns(2);
339
                        jPnlFields = new JPanel();
340
                        jPnlFields.setLayout(gridLayout);
341
                        jPnlFields.setBounds(new java.awt.Rectangle(5,12,290,142));
342
                        jLblDefaultValue = new JLabel();
343
                        jLblDefaultValue.setBounds(new java.awt.Rectangle(14,163,125,22));
344
                        jLblDefaultValue.setText(PluginServices.getText(this, "default_value"));
345
                        jLblFieldPrecision = new JLabel();
346
                        jLblFieldPrecision.setBounds(new java.awt.Rectangle(14,126,112,22));
347
                        jLblFieldPrecision.setText(PluginServices.getText(this, "precision"));
348
                        jLblFieldLength = new JLabel();
349
                        jLblFieldLength.setBounds(new java.awt.Rectangle(14,89,99,22));
350
                        jLblFieldLength.setText(PluginServices.getText(this, "field_length"));
351
                        jLblFieldType = new JLabel();
352
                        jLblFieldType.setBounds(new java.awt.Rectangle(14,52,94,22));
353
                        jLblFieldType.setText(PluginServices.getText(this, "field_type"));
354
                        jLblFieldName = new JLabel();
355
                        jLblFieldName.setText(PluginServices.getText(this, "field_name"));
356
                        jLblFieldName.setBounds(new java.awt.Rectangle(14,15,99,22));
357
                        jPnlFields.add(jLblFieldName, null);
358
                        jPnlFields.add(getJTxtFieldName(), null);
359
                        jPnlFields.add(jLblFieldType, null);
360
                        jPnlFields.add(getJCboFieldType(), null);
361
                        jPnlFields.add(jLblFieldLength, null);
362
                        jPnlFields.add(getJTxtFieldLength(), null);
363
                        jPnlFields.add(jLblFieldPrecision, null);
364
                        jPnlFields.add(getJTxtFieldPrecision(), null);
365
                        jPnlFields.add(jLblDefaultValue, null);
366
                        jPnlFields.add(getJTxtDefaultValue(), null);
367
                }
368
                return jPnlFields;
369
        }
370

    
371
        public void setCurrentFieldNames(String[] fieldNames) {
372
                currentFieldNames = fieldNames;
373
                String newField = PluginServices.getText(this, "field").replaceAll(" +", "_");
374
                int index=0;
375
                for (int i = 0; i < currentFieldNames.length; i++) {
376
                        if (currentFieldNames[i].startsWith(newField)) {
377
                                try {
378
                                        index = Integer.parseInt(currentFieldNames[i].replaceAll(newField,""));
379
                                } catch (Exception e) { /* we don't care */}
380
                        }
381
                }
382
                jTxtFieldName.setText(newField+(++index));
383
        }
384

    
385
}  //  @jve:decl-index=0:visual-constraint="9,10"