Statistics
| Revision:

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

History | View | Annotate | Download (11.4 KB)

1 6458 fjp
/* 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 7738 jaume
import java.awt.GridLayout;
45 6458 fjp
import java.awt.LayoutManager;
46
import java.awt.event.ActionListener;
47 6843 jaume
import java.awt.event.KeyEvent;
48
import java.awt.event.KeyListener;
49 6478 fjp
import java.text.ParseException;
50 6458 fjp
51
import javax.swing.JComboBox;
52
import javax.swing.JLabel;
53
import javax.swing.JPanel;
54
import javax.swing.JTextField;
55
56 24759 jmvivo
import org.gvsig.fmap.dal.DataTypes;
57
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
58 6458 fjp
import org.gvsig.gui.beans.AcceptCancelPanel;
59
60
import com.iver.andami.PluginServices;
61 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
62 6880 cesar
import com.iver.andami.ui.mdiManager.WindowInfo;
63 6458 fjp
64 6877 cesar
public class FPanelCreateField extends JPanel implements IWindow {
65 6458 fjp
66 6829 jaume
        private static final String DEFAULT_FIELD_LENGTH = "50";
67 6458 fjp
        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 6880 cesar
        private WindowInfo viewInfo;
78 6458 fjp
        private JPanel jPanel = null;  //  @jve:decl-index=0:visual-constraint="299,27"
79
        private AcceptCancelPanel jPanelOkCancel = null;
80 6828 jaume
        private JPanel jPnlFields = null;
81 6843 jaume
        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 7265 jaume
        private String[] currentFieldNames;
101 6843 jaume
102 6458 fjp
        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 6880 cesar
        public WindowInfo getWindowInfo() {
127 6458 fjp
                if (viewInfo == null)
128
                {
129 6880 cesar
                        viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
130 6828 jaume
                        viewInfo.setWidth(this.getWidth()+8);
131
                        viewInfo.setHeight(this.getHeight());
132 6843 jaume
                        viewInfo.setTitle(PluginServices.getText(this, "new_field_properties"));
133 6458 fjp
                }
134
                return viewInfo;
135
        }
136
137
        /**
138
         * This method initializes this
139 6828 jaume
         *
140 6458 fjp
         * @return void
141
         */
142
        private void initialize() {
143 6828 jaume
144 6458 fjp
                this.setLayout(new BorderLayout());
145 6828 jaume
                this.setSize(300, 210);
146
                this.setPreferredSize(new java.awt.Dimension(300,210));
147 6458 fjp
                this.add(getJPanel(), java.awt.BorderLayout.CENTER);
148
                this.add(getJPanelOkCancel(), java.awt.BorderLayout.SOUTH);
149 6828 jaume
150 6458 fjp
        }
151
152
        /**
153 6828 jaume
         * This method initializes jTxtFieldName
154
         *
155
         * @return javax.swing.JTextField
156 6458 fjp
         */
157
        private JTextField getJTxtFieldName() {
158
                if (jTxtFieldName == null) {
159
                        jTxtFieldName = new JTextField();
160
                        jTxtFieldName.setBounds(new java.awt.Rectangle(147,15,138,22));
161 7265 jaume
162 6458 fjp
                }
163
                return jTxtFieldName;
164
        }
165
166
        /**
167 6828 jaume
         * This method initializes jCboFieldType
168
         *
169
         * @return javax.swing.JComboBox
170 6458 fjp
         */
171
        private JComboBox getJCboFieldType() {
172
                if (jCboFieldType == null) {
173
                        jCboFieldType = new JComboBox();
174
                        jCboFieldType.setBounds(new java.awt.Rectangle(147,52,138,22));
175 24759 jmvivo
                        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 6828 jaume
181 6629 fjp
                        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 24759 jmvivo
                                        if (strType == DataTypes.TYPE_NAMES[DataTypes.STRING]) {
187 11727 jmvivo
                                                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 24759 jmvivo
                                        if (strType == DataTypes.TYPE_NAMES[DataTypes.BOOLEAN])
201 6629 fjp
                                        {
202
                                                getJTxtFieldLength().setText("0");
203
                                                getJTxtFieldLength().setEnabled(false);
204 22360 jmvivo
                                        } else {
205
                                                getJTxtFieldLength().setEnabled(true);
206 6629 fjp
                                        }
207 6828 jaume
208 6629 fjp
                                }
209
                        });
210 6828 jaume
211 6458 fjp
                }
212
                return jCboFieldType;
213
        }
214
215
        /**
216 6828 jaume
         * This method initializes jTxtFieldLength
217
         *
218
         * @return javax.swing.JTextField
219 6458 fjp
         */
220
        private JTextField getJTxtFieldLength() {
221
                if (jTxtFieldLength == null) {
222
                        jTxtFieldLength = new JTextField();
223
                        jTxtFieldLength.setBounds(new java.awt.Rectangle(147,89,138,22));
224 6829 jaume
                        jTxtFieldLength.setText(DEFAULT_FIELD_LENGTH);
225 6843 jaume
                        jTxtFieldLength.addKeyListener(checkInt);
226 6458 fjp
                }
227
                return jTxtFieldLength;
228
        }
