Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / properties / panels / BandSetupListener.java @ 11893

History | View | Annotate | Download (10.3 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.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

    
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

    
46

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

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

    
79
                private String driverName = null;
80

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

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

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

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

    
148
            if (vBands.compareTo("2") == 0)
149
                    bandSetupPanel.assignMode((byte)2);
150

    
151
            if (vBands.compareTo("1") == 0)
152
                    bandSetupPanel.assignMode((byte)1);
153

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

    
165
                //Creaci?n del dialogo para selecci?n de ficheros
166

    
167
                fileChooser = new JFileChooser(RasterModule.getPath());
168
                fileChooser.setMultiSelectionEnabled(true);
169
                fileChooser.setAcceptAllFileFilterUsed(false);
170
                
171
        try {
172
                        FileFilter auxF = new DriverFileFilter((String)dataset.getInfo("DriverName"));
173
                        fileChooser.addChoosableFileFilter(auxF);        
174
                } catch (DriverLoadException e1) {
175
                        NotificationManager.addError("No se pudo acceder a los drivers", e1);
176
                }
177
                
178
                int result = fileChooser.showOpenDialog(bandSetupPanel);
179
                
180
                if(result == JFileChooser.APPROVE_OPTION){
181
                        MultiRasterDataset grmd = dataset.getGeoRasterMultiDataset();
182
                        File[] files = fileChooser.getSelectedFiles();
183

    
184
                        RasterModule.setPath(files[0].getPath());
185

    
186
                         //Lo a?adimos a la capa si no esta
187

    
188
            for(int i = 0; i < files.length; i++){
189

    
190
                    //Comprobamos que el fichero no est?
191
                    boolean exist = false;
192
                    for(int j = 0; j < grmd.getDatasetCount(); j++){
193
                            if(dataset.getGeoRasterMultiDataset().getDataset(j).getFName().endsWith(files[i].getName()))
194
                                    exist = true;
195
                    }
196
                    if(!exist){
197
                            try{
198
                                    Rectangle2D extentOrigin = prop.getFullRasterExtent().toRectangle2D();
199

    
200
                                    RasterDataset geoRasterDataset = RasterDataset.open(prop.getProjection(), files[i].getAbsolutePath());
201
                                    Extent extentNewFile = geoRasterDataset.getExtent();
202
                                    nbands += geoRasterDataset.getBandCount();
203

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

    
207
                                    double widthNewFile = (extentNewFile.getMax().getX()-extentNewFile.getMin().getX());
208
                                    double heightNewFile = (extentNewFile.getMax().getY()-extentNewFile.getMin().getY());
209

    
210
                                    if( (widthNewFile-extentOrigin.getWidth()) > 1.0 ||
211
                                            (widthNewFile-extentOrigin.getWidth()) < -1.0 ||
212
                                            (heightNewFile-extentOrigin.getHeight()) > 1.0 ||
213
                                            (heightNewFile-extentOrigin.getHeight()) < -1.0){
214
                                            JOptionPane.showMessageDialog(        null,
215
                                                                                                            PluginServices.getText(this, "extents_no_coincidentes"),
216
                                                                                                                        "",
217
                                                                                                                        JOptionPane.ERROR_MESSAGE);
218
                                            return;
219
                                    }
220

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

    
230
                            }catch(Exception exc){
231
                                    exc.printStackTrace();
232
                            }
233

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

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

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