Statistics
| Revision:

gvsig-raster / org.gvsig.raster.multifile / branches / org.gvsig.raster.multifile_dataaccess_refactoring / org.gvsig.raster.multifile.app.multifileclient / src / main / java / org / gvsig / raster / multifile / app / panel / BandSelectorPropertiesListener.java @ 2311

History | View | Annotate | Download (11.6 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.multifile.app.panel;
23

    
24
import java.awt.event.ActionEvent;
25
import java.awt.geom.Point2D;
26
import java.io.File;
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.ui.mdiManager.WindowInfo;
32
import org.gvsig.fmap.dal.DALLocator;
33
import org.gvsig.fmap.dal.coverage.RasterLocator;
34
import org.gvsig.fmap.dal.coverage.exception.InvalidSourceException;
35
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
36
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
37
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
38
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
39
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
40
import org.gvsig.fmap.dal.coverage.util.ProviderServices;
41
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider;
42
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
43
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
44
import org.gvsig.fmap.dal.spi.DataStoreProvider;
45
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
46
import org.gvsig.gui.beans.swing.JFileChooser;
47
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
48
import org.gvsig.i18n.Messages;
49
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster;
50
import org.gvsig.raster.fmap.layers.FLyrRaster;
51
import org.gvsig.raster.mainplugin.RasterMainPluginUtils;
52
import org.gvsig.raster.mainplugin.config.Configuration;
53
import org.gvsig.raster.multifile.io.MultiFileDataParameters;
54
import org.gvsig.raster.multifile.io.MultiFileProvider;
55
import org.gvsig.raster.swing.RasterSwingLibrary;
56
import org.gvsig.raster.swing.basepanel.ButtonsPanelEvent;
57

    
58
/**
59
 * Clase que maneja los eventos del panel BandSetupPanel. Gestiona el a?adir o
60
 * eliminar ficheros de la lista y contiene las acciones a realizar cuando en
61
 * panel registrable se pulsa aceptar, aplicar o cancelar.
62
 *
63
 * @author Nacho Brodin (brodin_ign@gva.es)
64
 */
65
public class BandSelectorPropertiesListener extends AbstractBandSelectorListener {
66
        private JFileChooser          fileChooser    = null;
67
        private FLyrRaster            fLayer         = null;
68
        private List<File>            fileList       = null;
69

    
70
        /**
71
         * Constructor
72
         * @param bs Panel del selector de bandas
73
         * @param lyr Capa raster
74
         */
75
        public BandSelectorPropertiesListener(BandSelectorPanel bs) {
76
                super(bs);
77
        }
78

    
79
        /**
80
         * Constructor
81
         * @param bs Panel del selector de bandas
82
         * @param lyr Capa raster
83
         */
84
        public void init(FLyrRaster lyr) {
85
                fLayer = lyr;
86
        }
87
        
88
        public RasterDataStore getResult() {
89
                return fLayer.getDataStore();
90
        }
91
        
92
        /**
93
         * Listener para la gesti?n de los botones de a?adir, eliminar fichero y
94
         * el combo de selecci?n de bandas.
95
         */
96
        public void actionPerformed(ActionEvent e) {
97
                super.actionPerformed(e);
98
                
99
                if (e.getSource().equals(bandSetupPanel.getNumBandSelectorCombo())) {
100
                        String vBands = (String) bandSetupPanel.getNumBandSelectorCombo().getSelectedItem();
101
                        if (vBands != null) {
102
                                if (vBands.compareTo("3") == 0)
103
                                        bandSetupPanel.resetMode(3);
104

    
105
                                if (vBands.compareTo("2") == 0)
106
                                        bandSetupPanel.resetMode(2);
107

    
108
                                if (vBands.compareTo("1") == 0)
109
                                        bandSetupPanel.resetMode(1);
110
                        }
111
                }
112

    
113
                if (e.getSource().equals(bandSetupPanel.getSaveButton())) {
114
                        int rBand = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.RED_BAND);
115
                        int gBand = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.GREEN_BAND);
116
                        int bBand = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.BLUE_BAND);
117
                        int aBand = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.ALPHA_BAND);
118

    
119
                        if (!isCorrectAssignedBand(rBand, gBand, bBand, aBand)) {
120
                                RasterSwingLibrary.messageBoxError(Messages.getText("combinacion_no_asignable"), bandSetupPanel);
121
                                return;
122
                        }
123

    
124
                        RasterSwingLibrary.messageBoxYesOrNot(Messages.getText("color_interpretation_continue"), this);
125
                        RasterDataStore dataSource = fLayer.getDataStore();
