Statistics
| Revision:

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

History | View | Annotate | Download (10.1 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
import javax.swing.filechooser.FileFilter;
29

    
30
import org.gvsig.fmap.layers.FLyrRasterSE;
31
import org.gvsig.raster.dataset.MultiRasterDataset;
32
import org.gvsig.raster.dataset.NotSupportedExtensionException;
33
import org.gvsig.raster.dataset.RasterDataset;
34
import org.gvsig.raster.dataset.RasterDriverException;
35
import org.gvsig.raster.grid.render.Rendering;
36
import org.gvsig.raster.shared.Extent;
37
import org.gvsig.raster.shared.IRasterDataset;
38
import org.gvsig.raster.shared.IRasterProperties;
39
import org.gvsig.rastertools.RasterModule;
40
import org.gvsig.rastertools.properties.panels.BandSetupPanel;
41

    
42
import com.hardcode.driverManager.DriverLoadException;
43
import com.iver.andami.PluginServices;
44
import com.iver.andami.messages.NotificationManager;
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 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
         * Filtro para la selecci?n de ficheros raster
73
         * 
74
         * @author Nacho Brodin (nachobrodin@gmail.com)
75
         */
76
        public class DriverFileFilter extends FileFilter {
77

    
78
                //private String driverName = null;
79

    
80
                public DriverFileFilter(String driverName) throws DriverLoadException {
81
                        //this.driverName = driverName;
82
                }
83

    
84
                /**
85
                 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
86
                 */
87
                public boolean accept(File f) {
88
                        if (f.isDirectory())
89
                                return true;
90
                        if (fLayer instanceof FLyrRasterSE)
91
                                return FLyrRasterSE.isFileAccepted(f);
92
                        else
93
                                throw new RuntimeException("Tipo no reconocido");
94
                }
95

    
96
                /**
97
                 * @see javax.swing.filechooser.FileFilter#getDescription()
98
                 */
99
                public String getDescription() {
100
                        return (String)dataset.getInfo("DriverName");
101
                }
102
        }
103

    
104
        /**
105
         * Constructor
106
         * @param bs Panel del selector de bandas
107
         * @param lyr Capa raster
108
         */
109
        public BandSetupListener(BandSetupPanel bs) {
110
                this.bandSetupPanel = bs;
111
                bs.getFileList().getJButtonAdd().addActionListener(this);
112
                bs.getFileList().getJButtonRemove().addActionListener(this);
113
                bs.getFileList().getJComboBox().addActionListener(this);
114
        }
115

    
116
        /**
117
         * Constructor
118
         * @param bs Panel del selector de bandas
119
         * @param lyr Capa raster
120
         */
121
        public void init(IRasterDataset dset, IRasterProperties prop, FLayer lyr) {
122
                //TODO: FUNCIONALIDAD: Cancelaci?n para la selecci?n de bandas
123
                this.dataset = dset;
124
                this.prop = prop;
125
                fLayer = lyr;
126
        }
127

    
128
        /**
129
         * Listener para la gesti?n de los botones de a?adir y eliminar fichero y 
130
         * el combo de selecci?n de bandas.
131
         */
132
        public void actionPerformed(ActionEvent e) {
133
                if (e.getSource().equals(bandSetupPanel.getFileList().getJButtonAdd()))
134
                        addFileBand();
135

    
136
                if (e.getSource().equals(bandSetupPanel.getFileList().getJButtonRemove()))
137
                        delFileBand();
138

    
139
                if (e.getSource().equals(bandSetupPanel.getFileList().getJComboBox()))
140
                        numberBandSelection();
141
        }
142

    
143
        /**
144
         * Evento que activado cuando se modifica el n?mero de bandas que se desean
145
         * visualizar. Se obtiene este n?mero y se llama a la funci?n que controla el
146
         * cambio de n?mero de bandas visualizadas.
147
         */
148
        public void numberBandSelection() {
149
                String vBands = (String) bandSetupPanel.getFileList().getJComboBox().getSelectedItem();
150

    
151
                if (vBands != null) {
152
                        if (vBands.compareTo("3") == 0)
153
                                bandSetupPanel.assignMode((byte) 3);
154

    
155
                        if (vBands.compareTo("2") == 0)
156
                                bandSetupPanel.assignMode((byte) 2);
157

    
158
                        if (vBands.compareTo("1") == 0)
159
                                bandSetupPanel.assignMode((byte) 1);
160

    
161
                        bandSetupPanel.resetMode(bandSetupPanel.getMode());
162
                }
163
        }
164

    
165
        /**
166
         * A?ade una banda al raster
167
         * 
168
         * @param e
169
         */
170
        private void addFileBand() {
171
                // String[] driverNames = null;
172

    
173
                // Creaci?n del dialogo para selecci?n de ficheros
174

    
175
                fileChooser = new JFileChooser(RasterModule.getPath());
176
                fileChooser.setMultiSelectionEnabled(true);
177
                fileChooser.setAcceptAllFileFilterUsed(false);
178

    
179
                try {
180
                        FileFilter auxF = new DriverFileFilter((String) dataset.getInfo("DriverName"));
181
                        fileChooser.addChoosableFileFilter(auxF);
182
                } catch (DriverLoadException e1) {
183
                        NotificationManager.addError("No se pudo acceder a los drivers", e1);
184
                }
185

    
186
                int result = fileChooser.showOpenDialog(bandSetupPanel);
187

    
188
                if (result == JFileChooser.APPROVE_OPTION) {
189
                        MultiRasterDataset grmd = dataset.getGeoRasterMultiDataset();
190
                        File[] files = fileChooser.getSelectedFiles();
191

    
192
                        RasterModule.setPath(files[0].getPath());
193

    
194
                        // Lo a?adimos a la capa si no esta
195

    
196
                        for (int i = 0; i < files.length; i++) {
197

    
198
                                // Comprobamos que el fichero no est?
199
                                boolean exist = false;
200
                                for (int j = 0; j < grmd.getDatasetCount(); j++) {
201
                                        if (dataset.getGeoRasterMultiDataset().getDataset(j).getFName().endsWith(files[i].getName()))
202
                                                exist = true;
203
                                }
204
                                if (!exist) {
205
                                        try {
206
                                                Rectangle2D extentOrigin = prop.getFullRasterExtent().toRectangle2D();
207

    
208
                                                RasterDataset geoRasterDataset = RasterDataset.open(prop.getProjection(), files[i].getAbsolutePath());
209
                                                Extent extentNewFile = geoRasterDataset.getExtent();
210
                                                nbands += geoRasterDataset.getBandCount();
211

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

    
215
                                                double widthNewFile = (extentNewFile.getMax().getX() - extentNewFile.getMin().getX());
216
                                                double heightNewFile = (extentNewFile.getMax().getY() - extentNewFile.getMin().getY());
217

    
218
                                                if ((widthNewFile - extentOrigin.getWidth()) > 1.0 || (widthNewFile - extentOrigin.getWidth()) < -1.0 || (heightNewFile - extentOrigin.getHeight()) > 1.0
219
                                                                || (heightNewFile - extentOrigin.getHeight()) < -1.0) {
220
                                                        JOptionPane.showMessageDialog(null, PluginServices.getText(this, "extents_no_coincidentes"), "", JOptionPane.ERROR_MESSAGE);
221
                                                        return;
222
                                                }
223

    
224
                                                if ((extentNewFile.getMax().getX() - extentNewFile.getMin().getX()) != extentOrigin.getWidth()
225
                                                                || (extentNewFile.getMax().getY() - extentNewFile.getMin().getY()) != extentOrigin.getHeight()) {
226
                                                        JOptionPane.showMessageDialog(null, PluginServices.getText(this, "extents_no_coincidentes"), "", JOptionPane.ERROR_MESSAGE);
227
                                                        return;
228
                                                }
229

    
230
                                                dataset.getGeoRasterMultiDataset().addDataset(geoRasterDataset);
231

    
232
                                        } catch (Exception exc) {
233
                                                exc.printStackTrace();
234
                                        }
235

    
236
                                        // Lo a?adimos a la capa
237
                                        dataset.addFile(files[i].getAbsolutePath());
238
                                } else {
239
                                        JOptionPane.showMessageDialog(null, PluginServices.getText(this, "fichero_existe") + " " + files[i].getAbsolutePath(), "", JOptionPane.ERROR_MESSAGE);
240
                                        return;
241
                                }
242
                        }
243

    
244
                        // A?adimos los georasterfile a la tabla del Panel
245
                        bandSetupPanel.addFiles(grmd);
246
                }
247
        }
248

    
249
        /**
250
         * Elimina una banda del raster. Si queda solo un fichero o no se ha
251
         * seleccionado ninguna banda no hace nada.
252
         * 
253
         * @param e
254
         */
255
        private void delFileBand() {
256
                if (bandSetupPanel.getFileList().getNFiles() > 1 && bandSetupPanel.getFileList().getJList().getSelectedValue() != null) {
257
                        String pathName = bandSetupPanel.getFileList().getJList().getSelectedValue().toString();
258
                        RasterDataset geoRasterFile = null;
259
                        try {
260
                                geoRasterFile = RasterDataset.open(prop.getProjection(), pathName);
261
                        } catch (NotSupportedExtensionException e) {
262
                                NotificationManager.addError(e.getMessage(), e);
263
                        } catch (RasterDriverException e1) {
264
                                NotificationManager.addError(e1.getMessage(), e1);
265
                        }
266
                        nbands -= geoRasterFile.getBandCount();
267
                        dataset.delFile(pathName);
268
                        String file = pathName.substring(pathName.lastIndexOf("/") + 1);
269
                        file = file.substring(file.lastIndexOf("\\") + 1);
270
                        bandSetupPanel.removeFile(file);
271
                }
272
        }
273

    
274
        /**
275
         * Acciones a ejecutar cuando se acepta
276
         */
277
        public void accept() {
278
                if (enabled)
279
                        setNewBandsPositionInRendering();
280
        }
281

    
282
        /**
283
         * Acciones a ejecutar cuando se aplica
284
         */
285
        public void apply() {
286
                if (enabled)
287
                        setNewBandsPositionInRendering();
288
        }
289

    
290
        /**
291
         * Acciones a ejecutar cuando se cancela
292
         */
293
        public void cancel() {
294
        }
295

    
296
        /**
297
         * Asigna la posici?n de las bandas en el rederizado basandose en la selecci?n
298
         * hecho en la tabla de bandas. 
299
         *
300
         */
301
        public void setNewBandsPositionInRendering() {
302
                if (prop != null && prop.getRender() != null) {
303
                        Rendering render = prop.getRender();
304
                        int[] renderBands = new int[] { bandSetupPanel.getAssignedBand(RasterDataset.RED_BAND), bandSetupPanel.getAssignedBand(RasterDataset.GREEN_BAND),
305
                                        bandSetupPanel.getAssignedBand(RasterDataset.BLUE_BAND) };
306
                        render.setRenderBands(renderBands);
307
                        fLayer.getMapContext().invalidate();
308
                }
309
        }
310

    
311
        /**
312
         * Activa o desactiva la acci?n del panel
313
         * 
314
         * @param enabled true para activa y false para desactivar.
315
         */
316
        public void setEnabledPanelAction(boolean enabled) {
317
                this.enabled = enabled;
318
        }
319
}