Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools / src / com / iver / cit / gvsig / rasterTools / saveRaster / pruebas / RGBInputPanel.java @ 4244

History | View | Annotate | Download (9.62 KB)

1
package com.iver.cit.gvsig.rasterTools.saveRaster.pruebas;
2

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

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

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

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

    
43
        /**
44
         * This is the default constructor
45
         */
46
        public RGBInputPanel() {
47
                super();
48
                initialize();
49
        }
50

    
51
        /**
52
         * This method initializes this
53
         * 
54
         * @return void
55
         */
56
        private void initialize() {
57
                GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
58
                gridBagConstraints2.gridx = 0;
59
                gridBagConstraints2.gridy = 2;
60
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
61
                gridBagConstraints1.gridx = 0;
62
                gridBagConstraints1.gridy = 1;
63
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
64
                gridBagConstraints.gridx = 0;
65
                gridBagConstraints.gridy = 0;
66
                this.setLayout(new GridBagLayout());
67
                this.setSize(77, 60);
68
                this.setPreferredSize(new java.awt.Dimension(77,60));
69
                this.add(getPRed(), gridBagConstraints);
70
                this.add(getPGreen(), gridBagConstraints1);
71
                this.add(getPBlue(), gridBagConstraints2);
72
                this.getTBlue().addFocusListener(this);
73
                this.getTGreen().addFocusListener(this);
74
                this.getTRed().addFocusListener(this);
75
        }
76

    
77
        /**
78
         * This method initializes jPanel        
79
         *         
80
         * @return javax.swing.JPanel        
81
         */
82
        private JPanel getPRed() {
83
                if (pRed == null) {
84
                        FlowLayout flowLayout = new FlowLayout();
85
                        flowLayout.setHgap(0);
86
                        flowLayout.setVgap(0);
87
                        lRed = new JLabel();
88
                        lRed.setText("R :");
89
                        pRed = new JPanel();
90
                        pRed.setLayout(flowLayout);
91
                        pRed.setPreferredSize(new java.awt.Dimension(76,20));
92
                        pRed.add(lRed, null);
93
                        pRed.add(getTRed(), null);
94
                }
95
                return pRed;
96
        }
97

    
98
        /**
99
         * This method initializes jPanel1        
100
         *         
101
         * @return javax.swing.JPanel        
102
         */
103
        private JPanel getPGreen() {
104
                if (pGreen == null) {
105
                        FlowLayout flowLayout1 = new FlowLayout();
106
                        flowLayout1.setHgap(0);
107
                        flowLayout1.setVgap(0);
108
                        lGreen = new JLabel();
109
                        lGreen.setText("G :");
110
                        pGreen = new JPanel();
111
                        pGreen.setLayout(flowLayout1);
112
                        pGreen.setPreferredSize(new java.awt.Dimension(77,20));
113
                        pGreen.add(lGreen, null);
114
                        pGreen.add(getTGreen(), null);
115
                }
116
                return pGreen;
117
        }
118

    
119
        /**
120
         * This method initializes jPanel2        
121
         *         
122
         * @return javax.swing.JPanel        
123
         */
124
        private JPanel getPBlue() {
125
                if (pBlue == null) {
126
                        FlowLayout flowLayout2 = new FlowLayout();
127
                        flowLayout2.setHgap(0);
128
                        flowLayout2.setVgap(0);
129
                        lBlue = new JLabel();
130
                        lBlue.setText("B :");
131
                        pBlue = new JPanel();
132
                        pBlue.setLayout(flowLayout2);
133
                        pBlue.setPreferredSize(new java.awt.Dimension(76,20));
134
                        pBlue.add(lBlue, null);
135
                        pBlue.add(getTBlue(), null);
136
                }
137
                return pBlue;
138
        }
139

    
140
        /**
141
         * This method initializes jTextField        
142
         *         
143
         * @return javax.swing.JTextField        
144
         */
145
        public JTextField getTRed() {
146
                if (tRed == null) {
147
                        tRed = new JTextField();
148
                        tRed.setPreferredSize(new java.awt.Dimension(60,19));
149
                }
150
                return tRed;
151
        }
152

    
153
        /**
154
         * This method initializes jTextField1        
155
         *         
156
         * @return javax.swing.JTextField        
157
         */
158
        public JTextField getTGreen() {
159
                if (tGreen == null) {
160
                        tGreen = new JTextField();
161
                        tGreen.setPreferredSize(new java.awt.Dimension(60,19));
162
                }
163
                return tGreen;
164
        }
165

    
166
        /**
167
         * This method initializes jTextField2        
168
         *         
169
         * @return javax.swing.JTextField        
170
         */
171
        public JTextField getTBlue() {
172
                if (tBlue == null) {
173
                        tBlue = new JTextField();
174
                        tBlue.setPreferredSize(new java.awt.Dimension(60,19));
175
                }
176
                return tBlue;
177
        }
178

    
179
        public void focusGained(FocusEvent e) {
180
                // TODO Auto-generated method stub
181
                
182
        }