229
230
        /**
231 6828 jaume
         * This method initializes jTxtFieldPrecision
232
         *
233
         * @return javax.swing.JTextField
234 6458 fjp
         */
235
        private JTextField getJTxtFieldPrecision() {
236
                if (jTxtFieldPrecision == null) {
237
                        jTxtFieldPrecision = new JTextField();
238
                        jTxtFieldPrecision.setBounds(new java.awt.Rectangle(147,126,138,22));
239 6629 fjp
                        jTxtFieldPrecision.setEnabled(false);
240 6843 jaume
                        jTxtFieldPrecision.addKeyListener(checkInt );
241 6458 fjp
                }
242
                return jTxtFieldPrecision;
243
        }
244
245
        /**
246 6828 jaume
         * This method initializes jTxtDefaultValue
247
         *
248
         * @return javax.swing.JTextField
249 6458 fjp
         */
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 24759 jmvivo
        public void loadFieldDescription(
259
                        EditableFeatureAttributeDescriptor attribute)
260
                        throws ParseException {
261 21376 jmvivo
                attribute.setName(getJTxtFieldName().getText());
262
                String strType = (String) getJCboFieldType().getModel()
263
                                .getSelectedItem();
264 24759 jmvivo
                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 6828 jaume
                try {
272
                        int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
273 21376 jmvivo
                        attribute.setSize(fieldLength);
274 6828 jaume
                } catch (Exception e) {
275
                        throw new ParseException(e.getMessage(), 0);
276
                }
277
278 24759 jmvivo
                if (strType == DataTypes.TYPE_NAMES[DataTypes.DOUBLE]) {
279 11727 jmvivo
                        try {
280 21376 jmvivo
                                attribute.setPrecision(Integer.parseInt(getJTxtFieldPrecision()
281
                                                .getText()));
282
                        } catch (NumberFormatException e) {
283
                                attribute.setPrecision(3);
284
                        }
285 22360 jmvivo
                }
286 21376 jmvivo
                attribute.setDefaultValue(getJTxtDefaultValue().getText());
287 6458 fjp
        }
288
289
        public void setOkAction(ActionListener okAction) {
290
                getJPanelOkCancel().setOkButtonActionListener(okAction);
291 6828 jaume
292 6458 fjp
        }
293
294
        /**
295 6828 jaume
         * This method initializes jPanel
296
         *
297
         * @return javax.swing.JPanel
298 6458 fjp
         */
299
        private JPanel getJPanel() {
300
                if (jPanel == null) {
301
                        jPanel = new JPanel();
302
                        jPanel.setLayout(null);
303 6828 jaume
304
                        jPanel.add(getJPnlFields(), null);
305 6458 fjp
                }
306
                return jPanel;
307
        }
308
309
        /**
310 6828 jaume
         * This method initializes jPanelOkCancel
311
         *
312
         * @return javax.swing.JPanel
313 6458 fjp
         */
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 6880 cesar
                                        PluginServices.getMDIManager().closeWindow(FPanelCreateField.this);
320 6458 fjp
                                };
321 6828 jaume
                        });
322 6458 fjp
                        jPanelOkCancel.setPreferredSize(new java.awt.Dimension(10,50));
323
                }
324
                return jPanelOkCancel;
325
        }
326
327 6828 jaume
        /**
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 6837 jaume
                        jLblDefaultValue.setText(PluginServices.getText(this, "default_value"));
345 6828 jaume
                        jLblFieldPrecision = new JLabel();
346
                        jLblFieldPrecision.setBounds(new java.awt.Rectangle(14,126,112,22));
347 6837 jaume
                        jLblFieldPrecision.setText(PluginServices.getText(this, "precision"));
348 6828 jaume
                        jLblFieldLength = new JLabel();
349
                        jLblFieldLength.setBounds(new java.awt.Rectangle(14,89,99,22));
350 6837 jaume
                        jLblFieldLength.setText(PluginServices.getText(this, "field_length"));
351 6828 jaume
                        jLblFieldType = new JLabel();
352
                        jLblFieldType.setBounds(new java.awt.Rectangle(14,52,94,22));
353 6837 jaume
                        jLblFieldType.setText(PluginServices.getText(this, "field_type"));
354 6828 jaume
                        jLblFieldName = new JLabel();
355 6837 jaume
                        jLblFieldName.setText(PluginServices.getText(this, "field_name"));
356 6828 jaume
                        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 7265 jaume
        public void setCurrentFieldNames(String[] fieldNames) {
372
                currentFieldNames = fieldNames;
373 11727 jmvivo
                String newField = PluginServices.getText(this, "field").replaceAll(" +", "_");
374 7265 jaume
                int index=0;
375 7266 jaume
                for (int i = 0; i < currentFieldNames.length; i++) {
376 7265 jaume
                        if (currentFieldNames[i].startsWith(newField)) {
377 7266 jaume
                                try {
378
                                        index = Integer.parseInt(currentFieldNames[i].replaceAll(newField,""));
379
                                } catch (Exception e) { /* we don't care */}
380
                        }
381 7265 jaume
                }
382
                jTxtFieldName.setText(newField+(++index));
383
        }
384
385 6458 fjp
}  //  @jve:decl-index=0:visual-constraint="9,10"