Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_898 / libraries / libCq CMS for java.old / src / org / cresques / ui / filter / RasterTransparencyPanel.java @ 10513

History | View | Annotate | Download (14.1 KB)

1 9056 nacho
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.ui.filter;
25
26
import java.awt.BorderLayout;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.awt.event.ComponentEvent;
32
import java.awt.event.ComponentListener;
33
import java.awt.event.FocusEvent;
34
import java.awt.event.FocusListener;
35
36
import javax.swing.JCheckBox;
37
import javax.swing.JPanel;
38
import javax.swing.JSlider;
39
import javax.swing.JTextField;
40
import javax.swing.event.ChangeEvent;
41
import javax.swing.event.ChangeListener;
42
43
import org.cresques.ui.raster.IResize;
44
import org.cresques.ui.raster.RGBInputPanel;
45
46
import java.awt.FlowLayout;
47
import java.awt.Insets;
48
49
50
/**
51
 * Dialogo para asignar la transparencia por pixel y global al raster.
52
 * @author Nacho Brodin (brodin_ign@gva.es)
53
 */
54
public class RasterTransparencyPanel extends JPanel implements ActionListener, FocusListener, ChangeListener, IResize{
55
    final private static long serialVersionUID = -3370601314380922368L;
56
57
    /**
58
     * Variables para la asignaci?n de tama?o de los componentes del panel.
59
     */
60
    private int                                                wComp = 445, hComp = 239;
61
    private int                                                wPixel = wComp - 15, hPixel = hComp - 59;
62
    private int                                                wList = wPixel - 20, hList = hPixel - 60;
63
    private int                                                wGeneral = wPixel, hGeneral = 55;
64
    private int                                                wSlider = wGeneral - 160;
65
66
67
    /**
68
     * Nombre del panel
69
     */
70
    private String                                                 nom = "Transparencia";
71
    FilterRasterDialogPanel                         parent = null;
72
73
    /**
74
     * N?mero de bandas del raster
75
     */
76
    public int                                                         nBands = 3;
77
    private JPanel                                                 jPanel1 = null;
78
    private JCheckBox                                         cbTransparencia = null;
79
80
    /**
81
     * Variable p?blica para la traducci?n
82
     */
83
    public JPanel                                                 pGeneralTrans = null;
84
    private JCheckBox                                         cbOpacidad = null;
85
    private JSlider                                         slOpacidad = null;
86
    private JTextField                                         tOpacidad = null;
87
        private TransparencyByPixelPanel         pTranspByPixel = null;
88
        private JPanel                                                 pTransparencyByPixel = null;
89
90
    /**
91
     * Constructor. Asigna la variable panel que contiene este
92
     * @param parent FilterRasterDialogPanel
93
     */
94
    public RasterTransparencyPanel(FilterRasterDialogPanel parent) {
95
        this.parent = parent;
96
        initialize();
97
    }
98
99
    /**
100
     * This method initializes this
101
     *
102
     * @return void
103
     */
104
    private void initialize() {
105
        GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
106
        gridBagConstraints4.insets = new java.awt.Insets(0,0,0,0);
107
        gridBagConstraints4.gridx = 0;
108
        gridBagConstraints4.gridy = 1;
109
        gridBagConstraints4.gridwidth = 1;
110
        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
111
        gridBagConstraints1.gridx = 0;
112
        gridBagConstraints1.gridy = 0;
113
        this.setLayout(new GridBagLayout());
114
        this.setBounds(0, 0, wComp, hComp);
115
        this.setPreferredSize(new java.awt.Dimension(wComp, hComp));
116
        this.add(getPTransparencyByPixel(), gridBagConstraints1);
117
        this.add(getOpacityPanel(), gridBagConstraints4);
118
        initControls();
119
        //this.setComponentSize(wComp, hComp);
120
    }
121
122
    /**
123
     * Obtiene el nombre del panel
124
     * @return Cadena con el nombre del panel
125
     */
126
    public String getName(){
127
            return this.nom;
128
    }
129
130
    /**
131
     * Asigna el n?mero de bandas de la imagen
132
     * @param nBands
133
     */
134
    public void setBands(int nBands) {
135
        this.nBands = nBands;
136
    }
137
138
    /**
139
     * Inicializa controles a sus valores por defecto
140
     */
141
    public void initControls() {
142
        this.getOpacityText().setText("100");
143
        this.setActiveOpacityControl(false);
144
        this.setActiveTransparencyControl(false);
145
    }
146
147
    /**
148
     * This method initializes jPanel1
149
     *
150
     * @return javax.swing.JPanel
151
     */
152
    private JPanel getJPanel1() {
153
        if (jPanel1 == null) {
154
            jPanel1 = new JPanel();
155
            jPanel1.setLayout(new BorderLayout());
156
            jPanel1.setPreferredSize(new java.awt.Dimension(415,25));
157
            jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
158
            jPanel1.add(getTransparencyCheck(), java.awt.BorderLayout.NORTH);
159
        }
160
161
        return jPanel1;
162
    }
163
164
    /**
165
     * This method initializes jCheckBox
166
     *
167
     * @return javax.swing.JCheckBox
168
     */
169
    public JCheckBox getTransparencyCheck() {
170
        if (cbTransparencia == null) {
171
            cbTransparencia = new JCheckBox();
172
            cbTransparencia.setText("Activar");
173
            cbTransparencia.addActionListener(this);
174
        }
175
176
        return cbTransparencia;
177
    }
178
179
    /**
180
     * This method initializes jPanel4
181
     *
182
     * @return javax.swing.JPanel
183
     */
184
    public JPanel getOpacityPanel() {
185
        if (pGeneralTrans == null) {
186
                pGeneralTrans = new JPanel();
187
                pGeneralTrans.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opacidad", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
188
                pGeneralTrans.setPreferredSize(new java.awt.Dimension(wGeneral,hGeneral));
189
                pGeneralTrans.add(getOpacityCheck(), null);
190
                pGeneralTrans.add(getOpacitySlider(), null);
191
                pGeneralTrans.add(getOpacityText(), null);
192
        }
193
194
        return pGeneralTrans;
195
    }
196
197
    /**
198
     * This method initializes jCheckBox1
199
     *
200
     * @return javax.swing.JCheckBox
201
     */
202
    public JCheckBox getOpacityCheck() {
203
        if (cbOpacidad == null) {
204
            cbOpacidad = new JCheckBox();
205
            cbOpacidad.setText("Activar");
206
            cbOpacidad.addActionListener(this);
207
            cbOpacidad.addFocusListener(this);
208
        }
209
210
        return cbOpacidad;
211
    }
212
213
    /**
214
     * This method initializes jSlider
215
     *
216
     * @return javax.swing.JSlider
217
     */
218
    public JSlider getOpacitySlider() {
219
        if (slOpacidad == null) {
220
            slOpacidad = new JSlider();
221
            slOpacidad.setPreferredSize(new java.awt.Dimension(wSlider, 16));
222
            slOpacidad.addChangeListener(this);
223
        }
224
225
        return slOpacidad;
226
    }
227
228
    /**
229
     * This method initializes jTextField3
230
     *
231
     * @return javax.swing.JTextField
232
     */
233
    public JTextField getOpacityText() {
234
        if (tOpacidad == null) {
235
            tOpacidad = new JTextField();
236
            tOpacidad.setPreferredSize(new java.awt.Dimension(30, 19));
237
            tOpacidad.addActionListener(this);
238 9279 nacho
            tOpacidad.addFocusListener(this);
239 9056 nacho
        }
240
241
        return tOpacidad;
242
    }
243
244
    /**
245
     * Activa/Desactiva los controles de opacidad
246
     * @param active
247
     */
248
    public void setActiveOpacityControl(boolean active) {
249
        this.getOpacityCheck().setSelected(active);
250
        this.getOpacitySlider().setEnabled(active);
251
        this.getOpacityText().setEnabled(active);
252
    }
253
254
    /**
255
     * Activa/Desactiva los controles de transparencia
256
     * @param active
257
     */
258
    public void setActiveTransparencyControl(boolean active) {
259
        this.getTransparencyCheck().setSelected(active);
260
        RGBInputPanel rgbPanel = this.getPTranspByPixel().getPRGBInput();
261
        rgbPanel.getTRed().setEnabled(active);
262
263
        if (parent.nbands == 2) {
264
                rgbPanel.getTGreen().setEnabled(active);
265
                rgbPanel.getTBlue().setEnabled(false);
266
        }
267
268
        if (parent.nbands == 3) {
269
                rgbPanel.getTGreen().setEnabled(active);
270
                rgbPanel.getTBlue().setEnabled(active);
271
        }
272
    }
273
274
    /**
275
     * Asigna el valor de opacidad a los controles de la ventana
276
     * para que cuando esta se abra tenga los valores seleccionados en la
277
     * imagen.
278
     * @param alpha
279
     */
280
    public void setOpacity(int alpha) {
281
        int opacityPercent = (int) ((alpha * 100) / 255);
282
        this.getOpacityText().setText(String.valueOf(opacityPercent));
283
        this.getOpacitySlider().setValue(opacityPercent);
284
        this.setActiveOpacityControl(true);
285
286
        if (opacityPercent == 100) {
287
            this.setActiveOpacityControl(false);
288
        }
289
    }
290
291
        /**
292
         * This method initializes jPanel2
293
         *
294
         * @return javax.swing.JPanel
295
         */
296
        public TransparencyByPixelPanel getPTranspByPixel() {
297
                if (pTranspByPixel == null) {
298
                        pTranspByPixel = new TransparencyByPixelPanel();
299
                        pTranspByPixel.setPreferredSize(new java.awt.Dimension(wList,hList));
300
                        pTranspByPixel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
301
                        pTranspByPixel.setControlEnabled(false);
302
                }
303
                return pTranspByPixel;
304
        }
305
306
        /**
307
         * This method initializes jPanel2
308
         *
309
         * @return javax.swing.JPanel
310
         */
311
        public JPanel getPTransparencyByPixel() {
312
                if (pTransparencyByPixel == null) {
313
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
314
                        gridBagConstraints.gridx = 0;
315
                        gridBagConstraints.gridy = 1;
316
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
317
                        gridBagConstraints2.anchor = GridBagConstraints.WEST;
318
                        gridBagConstraints2.insets = new java.awt.Insets(0,0,0,0);
319
                        gridBagConstraints2.gridheight = 1;
320
                        gridBagConstraints2.gridwidth = 1;
321
                        gridBagConstraints2.gridx = 0;
322
                        gridBagConstraints2.gridy = 0;
323
                        gridBagConstraints2.ipady = 0;
324
                        gridBagConstraints2.fill = GridBagConstraints.NONE;
325
                        pTransparencyByPixel = new JPanel();
326
                        pTransparencyByPixel.setLayout(new GridBagLayout());
327
                        pTransparencyByPixel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Transparencia por pixel", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
328
                        pTransparencyByPixel.setPreferredSize(new java.awt.Dimension(wPixel,hPixel));
329
                        pTransparencyByPixel.add(getJPanel1(), gridBagConstraints2);
330
                        pTransparencyByPixel.add(getPTranspByPixel(), gridBagConstraints);
331
                }
332
                return pTransparencyByPixel;
333
        }
334
335
        /**
336
         * Ajusta el tama?o de todos los componentes a partir del tama?o
337
         * introducido para el panel.
338
         * @param w
339
         * @param h
340
         */
341
        public void setComponentSize(int w, int h){
342
                wComp = w; hComp = h;
343
                wPixel = wComp - 15; hPixel = hComp - 59;
344
                wList = wPixel - 20; hList = hPixel - 60;
345
                wGeneral = wComp - 15; hGeneral = 55;
346
                wSlider = wGeneral - 160;
347
348
                this.setPreferredSize(new java.awt.Dimension(wComp, hComp));
349
                pTransparencyByPixel.setPreferredSize(new java.awt.Dimension(wPixel,hPixel));
350
                pGeneralTrans.setPreferredSize(new java.awt.Dimension(wGeneral,hGeneral));
351
                pTranspByPixel.setPreferredSize(new java.awt.Dimension(wList,hList));
352
                pTranspByPixel.setComponentSize(wList, hList);
353
                slOpacidad.setPreferredSize(new java.awt.Dimension(wSlider, 16));
354
355
356
        }
357
358
        /**
359
     * Eventos sobre TextField y CheckBox. Controla eventos de checkbox de opacidad, transparencia,
360
     * recorte de colas y los textfield de opacidad, valores de transparencia por banda y
361
     * porcentaje de recorte.
362
     */
363
        public void actionPerformed(ActionEvent e) {
364
                //Evento sobre el checkbox de opacidad
365
                if (e.getSource().equals(getOpacityCheck())) {
366
                   //Check de opacidad activado -> Activar controles de opacidad
367
                   if (getOpacityCheck().isSelected()) {
368
                       getOpacitySlider().setEnabled(true);
369
                       getOpacityText().setEnabled(true);
370
                   } else {
371
                       getOpacitySlider().setEnabled(false);
372
                       getOpacityText().setEnabled(false);
373
                   }
374
                }
375
376
                //Evento sobre el checkbox de transparencia
377
            if (e.getSource().equals(getTransparencyCheck())) {
378
                   //Check de opacidad activado -> Activar controles de opacidad
379
                   if (getTransparencyCheck().isSelected()) {
380
                           getPTranspByPixel().setControlEnabled(true);
381
                           getPTranspByPixel().getPRGBInput().setActiveBands(nBands);
382
                   }else
383
                           getPTranspByPixel().setControlEnabled(false);
384
            }
385
386
            //Evento sobre el textfield de opacidad
387
        if (e.getSource().equals(getOpacityText()))
388
            checkOpacityText();
389
        }
390
391
    public void focusGained(FocusEvent e) {
392
        //pTrans.updateTextBox();
393
    }
394
395
    public void focusLost(FocusEvent e) {
396
        checkOpacityText();
397
    }
398
399
    /*
400
    *
401
    */
402
   public void stateChanged(ChangeEvent e) {
403
       //Ponemos el valor del texto de la opacidad de pendiendo de la posici?n del Slider
404
       if (e.getSource().equals(getOpacitySlider())) {
405
           getOpacityText().setText(
406
                           String.valueOf(getOpacitySlider().getValue()));
407
       }
408
   }
409
410
        /**
411
     * Controla que si el formato introducido en el textfield
412
     * de opacidad es numerico se actualiza la posici?n del
413 9279 nacho
     * slider. Si es numerico pero excede del rango 0-100 pone
414
     * el valor a 0 o a 100 dependiendo de si excede por arriba o por
415
     * abajo
416 9056 nacho
     */
417
    private void checkOpacityText() {
418
        String op = getOpacityText().getText();
419
        int value = 0;
420
421
        try {
422 9279 nacho
                if(op == null)
423
                        throw new NumberFormatException();
424
425
            value = Integer.parseInt(op);
426
427
            if(value > 100)
428
                    getOpacityText().setText("100");
429
            else if(value < 0)
430
                    getOpacityText().setText("0");
431
432 9056 nacho
            getOpacitySlider().setValue(value);
433
        } catch (NumberFormatException exc) {
434
            System.err.println("Formato no numerico");
435
            getOpacityText().setText("100");
436
            getOpacitySlider().setValue(100);
437
        }
438
    }
439
440
} //  @jve:decl-index=0:visual-constraint="36,15"