183

    
184
        /**
185
     * Obtiene el rango de valores a partir de la cadena de
186
     * texto introducida por el usuario. Sirve para parsear 
187
     * el contenido de los campos de texto que contienen listas
188
     * de valores para RGB.
189
     * @param values
190
     */
191
    private int[][] getTransparencyValues(String values)
192
                                   throws IOException {
193
        int[][] rangeTransparency = null;
194

    
195
        for (int i = 0; i < values.length(); i++) {
196
            char c = values.charAt(i);
197

    
198
            //Control de caracter valido
199
            if ((c != ':') && (c != ',')) {
200
                try {
201
                    Integer.parseInt(String.valueOf(c));
202
                } catch (Exception exp) {
203
                    System.err.println("Caracteres incorrectos en la cadena:" +
204
                                       c);
205
                    throw new IOException("Caracteres incorrectos en la cadena.");
206
                }
207
            }
208

    
209
            //Control de comienzo por un simbolo
210
            if (values.startsWith(",") || values.startsWith(":") ||
211
                    values.endsWith(",") || values.endsWith(":")) {
212
                System.err.println("La cadena empieza o acaba con simbolos incorrectos");
213
                throw new IOException();
214
            }
215

    
216
            //Control de signos consecutivos
217
            if (i < (values.length() - 1)) {
218
                char cmas = values.charAt(i + 1);
219

    
220
                if (((c == ',') || (c == ':')) &&
221
                        ((cmas == ',') || (cmas == ':'))) {
222
                    System.err.println("Signos consecutivos");
223
                    throw new IOException();
224
                }
225
            }
226

    
227
            //Control de dos : seguidos con un n?mero en el medio
228
            if ((i < (values.length() - 3)) && (c == ':')) {
229
                int n = i + 1;
230

    
231
                while ((n < (values.length() - 1)) &&
232
                           (values.charAt(n) != ',') &&
233
                           (values.charAt(n) != ':'))
234
                    n++;
235

    
236
                char signoSgte = values.charAt(n);
237

    
238
                if (signoSgte == ':') {
239
                    System.err.println("Dos separadores de rango consecutivos");
240
                    throw new IOException();
241
                }
242
            }
243
        }
244

    
245
        //Obtenemos los valores de los intervalos
246
        if (!values.equals("")) {
247
            String[] t1 = null;
248
            t1 = values.split("\\,");
249
            rangeTransparency = new int[t1.length][2];
250

    
251
            for (int i = 0; i < t1.length; i++) {
252
                if (t1[i].indexOf(":") == -1) {
253
                    rangeTransparency[i][0] = rangeTransparency[i][1] = Integer.parseInt(t1[i]);
254
                } else {
255
                    String[] t2 = null;
256
                    t2 = t1[i].split("\\:");
257

    
258
                    if (Integer.parseInt(t2[1]) > Integer.parseInt(t2[0])) {
259
                        rangeTransparency[i][0] = Integer.parseInt(t2[0]);
260
                        rangeTransparency[i][1] = Integer.parseInt(t2[1]);
261
                    } else {
262
                        rangeTransparency[i][0] = Integer.parseInt(t2[1]);
263
                        rangeTransparency[i][1] = Integer.parseInt(t2[0]);
264
                    }
265
                }
266

    
267
                if ((rangeTransparency[i][0] < 0) ||
268
                        (rangeTransparency[i][0] > 255) ||
269
                        (rangeTransparency[i][1] < 0) ||
270
                        (rangeTransparency[i][1] > 255)) {
271
                    System.err.println("Valores fuera de rango (0-255)");
272
                    throw new IOException();
273
                }
274
            }
275

    
276
            return rangeTransparency;
277

    
278
            //for(int i=0;i<rangeTransparency.length;i++)
279
            //        System.out.println("("+rangeTransparency[i][0]+":"+rangeTransparency[i][1]+")");
280
        }
281

    
282
        return null;
283
    }
284
    
285
        public void focusLost(FocusEvent e) {
286
                if (!getTRed().getText().equals("")) {
287
            try {
288
                rangeRed = getTransparencyValues(getTRed().getText());
289
            } catch (IOException exc) {
290
                getTRed().setText("");
291
            }
292
        }
293

    
294
        if (!getTGreen().getText().equals("")) {
295
            try {
296
                rangeGreen = getTransparencyValues(getTGreen().getText());
297
            } catch (IOException exc) {
298
                getTGreen().setText("");
299
            }
300
        }
301

    
302
        if (!getTBlue().getText().equals("")) {
303
            try {
304
                rangeBlue = getTransparencyValues(getTBlue().getText());
305
            } catch (IOException exc) {
306
                getTBlue().setText("");
307
            }
308
        }
309
        }
310

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

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

    
327
        /**
328
         * Obtiene la lista de valores del campo Red.
329
         * @return array multidimensional con los intervalos
330
         */
331
        public int[][] getRangeRed() {
332
                return rangeRed;
333
        }
334

    
335
}