126
                        if(dataSource == null) {
127
                                RasterSwingLibrary.messageBoxError(Messages.getText("error_carga_capa"), bandSetupPanel);
128
                                return;
129
                        }
130

    
131
                        ColorInterpretation ci = dataSource.getColorInterpretation();
132
                        try {
133
                                // Combinaci?n GRAY
134
                                if ((rBand == gBand) && (rBand == bBand) && (rBand >= 0)) {
135
                                        for (int iBand = 0; iBand < bandSetupPanel.getARGBTable().getRowCount(); iBand++) {
136
                                                ci.setColorInterpValue(iBand, ColorInterpretation.UNDEF_BAND);
137
                                        }
138
                                        ci.setColorInterpValue(rBand, ColorInterpretation.GRAY_BAND);
139
                                        ci.setColorInterpValue(aBand, ColorInterpretation.ALPHA_BAND);
140
                                } else {
141
                                        // Combinaci?n RGB
142
                                        for (int iBand = 0; iBand < bandSetupPanel.getARGBTable().getRowCount(); iBand++)
143
                                                ci.setColorInterpValue(iBand, bandSetupPanel.getColorInterpretationByBand(iBand));
144
                                }
145
                                String fileName = fLayer.getDataStore().getName();
146
                                RasterLocator.getManager().getProviderServices().saveObjectToRmfFile(fileName, ci);
147
                        } catch (RmfSerializerException exc) {
148
                                RasterSwingLibrary.messageBoxError(Messages.getText("error_salvando_rmf"), bandSetupPanel, exc);
149
                        } catch (NotInitializeException exc) {
150
                                RasterSwingLibrary.messageBoxError(Messages.getText("table_not_initialize"), bandSetupPanel, exc);
151
                        }
152
                }
153

    
154
                boolean autoRefreshView = Configuration.getValue("general_auto_preview", Boolean.TRUE).booleanValue();
155
                
156
                if (!autoRefreshView)
157
                        return;
158

    
159
                bandSetupPanel.onlyApply();
160
        }
161

    
162
        /**
163
         * A?ade una banda al raster
164
         * @param e
165
         */
166
        public void addFileBand() {
167
                fileChooser = createJFileChooser();
168
                int result = fileChooser.showOpenDialog(bandSetupPanel);
169

    
170
                if (result == JFileChooser.APPROVE_OPTION) {
171
                        RasterDataStore dataStore = fLayer.getDataStore();
172
                        File[] files = fileChooser.getSelectedFiles();
173

    
174
                        JFileChooser.setLastPath(FilesystemExplorerWizardPanel.OPEN_LAYER_FILE_CHOOSER_ID, files[0]);
175
                        
176
                        fileList = new ArrayList<File>();
177
                        
178
                        for (int i = 0; i < files.length; i++) {
179
                                //Checks that the file does not exist
180
                                String[] uris = dataStore.getURIByProvider();
181
                                boolean exists = false;
182
                                for (int j = 0; j < uris.length; j++) {
183
                                        if (uris[j].endsWith(files[i].getName())) {
184
                                                RasterSwingLibrary.messageBoxError( Messages.getText("fichero_existe") + ": " + files[i].getAbsolutePath(), bandSetupPanel);
185
                                                exists = true;
186
                                                break;
187
                                        }
188
                                }
189
                                if(!exists)
190
                                        fileList.add(files[i]);
191
                        }
192
                        
193
                        if(!checkStoresCompatibility(fLayer.getDataStore(), fileList))
194
                                return;
195
                        
196
                        if(dataStore.isMultiFile()) {
197
                                for (int i = 0; i < fileList.size(); i++) {
198
                                        try {
199
                                                dataStore.addFile(fileList.get(i).getAbsolutePath());
200
                                        } catch (InvalidSourceException e) {
201
                                                RasterSwingLibrary.messageBoxError(Messages.getText("addband_error"), bandSetupPanel, e);
202
                                        }
203
                                }
204
                                
205
                                dataStore.setProvider(dataStore.getProvider());
206
                                
207
                                //It shows the files and bands in the panel
208
                                try {
209
                                        bandSetupPanel.addFiles(dataStore);
210
                                } catch (NotInitializeException e) {
211
                                        RasterSwingLibrary.messageBoxError("table_not_initialize", this, e);
212
                                }
213
                        } else {
214
                                //New layer name
215
                                WindowInfo wi = PluginServices.getMDIManager().getActiveWindow().getWindowInfo();
216
                                LayerNameDialog dialog = new LayerNameDialog(new Point2D.Double(wi.getX(), wi.getY()), 300, 80, this);
217
                                RasterMainPluginUtils.addWindow(dialog);
218
                        }
219
                }
220
        }
