Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / properties / panels / TranspByPixelRGBInputPanel.java @ 12127

History | View | Annotate | Download (8.47 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.io.IOException;
23

    
24
import javax.swing.JPanel;
25

    
26
import org.gvsig.gui.beans.checkslidertext.CheckColorSliderTextContainer;
27
import org.gvsig.gui.beans.doubleslider.DoubleSliderEvent;
28
import org.gvsig.gui.beans.doubleslider.DoubleSliderListener;
29
/**
30
 * Panel con los 3 campos para introducir listas de valores RGB. Este gestiona
31
 * los eventos de FocusListener para validar que los valores que se introducen
32
 * son correctos. Si el TextField pierde el foco y tiene un valor incorrecto
33
 * resetea el contenido. Los m?todos publicos getRangeXXX devuelven el intervalo
34
 * de valores en forma de array multidimensional de 2xN elementos. Cada pareja
35
 * de valores es un rango de color en ese valor de pixel. Un rango de valores
36
 * podria ser por ejemplo {{1,3},{15,30},{200, 255}}
37
 * 
38
 * @author Nacho Brodin (brodin_ign@gva.es)
39
 */
40
public class TranspByPixelRGBInputPanel extends JPanel implements DoubleSliderListener {
41
        private static final long     serialVersionUID = 5858119425941331458L;
42
        private int[]                 rangeRed   = null;
43
        private int[]                 rangeGreen = null;
44
        private int[]                 rangeBlue  = null;
45
        private boolean               validValue = false;
46
        CheckColorSliderTextContainer tRed       = null;
47
        CheckColorSliderTextContainer tGreen     = null;
48
        CheckColorSliderTextContainer tBlue      = null;
49
        CheckColorSliderTextContainer tAlpha     = null;
50

    
51
        /**
52
         * This is the default constructor
53
         */
54
        public TranspByPixelRGBInputPanel() {
55
                super();
56
                initialize();
57
                updateColors();
58
                validateValues();
59
        }
60

    
61
        /**
62
         * This method initializes this
63
         * 
64
         * @return void
65
         */
66
        private void initialize() {
67
                this.setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));
68
                this.add(getTRed());
69
                this.add(getTGreen());
70
                this.add(getTBlue());
71
                this.add(getTAlpha());
72
                this.getTBlue().addValueChangedListener(this);
73
                this.getTGreen().addValueChangedListener(this);
74
                this.getTRed().addValueChangedListener(this);
75
                this.getTAlpha().addValueChangedListener(this);
76
        }
77

    
78
        /**
79
         * This method initializes jTextField        
80
         *         
81
         * @return javax.swing.JTextField        
82
         */
83
        public CheckColorSliderTextContainer getTRed() {
84
                if (tRed == null) {
85
                        tRed = new CheckColorSliderTextContainer(0, 255, 0, "R", true);
86
                }
87
                return tRed;
88
        }
89

    
90
        /**
91
         * This method initializes jTextField        
92
         *         
93
         * @return javax.swing.JTextField        
94
         */
95
        public CheckColorSliderTextContainer getTAlpha() {
96
                if (tAlpha == null) {
97
                        tAlpha = new CheckColorSliderTextContainer(0, 255, 255, "A", false);
98
                        tAlpha.setColor2(this.getBackground());
99
                }
100
                return tAlpha;
101
        }
102

    
103
        /**
104
         * This method initializes jTextField1        
105
         *         
106
         * @return javax.swing.JTextField        
107
         */
108
        public CheckColorSliderTextContainer getTGreen() {
109
                if (tGreen == null) {
110
                        tGreen = new CheckColorSliderTextContainer(0, 255, 0, "G", true);
111
                }
112
                return tGreen;
113
        }
114

    
115
        /**
116
         * This method initializes jTextField2        
117
         *         
118
         * @return javax.swing.JTextField        
119
         */
120
        public CheckColorSliderTextContainer getTBlue() {
121
                if (tBlue == null) {
122
                        tBlue = new CheckColorSliderTextContainer(0, 255, 0, "B", true);
123
                }
124
                return tBlue;
125
        }
126

    
127
        /**
128
         * Obtiene el rango de valores a partir de la cadena de texto introducida por
129
         * el usuario. Sirve para parsear el contenido de los campos de texto que
130
         * contienen listas de valores para RGB.
131
         * 
132
         * @param values
133
         */
134
        public static int[] stringToInterval(String values) throws IOException {
135
                int[] rangeTransparency = new int[2];
136

    
137
                boolean dots = false;
138
                for (int i = 0; i < values.length(); i++) {
139
                        char c = values.charAt(i);
140
                        if (c != ':' && (c < 48 || c > 57))
141
                                throw new IOException("Caracteres incorrectos en la cadena.");
142
                        if (c == ':' && dots)
143
                                throw new IOException("Se repite el caracter :.");
144
                        if (c == ':' && !dots)
145
                                dots = true;
146
                        if (c == ':' && (i == 0 || i == (values.length() - 1)))
147
                                throw new IOException("No puede comenzar ni acabar por :.");
148
                }
149

    
150
                if (dots) {
151
                        String[] interv = values.split(":");
152
                        int val1 = Integer.parseInt(interv[0]);
153
                        int val2 = Integer.parseInt(interv[1]);
154
                        if (val1 > 255 || val2 > 255)
155
                                throw new IOException("Valor de pixel erroneo.");
156
                        else {
157
                                if (val1 < val2) {
158
                                        rangeTransparency[0] = val1;
159
                                        rangeTransparency[1] = val2;
160
                                } else {
161
                                        rangeTransparency[0] = val2;
162
                                        rangeTransparency[1] = val1;
163
                                }
164
                                return rangeTransparency;
165
                        }
166
                } else {
167
                        rangeTransparency[0] = rangeTransparency[1] = Integer.parseInt(values);
168
                        if (rangeTransparency[0] > 255)
169
                                throw new IOException("Valor de pixel erroneo.");
170
                        return rangeTransparency;
171
                }
172
        }
