Statistics
| Revision:

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

History | View | Annotate | Download (7.14 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

    
23
import javax.swing.JPanel;
24

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

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

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

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

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

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

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

    
125
        /**
126
         * Obtiene la lista de valores del campo Blue.
127
         *
128
         * @return array multidimensional con los intervalos
129
         */
130
        public int[] getRangeBlue() {
131
                return rangeBlue;
132
        }
133

    
134
        /**
135
         * Obtiene la lista de valores del campo Green.
136
         * @return array multidimensional con los intervalos
137
         */
138
        public int[] getRangeGreen() {
139
                return rangeGreen;
140
        }
141

    
142
        /**
143
         * Obtiene la lista de valores del campo Red.
144
         * @return array multidimensional con los intervalos
145
         */
146
        public int[] getRangeRed() {
147
                return rangeRed;
148
        }
149

    
150
        /**
151
         * Activa o desactiva el control
152
         * @param enable True activa el control y false lo desactiva
153
         */
154
        public void setControlEnabled(boolean enabled){
155
                getTRed().setEnabled(enabled);
156
                getTGreen().setEnabled(enabled);
157
                getTBlue().setEnabled(enabled);
158
                getTAlpha().setEnabled(enabled);
159
        }
160

    
161
        /**
162
         * Asigna el n?mero de bandas activas
163
         * @param n N?mero de bandas
164
         */
165
        public void setActiveBands(int n){
166
                this.getTRed().setEnabled(true);
167
                switch (n) {
168
                        case 1:
169
                                this.getTGreen().setEnabled(false);
170
                                this.getTBlue().setEnabled(false);
171
                                break;
172
                        case 2:
173
                                this.getTGreen().setEnabled(true);
174
                                this.getTBlue().setEnabled(false);
175
                                break;
176
                        case 3:
177
                                this.getTGreen().setEnabled(true);
178
                                this.getTBlue().setEnabled(true);
179
                                break;
180
                }
181
        }
182

    
183
        /**
184
         * Limpia el valor de las cajas de texto R, G y B
185
         */
186
        public void clear(){
187
                getTRed().setValue(0);
188
                getTGreen().setValue(0);
189
                getTBlue().setValue(0);
190
                getTRed().setControlEnabled(true);
191
                getTGreen().setControlEnabled(true);
192
                getTBlue().setControlEnabled(true);
193
        }
194

    
195
        /**
196
         * Actualiza los colores de los componentes de seleccion de color, el ultimo
197
         * parametro true o false es para que haga o no un refresco del componente, lo
198
         * hacemos solo una vez por componente para que consuma menos CPU
199
         */
200
        public void updateColors() {
201
                int r = tRed.getValue();
202
                int g = tGreen.getValue();
203
                int b = tBlue.getValue();
204

    
205
                if (!tRed.isChecked()) r = 0;
206
                if (!tGreen.isChecked()) g = 0;
207
                if (!tBlue.isChecked()) b = 0;
208

    
209
                tRed.setColor1(new Color(0, g, b), false);
210
                tRed.setColor2(new Color(255, g, b), true);
211
                tGreen.setColor1(new Color(r, 0, b), false);
212
                tGreen.setColor2(new Color(r, 255, b), true);
213
                tBlue.setColor1(new Color(r, g, 0), false);
214
                tBlue.setColor2(new Color(r, g, 255), true);
215
        }
216

    
217
        /**
218
         * En cada cambio hay que validar los datos y refrescarlos, este m?todo se
219
         * encarga de la validaci?n de datos y actualizar los rangos de selecci?n
220
         */
221
        public void validateValues() {
222
                rangeRed = new int[] {0, 255};
223
                rangeGreen = new int[] {0, 255};
224
                rangeBlue = new int[] {0, 255};
225

    
226
                if (getTRed().isChecked())
227
                        rangeRed[0] = rangeRed[1] = getTRed().getValue();
228

    
229
                if (getTGreen().isChecked())
230
                        rangeGreen[0] = rangeGreen[1] = getTGreen().getValue();
231

    
232
                if (getTBlue().isChecked())
233
                        rangeBlue[0] = rangeBlue[1] = getTBlue().getValue();
234
        }
235

    
236
        /*
237
         * (non-Javadoc)
238
         * @see org.gvsig.gui.beans.doubleslider.DoubleSliderListener#actionValueChanged(org.gvsig.gui.beans.doubleslider.DoubleSliderEvent)
239
         */
240
        public void actionValueChanged(DoubleSliderEvent e) {
241
                updateColors();
242
                validateValues();
243
        }
244

    
245
        /*
246
         * (non-Javadoc)
247
         * @see org.gvsig.gui.beans.doubleslider.DoubleSliderListener#actionValueDragged(org.gvsig.gui.beans.doubleslider.DoubleSliderEvent)
248
         */
249
        public void actionValueDragged(DoubleSliderEvent e) {
250
                updateColors();
251
                validateValues();
252
        }
253
}