Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / properties / panel / TransparencyPanel.java @ 4171

History | View | Annotate | Download (7.69 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22
package org.gvsig.raster.tools.app.basic.tool.properties.panel;
23

    
24
import java.awt.BorderLayout;
25
import java.awt.Dimension;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28

    
29
import javax.swing.BorderFactory;
30
import javax.swing.JCheckBox;
31
import javax.swing.border.TitledBorder;
32

    
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.fmap.mapcontext.layers.FLayer;
35
import org.gvsig.gui.beans.checkslidertext.CheckSliderTextContainer;
36
import org.gvsig.gui.beans.panelGroup.panels.AbstractPanel;
37
import org.gvsig.gui.beans.slidertext.listeners.SliderEvent;
38
import org.gvsig.gui.beans.slidertext.listeners.SliderListener;
39
import org.gvsig.gui.util.StatusComponent;
40
import org.gvsig.raster.fmap.layers.FLyrRaster;
41
import org.gvsig.raster.fmap.layers.IRasterLayerActions;
42
import org.gvsig.raster.tools.app.basic.RasterExtension;
43
import org.gvsig.raster.tools.app.basic.tool.properties.control.TransparencyListener;
44

    
45
/**
46
 * Dialogo para asignar la transparencia por pixel y global al raster.
47
 *
48
 * @author Nacho Brodin (nachobrodin@gmail.com)
49
 */
50
public class TransparencyPanel extends AbstractPanel implements ActionListener, SliderListener {
51
        private static final long serialVersionUID  = -4556920949255458471L;
52
        private FLyrRaster        lyr               = null;
53

    
54
        /**
55
         * N?mero de bandas del raster
56
         */
57
        public int                       nBands               = 3;
58
        private JCheckBox                cbTransparencia      = null;
59
        private TranspByPixelPanel       pTranspByPixel       = null;
60
        private CheckSliderTextContainer pOpacity             = null;
61
        private TransparencyListener     transparencyListener = null;
62

    
63
        /**
64
         * Constructor.
65
         */
66
        public TransparencyPanel() {
67
                setLabel(PluginServices.getText(this, "transparencia"));
68
                initialize();
69
        }
70

    
71
        private TransparencyListener getTransparencyListener() {
72
                if (transparencyListener == null) {
73
                        transparencyListener = new TransparencyListener(this);
74
                }
75
                return transparencyListener;
76
        }
77

    
78
        /**
79
         * This method initializes this
80
         * @return void
81
         */
82
        protected void initialize() {
83
                this.setLayout(new BorderLayout());
84
                this.add(getOpacityPanel(), BorderLayout.NORTH);
85
                this.add(getPTranspByPixel(), BorderLayout.CENTER);
86
                initControls();
87
                this.setPreferredSize(new Dimension(100, 80));
88
                this.setPriority(70);
89
        }
90

    
91
        /**
92
         * Asigna el n?mero de bandas de la imagen
93
         * @param nBands
94
         */
95
        public void setBands(int nBands) {
96
                this.nBands = nBands;
97
        }
98

    
99
        /**
100
         * Inicializa controles a sus valores por defecto
101
         */
102
        public void initControls() {
103
                this.setActiveTransparencyControl(false);
104
        }
105

    
106
        /**
107
         * This method initializes jCheckBox
108
         * @return javax.swing.JCheckBox
109
         */
110
        public JCheckBox getTransparencyCheck() {
111
                if (cbTransparencia == null) {
112
                        cbTransparencia = new JCheckBox();
113
                        cbTransparencia.setText("Activar");
114
                        cbTransparencia.addActionListener(this);
115
                }
116

    
117
                return cbTransparencia;
118
        }
119

    
120
        /**
121
         * This method initializes TranspOpacitySliderPanel
122
         * @return javax.swing.JPanel
123
         */
124
        public CheckSliderTextContainer getOpacityPanel() {
125
                if (pOpacity == null) {
126
                        pOpacity = new CheckSliderTextContainer(0, 100, 100, false, PluginServices.getText(this, "activar"), false, false, true);
127
                        pOpacity.setDecimal(false);
128
                        pOpacity.setBorder(PluginServices.getText(this, "opacidad"));
129
                        pOpacity.addValueChangedListener(this);
130
                }
131

    
132
                return pOpacity;
133
        }
134

    
135
        /**
136
         * Activa/Desactiva los controles de transparencia
137
         * @param active
138
         */
139
        public void setActiveTransparencyControl(boolean active) {
140
                this.getTransparencyCheck().setSelected(active);
141
                TranspByPixelRGBInputPanel rgbPanel = this.getPTranspByPixel().getPRGBInput();
142
                rgbPanel.getTRed().setEnabled(active);
143

    
144
                if (lyr != null) {
145
                        if (lyr.getDataStore().getBandCount() == 2) {
146
                                rgbPanel.getTGreen().setEnabled(active);
147
                                rgbPanel.getTBlue().setEnabled(false);
148
                        }
149

    
150
                        if (lyr.getDataStore().getBandCount() == 3) {
151
                                rgbPanel.getTGreen().setEnabled(active);
152
                                rgbPanel.getTBlue().setEnabled(active);
153
                        }
154
                }
155
        }
156

    
157
        /**
158
         * This method initializes jPanel2
159
         * @return javax.swing.JPanel
160
         */
161
        public TranspByPixelPanel getPTranspByPixel() {
162
                if (pTranspByPixel == null) {
163
                        pTranspByPixel = new TranspByPixelPanel();
164
                        pTranspByPixel.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "transp_by_pixel"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
165
                }
166

    
167
                return pTranspByPixel;
168
        }
169

    
170
        /**
171
         * Eventos sobre TextField y CheckBox. Controla eventos de checkbox de
172
         * opacidad, transparencia, recorte de colas y los textfield de opacidad,
173
         * valores de transparencia por banda y porcentaje de recorte.
174
         */
175
        public void actionPerformed(ActionEvent e) {
176
                // Evento sobre el checkbox de transparencia
177
                if (e.getSource().equals(getTransparencyCheck())) {
178
                        // Check de opacidad activado -> Activar controles de opacidad
179
                        if (getTransparencyCheck().isSelected()) {
180
                                getPTranspByPixel().setControlEnabled(true);
181
                                getPTranspByPixel().getPRGBInput().setActiveBands(nBands);
182
                        } else
183
                                getPTranspByPixel().setControlEnabled(false);
184
                }
185
        }
186

    
187
        /**
188
         * Obtiene la capa si existe esta.
189
         * @return FLayer si existe una capa o null si no existe.
190
         */
191
        public FLayer getLayer() {
192
                if (lyr instanceof FLayer)
193
                        return (FLayer) lyr;
194
                return null;
195
        }
196

    
197
        public void setReference(Object ref) {
198
                super.setReference(ref);
199

    
200
                if (!(ref instanceof FLyrRaster))
201
                        return;
202

    
203
                this.lyr = (FLyrRaster) ref;
204

    
205
                getTransparencyListener().setLayer(this.lyr);
206
                actionEnabled();
207
        }
208

    
209
        private void actionEnabled() {
210
                if (lyr == null) {
211
                        setVisible(false);
212
                        return;
213
                }
214

    
215
                IRasterLayerActions fLyrRasterSE = ((IRasterLayerActions) lyr);
216

    
217
                if (!fLyrRasterSE.isActionEnabled(IRasterLayerActions.OPACITY))
218
                        StatusComponent.setDisabled(getOpacityPanel());
219

    
220
                if (!fLyrRasterSE.isActionEnabled(IRasterLayerActions.TRANSPARENCY))
221
                        StatusComponent.setDisabled(getPTranspByPixel());
222

    
223
                if (!fLyrRasterSE.isActionEnabled(IRasterLayerActions.TRANSPARENCY) &&
224
                                !fLyrRasterSE.isActionEnabled(IRasterLayerActions.OPACITY))
225
                        setVisible(false);
226
                else
227
                        setVisible(true);
228
        }
229

    
230
        /*
231
         *  (non-Javadoc)
232
         * @see org.gvsig.rastertools.properties.dialog.IRegistrablePanel#accept()
233
         */
234
        public void accept() {
235
                getTransparencyListener().accept();
236
        }
237

    
238
        /*
239
         *  (non-Javadoc)
240
         * @see org.gvsig.rastertools.properties.dialog.IRegistrablePanel#apply()
241
         */
242
        public void apply() {
243
                getTransparencyListener().apply();
244
        }
245

    
246
        /*
247
         *  (non-Javadoc)
248
         * @see org.gvsig.rastertools.properties.dialog.IRegistrablePanel#cancel()
249
         */
250
        public void cancel() {
251
                getTransparencyListener().cancel();
252
        }
253

    
254
        /*
255
         * (non-Javadoc)
256
         * @see org.gvsig.gui.beans.slidertext.listeners.SliderListener#actionValueChanged(org.gvsig.gui.beans.slidertext.listeners.SliderEvent)
257
         */
258
        public void actionValueChanged(SliderEvent e) {
259
                if (!RasterExtension.autoRefreshView)
260
                        return;
261
                getTransparencyListener().onlyApply();
262
        }
263

    
264
        public void actionValueDragged(SliderEvent e) {}
265
        public void selected() {}
266
}