173

    
174
        /**
175
         * Obtiene la lista de valores del campo Blue.
176
         * 
177
         * @return array multidimensional con los intervalos
178
         */
179
        public int[] getRangeBlue() {
180
                return rangeBlue;
181
        }
182

    
183
        /**
184
         * Obtiene la lista de valores del campo Green.
185
         * @return array multidimensional con los intervalos
186
         */
187
        public int[] getRangeGreen() {
188
                return rangeGreen;
189
        }
190

    
191
        /**
192
         * Obtiene la lista de valores del campo Red.
193
         * @return array multidimensional con los intervalos
194
         */
195
        public int[] getRangeRed() {
196
                return rangeRed;
197
        }
198

    
199
        /**
200
         * Obtiene true si los valores de RGB que han introducido son 
201
         * validos y false si no lo son.
202
         * @return
203
         */
204
        public boolean isValidValue() {
205
                return validValue;
206
        }
207

    
208
        /**
209
         * Activa o desactiva el control
210
         * @param enable True activa el control y false lo desactiva
211
         */
212
        public void setControlEnabled(boolean enabled){
213
                getTRed().setEnabled(enabled);
214
                getTGreen().setEnabled(enabled);
215
                getTBlue().setEnabled(enabled);
216
                getTAlpha().setEnabled(enabled);
217
        }
218

    
219
        /**
220
         * Asigna el n?mero de bandas activas 
221
         * @param n N?mero de bandas
222
         */
223
        public void setActiveBands(int n){
224
                this.getTRed().setEnabled(true);
225
                switch (n) {
226
                        case 1:
227
                                this.getTGreen().setEnabled(false);
228
                                this.getTBlue().setEnabled(false);
229
                                break;
230
                        case 2:
231
                                this.getTGreen().setEnabled(true);
232
                                this.getTBlue().setEnabled(false);
233
                                break;
234
                        case 3:
235
                                this.getTGreen().setEnabled(true);
236
                                this.getTBlue().setEnabled(true);
237
                                break;
238
                }
239
        }
240

    
241
        /**
242
         * Limpia el valor de las cajas de texto R, G y B
243
         */
244
        public void clear(){
245
                getTRed().setValue(0);
246
                getTGreen().setValue(0);
247
                getTBlue().setValue(0);
248
                getTRed().setControlEnabled(true);
249
                getTGreen().setControlEnabled(true);
250
                getTBlue().setControlEnabled(true);
251
        }
252

    
253
        public void updateColors() {
254
                int r = tRed.getValue();
255
                int g = tGreen.getValue();
256
                int b = tBlue.getValue();
257

    
258
                if (!tRed.isChecked()) r = 0;
259
                if (!tGreen.isChecked()) g = 0;
260
                if (!tBlue.isChecked()) b = 0;
261

    
262
                tRed.setColor1(new Color(0, g, b));
263
                tRed.setColor2(new Color(255, g, b));
264
                tGreen.setColor1(new Color(r, 0, b));
265
                tGreen.setColor2(new Color(r, 255, b));
266
                tBlue.setColor1(new Color(r, g, 0));
267
                tBlue.setColor2(new Color(r, g, 255));
268
        }
269

    
270
        public void validateValues() {
271
                validValue = true;
272
                rangeRed = null;
273
                rangeGreen = null;
274
                rangeBlue = null;
275
                if (getTRed().isChecked()) {
276
                        try {
277
                                rangeRed = TranspByPixelRGBInputPanel.stringToInterval(getTRed().getValue() + "");
278
                        } catch (IOException exc) {
279
                                exc.printStackTrace();
280
                                getTRed().setChecked(false);
281
                                validValue = false;
282
                        }
283
                }
284

    
285
                if (getTGreen().isChecked()) {
286
                        try {
287
                                rangeGreen = TranspByPixelRGBInputPanel.stringToInterval(getTGreen().getValue() + "");
288
                        } catch (IOException exc) {
289
                                exc.printStackTrace();
290
                                getTGreen().setChecked(false);
291
                                validValue = false;
292
                        }
293
                }
294

    
295
                if (getTBlue().isChecked()) {
296
                        try {
297
                                rangeBlue = TranspByPixelRGBInputPanel.stringToInterval(getTBlue().getValue() + "");
298
                        } catch (IOException exc) {
299
                                exc.printStackTrace();
300
                                getTBlue().setChecked(false);
301
                                validValue = false;
302
                        }
303
                }
304
        }
305

    
306
        public void actionValueChanged(DoubleSliderEvent e) {
307
                updateColors();
308
                validateValues();
309
        }
310
}