221
        
222
        /**
223
         * Elimina una banda del raster. Si queda solo un fichero o no se ha
224
         * seleccionado ninguna banda no hace nada.
225
         *
226
         * @param e
227
         */
228
        public void delFileBand() {
229
                Object[] objects = bandSetupPanel.getFileList().getJList().getSelectedValues();
230
                RasterDataStore dataStore = fLayer.getDataStore();
231
                
232
                for (int i = objects.length - 1; i >= 0; i--) {
233
                        if (bandSetupPanel.getFileList().getNFiles() > 1) {
234
                                String pathName = objects[i].toString();
235
                                dataStore.removeFile(pathName);
236

    
237
                                String file = pathName.substring(pathName.lastIndexOf(File.separator) + 1);
238
                                file = file.substring(file.lastIndexOf("\\") + 1);
239
                                bandSetupPanel.removeFile(file);
240
                        }
241
                }
242
                
243
                setNewBandsPositionInRendering();
244
        }
245
        
246
        /**
247
         * Catchs the events from LayerNameDialog to get the name of the new layer
248
         */
249
        public void actionButtonPressed(ButtonsPanelEvent e) {
250
                String layerName = (String)e.getSource();
251
        
252
                RasterDataStore dataStore = fLayer.getDataStore();
253
                RasterDataParameters paramFirstFile = (RasterDataParameters)dataStore.getParameters();//(RasterDataParameters)((RasterProvider)dataStore.getProvider()).getDataParameters();
254
                String path = paramFirstFile.getURI().substring(0, paramFirstFile.getURI().lastIndexOf(File.separator) + 1);
255
                
256
                ProviderServices provServ = RasterLocator.getManager().getProviderServices();
257
                DataManagerProviderServices dataManager = (DataManagerProviderServices)DALLocator.getDataManager();
258

    
259
                try {
260
                        MultiFileProvider provMultifile = createMultiFileProvider(layerName, path);
261
                        MultiFileDataParameters newParamsMultifile = (MultiFileDataParameters)provMultifile.getDataParameters();
262
                        newParamsMultifile.addProvider(dataStore);
263

    
264
                        //And now it creates and adds the new ones
265
                        for (int i = 0; i < fileList.size(); i++) {
266
                                ArrayList<RasterDataParameters> storeParametersList = provServ.createParametersList(fileList.get(i).getAbsolutePath());
267
                                for (int j = 0; j < storeParametersList.size(); j++) {
268
                                        DataStoreProvider newFileProv = dataManager.createProvider((DataStoreProviderServices)dataStore, storeParametersList.get(j));
269
                                        newParamsMultifile.addProviderNotTiled(newFileProv);
270
                                }
271
                        }
272

    
273
                        ((DefaultFLyrRaster)fLayer).setName(layerName);
274
                        //Assigns the MultifileProvider to the store
275
                        dataStore.setProvider((CoverageStoreProvider)provMultifile);
276

    
277
                        //It shows the files and bands in the panel
278
                        try {
279
                                bandSetupPanel.addFiles(dataStore);
280
                        } catch (NotInitializeException ex) {
281
                                RasterSwingLibrary.messageBoxError(Messages.getText("table_not_initialize"), this, ex);
282
                        }
283
                        
284
                        ArrayList<File> uriList = new ArrayList<File>();
285
                        uriList.add(new File(paramFirstFile.getURI()));
286
                        for (int i = 0; i < fileList.size(); i++) {
287
                                uriList.add(fileList.get(i));
288
                        }
289
                        saveMultiFileLayer(layerName, paramFirstFile.getURI(), uriList);
290

    
291
                } catch (Exception exc) {
292
                        RasterSwingLibrary.messageBoxError(Messages.getText("addband_error"), bandSetupPanel, exc);
293
                }
294
        }
295
        
296
        /**
297
         * Acciones a ejecutar cuando se aplica
298
         */
299
        public void apply() {
300
                if (enabled)
301
                        setNewBandsPositionInRendering();
302
        }
303

    
304
        public void setNewBandsPositionInRendering() {
305
                if (fLayer != null && fLayer.getRender() != null) {
306
                        fLayer.getRender().setRenderColorInterpretation(bandSetupPanel.getSelectedColorInterpretation());
307
                        
308
                        int alphaBand = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.ALPHA_BAND);
309
                        Transparency gt = fLayer.getRender().getRenderingTransparency();
310
                        if(gt != null)
311
                                gt.setTransparencyBand(alphaBand);
312
                        fLayer.getMapContext().invalidate();
313
                }
314
        }
315

    
316
}