Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / ui / raster / RGBInputPanel.java @ 8026

History | View | Annotate | Download (9.83 KB)

1
package org.cresques.ui.raster;
2

    
3
import java.awt.Color;
4
import java.awt.FlowLayout;
5
import java.awt.GridBagConstraints;
6
import java.awt.GridBagLayout;
7
import java.awt.event.FocusEvent;
8
import java.awt.event.FocusListener;
9
import java.io.IOException;
10

    
11
import javax.swing.JLabel;
12
import javax.swing.JPanel;
13
import javax.swing.JTextField;
14

    
15
/**
16
 * Panel con los 3 campos para introducir listas de valores RGB. Este
17
 * gestiona los eventos de FocusListener para validar que los valores 
18
 * que se introducen son correctos. Si el TextField pierde el foco y
19
 * tiene un valor incorrecto resetea el contenido.
20
 * 
21
 * Los m?todos publicos getRangeXXX devuelven el intervalo de valores
22
 * en forma de array multidimensional de 2xN elementos. Cada pareja de 
23
 * valores es un rango de color en ese valor de pixel. Un rango de valores
24
 * podria ser por ejemplo {{1,3},{15,30},{200, 255}}
25
 *  
26
 * @author Nacho Brodin (brodin_ign@gva.es)
27
 *
28
 */
