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 @ 2370

History | View | Annotate | Download (12 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.RasterLibrary;
34
import org.gvsig.fmap.dal.coverage.RasterLocator;
35
import org.gvsig.fmap.dal.coverage.exception.InvalidSourceException;
36
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
37
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
38
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
39
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
40
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
41
import org.gvsig.fmap.dal.coverage.util.ProviderServices;
42
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider;
43
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
44
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
45
import org.gvsig.fmap.dal.spi.DataStoreProvider;
46
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
47
import org.gvsig.gui.beans.swing.JFileChooser;
48
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
49
import org.gvsig.i18n.Messages;
50
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster;
51
import org.gvsig.raster.fmap.layers.FLyrRaster;
52
import org.gvsig.raster.mainplugin.RasterMainPluginUtils;
53
import org.gvsig.raster.mainplugin.config.Configuration;
54
import org.gvsig.raster.multifile.io.MultiFileDataParameters;
55
import org.gvsig.raster.multifile.io.MultiFileProvider;
56
import org.gvsig.raster.swing.RasterSwingLibrary;
57
import org.gvsig.raster.swing.basepanel.ButtonsPanelEvent;
58

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

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

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

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

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

    
114
                if (e.getSource().equals(bandSetupPanel.getSaveButton())) {
115
                        int numBandToRed = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.RED_BAND);
116
                        int numBandToGreen = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.GREEN_BAND);
117
                        int numBandToBlue = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.BLUE_BAND);
118
                        int numBandToAlpha = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.ALPHA_BAND);
119

    
120
                        if (!isCorrectAssignedBand(numBandToRed, numBandToGreen, numBandToBlue, numBandToAlpha)) {
121
                                RasterSwingLibrary.messageBoxError(Messages.getText("combinacion_no_asignable"), bandSetupPanel);
122
                                return;
123
                        }
124

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

    
132
                        //ColorInterpretation ci = dataSource.getColorInterpretation();
133
                        try {
134
                                String[] bands = new String[bandSetupPanel.getARGBTable().getRowCount()];
135
                                ColorInterpretation ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(bands);
136
                                
137
                                // Combinaci?n GRAY
138
                                if ((numBandToRed == numBandToGreen) && (numBandToRed == numBandToBlue) && (numBandToRed >= 0)) {
139
                                        for (int iBand = 0; iBand < bandSetupPanel.getARGBTable().getRowCount(); iBand++) {
140
                                                ci.setColorInterpValue(iBand, ColorInterpretation.UNDEF_BAND);
141
                                        }
142
                                        ci.setColorInterpValue(0, ColorInterpretation.GRAY_BAND);
143
                                        ci.setColorInterpValue(numBandToAlpha, ColorInterpretation.ALPHA_BAND);
144
                                } else {
145
                                        // Combinaci?n RGB
146
                                        for (int iBand = 0; iBand < bandSetupPanel.getARGBTable().getRowCount(); iBand++)
147
                                                ci.setColorInterpValue(iBand, bandSetupPanel.getColorInterpretationByBand(iBand));
148
                                }
149
                                String fileName = fLayer.getDataStore().getName();
150
                                dataSource.setColorInterpretation(ci);
151
                                RasterLocator.getManager().getProviderServices().saveObjectToRmfFile(fileName, ci);
152
                        } catch (RmfSerializerException exc) {
153
                                RasterSwingLibrary.messageBoxError(Messages.getText("error_salvando_rmf"), bandSetupPanel, exc);
154
                        } catch (NotInitializeException exc) {
155
                                RasterSwingLibrary.messageBoxError(Messages.getText("table_not_initialize"), bandSetupPanel, exc);
156
                        }
157
                }
158

    
159
                boolean autoRefreshView = Configuration.getValue("general_auto_preview", Boolean.TRUE).booleanValue();
160
                
161
                if (!autoRefreshView)
162
                        return;
163

    
164
                bandSetupPanel.onlyApply();
165
        }
166

    
167
        /**
168
         * A?ade una banda al raster
169
         * @param e
170
         */
171
        public void addFileBand() {
172
                fileChooser = createJFileChooser();
173
                int result = fileChooser.showOpenDialog(bandSetupPanel);
174

    
175
                if (result == JFileChooser.APPROVE_OPTION) {
176
                        RasterDataStore dataStore = fLayer.getDataStore();
177
                        File[] files = fileChooser.getSelectedFiles();
178

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

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

    
264
                try {
265
                        MultiFileProvider provMultifile = createMultiFileProvider(layerName, path);
266
                        MultiFileDataParameters newParamsMultifile = (MultiFileDataParameters)provMultifile.getDataParameters();
267
                        newParamsMultifile.addProvider(dataStore);
268

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

    
278
                        ((DefaultFLyrRaster)fLayer).setName(layerName);
279
                        //Assigns the MultifileProvider to the store
280
                        dataStore.setProvider((CoverageStoreProvider)provMultifile);
281

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

    
296
                } catch (Exception exc) {
297
                        RasterSwingLibrary.messageBoxError(Messages.getText("addband_error"), bandSetupPanel, exc);
298
                }
299
        }
300
        
301
        /**
302
         * Acciones a ejecutar cuando se aplica
303
         */
304
        public void apply() {
305
                if (enabled)
306
                        setNewBandsPositionInRendering();
307
        }
308

    
309
        public void setNewBandsPositionInRendering() {
310
                if (fLayer != null && fLayer.getRender() != null) {
311
                        fLayer.getRender().setRenderColorInterpretation(bandSetupPanel.getSelectedColorInterpretation());
312
                        
313
                        int alphaBand = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.ALPHA_BAND);
314
                        Transparency gt = fLayer.getRender().getRenderingTransparency();
315
                        if(gt != null)
316
                                gt.setTransparencyBand(alphaBand);
317
                        fLayer.getMapContext().invalidate();
318
                }
319
        }
320

    
321
}