Statistics
| Revision:

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

History | View | Annotate | Download (6.71 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.FlowLayout;
22
import java.awt.event.ActionEvent;
23
import java.awt.event.ActionListener;
24
import java.awt.event.FocusEvent;
25
import java.awt.event.FocusListener;
26

    
27
import javax.swing.BorderFactory;
28
import javax.swing.JCheckBox;
29
import javax.swing.JPanel;
30
import javax.swing.JSlider;
31
import javax.swing.JTextField;
32
import javax.swing.border.TitledBorder;
33
import javax.swing.event.ChangeEvent;
34
import javax.swing.event.ChangeListener;
35

    
36
import com.iver.andami.PluginServices;
37
/**
38
 * Panel con el slider para regular la opacidad de la capa.
39
 *
40
 * @author Nacho Brodin (nachobrodin@gmail.com)
41
 */
42
public class TranspOpacitySliderPanel extends JPanel implements ActionListener, FocusListener, ChangeListener{
43
        final private static long serialVersionUID = 0;
44
        private JCheckBox         cbTransparencia  = null;
45
        private JCheckBox         cbOpacidad       = null;
46
        private JSlider           slOpacidad       = null;
47
        private JTextField        tOpacidad        = null;
48

    
49
        /**
50
         * Constructor.
51
         */
52
        public TranspOpacitySliderPanel() {
53
                initialize();
54
        }
55

    
56
        /**
57
         * This method initializes this
58
         * @return void
59
         */
60
        private void initialize() {
61
                setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "opacidad"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
62
                setLayout(new FlowLayout());
63
                add(getOpacityCheck(), null);
64
                add(getOpacitySlider(), null);
65
                add(getOpacityText(), null);
66
        }
67

    
68
        /**
69
         * Inicializa controles a sus valores por defecto
70
         */
71
        public void initControls() {
72
                this.getOpacityText().setText("100");
73
                this.setActiveOpacityControl(false);
74
        }
75

    
76
        /**
77
         * This method initializes jCheckBox
78
         * @return javax.swing.JCheckBox
79
         */
80
        public JCheckBox getTransparencyCheck() {
81
                if (cbTransparencia == null) {
82
                        cbTransparencia = new JCheckBox();
83
                        cbTransparencia.setText(PluginServices.getText(this, "activar"));
84
                        cbTransparencia.addActionListener(this);
85
                }
86

    
87
                return cbTransparencia;
88
        }
89

    
90
        /**
91
         * This method initializes jCheckBox1
92
         * @return javax.swing.JCheckBox
93
         */
94
        public JCheckBox getOpacityCheck() {
95
                if (cbOpacidad == null) {
96
                        cbOpacidad = new JCheckBox();
97
                        cbOpacidad.setText(PluginServices.getText(this, "activar"));
98
                        cbOpacidad.addActionListener(this);
99
                        cbOpacidad.addFocusListener(this);
100
                }
101

    
102
                return cbOpacidad;
103
        }
104

    
105
        /**
106
         * This method initializes jSlider
107
         * @return javax.swing.JSlider
108
         */
109
        public JSlider getOpacitySlider() {
110
                if (slOpacidad == null) {
111
                        slOpacidad = new JSlider();
112
//                         slOpacidad.setPreferredSize(new java.awt.Dimension(wSlider, 16));
113
                        slOpacidad.addChangeListener(this);
114
                }
115

    
116
                return slOpacidad;
117
        }
118

    
119
        /**
120
         * This method initializes jTextField3
121
         * @return javax.swing.JTextField
122
         */
123
        public JTextField getOpacityText() {
124
                if (tOpacidad == null) {
125
                        tOpacidad = new JTextField();
126
                        tOpacidad.setPreferredSize(new java.awt.Dimension(30, 19));
127
                        tOpacidad.addActionListener(this);
128
                        tOpacidad.addFocusListener(this);
129
                }
130

    
131
                return tOpacidad;
132
        }
133

    
134
        /**
135
         * Activa/Desactiva los controles de opacidad
136
         * @param active
137
         */
138
        public void setActiveOpacityControl(boolean active) {
139
                this.getOpacityCheck().setSelected(active);
140
                this.getOpacitySlider().setEnabled(active);
141
                this.getOpacityText().setEnabled(active);
142
        }
143

    
144
        /**
145
         * Asigna el valor de opacidad a los controles de la ventana para que cuando
146
         * esta se abra tenga los valores seleccionados en la imagen.
147
         * @param alpha
148
         */
149
        public void setOpacity(int alpha) {
150
                int opacityPercent = (int) ((alpha * 100) / 255);
151
                this.getOpacityText().setText(String.valueOf(opacityPercent));
152
                this.getOpacitySlider().setValue(opacityPercent);
153
                this.setActiveOpacityControl(true);
154

    
155
                if (opacityPercent == 100)
156
                        this.setActiveOpacityControl(false);
157
        }
158

    
159
        /**
160
         * Eventos sobre TextField y CheckBox. Controla eventos de checkbox de
161
         * opacidad, transparencia, recorte de colas y los textfield de opacidad,
162
         * valores de transparencia por banda y porcentaje de recorte.
163
         */
164
        public void actionPerformed(ActionEvent e) {
165
                // Evento sobre el checkbox de opacidad
166
                if (e.getSource().equals(getOpacityCheck())) {
167
                        // Check de opacidad activado -> Activar controles de opacidad
168
                        if (getOpacityCheck().isSelected()) {
169
                                getOpacitySlider().setEnabled(true);
170
                                getOpacityText().setEnabled(true);
171
                        } else {
172
                                getOpacitySlider().setEnabled(false);
173
                                getOpacityText().setEnabled(false);
174
                        }
175
                }
176

    
177
                //Evento sobre el textfield de opacidad
178
                if (e.getSource().equals(getOpacityText()))
179
                        checkOpacityText();
180
        }
181

    
182
        /*
183
         * (non-Javadoc)
184
         * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
185
         */
186
        public void focusGained(FocusEvent e) {
187
//        pTrans.updateTextBox();
188
        }
189

    
190
        /*
191
         * (non-Javadoc)
192
         * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
193
         */
194
        public void focusLost(FocusEvent e) {
195
                checkOpacityText();
196
        }
197

    
198
        /*
199
         * (non-Javadoc)
200
         * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
201
         */
202
        public void stateChanged(ChangeEvent e) {
203
                // Ponemos el valor del texto de la opacidad de pendiendo de la posici?n del
204
                // Slider
205
                if (e.getSource().equals(getOpacitySlider())) {
206
                        getOpacityText().setText(String.valueOf(getOpacitySlider().getValue()));
207
                }
208
        }
209

    
210
        /**
211
         * Controla que si el formato introducido en el textfield de opacidad es
212
         * numerico se actualiza la posici?n del slider. Si es numerico pero excede
213
         * del rango 0-100 pone el valor a 0 o a 100 dependiendo de si excede por
214
         * arriba o por abajo
215
         */
216
        private void checkOpacityText() {
217
                String op = getOpacityText().getText();
218
                int value = 0;
219

    
220
                try {
221
                        if (op == null)
222
                                throw new NumberFormatException();
223

    
224
                        value = Integer.parseInt(op);
225

    
226
                        if (value > 100)
227
                                getOpacityText().setText("100");
228
                        else
229
                                if (value < 0)
230
                                        getOpacityText().setText("0");
231

    
232
                        getOpacitySlider().setValue(value);
233
                } catch (NumberFormatException exc) {
234
                        getOpacityText().setText("100");
235
                        getOpacitySlider().setValue(100);
236
                }
237
        }
238
}