root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / properties / panels / TranspByPixelRGBInputPanel.java @ 10740
History | View | Annotate | Download (11 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 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.rastertools.properties.panels; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.awt.FlowLayout; |
23 |
import java.awt.GridBagConstraints; |
24 |
import java.awt.GridBagLayout; |
25 |
import java.awt.event.FocusEvent; |
26 |
import java.awt.event.FocusListener; |
27 |
import java.io.IOException; |
28 |
|
29 |
import javax.swing.JLabel; |
30 |
import javax.swing.JPanel; |
31 |
import javax.swing.JTextField; |
32 |
|
33 |
/**
|
34 |
* Panel con los 3 campos para introducir listas de valores RGB. Este
|
35 |
* gestiona los eventos de FocusListener para validar que los valores
|
36 |
* que se introducen son correctos. Si el TextField pierde el foco y
|
37 |
* tiene un valor incorrecto resetea el contenido.
|
38 |
*
|
39 |
* Los m?todos publicos getRangeXXX devuelven el intervalo de valores
|
40 |
* en forma de array multidimensional de 2xN elementos. Cada pareja de
|
41 |
* valores es un rango de color en ese valor de pixel. Un rango de valores
|
42 |
* podria ser por ejemplo {{1,3},{15,30},{200, 255}}
|
43 |
*
|
44 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
45 |
*
|
46 |
*/
|
47 |
public class TranspByPixelRGBInputPanel extends JPanel implements FocusListener{ |
48 |
final private static long serialVersionUID = 0; |
49 |
private JPanel pRed = null; |
50 |
private JPanel pGreen = null; |
51 |
private JPanel pBlue = null; |
52 |
private JLabel lRed = null; |
53 |
private JLabel lGreen = null; |
54 |
private JLabel lBlue = null; |
55 |
private JTextFieldExt tRed = null; |
56 |
private JTextFieldExt tGreen = null; |
57 |
private JTextFieldExt tBlue = null; |
58 |
private int[] rangeRed = null; |
59 |
private int[] rangeGreen = null; |
60 |
private int[] rangeBlue = null; |
61 |
private boolean validValue = false; |
62 |
|
63 |
/**
|
64 |
* Componente JTextField que se pone con el color de fondo cuando
|
65 |
* est? desactivado y blanco cuando est? activo.
|
66 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
67 |
*/
|
68 |
public class JTextFieldExt extends JTextField { |
69 |
final private static long serialVersionUID = -3370601314380922368L; |
70 |
private Color color = null; |
71 |
|
72 |
public JTextFieldExt(Color color) { |
73 |
this.color = color;
|
74 |
} |
75 |
|
76 |
public JTextFieldExt() {
|
77 |
super();
|
78 |
} |
79 |
|
80 |
public void setEnabled(boolean enabled) { |
81 |
super.setEnabled(enabled);
|
82 |
|
83 |
if (!enabled) {
|
84 |
if (color == null) { |
85 |
this.setBackground(new Color(210, 210, 210)); |
86 |
} else {
|
87 |
this.setBackground(color);
|
88 |
} |
89 |
} else {
|
90 |
this.setBackground(new Color(255, 255, 255)); |
91 |
} |
92 |
} |
93 |
} |
94 |
|
95 |
/**
|
96 |
* This is the default constructor
|
97 |
*/
|
98 |
public TranspByPixelRGBInputPanel() {
|
99 |
super();
|
100 |
initialize(); |
101 |
} |
102 |
|
103 |
/**
|
104 |
* This method initializes this
|
105 |
*
|
106 |
* @return void
|
107 |
*/
|
108 |
private void initialize() { |
109 |
GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
110 |
gridBagConstraints2.gridx = 0;
|
111 |
gridBagConstraints2.gridy = 2;
|
112 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
113 |
gridBagConstraints1.gridx = 0;
|
114 |
gridBagConstraints1.gridy = 1;
|
115 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
116 |
gridBagConstraints.gridx = 0;
|
117 |
gridBagConstraints.gridy = 0;
|
118 |
this.setLayout(new GridBagLayout()); |
119 |
this.setSize(100, 75); |
120 |
this.setPreferredSize(new java.awt.Dimension(77,60)); |
121 |
this.add(getPRed(), gridBagConstraints);
|
122 |
this.add(getPGreen(), gridBagConstraints1);
|
123 |
this.add(getPBlue(), gridBagConstraints2);
|
124 |
this.getTBlue().addFocusListener(this); |
125 |
this.getTGreen().addFocusListener(this); |
126 |
this.getTRed().addFocusListener(this); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* This method initializes jPanel
|
131 |
*
|
132 |
* @return javax.swing.JPanel
|
133 |
*/
|
134 |
private JPanel getPRed() { |
135 |
if (pRed == null) { |
136 |
FlowLayout flowLayout = new FlowLayout(); |
137 |
flowLayout.setHgap(0);
|
138 |
flowLayout.setAlignment(java.awt.FlowLayout.RIGHT); |
139 |
flowLayout.setVgap(0);
|
140 |
lRed = new JLabel(); |
141 |
lRed.setText("R :");
|
142 |
pRed = new JPanel(); |
143 |
pRed.setLayout(flowLayout); |
144 |
pRed.setPreferredSize(new java.awt.Dimension(100,25)); |
145 |
pRed.add(lRed, null);
|
146 |
pRed.add(getTRed(), null);
|
147 |
} |
148 |
return pRed;
|
149 |
} |
150 |
|
151 |
/**
|
152 |
* This method initializes jPanel1
|
153 |
*
|
154 |
* @return javax.swing.JPanel
|
155 |
*/
|
156 |
private JPanel getPGreen() { |
157 |
if (pGreen == null) { |
158 |
FlowLayout flowLayout1 = new FlowLayout(); |
159 |
flowLayout1.setHgap(0);
|
160 |
flowLayout1.setAlignment(java.awt.FlowLayout.RIGHT); |
161 |
flowLayout1.setVgap(0);
|
162 |
lGreen = new JLabel(); |
163 |
lGreen.setText("G :");
|
164 |
pGreen = new JPanel(); |
165 |
pGreen.setLayout(flowLayout1); |
166 |
pGreen.setPreferredSize(new java.awt.Dimension(100,25)); |
167 |
pGreen.add(lGreen, null);
|
168 |
pGreen.add(getTGreen(), null);
|
169 |
} |
170 |
return pGreen;
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* This method initializes jPanel2
|
175 |
*
|
176 |
* @return javax.swing.JPanel
|
177 |
*/
|
178 |
private JPanel getPBlue() { |
179 |
if (pBlue == null) { |
180 |
FlowLayout flowLayout2 = new FlowLayout(); |
181 |
flowLayout2.setHgap(0);
|
182 |
flowLayout2.setAlignment(java.awt.FlowLayout.RIGHT); |
183 |
flowLayout2.setVgap(0);
|
184 |
lBlue = new JLabel(); |
185 |
lBlue.setText("B :");
|
186 |
pBlue = new JPanel(); |
187 |
pBlue.setLayout(flowLayout2); |
188 |
pBlue.setPreferredSize(new java.awt.Dimension(100,25)); |
189 |
pBlue.add(lBlue, null);
|
190 |
pBlue.add(getTBlue(), null);
|
191 |
} |
192 |
return pBlue;
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* This method initializes jTextField
|
197 |
*
|
198 |
* @return javax.swing.JTextField
|
199 |
*/
|
200 |
public JTextField getTRed() { |
201 |
if (tRed == null) { |
202 |
tRed = new JTextFieldExt();
|
203 |
tRed.setPreferredSize(new java.awt.Dimension(60,20)); |
204 |
} |
205 |
return tRed;
|
206 |
} |
207 |
|
208 |
/**
|
209 |
* This method initializes jTextField1
|
210 |
*
|
211 |
* @return javax.swing.JTextField
|
212 |
*/
|
213 |
public JTextField getTGreen() { |
214 |
if (tGreen == null) { |
215 |
tGreen = new JTextFieldExt();
|
216 |
tGreen.setPreferredSize(new java.awt.Dimension(60,20)); |
217 |
} |
218 |
return tGreen;
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* This method initializes jTextField2
|
223 |
*
|
224 |
* @return javax.swing.JTextField
|
225 |
*/
|
226 |
public JTextField getTBlue() { |
227 |
if (tBlue == null) { |
228 |
tBlue = new JTextFieldExt();
|
229 |
tBlue.setPreferredSize(new java.awt.Dimension(60,20)); |
230 |
} |
231 |
return tBlue;
|
232 |
} |
233 |
|
234 |
public void focusGained(FocusEvent e) { |
235 |
// TODO Auto-generated method stub
|
236 |
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* Obtiene el rango de valores a partir de la cadena de texto introducida por el usuario.
|
241 |
* Sirve para parsear el contenido de los campos de texto que contienen listas
|
242 |
* de valores para RGB.
|
243 |
* @param values
|
244 |
*/
|
245 |
public static int[] stringToInterval(String values) throws IOException { |
246 |
int[] rangeTransparency = new int[2]; |
247 |
|
248 |
boolean dots = false; |
249 |
for (int i = 0; i < values.length(); i++){ |
250 |
char c = values.charAt(i);
|
251 |
if(c != ':' && (c < 48 || c > 57)) |
252 |
throw new IOException("Caracteres incorrectos en la cadena."); |
253 |
if(c == ':' && dots) |
254 |
throw new IOException("Se repite el caracter :."); |
255 |
if(c == ':' && !dots) |
256 |
dots = true;
|
257 |
if(c == ':' && (i == 0 || i == (values.length() - 1))) |
258 |
throw new IOException("No puede comenzar ni acabar por :."); |
259 |
} |
260 |
|
261 |
if(dots){
|
262 |
String[] interv = values.split(":"); |
263 |
int val1 = Integer.parseInt(interv[0]); |
264 |
int val2 = Integer.parseInt(interv[1]); |
265 |
if(val1 > 255 || val2 > 255) |
266 |
throw new IOException("Valor de pixel erroneo."); |
267 |
else{
|
268 |
if(val1 < val2){
|
269 |
rangeTransparency[0] = val1;
|
270 |
rangeTransparency[1] = val2;
|
271 |
}else{
|
272 |
rangeTransparency[0] = val2;
|
273 |
rangeTransparency[1] = val1;
|
274 |
} |
275 |
return rangeTransparency;
|
276 |
} |
277 |
}else{
|
278 |
rangeTransparency[0] = rangeTransparency[1] = Integer.parseInt(values); |
279 |
if(rangeTransparency[0] > 255) |
280 |
throw new IOException("Valor de pixel erroneo."); |
281 |
return rangeTransparency;
|
282 |
} |
283 |
|
284 |
} |
285 |
|
286 |
public void focusLost(FocusEvent e) { |
287 |
validValue = true;
|
288 |
rangeRed = null;
|
289 |
rangeGreen = null;
|
290 |
rangeBlue = null;
|
291 |
if (!getTRed().getText().equals("")) { |
292 |
try {
|
293 |
rangeRed = TranspByPixelRGBInputPanel.stringToInterval(getTRed().getText()); |
294 |
} catch (IOException exc) { |
295 |
exc.printStackTrace(); |
296 |
getTRed().setText("");
|
297 |
validValue = false;
|
298 |
} |
299 |
} |
300 |
|
301 |
if (!getTGreen().getText().equals("")) { |
302 |
try {
|
303 |
rangeGreen = TranspByPixelRGBInputPanel.stringToInterval(getTGreen().getText()); |
304 |
} catch (IOException exc) { |
305 |
exc.printStackTrace(); |
306 |
getTGreen().setText("");
|
307 |
validValue = false;
|
308 |
} |
309 |
} |
310 |
|
311 |
if (!getTBlue().getText().equals("")) { |
312 |
try {
|
313 |
rangeBlue = TranspByPixelRGBInputPanel.stringToInterval(getTBlue().getText()); |
314 |
} catch (IOException exc) { |
315 |
exc.printStackTrace(); |
316 |
getTBlue().setText("");
|
317 |
validValue = false;
|
318 |
} |
319 |
} |
320 |
} |
321 |
|
322 |
/**
|
323 |
* Obtiene la lista de valores del campo Blue.
|
324 |
* @return array multidimensional con los intervalos
|
325 |
*/
|
326 |
public int[] getRangeBlue() { |
327 |
return rangeBlue;
|
328 |
} |
329 |
|
330 |
/**
|
331 |
* Obtiene la lista de valores del campo Green.
|
332 |
* @return array multidimensional con los intervalos
|
333 |
*/
|
334 |
public int[] getRangeGreen() { |
335 |
return rangeGreen;
|
336 |
} |
337 |
|
338 |
/**
|
339 |
* Obtiene la lista de valores del campo Red.
|
340 |
* @return array multidimensional con los intervalos
|
341 |
*/
|
342 |
public int[] getRangeRed() { |
343 |
return rangeRed;
|
344 |
} |
345 |
|
346 |
/**
|
347 |
* Obtiene true si los valores de RGB que han introducido son
|
348 |
* validos y false si no lo son.
|
349 |
* @return
|
350 |
*/
|
351 |
public boolean isValidValue() { |
352 |
return validValue;
|
353 |
} |
354 |
|
355 |
/**
|
356 |
* Activa o desactiva el control
|
357 |
* @param enable True activa el control y false lo desactiva
|
358 |
*/
|
359 |
public void setControlEnabled(boolean enabled){ |
360 |
getTRed().setEnabled(enabled); |
361 |
getTGreen().setEnabled(enabled); |
362 |
getTBlue().setEnabled(enabled); |
363 |
} |
364 |
|
365 |
/**
|
366 |
* Asigna el n?mero de bandas activas
|
367 |
* @param n N?mero de bandas
|
368 |
*/
|
369 |
public void setActiveBands(int n){ |
370 |
this.getTRed().setEnabled(true); |
371 |
switch(n){
|
372 |
case 1: this.getTGreen().setEnabled(false); |
373 |
this.getTBlue().setEnabled(false); |
374 |
break;
|
375 |
case 2: this.getTGreen().setEnabled(true); |
376 |
this.getTBlue().setEnabled(false); |
377 |
break;
|
378 |
case 3: this.getTGreen().setEnabled(true); |
379 |
this.getTBlue().setEnabled(true); |
380 |
break;
|
381 |
} |
382 |
} |
383 |
|
384 |
/**
|
385 |
* Limpia el valor de las cajas de texto R, G y B
|
386 |
*/
|
387 |
public void clear(){ |
388 |
getTRed().setText("");
|
389 |
getTGreen().setText("");
|
390 |
getTBlue().setText("");
|
391 |
} |
392 |
} |