Statistics
| Revision:

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

History | View | Annotate | Download (9.44 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.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.properties.panels.BandSetupPanel;
40

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

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

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

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

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

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

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

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

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

    
112
                bandSetupPanel.onlyApply();
113
        }
114

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

    
123
                if (vBands != null) {
124
                        if (vBands.compareTo("3") == 0)
125
                                bandSetupPanel.assignMode((byte) 3);
126

    
127
                        if (vBands.compareTo("2") == 0)
128
                                bandSetupPanel.assignMode((byte) 2);
129

    
130
                        if (vBands.compareTo("1") == 0)
131
                                bandSetupPanel.assignMode((byte) 1);
132

    
133
                        bandSetupPanel.resetMode(bandSetupPanel.getMode());
134
                }
135
        }
136

    
137
        /**
138
         * A?ade una banda al raster
139
         * @param e
140
         */
141
        private void addFileBand() {
142
                // String[] driverNames = null;
143

    
144
                // Creaci?n del dialogo para selecci?n de ficheros
145

    
146
                fileChooser = new JFileChooser(FileOpenWizard.getLastPath());
147
                fileChooser.setMultiSelectionEnabled(true);
148
                fileChooser.setAcceptAllFileFilterUsed(false);
149

    
150
                fileChooser.addChoosableFileFilter(new DriverFileFilter());
151

    
152
                int result = fileChooser.showOpenDialog(bandSetupPanel);
153

    
154
                if (result == JFileChooser.APPROVE_OPTION) {
155
                        IRasterDataSource ds = dataset.getDataSource();
156
                        File[] files = fileChooser.getSelectedFiles();
157

    
158
                        FileOpenWizard.setLastPath(files[0].getPath());
159

    
160
                        // Lo a?adimos a la capa si no esta
161

    
162
                        for (int i = 0; i < files.length; i++) {
163

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

    
174
                                                RasterDataset geoRasterDataset = RasterDataset.open(prop.getProjection(), files[i].getAbsolutePath());
175
                                                Extent extentNewFile = geoRasterDataset.getExtent();
176
                                                nbands += geoRasterDataset.getBandCount();
177

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

    
181
                                                double widthNewFile = (extentNewFile.getMax().getX() - extentNewFile.getMin().getX());
182
                                                double heightNewFile = (extentNewFile.getMax().getY() - extentNewFile.getMin().getY());
183

    
184
                                                if ((widthNewFile - extentOrigin.getWidth()) > 1.0 || (widthNewFile - extentOrigin.getWidth()) < -1.0 || (heightNewFile - extentOrigin.getHeight()) > 1.0
185
                                                                || (heightNewFile - extentOrigin.getHeight()) < -1.0) {
186
                                                        JOptionPane.showMessageDialog(null, PluginServices.getText(this, "extents_no_coincidentes"), "", JOptionPane.ERROR_MESSAGE);
187
                                                        continue;
188
                                                }
189

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

    
196
                                                geoRasterDataset.close();
197

    
198
                                        } catch (Exception exc) {
199
                                                RasterToolsUtil.messageBoxError("addband_error", bandSetupPanel, exc);
200
                                        }
201

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

    
216
                        // A?adimos los georasterfile a la tabla del Panel
217
                        bandSetupPanel.addFiles(ds);
218
                }
219
        }
220

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

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

    
250
        /**
251
         * Acciones a ejecutar cuando se aplica
252
         */
253
        public void apply() {
254
                if (enabled)
255
                        setNewBandsPositionInRendering();
256
        }
257

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

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