Statistics
| Revision:

root / trunk / libraries / libUIComponent_praster / src / org / gvsig / gui / beans / dataInput / DataInputContainer.java @ 8026

History | View | Annotate | Download (7.46 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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
package org.gvsig.gui.beans.dataInput;
20

    
21
import java.awt.FlowLayout;
22
import java.awt.GridBagConstraints;
23
import java.awt.event.FocusEvent;
24
import java.awt.event.FocusListener;
25
import java.awt.event.KeyEvent;
26
import java.awt.event.KeyListener;
27

    
28
import javax.swing.JLabel;
29
import javax.swing.JPanel;
30
import javax.swing.JTextField;
31

    
32
import org.gvsig.gui.beans.BaseComponent;
33

    
34
public class DataInputContainer extends BaseComponent implements FocusListener, KeyListener{
35

    
36
        private int                                        wComp = 150, hComp = 22;
37
        private int                                        wLabel = 30, hLabel = 20;
38
        private int                                        wText = wComp - wLabel - 5, hText = 20;
39
        
40
        private boolean                                decimal = false;
41
        private boolean                                caracter = false;
42
        
43
        private JPanel pInput = null;
44
        private JLabel lText = null;
45
        private JTextField jTextField = null;
46
        
47
        private String                                focusText = null;
48

    
49
        /**
50
         * This is the default constructor
51
         */
52
        public DataInputContainer(int widthLabel) {
53
                super();
54
                if(widthLabel > 0){
55
                        wLabel = widthLabel;
56
                        wText = wComp - wLabel - 5;
57
                }
58
                initialize();
59
        }
60

    
61
        /**
62
         * This method initializes this
63
         * 
64
         * @return void
65
         */
66
        private void initialize() {
67
                FlowLayout flowLayout = new FlowLayout();
68
                flowLayout.setHgap(0);
69
                flowLayout.setVgap(0);
70
                flowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
71
                this.setLayout(flowLayout);
72
                //this.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
73
                this.add(getPInput(), null);
74
                this.getJTextField().addFocusListener(this);
75
                this.getJTextField().addKeyListener(this);
76
                setComponentSize(wComp, hComp);
77
        }
78

    
79
        /**
80
         * This method initializes jPanel        
81
         *         
82
         * @return javax.swing.JPanel        
83
         */
84
        private JPanel getPInput() {
85
                if (pInput == null) {
86
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
87
                        gridBagConstraints1.gridy = 0;
88
                        gridBagConstraints1.gridx = 1;
89
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
90
                        gridBagConstraints.gridx = 0;
91
                        gridBagConstraints.gridy = 0;
92
                        FlowLayout flowLayout3 = new FlowLayout();
93
                        flowLayout3.setAlignment(java.awt.FlowLayout.RIGHT);
94
                        flowLayout3.setVgap(0);
95
                        flowLayout3.setHgap(0);
96
                        pInput = new JPanel();
97
                        pInput.setLayout(flowLayout3);
98
                        pInput.add(getLText(), null);
99
                        pInput.add(getJTextField(), null);
100
                }
101
                return pInput;
102
        }
103

    
104
        private JLabel getLText(){
105
                if (lText == null){
106
                        lText = new JLabel();
107
                        lText.setText("JLabel");
108
                        lText.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
109
                }
110
                return lText;
111
        }
112
        
113
        
114
        /**
115
         * Da nombre al campo de texto del componente.
116
         * @param text
117
         */
118
        public void setLabelText(String text){
119
                this.lText.setText(text + ": ");
120
        }
121

    
122
        /**
123
         * This method initializes jTextField        
124
         *         
125
         * @return javax.swing.JTextField        
126
         */
127
        public JTextField getJTextField() {
128
                if (jTextField == null) {
129
                        jTextField = new JTextField();
130
                        jTextField.setBackground(java.awt.Color.white);
131
                }
132
                return jTextField;
133
        }
134
        
135
        /**
136
         * Ajusta el tama?o del componente.
137
         * @param w
138
         * @param h
139
         */
140
        public void setComponentSize(int w, int h){
141
                wComp = w;
142
                int wPanel = (int)(wComp * 0.9);
143
                int hPanel = (int)(hComp * 0.95);
144
                wText = wPanel - wLabel - 5;
145
                this.setPreferredSize(new java.awt.Dimension(wComp, hComp));
146
                this.getPInput().setPreferredSize(new java.awt.Dimension(wPanel, hPanel));
147
                this.getJTextField().setPreferredSize(new java.awt.Dimension(wText, hText));
148
        }
149

    
150
        /**
151
         * Modifica la altura del componente.
152
         * @param h
153
         */
154
        public void setComponentHeight(int h){
155
                this.hComp = h;
156
                this.setPreferredSize(new java.awt.Dimension(wComp, hComp));
157
        }
158
        
159
        /**
160
         * Devuelve el valor del campo de texto del componente.
161
         * @return
162
         */
163
        public String getTextValue(){
164
                return this.jTextField.getText();
165
        }
166
        
167
        /**
168
         * metodos para indicar y conocer el tipo de valor acepatado por el 
169
         * campo de texto.
170
         * @param flag
171
         */
172
        public void setDecimal(boolean flag){
173
                this.decimal = flag;
174
        }
175
        
176
        public void setCaracter(boolean flag){
177
                this.caracter = flag; 
178
        }
179
        
180
        public boolean isDecimal(){
181
                return this.decimal;
182
        }
183
        
184
        public boolean isCaracter(){
185
                return this.caracter;
186
        }
187
        
188
        /**
189
         * Modifica el tama?o de la etiqueta del componente.
190
         * @param w
191
         */
192
        public void setLabelSize(int w){
193
                this.wLabel = w;
194
        }
195
        
196
        /**
197
         * Habilita o deshabilita el control
198
         * @param en
199
         */
200
        public void setControlEnabled(boolean en){
201
                this.getJTextField().setEnabled(en);
202
                if (en)
203
                        this.getJTextField().setBackground(java.awt.Color.white);
204
                else
205
                        this.getJTextField().setBackground(getBackground());
206
        }
207
        
208
        /**
209
         * Devuelve el valor del campo de texto.
210
         * @return
211
         */
212
        public String getValue(){
213
                return this.getJTextField().getText();
214
        }
215
        
216
        /**
217
         * Asigna el valor al campo de texto.
218
         * @return
219
         */
220
        public void setValue(String value){
221
                this.getJTextField().setText(value);
222
        }
223
        
224
        
225
        /*********************************************************/
226
        /*********************LISTENERS***************************/
227
        /*********************************************************/
228
        
229
        
230
        public void focusGained(FocusEvent e) {
231
                if (e.getSource() == this.getJTextField()){
232
                        if(!validateNumericFormat(this.getJTextField().getText()))
233
                                this.getJTextField().setText("");
234
                }
235
                
236
        }
237

    
238
        public void focusLost(FocusEvent e) {
239
                //if(e.getSource() == this.getJTextField())
240
                        //checkTextValue();
241
                
242
        }
243
        
244
        
245

    
246
        public void keyPressed(KeyEvent e) {
247
                
248
        }
249
                
250

    
251
        public void keyReleased(KeyEvent e) {
252
                String cad = this.getJTextField().getText();
253
                
254
                if (e.getSource()==this.getJTextField()){
255
                        if (e.getKeyCode() == 10){
256
                                if(!validateNumericFormat(cad))
257
                                        this.getJTextField().setText("");
258
                                return;
259
                        }
260
                        if (!caracter){        
261
                                if (checkTextField(e.getKeyCode()))
262
                                        focusText = cad;
263
                                else
264
                                        this.getJTextField().setText(focusText);
265
                        }
266
                }
267
        }
268

    
269
        public void keyTyped(KeyEvent e) {
270
                
271
        }
272
        
273
        
274
        /**
275
         * Checkea un textField para que contenga caracteres validos. Consideramos validos
276
         * los caracteres del 0 al 9, el punto, DEL, Supr, flechas y Enter teniendo en cuenta que el 
277
         * c?digo ascii del teclado numerico es distinto.
278
         */
279
        public boolean checkTextField(int code){
280
                
281
                //Si el caracter es erroneo pasamos de el y reponemos el texto que habia
282
                if(        (code >= 0 && code <= 7) || 
283
                        code == 9 || 
284
                        (code >= 11 && code <= 36) ||
285
                        (code >= 41 && code <= 45) ||
286
                        code == 47 ||
287
                        (code >= 58 && code <= 95) ||
288
                        (code >= 106 && code <= 109) ||
289
                        (code >= 111 && code <= 126) ||
290
                        code >= 128){
291
                        return false;
292
                }
293
                
294
                return true;
295
        }
296
        
297
        /**
298
         * Validaci?n de que el par?metro tenga formato numerico
299
         * @param t Valor del campo
300
         * @return true si es valido y false si no lo es
301
         */
302
        private boolean validateNumericFormat(String field){
303
                double d = 0D;
304
                try{
305
                        d = Double.valueOf(field).doubleValue();
306
                }catch(NumberFormatException exc){
307
                        return false;
308
                }
309
                return true;
310
        }
311
        
312
        
313
}