Statistics
| Revision:

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

History | View | Annotate | Download (10.6 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.panels;
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.drivers.GenericRasterDriver;
31
import org.gvsig.raster.dataset.NotSupportedExtensionException;
32
import org.gvsig.raster.dataset.RasterDataset;
33
import org.gvsig.raster.dataset.RasterDriverException;
34
import org.gvsig.raster.dataset.RasterMultiDataset;
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

    
40
import com.hardcode.driverManager.Driver;
41
import com.hardcode.driverManager.DriverLoadException;
42
import com.iver.andami.PluginServices;
43
import com.iver.andami.messages.NotificationManager;
44
import com.iver.cit.gvsig.fmap.layers.FLayer;
45
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
46

    
47

    
48
/**
49
 * Clase que maneja los eventos del panel BandSetupPanel. Gestiona el a?adir o eliminar
50
 * ficheros de la lista y contiene las acciones a realizar cuando en panel registrable
51
 * se pulsa aceptar, aplicar o cancelar.
52
 * 
53
 * @author Nacho Brodin (brodin_ign@gva.es)
54
 */
55
public class BandSetupListener implements ActionListener{
56

    
57
        private BandSetupPanel                         bandSetupPanel = null;
58
        private JFileChooser                        fileChooser = null;
59
        private String                                        lastPath = new String("./");
60
        
61
        private FLayer                                         fLayer = null;
62
        private IRasterDataset                        dataset = null;
63
        private IRasterProperties                prop = null;
64
        
65
        /**
66
     * N?mero de bandas.
67
     */
68
    protected int nbands = 0;
69
    /**
70
     * Lista de geoRasterDataset correspondiente a los ficheros de bandas
71
     */
72
    protected RasterDataset[] grd = null;
73
    
74
    /**
75
     * Filtro para la selecci?n de ficheros raster 
76
     * @author Nacho Brodin (nachobrodin@gmail.com)
77
     *
78
     */
79
    public class DriverFileFilter extends FileFilter{
80

    
81
                private Driver driver;
82

    
83
                public DriverFileFilter(String driverName) throws DriverLoadException{
84
                        driver = LayerFactory.getDM().getDriver(driverName);
85
                }
86

    
87
                /**
88
                 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
89
                 */
90
                public boolean accept(File f) {
91
                        if (f.isDirectory()) return true;
92
                        if (driver instanceof GenericRasterDriver){
93
                                return ((GenericRasterDriver) driver).fileAccepted(f);
94
                        }else{
95
                                throw new RuntimeException("Tipo no reconocido");
96
                        }
97
                }
98

    
99
                /**
100
                 * @see javax.swing.filechooser.FileFilter#getDescription()
101
                 */
102
                public String getDescription() {
103
                        return ((Driver) driver).getName();
104
                }
105
        }
106
        
107
    /**
108
     * Constructor
109
     * @param bs Panel del selector de bandas
110
     * @param lyr Capa raster
111
     */
112
        public BandSetupListener(BandSetupPanel bs, IRasterDataset dset, IRasterProperties prop, FLayer lyr){
113
                //TODO: FUNCIONALIDAD: Cancelaci?n para la selecci?n de bandas
114
                this.bandSetupPanel = bs;
115
                this.dataset = dset;
116
                this.prop = prop;
117
                fLayer = lyr;
118
                bs.getFileList().getJButtonAdd().addActionListener(this);
119
                bs.getFileList().getJButtonRemove().addActionListener(this);
120
        bs.getFileList().getJComboBox().addActionListener(this);
121
        }
122
        
123
        /**
124
         * Listener para la gesti?n de los botones de a?adir y eliminar fichero y 
125
         * el combo de selecci?n de bandas.
126
         */
127
        public void actionPerformed(ActionEvent e) {
128
                if(e.getSource().equals(bandSetupPanel.getFileList().getJButtonAdd()))
129
                        addFileBand();
130
                
131
                if(e.getSource().equals(bandSetupPanel.getFileList().getJButtonRemove()))
132
                        delFileBand();
133
                
134
                if(e.getSource().equals(bandSetupPanel.getFileList().getJComboBox()))
135
                        numberBandSelection();
136
        }
137
        
138
         /**
139
     *Evento que activado cuando se modifica el n?mero de bandas que se desean visualizar.
140
     *Se obtiene este n?mero y se llama a la funci?n que controla el cambio de n?mero
141
     *de bandas visualizadas. 
142
     */
143
    public void numberBandSelection() {
144
        String vBands = (String) bandSetupPanel.getFileList().getJComboBox().getSelectedItem();
145

    
146
        if (vBands != null) {
147
            if (vBands.compareTo("3") == 0)
148
                    bandSetupPanel.assignMode((byte)3);
149

    
150
            if (vBands.compareTo("2") == 0)
151
                    bandSetupPanel.assignMode((byte)2);
152

    
153
            if (vBands.compareTo("1") == 0)
154
                    bandSetupPanel.assignMode((byte)1);
155

    
156
            bandSetupPanel.resetMode(bandSetupPanel.getMode());
157
        }
158
    }
159
    
160
        /**
161
         * A?ade una banda al raster
162
         * @param e
163
         */
164
        private void addFileBand(){
165
                String[] driverNames = null;
166

    
167
                //Creaci?n del dialogo para selecci?n de ficheros
168

    
169
                fileChooser = new JFileChooser(lastPath);
170
                fileChooser.setMultiSelectionEnabled(true);
171
                fileChooser.setAcceptAllFileFilterUsed(false);
172
                
173
        try {
174
                        driverNames = LayerFactory.getDM().getDriverNames();
175
                        FileFilter auxF;
176
                        for (int i = 0; i < driverNames.length; i++) {
177
                                if (driverNames[i].endsWith((String)dataset.getInfo("DriverName"))){
178
                                    auxF = new DriverFileFilter(driverNames[i]);
179
                                        fileChooser.addChoosableFileFilter(auxF);
180
                                }
181
                        }
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
                        RasterMultiDataset grmd = dataset.getGeoRasterMultiDataset();
190
                        File[] files = fileChooser.getSelectedFiles();
191

    
192
                         lastPath = 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.openFile(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 ||
219
                                            (widthNewFile-extentOrigin.getWidth()) < -1.0 ||
220
                                            (heightNewFile-extentOrigin.getHeight()) > 1.0 ||
221
                                            (heightNewFile-extentOrigin.getHeight()) < -1.0){
222
                                            JOptionPane.showMessageDialog(        null,
223
                                                                                                            PluginServices.getText(this, "extents_no_coincidentes"),
224
                                                                                                                        "",
225
                                                                                                                        JOptionPane.ERROR_MESSAGE);
226
                                            return;
227
                                    }
228

    
229
                                    if(        (extentNewFile.getMax().getX()-extentNewFile.getMin().getX())!=extentOrigin.getWidth() ||
230
                                                                (extentNewFile.getMax().getY()-extentNewFile.getMin().getY())!=extentOrigin.getHeight()        ){
231
                                            JOptionPane.showMessageDialog(null,
232
                                                                        PluginServices.getText(this, "extents_no_coincidentes"), "", JOptionPane.ERROR_MESSAGE);
233
                                                        return;
234
                                    }
235
                                    
236
                                    dataset.getGeoRasterMultiDataset().addDataset(geoRasterDataset);
237

    
238
                            }catch(Exception exc){
239
                                    exc.printStackTrace();
240
                            }
241

    
242
                            //Lo a?adimos a la capa
243
                            dataset.addFile(files[i].getAbsolutePath());
244
                    }else{
245
                            JOptionPane.showMessageDialog(null,
246
                                                        PluginServices.getText(this, "fichero_existe")+" "+files[i].getAbsolutePath(), "", JOptionPane.ERROR_MESSAGE);
247
                            return;
248
                    }
249
            }
250

    
251
            //A?adimos los georasterfile a la tabla del Panel
252
            bandSetupPanel.addFiles(grmd);
253
                }
