Statistics
| Revision:

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

History | View | Annotate | Download (11.3 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.data.vectorial.AttributeDescriptor;
57
import org.gvsig.data.vectorial.FeatureAttributeDescriptor;
58
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
59
import org.gvsig.gui.beans.AcceptCancelPanel;
60

    
61
import com.iver.andami.PluginServices;
62
import com.iver.andami.ui.mdiManager.IWindow;
63
import com.iver.andami.ui.mdiManager.WindowInfo;
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(FeatureAttributeDescriptor.TYPE_BOOLEAN);
177
                        jCboFieldType.addItem(FeatureAttributeDescriptor.TYPE_DATE);
178
                        jCboFieldType.addItem(FeatureAttributeDescriptor.TYPE_INT);
179
                        jCboFieldType.addItem(FeatureAttributeDescriptor.TYPE_DOUBLE);
180
                        jCboFieldType.addItem(FeatureAttributeDescriptor.TYPE_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
                                        if (strType == FeatureAttributeDescriptor.TYPE_DOUBLE) {
188
                                                getJTxtFieldPrecision().setEnabled(true);
189
                                                if (getJTxtFieldPrecision().getText().equals("")){
190
                                                        getJTxtFieldPrecision().setText("3");
191
                                                } else {
192
                                                        try {
193
                                                                Integer.parseInt(getJTxtFieldPrecision().getText());
194
                                                        } catch (NumberFormatException e1){
195
                                                                getJTxtFieldPrecision().setText("3");
196
                                                        }
197
                                                }
198
                                        }else{
199
                                                getJTxtFieldPrecision().setEnabled(false);
200
                                        }
201
                                        if (strType == FeatureAttributeDescriptor.TYPE_BOOLEAN)
202
                                        {
203
                                                getJTxtFieldLength().setText("0");
204
                                                getJTxtFieldLength().setEnabled(false);
205
                                        }
206
                                        else
207
                                                getJTxtFieldLength().setEnabled(true);
208

    
209
                                }
210
                        });
211

    
212
                }
213
                return jCboFieldType;
214
        }
215

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

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

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

    
259
        public void loadFieldDescription(AttributeDescriptor attribute) throws ParseException, IsNotAttributeSettingException
260
        {
261
                attribute.setName(getJTxtFieldName().getText());
262
                String strType = (String) getJCboFieldType().getModel()
263
                                .getSelectedItem();
264
                attribute.setType(strType);
265
                try {
266
                        int fieldLength = Integer.parseInt(getJTxtFieldLength().getText());
267
                        attribute.setSize(fieldLength);
268
                } catch (Exception e) {
269
                        throw new ParseException(e.getMessage(), 0);
270
                }
271

    
272
                if (strType == FeatureAttributeDescriptor.TYPE_DOUBLE) {
273
                        try {
274
                                attribute.setPrecision(Integer.parseInt(getJTxtFieldPrecision()
275
                                                .getText()));
276
                        } catch (NumberFormatException e) {
277
                                attribute.setPrecision(3);
278
                        }
279
                } 
280
                attribute.setDefaultValue(getJTxtDefaultValue().getText());
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, "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

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