Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1003 / libraries / libCq_CMS_praster / src / org / cresques / ui / filter / RasterTransparencyPanel.java @ 12271

History | View | Annotate | Download (13.6 KB)

1
/*
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.FocusEvent;
32
import java.awt.event.FocusListener;
33

    
34
import javax.swing.JCheckBox;
35
import javax.swing.JPanel;
36
import javax.swing.JSlider;
37
import javax.swing.JTextField;
38
import javax.swing.event.ChangeEvent;
39
import javax.swing.event.ChangeListener;
40

    
41
import org.cresques.ui.raster.IResize;
42
import org.cresques.ui.raster.RGBInputPanel;
43

    
44

    
45
/**
46
 * Dialogo para asignar la transparencia por pixel y global al raster.
47
 * @author Nacho Brodin (brodin_ign@gva.es)
48
 */
49
public class RasterTransparencyPanel extends JPanel implements ActionListener, FocusListener, ChangeListener, IResize{
50
    final private static long serialVersionUID = -3370601314380922368L;
51

    
52
    /**
53
     * Variables para la asignaci?n de tama?o de los componentes del panel.
54
     */
55
    private int                                                wComp = 445, hComp = 239;
56
    private int                                                wPixel = wComp - 15, hPixel = hComp - 59;
57
    private int                                                wList = wPixel - 20, hList = hPixel - 60;
58
    private int                                                wGeneral = wPixel, hGeneral = 55;
59
    private int                                                wSlider = wGeneral - 160;
60
    
61
    
62
    /**
63
     * Nombre del panel
64
     */
65
    private String                                                 nom = "Transparencia";
66
    FilterRasterDialogPanel                         parent = null;
67

    
68
    /**
69
     * N?mero de bandas del raster
70
     */
71
    public int                                                         nBands = 3;
72
    private JPanel                                                 jPanel1 = null;
73
    private JCheckBox                                         cbTransparencia = null;
74

    
75
    /**
76
     * Variable p?blica para la traducci?n
77
     */
78
    public JPanel                                                 pGeneralTrans = null;
79
    private JCheckBox                                         cbOpacidad = null;
80
    private JSlider                                         slOpacidad = null;
81
    private JTextField                                         tOpacidad = null;
82
        private TransparencyByPixelPanel         pTranspByPixel = null;
83
        private JPanel                                                 pTransparencyByPixel = null;
84
                
85
    /**
86
     * Constructor. Asigna la variable panel que contiene este
87
     * @param parent FilterRasterDialogPanel
88
     */
89
    public RasterTransparencyPanel(FilterRasterDialogPanel parent) {
90
        this.parent = parent;
91
        initialize();
92
    }
93

    
94
    /**
95
     * This method initializes this
96
     *
97
     * @return void
98
     */
99
    private void initialize() {
100
        GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
101
        gridBagConstraints4.insets = new java.awt.Insets(0,0,0,0);
102
        gridBagConstraints4.gridx = 0;
103
        gridBagConstraints4.gridy = 1;
104
        gridBagConstraints4.gridwidth = 1;
105
        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
106
        gridBagConstraints1.gridx = 0;
107
        gridBagConstraints1.gridy = 0;
108
        this.setLayout(new GridBagLayout());
109
        this.setBounds(0, 0, wComp, hComp);
110
        this.setPreferredSize(new java.awt.Dimension(wComp, hComp));
111
        this.add(getPTransparencyByPixel(), gridBagConstraints1);
112
        this.add(getOpacityPanel(), gridBagConstraints4);
113
        initControls();
114
        //this.setComponentSize(wComp, hComp);
115
    }
116

    
117
    /**
118
     * Obtiene el nombre del panel
119
     * @return Cadena con el nombre del panel
120
     */
121
    public String getName(){
122
            return this.nom;
123
    }
124
    
125
    /**
126
     * Asigna el n?mero de bandas de la imagen
127
     * @param nBands
128
     */
129
    public void setBands(int nBands) {
130
        this.nBands = nBands;
131
    }
132

    
133
    /**
134
     * Inicializa controles a sus valores por defecto
135
     */
136
    public void initControls() {
137
        this.getOpacityText().setText("100");
138
        this.setActiveOpacityControl(false);
139
        this.setActiveTransparencyControl(false);
140
    }
141

    
142
    /**
143
     * This method initializes jPanel1
144
     *
145
     * @return javax.swing.JPanel
146
     */
147
    private JPanel getJPanel1() {
148
        if (jPanel1 == null) {
149
            jPanel1 = new JPanel();
150
            jPanel1.setLayout(new BorderLayout());
151
            jPanel1.setPreferredSize(new java.awt.Dimension(415,25));
152
            jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
153
            jPanel1.add(getTransparencyCheck(), java.awt.BorderLayout.NORTH);
154
        }
155

    
156
        return jPanel1;
157
    }
158

    
159
    /**
160
     * This method initializes jCheckBox
161
     *
162
     * @return javax.swing.JCheckBox
163
     */
164
    public JCheckBox getTransparencyCheck() {
165
        if (cbTransparencia == null) {
166
            cbTransparencia = new JCheckBox();
167
            cbTransparencia.setText("Activar");
168
            cbTransparencia.addActionListener(this);
169
        }
170

    
171
        return cbTransparencia;
172
    }
173

    
174
    /**
175
     * This method initializes jPanel4
176
     *
177
     * @return javax.swing.JPanel
178
     */
179
    public JPanel getOpacityPanel() {
180
        if (pGeneralTrans == null) {
181
                pGeneralTrans = new JPanel();
182
                pGeneralTrans.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opacidad", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
183
                pGeneralTrans.setPreferredSize(new java.awt.Dimension(wGeneral,hGeneral));
184
                pGeneralTrans.add(getOpacityCheck(), null);
185
                pGeneralTrans.add(getOpacitySlider(), null);
186
                pGeneralTrans.add(getOpacityText(), null);
187
        }
188

    
189
        return pGeneralTrans;
190
    }
191

    
192
    /**
193
     * This method initializes jCheckBox1
194
     *
195
     * @return javax.swing.JCheckBox
196
     */
197
    public JCheckBox getOpacityCheck() {
198
        if (cbOpacidad == null) {
199
            cbOpacidad = new JCheckBox();
200
            cbOpacidad.setText("Activar");
201
            cbOpacidad.addActionListener(this);
202
            cbOpacidad.addFocusListener(this);
203
        }
204

    
205
        return cbOpacidad;
206
    }
207

    
208
    /**
209
     * This method initializes jSlider
210
     *
211
     * @return javax.swing.JSlider
212
     */
213
    public JSlider getOpacitySlider() {
214
        if (slOpacidad == null) {
215
            slOpacidad = new JSlider();
216
            slOpacidad.setPreferredSize(new java.awt.Dimension(wSlider, 16));
217
            slOpacidad.addChangeListener(this);
218
        }
219

    
220
        return slOpacidad;
221
    }
222

    
223
    /**
224
     * This method initializes jTextField3
225
     *
226
     * @return javax.swing.JTextField
227
     */
228
    public JTextField getOpacityText() {
229
        if (tOpacidad == null) {
230
            tOpacidad = new JTextField();
231
            tOpacidad.setPreferredSize(new java.awt.Dimension(30, 19));
232
            tOpacidad.addActionListener(this);
233
        }
234

    
235
        return tOpacidad;
236
    }
237

    
238
    /**
239
     * Activa/Desactiva los controles de opacidad
240
     * @param active
241
     */
242
    public void setActiveOpacityControl(boolean active) {
243
        this.getOpacityCheck().setSelected(active);
244
        this.getOpacitySlider().setEnabled(active);
245
        this.getOpacityText().setEnabled(active);
246
    }
247

    
248
    /**
249
     * Activa/Desactiva los controles de transparencia
250
     * @param active
251
     */
252
    public void setActiveTransparencyControl(boolean active) {
253
        this.getTransparencyCheck().setSelected(active);
254
        RGBInputPanel rgbPanel = this.getPTranspByPixel().getPRGBInput();
255
        rgbPanel.getTRed().setEnabled(active);
256
        
257
        if (parent.nbands == 2) {
258
                rgbPanel.getTGreen().setEnabled(active);
259
                rgbPanel.getTBlue().setEnabled(false);
260
        }
261

    
262
        if (parent.nbands == 3) {
263
                rgbPanel.getTGreen().setEnabled(active);
264
                rgbPanel.getTBlue().setEnabled(active);
265
        }
266
    }
267

    
268
    /**
269
     * Asigna el valor de opacidad a los controles de la ventana
270
     * para que cuando esta se abra tenga los valores seleccionados en la
271
     * imagen.
272
     * @param alpha
273
     */
274
    public void setOpacity(int alpha) {
275
        int opacityPercent = (int) ((alpha * 100) / 255);
276
        this.getOpacityText().setText(String.valueOf(opacityPercent));
277
        this.getOpacitySlider().setValue(opacityPercent);
278
        this.setActiveOpacityControl(true);
279

    
280
        if (opacityPercent == 100) {
281
            this.setActiveOpacityControl(false);
282
        }
283
    }
284

    
285
        /**
286
         * This method initializes jPanel2        
287
         *         
288
         * @return javax.swing.JPanel        
289
         */
290
        public TransparencyByPixelPanel getPTranspByPixel() {
291
                if (pTranspByPixel == null) {
292
                        pTranspByPixel = new TransparencyByPixelPanel();
293
                        pTranspByPixel.setPreferredSize(new java.awt.Dimension(wList,hList));
294
                        pTranspByPixel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
295
                        pTranspByPixel.setControlEnabled(false);
296
                }
297
                return pTranspByPixel;
298
        }
299

    
300
        /**
301
         * This method initializes jPanel2        
302
         *         
303
         * @return javax.swing.JPanel        
304
         */
305
        public JPanel getPTransparencyByPixel() {
306
                if (pTransparencyByPixel == null) {
307
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
308
                        gridBagConstraints.gridx = 0;
309
                        gridBagConstraints.gridy = 1;
310
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
311
                        gridBagConstraints2.anchor = GridBagConstraints.WEST;
312
                        gridBagConstraints2.insets = new java.awt.Insets(0,0,0,0);
313
                        gridBagConstraints2.gridheight = 1;
314
                        gridBagConstraints2.gridwidth = 1;
315
                        gridBagConstraints2.gridx = 0;
316
                        gridBagConstraints2.gridy = 0;
317
                        gridBagConstraints2.ipady = 0;
318
                        gridBagConstraints2.fill = GridBagConstraints.NONE;
319
                        pTransparencyByPixel = new JPanel();
320
                        pTransparencyByPixel.setLayout(new GridBagLayout());
321
                        pTransparencyByPixel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Transparencia por pixel", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
322
                        pTransparencyByPixel.setPreferredSize(new java.awt.Dimension(wPixel,hPixel));
323
                        pTransparencyByPixel.add(getJPanel1(), gridBagConstraints2);
324
                        pTransparencyByPixel.add(getPTranspByPixel(), gridBagConstraints);
325
                }
326
                return pTransparencyByPixel;
327
        }
328

    
329
        /**
330
         * Ajusta el tama?o de todos los componentes a partir del tama?o
331
         * introducido para el panel.
332
         * @param w
333
         * @param h
334
         */
335
        public void setComponentSize(int w, int h){
336
                wComp = w; hComp = h;
337
                wPixel = wComp - 15; hPixel = hComp - 59;
338
                wList = wPixel - 20; hList = hPixel - 60;
339
                wGeneral = wComp - 15; hGeneral = 55; 
340
                wSlider = wGeneral - 160;
341
                    
342
                this.setPreferredSize(new java.awt.Dimension(wComp, hComp));
343
                pTransparencyByPixel.setPreferredSize(new java.awt.Dimension(wPixel,hPixel));
344
                pGeneralTrans.setPreferredSize(new java.awt.Dimension(wGeneral,hGeneral));
345
                pTranspByPixel.setPreferredSize(new java.awt.Dimension(wList,hList));
346
                pTranspByPixel.setComponentSize(wList, hList);
347
                slOpacidad.setPreferredSize(new java.awt.Dimension(wSlider, 16));
348
        
349
                
350
        }
351

    
352
        /**
353
     * Eventos sobre TextField y CheckBox. Controla eventos de checkbox de opacidad, transparencia,
354
     * recorte de colas y los textfield de opacidad, valores de transparencia por banda y
355
     * porcentaje de recorte.
356
     */
357
        public void actionPerformed(ActionEvent e) {
358
                //Evento sobre el checkbox de opacidad
359
                if (e.getSource().equals(getOpacityCheck())) {
360
                   //Check de opacidad activado -> Activar controles de opacidad
361
                   if (getOpacityCheck().isSelected()) {
362
                       getOpacitySlider().setEnabled(true);
363
                       getOpacityText().setEnabled(true);
364
                   } else {
365
                       getOpacitySlider().setEnabled(false);
366
                       getOpacityText().setEnabled(false);
367
                   }
368
                }
369
                 
370
                //Evento sobre el checkbox de transparencia
371
            if (e.getSource().equals(getTransparencyCheck())) {
372
                   //Check de opacidad activado -> Activar controles de opacidad
373
                   if (getTransparencyCheck().isSelected()) {
374
                           getPTranspByPixel().setControlEnabled(true);
375
                           getPTranspByPixel().getPRGBInput().setActiveBands(nBands);
376
                   }else
377
                           getPTranspByPixel().setControlEnabled(false);
378
            }
379
            
380
            //Evento sobre el textfield de opacidad
381
        if (e.getSource().equals(getOpacityText()))
382
            checkOpacityText();
383
        }
384
        
385
    public void focusGained(FocusEvent e) {
386
        //pTrans.updateTextBox();
387
    }
388

    
389
    public void focusLost(FocusEvent e) {
390
        checkOpacityText();
391
    }
392
    
393
    /*
394
    *
395
    */
396
   public void stateChanged(ChangeEvent e) {
397
       //Ponemos el valor del texto de la opacidad de pendiendo de la posici?n del Slider
398
       if (e.getSource().equals(getOpacitySlider())) {
399
           getOpacityText().setText(
400
                           String.valueOf(getOpacitySlider().getValue()));
401
       }        
402
   }
403
   
404
        /**
405
     * Controla que si el formato introducido en el textfield
406
     * de opacidad es numerico se actualiza la posici?n del
407
     * slider.
408
     */
409
    private void checkOpacityText() {
410
        String op = getOpacityText().getText();
411
        int value = 0;
412

    
413
        try {
414
            if (!op.equals("")) {
415
                value = Integer.parseInt(op);
416
            }
417

    
418
            getOpacitySlider().setValue(value);
419
        } catch (NumberFormatException exc) {
420
            System.err.println("Formato no numerico");
421
            getOpacityText().setText("100");
422
            getOpacitySlider().setValue(100);
423
        }
424
    }
425
    
426
} //  @jve:decl-index=0:visual-constraint="36,15"