254
        }
255

    
256
        /**
257
         * Elimina una banda del raster. Si queda solo un fichero o no se ha
258
         * seleccionado ninguna banda no hace nada.
259
         * @param e
260
         */
261
        private void delFileBand(){
262
                if(        bandSetupPanel.getFileList().getNFiles() > 1 && 
263
                        bandSetupPanel.getFileList().getJList().getSelectedValue() != null){
264
                        String pathName = bandSetupPanel.getFileList().getJList().getSelectedValue().toString();
265
                        RasterDataset geoRasterFile = null;
266
                        try {
267
                                geoRasterFile = RasterDataset.openFile(prop.getProjection(), pathName);
268
                        } catch (NotSupportedExtensionException e) {
269
                                NotificationManager.addError(e.getMessage(), e);
270
                        } catch (RasterDriverException e1) {
271
                                NotificationManager.addError(e1.getMessage(), e1);
272
                        }
273
                        nbands -= geoRasterFile.getBandCount();
274
                        dataset.delFile(pathName);
275
                        String file = pathName.substring(pathName.lastIndexOf("/")+1);
276
                        file = file.substring(file.lastIndexOf("\\")+1);
277
                        bandSetupPanel.removeFile(file);
278
                }
279
        }
280
        
281
        /**
282
         * Acciones a ejecutar cuando se acepta
283
         */
284
        public void accept(){
285
                setNewBandsPositionInRendering();
286
        }
287
        
288
        /**
289
         * Acciones a ejecutar cuando se aplica
290
         */
291
        public void apply(){
292
                setNewBandsPositionInRendering();
293
        }
294
        
295
        /**
296
         * Acciones a ejecutar cuando se cancela
297
         */
298
        public void cancel(){
299
                
300
        }
301
        
302
        /**
303
         * Asigna la posici?n de las bandas en el rederizado basandose en la selecci?n
304
         * hecho en la tabla de bandas. 
305
         *
306
         */
307
        public void setNewBandsPositionInRendering(){
308
                if(prop != null && prop.getRender() != null){
309
                        Rendering render = prop.getRender();
310
                        int[] renderBands = new int[]{        bandSetupPanel.getAssignedBand(RasterDataset.RED_BAND),
311
                                                                                        bandSetupPanel.getAssignedBand(RasterDataset.GREEN_BAND),
312
                                                                                        bandSetupPanel.getAssignedBand(RasterDataset.BLUE_BAND)};
313
                        render.setRenderBands(renderBands);
314
                        fLayer.getMapContext().invalidate();
315
                }
316
        }
317
}