Statistics
| Revision:

gvsig-raster / org.gvsig.raster.multifile / trunk / org.gvsig.raster.multifile / org.gvsig.raster.multifile.app.multifileclient / src / main / java / org / gvsig / raster / multifile / app / panel / BandSelectorPropertiesListener.java @ 5988

History | View | Annotate | Download (11.8 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.net.URI;
28
import java.util.ArrayList;
29
import java.util.List;
30

    
31
import org.apache.commons.io.FilenameUtils;
32

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

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

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

    
81
        /**
82
         * Constructor
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
         */
170
        public void addFileBand() {
171
                fileChooser = createJFileChooser();
172
                int result = fileChooser.showOpenDialog(bandSetupPanel);
173

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

    
178
                        JFileChooser.setLastPath(FilesystemExplorerWizardPanel.OPEN_LAYER_FILE_CHOOSER_ID, files[0]);
179

    
180
                        fileList = new ArrayList<File>();
181

    
182
                        for (int i = 0; i < files.length; i++) {
183
                                //Checks that the file does not exist
184
                                URI[] uris = dataStore.getURIByProvider();
185
                                boolean exists = false;
186
                                for (int j = 0; j < uris.length; j++) {
187
                                        if (new File(uris[j]).getAbsolutePath().endsWith(files[i].getName())) {
188
                                                RasterSwingLibrary.messageBoxError( Messages.getText("fichero_existe") + ": " + files[i].getAbsolutePath(), bandSetupPanel);
189
                                                exists = true;
190
                                                break;
191
                                        }
192
                                }
193
                                if(!exists)
194
                                        fileList.add(files[i]);
195
                        }
196

    
197
                        if(!checkStoresCompatibility(fLayer.getDataStore(), fileList))
198
                                return;
199

    
200
                        if(dataStore.isMultiFile()) {
201
                                for (int i = 0; i < fileList.size(); i++) {
202
                                        try {
203
                                                dataStore.addFile(fileList.get(i));
204
                                        } catch (InvalidSourceException e) {
205
                                                RasterSwingLibrary.messageBoxError(Messages.getText("addband_error"), bandSetupPanel, e);
206
                                        }
207
                                }
208

    
209
                                dataStore.setProvider(dataStore.getProvider());
210

    
211
                                //It shows the files and bands in the panel
212
                                try {
213
                                        bandSetupPanel.addFiles(dataStore);
214
                                } catch (NotInitializeException e) {
215
                                        RasterSwingLibrary.messageBoxError("table_not_initialize", this, e);
216
                                }
217
                        } else {
218
                                //New layer name
219
                                WindowInfo wi = PluginServices.getMDIManager().getActiveWindow().getWindowInfo();
220
                                LayerNameDialog dialog = new LayerNameDialog(new Point2D.Double(wi.getX(), wi.getY()), 300, 80, this);
221
                                RasterMainPluginUtils.addWindow(dialog);
222
                        }
223
                }
224
        }
225

    
226
        /**
227
         * Elimina una banda del raster. Si queda solo un fichero o no se ha
228
         * seleccionado ninguna banda no hace nada.
229
         *
230
         */
231
        public void delFileBand() {
232
                Object[] objects = bandSetupPanel.getFileList().getJList().getSelectedValues();
233
                RasterDataStore dataStore = fLayer.getDataStore();
234

    
235
                for (int i = objects.length - 1; i >= 0; i--) {
236
                        if (bandSetupPanel.getFileList().getNFiles() > 1) {
237
                                String pathName = objects[i].toString();
238
                                dataStore.removeFile(new File(pathName));
239

    
240
                                String file = pathName.substring(pathName.lastIndexOf(File.separator) + 1);
241
                                file = file.substring(file.lastIndexOf("\\") + 1);
242
                                bandSetupPanel.removeFile(file);
243
                        }
244
                }
245

    
246
                setNewBandsPositionInRendering();
247
        }
248

    
249
        /**
250
         * Catchs the events from LayerNameDialog to get the name of the new layer
251
         */
252
        public void actionButtonPressed(ButtonsPanelEvent e) {
253
                String layerName = (String)e.getSource();
254

    
255
                RasterDataStore dataStore = fLayer.getDataStore();
256
                RasterDataParameters paramFirstFile = (RasterDataParameters)dataStore.getParameters();//(RasterDataParameters)((RasterProvider)dataStore.getProvider()).getDataParameters();
257

    
258
                File firstFile = new File(paramFirstFile.getURI());
259
        String path = FilenameUtils.getFullPath(firstFile.getAbsolutePath());
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(firstFile);
291
                        for (int i = 0; i < fileList.size(); i++) {
292
                                uriList.add(fileList.get(i));
293
                        }
294
                        saveMultiFileLayer(layerName, path, 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
}