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 2458 nbrodin
/* 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 4181 fdiaz
*
6 2458 nbrodin
* 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 4181 fdiaz
*
11 2458 nbrodin
* 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 4181 fdiaz
*
16 2458 nbrodin
* 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 4181 fdiaz
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 2458 nbrodin
* MA  02110-1301, USA.
20 4181 fdiaz
*
21 2458 nbrodin
*/
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 4181 fdiaz
import java.net.URI;
28 2458 nbrodin
import java.util.ArrayList;
29
import java.util.List;
30
31 4597 fdiaz
import org.apache.commons.io.FilenameUtils;
32
33 2458 nbrodin
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 4181 fdiaz
89 2458 nbrodin
        public RasterDataStore getResult() {
90
                return fLayer.getDataStore();
91
        }
92 4181 fdiaz
93 2458 nbrodin
        /**
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 4181 fdiaz
100 2458 nbrodin
                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 4181 fdiaz
137 2458 nbrodin
                                // 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 4181 fdiaz
161 2458 nbrodin
                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 4181 fdiaz
180 2458 nbrodin
                        fileList = new ArrayList<File>();
181 4181 fdiaz
182 2458 nbrodin
                        for (int i = 0; i < files.length; i++) {
183
                                //Checks that the file does not exist
184 4181 fdiaz
                                URI[] uris = dataStore.getURIByProvider();
185 2458 nbrodin
                                boolean exists = false;
186
                                for (int j = 0; j < uris.length; j++) {
187 4597 fdiaz
                                        if (new File(uris[j]).getAbsolutePath().endsWith(files[i].getName())) {
188 2458 nbrodin
                                                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 4181 fdiaz
197 2458 nbrodin
                        if(!checkStoresCompatibility(fLayer.getDataStore(), fileList))
198
                                return;
199 4181 fdiaz
200 2458 nbrodin
                        if(dataStore.isMultiFile()) {
201
                                for (int i = 0; i < fileList.size(); i++) {
202
                                        try {
203 4181 fdiaz
                                                dataStore.addFile(fileList.get(i));
204 2458 nbrodin
                                        } catch (InvalidSourceException e) {
205
                                                RasterSwingLibrary.messageBoxError(Messages.getText("addband_error"), bandSetupPanel, e);
206
                                        }
207
                                }
208 4181 fdiaz
209 2458 nbrodin
                                dataStore.setProvider(dataStore.getProvider());
210 4181 fdiaz
211 2458 nbrodin
                                //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 4181 fdiaz
226 2458 nbrodin
        /**
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 4181 fdiaz
235 2458 nbrodin
                for (int i = objects.length - 1; i >= 0; i--) {
236
                        if (bandSetupPanel.getFileList().getNFiles() > 1) {
237
                                String pathName = objects[i].toString();
238 4181 fdiaz
                                dataStore.removeFile(new File(pathName));
239 2458 nbrodin
240
                                String file = pathName.substring(pathName.lastIndexOf(File.separator) + 1);
241
                                file = file.substring(file.lastIndexOf("\\") + 1);
242
                                bandSetupPanel.removeFile(file);
243
                        }
244
                }
245 4181 fdiaz
246 2458 nbrodin
                setNewBandsPositionInRendering();
247
        }
248 4181 fdiaz
249 2458 nbrodin
        /**
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 4181 fdiaz
255 2458 nbrodin
                RasterDataStore dataStore = fLayer.getDataStore();
256
                RasterDataParameters paramFirstFile = (RasterDataParameters)dataStore.getParameters();//(RasterDataParameters)((RasterProvider)dataStore.getProvider()).getDataParameters();
257 4181 fdiaz
258 4597 fdiaz
                File firstFile = new File(paramFirstFile.getURI());
259
        String path = FilenameUtils.getFullPath(firstFile.getAbsolutePath());
260
261 2458 nbrodin
                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 4181 fdiaz
                                ArrayList<RasterDataParameters> storeParametersList = provServ.createParametersList(fileList.get(i)); //.getAbsolutePath());
272 2458 nbrodin
                                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 4181 fdiaz
289 2458 nbrodin
                        ArrayList<File> uriList = new ArrayList<File>();
290 4597 fdiaz
                        uriList.add(firstFile);
291 2458 nbrodin
                        for (int i = 0; i < fileList.size(); i++) {
292
                                uriList.add(fileList.get(i));
293
                        }
294 4597 fdiaz
                        saveMultiFileLayer(layerName, path, uriList);
295 2458 nbrodin
296
                } catch (Exception exc) {
297
                        RasterSwingLibrary.messageBoxError(Messages.getText("addband_error"), bandSetupPanel, exc);
298
                }
299
        }
300 4181 fdiaz
301 2458 nbrodin
        /**
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 4181 fdiaz
313 2458 nbrodin
                        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
}