Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / properties / control / TransparencyControl.java @ 12198

History | View | Annotate | Download (5.97 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.control;
20

    
21
import java.util.ArrayList;
22
import java.util.EventObject;
23

    
24
import org.gvsig.raster.grid.GridTransparency;
25
import org.gvsig.raster.util.TransparencyRange;
26
import org.gvsig.rastertools.properties.dialog.RasterPropertiesTocMenuEntry;
27
import org.gvsig.rastertools.properties.dialog.RegistrableTabPanel;
28
import org.gvsig.rastertools.properties.panels.TransparencyPanel;
29
/**
30
 * Clase que hace de interfaz entre los objetos que contienen la informaci?n de 
31
 * transparencia y el panel. 
32
 *  
33
 * @author Nacho Brodin (nachobrodin@gmail.com)
34
 */
35
public class TransparencyControl implements TranspByPixelEventListener {
36
        private GridTransparency  transparency      = null;
37
        private TransparencyPanel tPanel            = null;
38

    
39
        /**
40
         * Construye un TransparencyControl
41
         * @param tp
42
         */
43
        public TransparencyControl(TransparencyPanel tp) {
44
                this.tPanel = tp;
45
                tPanel.getPTranspByPixel().addValueChangedListener(this);
46
        }
47

    
48
        /**
49
         * Aqui se definen los parametros iniciales para la transparencia del panel
50
         * @param t
51
         */
52
        public void setTransparencyObject(GridTransparency t) {
53
                this.transparency = t;
54

    
55
                saveStatus();
56

    
57
                setValuesFromGridTransparencyToPanel();
58
        }
59

    
60
        /**
61
         * Carga los valores del panel desde el objeto con la transparencia
62
         */
63
        private void setValuesFromGridTransparencyToPanel() {
64
                //Asignamos la opacidad al panel
65
                if (transparency.getOpacity() != 255) {
66
                        tPanel.getOpacityPanel().setControlEnabled(true);
67
                        tPanel.getOpacityPanel().setValue((int) (transparency.getOpacity() * 100) / 255);
68
                } else {
69
                        tPanel.getOpacityPanel().setControlEnabled(false);
70
                        tPanel.getOpacityPanel().setValue(100);
71
                }
72

    
73
                // Asignamos los rangos de transparencia al panel
74
                tPanel.getPTranspByPixel().getListener().setEventsDisabled(true);
75
                if (transparency.getTransparencyRange().size() > 0) {
76
                        tPanel.getPTranspByPixel().clear();
77
                        tPanel.getPTranspByPixel().setControlEnabled(true);
78
                        for (int i = 0; i < transparency.getTransparencyRange().size(); i++) {
79
                                TransparencyRange range = (TransparencyRange) transparency.getTransparencyRange().get(i);
80
                                tPanel.getPTranspByPixel().addStringElement(range);
81
                        }
82
                } else {
83
                        tPanel.getPTranspByPixel().clear();
84
                        tPanel.getPTranspByPixel().setControlEnabled(false);
85
                }
86
                tPanel.getPTranspByPixel().getListener().setEventsDisabled(false);
87
        }
88

    
89
        /**
90
         * Carga los valores del objeto transparencia desde el panel
91
         */
92
        private void setValuesFromPanelToGridTransparency() {
93
                if (transparency == null)
94
                        return;
95
                // Asignamos la opacidad al objeto
96
                if (tPanel.getOpacityPanel().getCheck().isSelected()) {
97
                        transparency.setOpacity((int) Math.round((tPanel.getOpacityPanel().getValue() * 255) / 100));
98
                } else
99
                        transparency.setOpacity(255);
100

    
101
                // Asignamos los rangos de transparencia al objeto
102
                if (tPanel.getPTranspByPixel().getActiveCheck().isSelected()) {
103
                        transparency.clearListOfTransparencyRange();
104
                        ArrayList entries = tPanel.getPTranspByPixel().getEntries();
105
                        for (int i = 0; i < entries.size(); i++)
106
                                transparency.setTransparencyRange((TransparencyRange) entries.get(i));
107
                } else
108
                        transparency.clearListOfTransparencyRange();
109

    
110
                transparency.activeTransparency();
111

    
112
                // Redibujamos
113
                if (tPanel.getLayer() != null)
114
                        tPanel.getLayer().getMapContext().invalidate();
115
        }
116

    
117
        /**
118
         * Aplica y guarda los cambios del panel
119
         */
120
        public void apply() {
121
                onlyApply();
122
                saveStatus();
123
        }
124

    
125
        /**
126
         * Aplicar los cambios sin guardar su estado
127
         */
128
        public void onlyApply() {
129
                if (RasterPropertiesTocMenuEntry.enableEvents)
130
                        setValuesFromPanelToGridTransparency();
131
        }
132

    
133
        /**
134
         * Guarda el estado actual del panel
135
         */
136
        private void saveStatus() {
137
                RegistrableTabPanel.initialProperties.put("opacity", new Integer(transparency.getOpacity()));
138
                RegistrableTabPanel.initialProperties.put("transparencyActive", new Boolean(transparency.isTransparencyActive()));
139

    
140
                ArrayList newArray = new ArrayList();
141
                for (int i = 0; i < transparency.getTransparencyRange().size(); i++) {
142
                        newArray.add(transparency.getTransparencyRange().get(i));
143
                }
144
                RegistrableTabPanel.initialProperties.put("transparencyRange", newArray);
145
        }
146

    
147
        /**
148
         * Deja la capa en el ?ltimo estado guardado y la refresca
149
         */
150
        public void restoreStatus() {
151
                transparency.setOpacity(((Integer) RegistrableTabPanel.initialProperties.get("opacity")).intValue());
152
                transparency.setTransparencyActive(((Boolean) RegistrableTabPanel.initialProperties.get("transparencyActive")).booleanValue());
153

    
154
                ArrayList newArray = (ArrayList) RegistrableTabPanel.initialProperties.get("transparencyRange");
155
                transparency.clearListOfTransparencyRange();
156
                for (int i = 0; i < newArray.size(); i++) {
157
                        transparency.setTransparencyRange((TransparencyRange) newArray.get(i));
158
                }
159

    
160
                if (tPanel.getLayer() != null)
161
                        tPanel.getLayer().getMapContext().invalidate();
162
        }
163

    
164
        /**
165
         * Acciones a ejecutar cuando se acepta
166
         */
167
        public void accept() {
168
                onlyApply();
169
        }
170

    
171
        /**
172
         * Acciones a ejecutar cuando se cancela
173
         */
174
        public void cancel() {
175
                restoreStatus();
176
        }
177

    
178
        /*
179
         * (non-Javadoc)
180
         * @see org.gvsig.rastertools.properties.panels.TranspByPixelEventListener#actionPixelListChanged(java.util.EventObject)
181
         */
182
        public void actionPixelListChanged(EventObject e) {
183
                onlyApply();
184
        }
185
}