Statistics
| Revision:

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

History | View | Annotate | Download (9.53 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.awt.event.ActionEvent;
22
import java.awt.event.ActionListener;
23
import java.awt.geom.Rectangle2D;
24
import java.io.File;
25

    
26
import javax.swing.JFileChooser;
27
import javax.swing.JOptionPane;
28

    
29
import org.gvsig.raster.dataset.IRasterDataSource;
30
import org.gvsig.raster.dataset.NotSupportedExtensionException;
31
import org.gvsig.raster.dataset.RasterDataset;
32
import org.gvsig.raster.dataset.io.RasterDriverException;
33
import org.gvsig.raster.datastruct.Extent;
34
import org.gvsig.raster.gui.wizards.DriverFileFilter;
35
import org.gvsig.raster.hierarchy.IRasterDataset;
36
import org.gvsig.raster.hierarchy.IRasterProperties;
37
import org.gvsig.raster.hierarchy.IRasterRendering;
38
import org.gvsig.raster.util.RasterToolsUtil;
39
import org.gvsig.rastertools.RasterModule;
40
import org.gvsig.rastertools.properties.panels.BandSetupPanel;
41

    
42
import com.iver.andami.PluginServices;
43
import com.iver.andami.messages.NotificationManager;
44
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard;
45
import com.iver.cit.gvsig.fmap.layers.FLayer;
46
/**
47
 * Clase que maneja los eventos del panel BandSetupPanel. Gestiona el a?adir o
48
 * eliminar ficheros de la lista y contiene las acciones a realizar cuando en
49
 * panel registrable se pulsa aceptar, aplicar o cancelar.
50
 *
51
 * @author Nacho Brodin (brodin_ign@gva.es)
52
 */
53
public class BandSetupListener implements ActionListener {
54
        private BandSetupPanel    bandSetupPanel = null;
55
        private JFileChooser      fileChooser    = null;
56
        private FLayer            fLayer         = null;
57
        private IRasterDataset    dataset        = null;
58
        private IRasterProperties prop           = null;
59
        private IRasterRendering  render         = null;
60
        private boolean           enabled        = true;
61

    
62
        /**
63
         * N?mero de bandas.
64
         */
65
        protected int             nbands         = 0;
66

    
67
        /**
68
         * Lista de geoRasterDataset correspondiente a los ficheros de bandas
69
         */
70
        protected RasterDataset[] grd            = null;
71

    
72
        /**
73
         * Constructor
74
         * @param bs Panel del selector de bandas
75
         * @param lyr Capa raster
76
         */
77
        public BandSetupListener(BandSetupPanel bs) {
78
                this.bandSetupPanel = bs;
79
                bs.getFileList().getJButtonAdd().addActionListener(this);
80
                bs.getFileList().getJButtonRemove().addActionListener(this);
81
                bs.getFileList().getJComboBox().addActionListener(this);
82
        }
83

    
84
        /**
85
         * Constructor
86
         * @param bs Panel del selector de bandas
87
         * @param lyr Capa raster
88
         */
89
        public void init(IRasterDataset dset, IRasterProperties prop, FLayer lyr) {
90
                //TODO: FUNCIONALIDAD: Cancelaci?n para la selecci?n de bandas
91
                this.dataset = dset;
92
                this.prop = prop;
93
                fLayer = lyr;
94
                if(fLayer instanceof IRasterRendering)
95
                        render = (IRasterRendering)fLayer;
96
        }
97

    
98
        /**
99
         * Listener para la gesti?n de los botones de a?adir y eliminar fichero y
100
         * el combo de selecci?n de bandas.
101
         */
102
        public void actionPerformed(ActionEvent e) {
103

    
104
                if (e.getSource().equals(bandSetupPanel.getFileList().getJButtonAdd()))
105
                        addFileBand();
106

    
107
                if (e.getSource().equals(bandSetupPanel.getFileList().getJButtonRemove()))
108
                        delFileBand();
109

    
110
                if (e.getSource().equals(bandSetupPanel.getFileList().getJComboBox()))
111
                        numberBandSelection();
112

    
113
                if (!RasterModule.autoRefreshView)
114
                        return;
115

    
116
                bandSetupPanel.onlyApply();
117
        }
118

    
119
        /**
120
         * Evento que activado cuando se modifica el n?mero de bandas que se desean
121
         * visualizar. Se obtiene este n?mero y se llama a la funci?n que controla el
122
         * cambio de n?mero de bandas visualizadas.
123
         */
124
        public void numberBandSelection() {
125
                String vBands = (String) bandSetupPanel.getFileList().getJComboBox().getSelectedItem();
126

    
127
                if (vBands != null) {
128
                        if (vBands.compareTo("3") == 0)
129
                                bandSetupPanel.assignMode((byte) 3);
130

    
131
                        if (vBands.compareTo("2") == 0)
132
                                bandSetupPanel.assignMode((byte) 2);
133

    
134
                        if (vBands.compareTo("1") == 0)
135
                                bandSetupPanel.assignMode((byte) 1);
136

    
137
                        bandSetupPanel.resetMode(bandSetupPanel.getMode());
138
                }
139
        }
140

    
141
        /**
142
         * A?ade una banda al raster
143
         * @param e
144
         */
145
        private void addFileBand() {
146
                // String[] driverNames = null;
147

    
148
                // Creaci?n del dialogo para selecci?n de ficheros
149

    
150
                fileChooser = new JFileChooser(FileOpenWizard.getLastPath());
151
                fileChooser.setMultiSelectionEnabled(true);
152
                fileChooser.setAcceptAllFileFilterUsed(false);
153

    
154
                fileChooser.addChoosableFileFilter(new DriverFileFilter());
155

    
156
                int result = fileChooser.showOpenDialog(bandSetupPanel);
157

    
158
                if (result == JFileChooser.APPROVE_OPTION) {
159
                        IRasterDataSource ds = dataset.getDataSource();
160
                        File[] files = fileChooser.getSelectedFiles();
161

    
162
                        FileOpenWizard.setLastPath(files[0].getPath());
163

    
164
                        // Lo a?adimos a la capa si no esta
165

    
166
                        for (int i = 0; i < files.length; i++) {
167

    
168
                                // Comprobamos que el fichero no est?
169
                                boolean exist = false;
170
                                for (int j = 0; j < ds.getDatasetCount(); j++) {
171
                                        if (dataset.getDataSource().getDataset(j)[0].getFName().endsWith(files[i].getName()))
172
                                                exist = true;
173
                                }
174
                                if (!exist) {
175
                                        try {
176
                                                Rectangle2D extentOrigin = prop.getFullRasterExtent().toRectangle2D();
177

    
178
                                                RasterDataset geoRasterDataset = RasterDataset.open(prop.getProjection(), files[i].getAbsolutePath());
179
                                                Extent extentNewFile = geoRasterDataset.getExtent();
180
                                                nbands += geoRasterDataset.getBandCount();
181

    
182
                                                // Comprobamos que el extent y tama?o del fichero a?adido sea igual al
183
                                                // fichero original. Si no es as? no abrimos la capa y mostramos un aviso
184

    
185
                                                double widthNewFile = (extentNewFile.getMax().getX() - extentNewFile.getMin().getX());
186
                                                double heightNewFile = (extentNewFile.getMax().getY() - extentNewFile.getMin().getY());
187

    
188
                                                if ((widthNewFile - extentOrigin.getWidth()) > 1.0 || (widthNewFile - extentOrigin.getWidth()) < -1.0 || (heightNewFile - extentOrigin.getHeight()) > 1.0
189
                                                                || (heightNewFile - extentOrigin.getHeight()) < -1.0) {
190
                                                        JOptionPane.showMessageDialog(null, PluginServices.getText(this, "extents_no_coincidentes"), "", JOptionPane.ERROR_MESSAGE);
191
                                                        continue;
192
                                                }
193

    
194
                                                if ((extentNewFile.getMax().getX() - extentNewFile.getMin().getX()) != extentOrigin.getWidth()
195
                                                                || (extentNewFile.getMax().getY() - extentNewFile.getMin().getY()) != extentOrigin.getHeight()) {
196
                                                        JOptionPane.showMessageDialog(null, PluginServices.getText(this, "extents_no_coincidentes"), "", JOptionPane.ERROR_MESSAGE);
197
                                                        continue;
198
                                                }
199

    
200
                                                geoRasterDataset.close();
201

    
202
                                        } catch (Exception exc) {
203
                                                RasterToolsUtil.messageBoxError("addband_error", bandSetupPanel, exc);
204
                                        }
205

    
206
                                        // Lo a?adimos a la capa
207
                                        try {
208
                                                dataset.addFile(files[i].getAbsolutePath());
209
                                        } catch (NotSupportedExtensionException e) {
210
                                                RasterToolsUtil.messageBoxError("addband_error", bandSetupPanel, e);
211
                                        } catch (RasterDriverException e) {
212
                                                RasterToolsUtil.messageBoxError("addband_error", bandSetupPanel, e);
213
                                        }
214
                                } else {
215
                                        RasterToolsUtil.messageBoxError("fichero_existe" + " " + files[i].getAbsolutePath(), bandSetupPanel);
216
                                        return;
217
                                }
218
                        }
219

    
220
                        // A?adimos los georasterfile a la tabla del Panel
221
                        bandSetupPanel.addFiles(ds);
222
                }
223
        }
224

    
225
        /**
226
         * Elimina una banda del raster. Si queda solo un fichero o no se ha
227
         * seleccionado ninguna banda no hace nada.
228
         *
229
         * @param e
230
         */
231
        private void delFileBand() {
232
                Object[] objects = bandSetupPanel.getFileList().getJList().getSelectedValues();
233

    
234
                for (int i = objects.length - 1; i >=0; i--) {
235
                        if (bandSetupPanel.getFileList().getNFiles() > 1) {
236
                                String pathName = objects[i].toString();
237
                                RasterDataset geoRasterFile = null;
238
                                try {
239
                                        geoRasterFile = RasterDataset.open(prop.getProjection(), pathName);
240
                                } catch (NotSupportedExtensionException e) {
241
                                        NotificationManager.addError(e.getMessage(), e);
242
                                } catch (RasterDriverException e1) {
243
                                        NotificationManager.addError(e1.getMessage(), e1);
244
                                }
245
                                nbands -= geoRasterFile.getBandCount();
246
                                dataset.delFile(pathName);
247
                                String file = pathName.substring(pathName.lastIndexOf("/") + 1);
248
                                file = file.substring(file.lastIndexOf("\\") + 1);
249
                                bandSetupPanel.removeFile(file);
250
                        }
251
                }
252
        }
253

    
254
        /**
255
         * Acciones a ejecutar cuando se aplica
256
         */
257
        public void apply() {
258
                if (enabled)
259
                        setNewBandsPositionInRendering();
260
        }
261

    
262
        /**
263
         * Asigna la posici?n de las bandas en el rederizado basandose en la selecci?n
264
         * hecho en la tabla de bandas.
265
         */
266
        public void setNewBandsPositionInRendering() {
267
                if (prop != null && prop.getRender() != null) {
268
                        if(render != null) {
269
                                render.getRenderBands()[0] = bandSetupPanel.getAssignedBand(RasterDataset.RED_BAND);
270
                                render.getRenderBands()[1] = bandSetupPanel.getAssignedBand(RasterDataset.GREEN_BAND);
271
                                render.getRenderBands()[2] = bandSetupPanel.getAssignedBand(RasterDataset.BLUE_BAND);
272
                        }
273
                        fLayer.getMapContext().invalidate();
274
                }
275
        }
276

    
277
        /**
278
         * Activa o desactiva la acci?n del panel
279
         * @param enabled true para activa y false para desactivar.
280
         */
281
        public void setEnabledPanelAction(boolean enabled) {
282
                this.enabled = enabled;
283
        }
284
}