root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / datainput / DataInputContainer.java @ 12127
History | View | Annotate | Download (7.37 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 |
public class DataInputContainer extends JPanel implements FocusListener, KeyListener{ |
33 |
private static final long serialVersionUID = 7084105134015956663L; |
34 |
private int wComp = 150, hComp = 26; |
35 |
private int wLabel = 20; |
36 |
private int wText = wComp - wLabel - 5, hText = 20; |
37 |
|
38 |
private boolean decimal = false; |
39 |
private boolean caracter = false; |
40 |
|
41 |
private JPanel pInput = null; |
42 |
private JLabel lText = null; |
43 |
private JTextField jTextField = null; |
44 |
|
45 |
private String focusText = null; |
46 |
|
47 |
/**
|
48 |
* This is the default constructor
|
49 |
*/
|
50 |
public DataInputContainer(int widthLabel) { |
51 |
super();
|
52 |
if(widthLabel > 0){ |
53 |
wLabel = widthLabel; |
54 |
wText = wComp - wLabel - 5;
|
55 |
} |
56 |
initialize(); |
57 |
} |
58 |
|
59 |
/**
|
60 |
* This method initializes this
|
61 |
*
|
62 |
* @return void
|
63 |
*/
|
64 |
private void initialize() { |
65 |
FlowLayout flowLayout = new FlowLayout(); |
66 |
flowLayout.setHgap(0);
|
67 |
flowLayout.setVgap(0);
|
68 |
flowLayout.setAlignment(java.awt.FlowLayout.RIGHT); |
69 |
this.setLayout(flowLayout);
|
70 |
//this.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
|
71 |
this.add(getPInput(), null); |
72 |
this.getJTextField().addFocusListener(this); |
73 |
this.getJTextField().addKeyListener(this); |
74 |
setComponentSize(wComp, hComp); |
75 |
} |
76 |
|
77 |
/**
|
78 |
* This method initializes jPanel
|
79 |
*
|
80 |
* @return javax.swing.JPanel
|
81 |
*/
|
82 |
private JPanel getPInput() { |
83 |
if (pInput == null) { |
84 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
85 |
gridBagConstraints1.gridy = 0;
|
86 |
gridBagConstraints1.gridx = 1;
|
87 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
88 |
gridBagConstraints.gridx = 0;
|
89 |
gridBagConstraints.gridy = 0;
|
90 |
FlowLayout flowLayout3 = new FlowLayout(); |
91 |
flowLayout3.setAlignment(java.awt.FlowLayout.RIGHT); |
92 |
flowLayout3.setVgap(0);
|
93 |
flowLayout3.setHgap(0);
|
94 |
pInput = new JPanel(); |
95 |
pInput.setLayout(flowLayout3); |
96 |
pInput.add(getLText(), null);
|
97 |
pInput.add(getJTextField(), null);
|
98 |
} |
99 |
return pInput;
|
100 |
} |
101 |
|
102 |
private JLabel getLText(){ |
103 |
if (lText == null){ |
104 |
lText = new JLabel(); |
105 |
lText.setText("JLabel");
|
106 |
lText.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT); |
107 |
} |
108 |
return lText;
|
109 |
} |
110 |
|
111 |
|
112 |
/**
|
113 |
* Da nombre al campo de texto del componente.
|
114 |
* @param text
|
115 |
*/
|
116 |
public void setLabelText(String text){ |
117 |
this.lText.setText(text + ": "); |
118 |
} |
119 |
|
120 |
/**
|
121 |
* This method initializes jTextField
|
122 |
*
|
123 |
* @return javax.swing.JTextField
|
124 |
*/
|
125 |
public JTextField getJTextField() { |
126 |
if (jTextField == null) { |
127 |
jTextField = new JTextField(); |
128 |
jTextField.setBackground(java.awt.Color.white); |
129 |
} |
130 |
return jTextField;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* Ajusta el tama?o del componente.
|
135 |
* @param w
|
136 |
* @param h
|
137 |
*/
|
138 |
public void setComponentSize(int w, int h){ |
139 |
wComp = w; |
140 |
wText = wComp - wLabel - 5;
|
141 |
this.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
142 |
this.getPInput().setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
143 |
this.getJTextField().setPreferredSize(new java.awt.Dimension(wText, hText)); |
144 |
} |
145 |
|
146 |
/**
|
147 |
* Modifica la altura del componente.
|
148 |
* @param h
|
149 |
*/
|
150 |
public void setComponentHeight(int h){ |
151 |
this.hComp = h;
|
152 |
this.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Devuelve el valor del campo de texto del componente.
|
157 |
* @return
|
158 |
*/
|
159 |
public String getTextValue(){ |
160 |
return this.jTextField.getText(); |
161 |
} |
162 |
|
163 |
/**
|
164 |
* metodos para indicar y conocer el tipo de valor acepatado por el
|
165 |
* campo de texto.
|
166 |
* @param flag
|
167 |
*/
|
168 |
public void setDecimal(boolean flag){ |
169 |
this.decimal = flag;
|
170 |
} |
171 |
|
172 |
public void setCaracter(boolean flag){ |
173 |
this.caracter = flag;
|
174 |
} |
175 |
|
176 |
public boolean isDecimal(){ |
177 |
return this.decimal; |
178 |
} |
179 |
|
180 |
public boolean isCaracter(){ |
181 |
return this.caracter; |
182 |
} |
183 |
|
184 |
/**
|
185 |
* Modifica el tama?o de la etiqueta del componente.
|
186 |
* @param w
|
187 |
*/
|
188 |
public void setLabelSize(int w){ |
189 |
this.wLabel = w;
|
190 |
} |
191 |
|
192 |
/**
|
193 |
* Habilita o deshabilita el control
|
194 |
* @param en
|
195 |
*/
|
196 |
public void setControlEnabled(boolean en){ |
197 |
this.getJTextField().setEnabled(en);
|
198 |
if (en)
|
199 |
this.getJTextField().setBackground(java.awt.Color.white);
|
200 |
else
|
201 |
this.getJTextField().setBackground(getBackground());
|
202 |
} |
203 |
|
204 |
/**
|
205 |
* Devuelve el valor del campo de texto.
|
206 |
* @return
|
207 |
*/
|
208 |
public String getValue(){ |
209 |
return this.getJTextField().getText(); |
210 |
} |
211 |
|
212 |
/**
|
213 |
* Asigna el valor al campo de texto.
|
214 |
* @return
|
215 |
*/
|
216 |
public void setValue(String value){ |
217 |
this.getJTextField().setText(value);
|
218 |
} |
219 |
|
220 |
|
221 |
/*********************************************************/
|
222 |
/*********************LISTENERS***************************/
|
223 |
/*********************************************************/
|
224 |
|
225 |
|
226 |
public void focusGained(FocusEvent e) { |
227 |
if (e.getSource() == this.getJTextField()){ |
228 |
if(!validateNumericFormat(this.getJTextField().getText())) |
229 |
this.getJTextField().setText(""); |
230 |
} |
231 |
|
232 |
} |
233 |
|
234 |
public void focusLost(FocusEvent e) { |
235 |
//if(e.getSource() == this.getJTextField())
|
236 |
//checkTextValue();
|
237 |
|
238 |
} |
239 |
|
240 |
|
241 |
|
242 |
public void keyPressed(KeyEvent e) { |
243 |
|
244 |
} |
245 |
|
246 |
|
247 |
public void keyReleased(KeyEvent e) { |
248 |
String cad = this.getJTextField().getText(); |
249 |
|
250 |
if (e.getSource()==this.getJTextField()){ |
251 |
if (e.getKeyCode() == 10){ |
252 |
if(!validateNumericFormat(cad))
|
253 |
this.getJTextField().setText(""); |
254 |
return;
|
255 |
} |
256 |
if (!caracter){
|
257 |
if (checkTextField(e.getKeyCode()))
|
258 |
focusText = cad; |
259 |
else
|
260 |
this.getJTextField().setText(focusText);
|
261 |
} |
262 |
} |
263 |
} |
264 |
|
265 |
public void keyTyped(KeyEvent e) { |
266 |
|
267 |
} |
268 |
|
269 |
|
270 |
/**
|
271 |
* Checkea un textField para que contenga caracteres validos. Consideramos validos
|
272 |
* los caracteres del 0 al 9, el punto, DEL, Supr, flechas y Enter teniendo en cuenta que el
|
273 |
* c?digo ascii del teclado numerico es distinto.
|
274 |
*/
|
275 |
public boolean checkTextField(int code){ |
276 |
|
277 |
//Si el caracter es erroneo pasamos de el y reponemos el texto que habia
|
278 |
if( (code >= 0 && code <= 7) || |
279 |
code == 9 ||
|
280 |
(code >= 11 && code <= 36) || |
281 |
(code >= 41 && code <= 45) || |
282 |
code == 47 ||
|
283 |
(code >= 58 && code <= 95) || |
284 |
(code >= 106 && code <= 109) || |
285 |
(code >= 111 && code <= 126) || |
286 |
code >= 128){
|
287 |
return false; |
288 |
} |
289 |
|
290 |
return true; |
291 |
} |
292 |
|
293 |
/**
|
294 |
* Validaci?n de que el par?metro tenga formato numerico
|
295 |
* @param t Valor del campo
|
296 |
* @return true si es valido y false si no lo es
|
297 |
*/
|
298 |
private boolean validateNumericFormat(String field){ |
299 |
try{
|
300 |
Double.valueOf(field).doubleValue();
|
301 |
}catch(NumberFormatException exc){ |
302 |
return false; |
303 |
} |
304 |
return true; |
305 |
} |
306 |
|
307 |
|
308 |
} |