Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / panels / FPanelCreateField.java @ 11727

History | View | Annotate | Download (11.5 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.sql.Types;
50
import java.text.ParseException;
51

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

    
57
import org.gvsig.gui.beans.AcceptCancelPanel;
58

    
59
import com.hardcode.gdbms.engine.values.ValueFactory;
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.ui.mdiManager.IWindow;
62
import com.iver.andami.ui.mdiManager.WindowInfo;
63
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
64

    
65
public class FPanelCreateField extends JPanel implements IWindow {
66

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

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

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

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

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

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

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

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

    
138
        /**
139
         * This method initializes this
140
         *
141
         * @return void
142
         */
143
        private void initialize() {
144

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

    
151
        }
152

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

    
163
                }
164
                return jTxtFieldName;
165
        }
166

    
167
        /**
168
         * This method initializes jCboFieldType
169
         *
170
         * @return javax.swing.JComboBox
171
         */
172
        private JComboBox getJCboFieldType() {
173
                if (jCboFieldType == null) {
174
                        jCboFieldType = new JComboBox();
175
                        jCboFieldType.setBounds(new java.awt.Rectangle(147,52,138,22));
176
                        jCboFieldType.addItem("Boolean");
177
                        jCboFieldType.addItem("Date");
178
                        jCboFieldType.addItem("Integer");
179
                        jCboFieldType.addItem("Double");
180
                        jCboFieldType.addItem("String");
181

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

    
210
                                }
211
                        });
212

    
213
                }
214
                return jCboFieldType;
215
        }
216

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

    
232
        /**
233
         * This method initializes jTxtFieldPrecision
234
         *
235
         * @return javax.swing.JTextField
236
         */
237
        private JTextField getJTxtFieldPrecision() {
238
                if (jTxtFieldPrecision == null) {
239
                        jTxtFieldPrecision = new JTextField();
240
                        jTxtFieldPrecision.setBounds(new java.awt.Rectangle(147,126,138,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.setBounds(new java.awt.Rectangle(147,163,138,22));
256
                }
257
                return jTxtDefaultValue;
258
        }
259

    
260
        public FieldDescription getFieldDescription() throws ParseException
261
        {
262
                FieldDescription newField = new FieldDescription();
263
                newField.setFieldName(getJTxtFieldName().getText());
264
                String strType = (String) getJCboFieldType().getModel().getSelectedItem();
265
                int fieldType = FieldDescription.stringToType(strType);
266
                newField.setFieldType(fieldType);
267
                try {
268
                        int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
269
                        newField.setFieldLength(fieldLength);
270
                } catch (Exception e) {
271
                        throw new ParseException(e.getMessage(), 0);
272
                }
273

    
274
                if (fieldType == Types.DOUBLE)
275
                {
276
                        try {
277
                        newField.setFieldDecimalCount(
278
                                        Integer.parseInt(
279
                                                        getJTxtFieldPrecision().getText()));
280
                        } catch (NumberFormatException e){
281
                                newField.setFieldDecimalCount(3);
282
                }
283
                }
284
                else
285
                        newField.setFieldDecimalCount(0);
286
                String defaultValue = getJTxtDefaultValue().getText();
287
                if (defaultValue != null)
288
                {
289

    
290
                        if (defaultValue.compareTo("")==0)
291
                                newField.setDefaultValue(ValueFactory.createNullValue());
292
                        else
293
                                newField.setDefaultValue(ValueFactory.createValueByType(defaultValue, fieldType));
294
                }
295

    
296
                return newField;
297
        }
298

    
299
        public void setOkAction(ActionListener okAction) {
300
                getJPanelOkCancel().setOkButtonActionListener(okAction);
301

    
302
        }
303

    
304
        /**
305
         * This method initializes jPanel
306
         *
307
         * @return javax.swing.JPanel
308
         */
309
        private JPanel getJPanel() {
310
                if (jPanel == null) {
311
                        jPanel = new JPanel();
312
                        jPanel.setLayout(null);
313

    
314
                        jPanel.add(getJPnlFields(), null);
315
                }
316
                return jPanel;
317
        }
318

    
319
        /**
320
         * This method initializes jPanelOkCancel
321
         *
322
         * @return javax.swing.JPanel
323
         */
324
        private AcceptCancelPanel getJPanelOkCancel() {
325
                if (jPanelOkCancel == null) {
326
                        jPanelOkCancel = new AcceptCancelPanel();
327
                        jPanelOkCancel.setCancelButtonActionListener(new ActionListener(){
328
                                public void actionPerformed(java.awt.event.ActionEvent e) {
329
                                        PluginServices.getMDIManager().closeWindow(FPanelCreateField.this);
330
                                };
331
                        });
332
                        jPanelOkCancel.setPreferredSize(new java.awt.Dimension(10,50));
333
                }
334
                return jPanelOkCancel;
335
        }
336

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

    
381
        public void setCurrentFieldNames(String[] fieldNames) {
382
                currentFieldNames = fieldNames;
383
                String newField = PluginServices.getText(this, "field").replaceAll(" +", "_");
384
                int index=0;
385
                for (int i = 0; i < currentFieldNames.length; i++) {
386
                        if (currentFieldNames[i].startsWith(newField)) {
387
                                try {
388
                                        index = Integer.parseInt(currentFieldNames[i].replaceAll(newField,""));
389
                                } catch (Exception e) { /* we don't care */}
390
                        }
391
                }
392
                jTxtFieldName.setText(newField+(++index));
393
        }
394

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