Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / gui / panels / FPanelCreateField.java @ 7268

History | View | Annotate | Download (11.2 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.LayoutManager;
45
import java.awt.event.ActionListener;
46
import java.awt.event.KeyEvent;
47
import java.awt.event.KeyListener;
48
import java.sql.Types;
49
import java.text.ParseException;
50
import java.util.Vector;
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.instruction.SemanticException;
60
import com.hardcode.gdbms.engine.values.ValueFactory;
61
import com.iver.andami.PluginServices;
62
import com.iver.andami.ui.mdiManager.IWindow;
63
import com.iver.andami.ui.mdiManager.WindowInfo;
64
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
65
import java.awt.GridLayout;
66

    
67
public class FPanelCreateField extends JPanel implements IWindow {
68

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

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

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

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

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

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

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

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

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

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

    
153
        }
154

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

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

    
183
                        jCboFieldType.setSelectedIndex(4);
184
                        jCboFieldType.addActionListener(new java.awt.event.ActionListener() {
185
                                public void actionPerformed(java.awt.event.ActionEvent e) {
186
                                        // System.out.println("actionPerformed()" + e.getActionCommand()); // TODO Auto-generated Event stub actionPerformed()
187
                                        String strType = (String) getJCboFieldType().getModel().getSelectedItem();
188
                                        int fieldType = FieldDescription.stringToType(strType);
189
                                        getJTxtFieldPrecision().setEnabled((fieldType == Types.DOUBLE));
190
                                        if (fieldType == Types.BOOLEAN)
191
                                        {
192
                                                getJTxtFieldLength().setText("0");
193
                                                getJTxtFieldLength().setEnabled(false);
194
                                        }
195
                                        else
196
                                                getJTxtFieldLength().setEnabled(true);
197

    
198
                                }
199
                        });
200

    
201
                }
202
                return jCboFieldType;
203
        }
204

    
205
        /**
206
         * This method initializes jTxtFieldLength
207
         *
208
         * @return javax.swing.JTextField
209
         */
210
        private JTextField getJTxtFieldLength() {
211
                if (jTxtFieldLength == null) {
212
                        jTxtFieldLength = new JTextField();
213
                        jTxtFieldLength.setBounds(new java.awt.Rectangle(147,89,138,22));
214
                        jTxtFieldLength.setText(DEFAULT_FIELD_LENGTH);
215
                        jTxtFieldLength.addKeyListener(checkInt);
216
                }
217
                return jTxtFieldLength;
218
        }
219

    
220
        /**
221
         * This method initializes jTxtFieldPrecision
222
         *
223
         * @return javax.swing.JTextField
224
         */
225
        private JTextField getJTxtFieldPrecision() {
226
                if (jTxtFieldPrecision == null) {
227
                        jTxtFieldPrecision = new JTextField();
228
                        jTxtFieldPrecision.setBounds(new java.awt.Rectangle(147,126,138,22));
229
                        jTxtFieldPrecision.setEnabled(false);
230
                        jTxtFieldPrecision.addKeyListener(checkInt );
231
                }
232
                return jTxtFieldPrecision;
233
        }
234

    
235
        /**
236
         * This method initializes jTxtDefaultValue
237
         *
238
         * @return javax.swing.JTextField
239
         */
240
        private JTextField getJTxtDefaultValue() {
241
                if (jTxtDefaultValue == null) {
242
                        jTxtDefaultValue = new JTextField();
243
                        jTxtDefaultValue.setBounds(new java.awt.Rectangle(147,163,138,22));
244
                }
245
                return jTxtDefaultValue;
246
        }
247

    
248
        public FieldDescription getFieldDescription() throws ParseException
249
        {
250
                FieldDescription newField = new FieldDescription();
251
                newField.setFieldName(getJTxtFieldName().getText());
252
                String strType = (String) getJCboFieldType().getModel().getSelectedItem();
253
                int fieldType = FieldDescription.stringToType(strType);
254
                newField.setFieldType(fieldType);
255
                try {
256
                        int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
257
                        newField.setFieldLength(fieldLength);
258
                } catch (Exception e) {
259
                        throw new ParseException(e.getMessage(), 0);
260
                }
261

    
262
                if (fieldType == Types.DOUBLE)
263
                {
264
                        newField.setFieldDecimalCount(
265
                                        Integer.parseInt(
266
                                                        getJTxtFieldPrecision().getText()));
267
                }
268
                else
269
                        newField.setFieldDecimalCount(0);
270
                String defaultValue = getJTxtDefaultValue().getText();
271
                if (defaultValue != null)
272
                {
273

    
274
                        if (defaultValue.compareTo("")==0)
275
                                newField.setDefaultValue(ValueFactory.createNullValue());
276
                        else
277
                                newField.setDefaultValue(ValueFactory.createValueByType(defaultValue, fieldType));
278
                }
279

    
280
                return newField;
281
        }
282

    
283
        public void setOkAction(ActionListener okAction) {
284
                getJPanelOkCancel().setOkButtonActionListener(okAction);
285

    
286
        }
287

    
288
        /**
289
         * This method initializes jPanel
290
         *
291
         * @return javax.swing.JPanel
292
         */
293
        private JPanel getJPanel() {
294
                if (jPanel == null) {
295
                        jPanel = new JPanel();
296
                        jPanel.setLayout(null);
297

    
298
                        jPanel.add(getJPnlFields(), null);
299
                }
300
                return jPanel;
301
        }
302

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

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

    
365
        public void setCurrentFieldNames(String[] fieldNames) {
366
                currentFieldNames = fieldNames;
367
                String newField = PluginServices.getText(this, "new_field").replaceAll(" +", "_");
368
                int index=0;
369
                for (int i = 0; i < currentFieldNames.length; i++) {
370
                        if (currentFieldNames[i].startsWith(newField)) {
371
                                try {
372
                                        index = Integer.parseInt(currentFieldNames[i].replaceAll(newField,""));
373
                                } catch (Exception e) { /* we don't care */}
374
                        }
375
                }
376
                jTxtFieldName.setText(newField+(++index));
377
        }
378
}  //  @jve:decl-index=0:visual-constraint="9,10"