Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / properties / panels / TranspByPixelRGBInputPanel.java @ 12091

History | View | Annotate | Download (8.32 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 = true;
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
        }
59

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
269
        public void actionValueChanged(DoubleSliderEvent e) {
270
                updateColors();
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
}