29
public class RGBInputPanel extends JPanel implements FocusListener{
30

    
31
        private JPanel                         pRed = null;
32
        private JPanel                        pGreen = null;
33
        private JPanel                         pBlue = null;
34
        private JLabel                        lRed = null;
35
        private JLabel                         lGreen = null;
36
        private JLabel                         lBlue = null;
37
        private JTextFieldExt        tRed = null;
38
        private JTextFieldExt        tGreen = null;
39
        private JTextFieldExt        tBlue = null;
40
        private int[]                         rangeRed = null;
41
    private int[]                         rangeGreen = null;
42
    private int[]                         rangeBlue = null;
43
    private boolean                        validValue = false;
44

    
45
    /**
46
     * Componente JTextField que se pone con el color de fondo cuando
47
     * est? desactivado y blanco cuando est? activo.
48
     * @author Nacho Brodin (brodin_ign@gva.es)
49
     */
50
    public class JTextFieldExt extends JTextField {
51
        final private static long serialVersionUID = -3370601314380922368L;
52
        private Color color = null;
53

    
54
        public JTextFieldExt(Color color) {
55
            this.color = color;
56
        }
57

    
58
        public JTextFieldExt() {
59
            super();
60
        }
61

    
62
        public void setEnabled(boolean enabled) {
63
            super.setEnabled(enabled);
64

    
65
            if (!enabled) {
66
                if (color == null) {
67
                    this.setBackground(new Color(210, 210, 210));
68
                } else {
69
                    this.setBackground(color);
70
                }
71
            } else {
72
                this.setBackground(new Color(255, 255, 255));
73
            }
74
        }
75
    }
76
    
77
        /**
78
         * This is the default constructor
79
         */
80
        public RGBInputPanel() {
81
                super();
82
                initialize();
83
        }
84

    
85
        /**
86
         * This method initializes this
87
         * 
88
         * @return void
89
         */
90
        private void initialize() {
91
                GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
92
                gridBagConstraints2.gridx = 0;
93
                gridBagConstraints2.gridy = 2;
94
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
95
                gridBagConstraints1.gridx = 0;
96
                gridBagConstraints1.gridy = 1;
97
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
98
                gridBagConstraints.gridx = 0;
99
                gridBagConstraints.gridy = 0;
100
                this.setLayout(new GridBagLayout());
101
                this.setSize(100, 75);
102
                this.setPreferredSize(new java.awt.Dimension(77,60));
103
                this.add(getPRed(), gridBagConstraints);
104
                this.add(getPGreen(), gridBagConstraints1);
105
                this.add(getPBlue(), gridBagConstraints2);
106
                this.getTBlue().addFocusListener(this);
107
                this.getTGreen().addFocusListener(this);
108
                this.getTRed().addFocusListener(this);
109
        }
110

    
111
        /**
112
         * This method initializes jPanel        
113
         *         
114
         * @return javax.swing.JPanel        
115
         */
116
        private JPanel getPRed() {
117
                if (pRed == null) {
118
                        FlowLayout flowLayout = new FlowLayout();
119
                        flowLayout.setHgap(0);
120
                        flowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
121
                        flowLayout.setVgap(0);
122
                        lRed = new JLabel();
123
                        lRed.setText("R :");
124
                        pRed = new JPanel();
125
                        pRed.setLayout(flowLayout);
126
                        pRed.setPreferredSize(new java.awt.Dimension(100,25));
127
                        pRed.add(lRed, null);
128
                        pRed.add(getTRed(), null);
129
                }
130
                return pRed;
131
        }
132

    
133
        /**
134
         * This method initializes jPanel1        
135
         *         
136
         * @return javax.swing.JPanel        
137
         */
138
        private JPanel getPGreen() {
139
                if (pGreen == null) {
140
                        FlowLayout flowLayout1 = new FlowLayout();
141
                        flowLayout1.setHgap(0);
142
                        flowLayout1.setAlignment(java.awt.FlowLayout.RIGHT);
143
                        flowLayout1.setVgap(0);
144
                        lGreen = new JLabel();
145
                        lGreen.setText("G :");
146
                        pGreen = new JPanel();
147
                        pGreen.setLayout(flowLayout1);
148
                        pGreen.setPreferredSize(new java.awt.Dimension(100,25));
149
                        pGreen.add(lGreen, null);
150
                        pGreen.add(getTGreen(), null);
151
                }
152
                return pGreen;
153
        }
154

    
155
        /**
156
         * This method initializes jPanel2        
157
         *         
158
         * @return javax.swing.JPanel        
159
         */
160
        private JPanel getPBlue() {
161
                if (pBlue == null) {
162
                        FlowLayout flowLayout2 = new FlowLayout();
163
                        flowLayout2.setHgap(0);
164
                        flowLayout2.setAlignment(java.awt.FlowLayout.RIGHT);
165
                        flowLayout2.setVgap(0);
166
                        lBlue = new JLabel();
167
                        lBlue.setText("B :");
168
                        pBlue = new JPanel();
169
                        pBlue.setLayout(flowLayout2);
170
                        pBlue.setPreferredSize(new java.awt.Dimension(100,25));
171
                        pBlue.add(lBlue, null);
172
                        pBlue.add(getTBlue(), null);
173
                }
174
                return pBlue;
175
        }
176

    
177
        /**
178
         * This method initializes jTextField        
179
         *         
180
         * @return javax.swing.JTextField        
181
         */
182
        public JTextField getTRed() {
183
                if (tRed == null) {
184
                        tRed = new JTextFieldExt();
185
                        tRed.setPreferredSize(new java.awt.Dimension(60,20));
186
                }
187
                return tRed;
188
        }
189

    
190
        /**
191
         * This method initializes jTextField1        
192
         *         
193
         * @return javax.swing.JTextField        
194
         */
195
        public JTextField getTGreen() {
196
                if (tGreen == null) {
197
                        tGreen = new JTextFieldExt();
198
                        tGreen.setPreferredSize(new java.awt.Dimension(60,20));
199
                }
200
                return tGreen;
201
        }
202

    
203
        /**
204
         * This method initializes jTextField2        
205
         *         
206
         * @return javax.swing.JTextField        
207
         */
208
        public JTextField getTBlue() {
209
                if (tBlue == null) {
210
                        tBlue = new JTextFieldExt();
211
                        tBlue.setPreferredSize(new java.awt.Dimension(60,20));
212
                }
213
                return tBlue;
214
        }
215

    
216
        public void focusGained(FocusEvent e) {
217
                // TODO Auto-generated method stub
218
                
219
        }
220

    
221
        /**
222
     * Obtiene el rango de valores a partir de la cadena de texto introducida por el usuario. 
223
     * Sirve para parsear el contenido de los campos de texto que contienen listas
224
     * de valores para RGB.
225
     * @param values
226
     */
227
    public static int[] stringToInterval(String values) throws IOException {
228
        int[] rangeTransparency = new int[2];
229

    
230
        boolean dots = false;
231
        for (int i = 0; i < values.length(); i++){
232
                char c = values.charAt(i);
233
                if(c != ':' && (c < 48 || c > 57))
234
                        throw new IOException("Caracteres incorrectos en la cadena.");
235
                if(c == ':' && dots)
236
                        throw new IOException("Se repite el caracter :.");
237
                if(c == ':' && !dots)
238
                        dots = true;
239
                if(c == ':' && (i == 0 || i == (values.length() - 1)))
240
                        throw new IOException("No puede comenzar ni acabar por :.");                
241
        }
242
        
243
        if(dots){
244
                String[] interv = values.split(":");
245
                int val1 = Integer.parseInt(interv[0]);
246
                int val2 = Integer.parseInt(interv[1]);
247
                if(val1 > 255 || val2 > 255)
248
                        throw new IOException("Valor de pixel erroneo.");
249
                else{
250
                        if(val1 < val2){
251
                                rangeTransparency[0] = val1;
252
                                rangeTransparency[1] = val2;
253
                        }else{
254
                                rangeTransparency[0] = val2;
255
                                rangeTransparency[1] = val1;
256
                        }
257
                        return rangeTransparency;
258
                }
259
        }else{
260
                rangeTransparency[0] = rangeTransparency[1] = Integer.parseInt(values);
261
                if(rangeTransparency[0] > 255)
262
                        throw new IOException("Valor de pixel erroneo.");
263
                return rangeTransparency;
264
        }
265

    
266
    }
267
    
268
        public void focusLost(FocusEvent e) {
269
                validValue = true;
270
                rangeRed = null;
271
                rangeGreen = null;
272
                rangeBlue = null;
273
                if (!getTRed().getText().equals("")) {
274
            try {
275
                rangeRed = RGBInputPanel.stringToInterval(getTRed().getText());
276
            } catch (IOException exc) {
277
                    exc.printStackTrace();
278
                getTRed().setText("");
279
                validValue = false;
280
            }
281
        }
282

    
283
        if (!getTGreen().getText().equals("")) {
284
            try {
285
                rangeGreen = RGBInputPanel.stringToInterval(getTGreen().getText());
286
            } catch (IOException exc) {
287
                    exc.printStackTrace();
288
                getTGreen().setText("");
289
                validValue = false;
290
            }
291
        }
292

    
293
        if (!getTBlue().getText().equals("")) {
294
            try {
295
                rangeBlue = RGBInputPanel.stringToInterval(getTBlue().getText());
296
            } catch (IOException exc) {
297
                    exc.printStackTrace();
298
                getTBlue().setText("");
299
                validValue = false;
300
            }
301
        }
302
        }
303

    
304
        /**
305
         * Obtiene la lista de valores del campo Blue.
306
         * @return array multidimensional con los intervalos
307
         */
308
        public int[] getRangeBlue() {
309
                return rangeBlue;
310
        }
311

    
312
        /**
313
         * Obtiene la lista de valores del campo Green.
314
         * @return array multidimensional con los intervalos
315
         */
316
        public int[] getRangeGreen() {
317
                return rangeGreen;
318
        }
319

    
320
        /**
321
         * Obtiene la lista de valores del campo Red.
322
         * @return array multidimensional con los intervalos
323
         */
324
        public int[] getRangeRed() {
325
                return rangeRed;
326
        }
327

    
328
        /**
329
         * Obtiene true si los valores de RGB que han introducido son 
330
         * validos y false si no lo son.
331
         * @return
332
         */
333
        public boolean isValidValue() {
334
                return validValue;
335
        }
336

    
337
        /**
338
         * Activa o desactiva el control
339
         * @param enable True activa el control y false lo desactiva
340
         */
341
        public void setControlEnabled(boolean enabled){
342
                getTRed().setEnabled(enabled);
343
                getTGreen().setEnabled(enabled);
344
                getTBlue().setEnabled(enabled);
345
        }
346

    
347
        /**
348
         * Asigna el n?mero de bandas activas 
349
         * @param n N?mero de bandas
350
         */
351
        public void setActiveBands(int n){
352
                this.getTRed().setEnabled(true);
353
                switch(n){
354
                case 1:        this.getTGreen().setEnabled(false);
355
                                this.getTBlue().setEnabled(false);
356
                                break;
357
                case 2:        this.getTGreen().setEnabled(true);
358
                                this.getTBlue().setEnabled(false);
359
                                break;
360
                case 3:        this.getTGreen().setEnabled(true);
361
                                this.getTBlue().setEnabled(true);
362
                                break;
363
                }
364
        }
365
}