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

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.net.URI;
28
import java.util.ArrayList;
29
import java.util.List;
30

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

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

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

    
81
        /**
82
         * Constructor
83
         * @param bs Panel del selector de bandas
84
         * @param lyr Capa raster
85
         */
86
        public void init(FLyrRaster lyr) {
87
                fLayer = lyr;
88
        }
89

    
90
        public RasterDataStore getResult() {
91
                return fLayer.getDataStore();
92
        }
93

    
94
        /**
95
         * Listener para la gesti?n de los botones de a?adir, eliminar fichero y
96
         * el combo de selecci?n de bandas.
97
         */
98
        public void actionPerformed(ActionEvent e) {
99
                super.actionPerformed(e);
100

    
101
                if (e.getSource().equals(bandSetupPanel.getNumBandSelectorCombo())) {
102
                        String vBands = (String) bandSetupPanel.getNumBandSelectorCombo().getSelectedItem();
103
                        if (vBands != null) {
104
                                if (vBands.compareTo("3") == 0)
105
                                        bandSetupPanel.resetMode(3);
106

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

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

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

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

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

    
133
                        //ColorInterpretation ci = dataSource.getColorInterpretation();
134
                        try {
135
                                String[] bands = new String[bandSetupPanel.getARGBTable().getRowCount()];
136
                                ColorInterpretation ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(bands);
137

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

    
160
                boolean autoRefreshView = Configuration.getValue("general_auto_preview", Boolean.TRUE).booleanValue();
161

    
162
                if (!autoRefreshView)
163
                        return;
164

    
165
                bandSetupPanel.onlyApply();
166
        }
167

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

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

    
180
                        JFileChooser.setLastPath(FilesystemExplorerWizardPanel.OPEN_LAYER_FILE_CHOOSER_ID, files[0]);
181

    
182
                        fileList = new ArrayList<File>();
183

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

    
199
                        if(!checkStoresCompatibility(fLayer.getDataStore(), fileList))
200
                                return;
201

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

    
211
                                dataStore.setProvider(dataStore.getProvider());
212

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

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

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

    
243
                                String file = pathName.substring(pathName.lastIndexOf(File.separator) + 1);
244
                                file = file.substring(file.lastIndexOf("\\") + 1);
245
                                bandSetupPanel.removeFile(file);
246
                        }
247
                }
248

    
249
                setNewBandsPositionInRendering();
250
        }
251

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

    
258
                RasterDataStore dataStore = fLayer.getDataStore();
259
                RasterDataParameters paramFirstFile = (RasterDataParameters)dataStore.getParameters();//(RasterDataParameters)((RasterProvider)dataStore.getProvider()).getDataParameters();
260
                String path = paramFirstFile.getURI().getPath().substring(0, paramFirstFile.getURI().getPath().lastIndexOf(File.separator) + 1);
261

    
262
                ProviderServices provServ = RasterLocator.getManager().getProviderServices();
263
                DataManagerProviderServices dataManager = (DataManagerProviderServices)DALLocator.getDataManager();
264

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

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

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

    
283
                        //It shows the files and bands in the panel
284
                        try {
285
                                bandSetupPanel.addFiles(dataStore);
286
                        } catch (NotInitializeException ex) {
287
                                RasterSwingLibrary.messageBoxError(Messages.getText("table_not_initialize"), this, ex);
288
                        }
289

    
290
                        ArrayList<File> uriList = new ArrayList<File>();
291
                        uriList.add(new File(paramFirstFile.getURI()));
292
                        for (int i = 0; i < fileList.size(); i++) {
293
                                uriList.add(fileList.get(i));
294
                        }
295
                        saveMultiFileLayer(layerName, paramFirstFile.getURI().getPath(), uriList);
296

    
297
                } catch (Exception exc) {
298
                        RasterSwingLibrary.messageBoxError(Messages.getText("addband_error"), bandSetupPanel, exc);
299
                }
300
        }
301

    
302
        /**
303
         * Acciones a ejecutar cuando se aplica
304
         */
305
        public void apply() {
306
                if (enabled)
307
                        setNewBandsPositionInRendering();
308
        }
309

    
310
        public void setNewBandsPositionInRendering() {
311
                if (fLayer != null && fLayer.getRender() != null) {
312
                        fLayer.getRender().setRenderColorInterpretation(bandSetupPanel.getSelectedColorInterpretation());
313

    
314
                        int alphaBand = bandSetupPanel.getColorInterpretationByColorBandBand(RasterDataStore.ALPHA_BAND);
315
                        Transparency gt = fLayer.getRender().getRenderingTransparency();
316
                        if(gt != null)
317
                                gt.setTransparencyBand(alphaBand);
318
                        fLayer.getMapContext().invalidate();
319
                }
320
        }
321

    
322
}