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 / BandSelectorNewLayerListener.java @ 4181

History | View | Annotate | Download (6.36 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.io.File;
26
import java.net.URI;
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import javax.swing.JRadioButton;
31

    
32
import org.gvsig.fmap.dal.coverage.RasterLocator;
33
import org.gvsig.fmap.dal.coverage.exception.InvalidSourceException;
34
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
35
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
36
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
37
import org.gvsig.fmap.dal.exception.InitializeException;
38
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
39
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
40
import org.gvsig.gui.beans.swing.JFileChooser;
41
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
42
import org.gvsig.i18n.Messages;
43
import org.gvsig.raster.multifile.io.MultiFileDataParameters;
44
import org.gvsig.raster.multifile.io.MultiFileProvider;
45
import org.gvsig.raster.swing.RasterSwingLibrary;
46
import org.gvsig.raster.swing.basepanel.ButtonsPanelEvent;
47
import org.gvsig.tools.locator.LocatorException;
48

    
49
/**
50
 * Class to manage events when this functionality is call to create a new multifile layer
51
 *
52
 * @author Nacho Brodin (nachobrodin@gmail.com)
53
 */
54
public class BandSelectorNewLayerListener extends AbstractBandSelectorListener {
55
        protected RasterDataStore       mainRasterStore  = null;
56

    
57
        /**
58
         * Constructor
59
         * @param bs Panel del selector de bandas
60
         * @param lyr Capa raster
61
         */
62
        public BandSelectorNewLayerListener(BandSelectorPanel bs) {
63
                super(bs);
64
        }
65

    
66
        public RasterDataStore getResult() {
67
                return mainRasterStore;
68
        }
69

    
70
        /**
71
         * Listener para la gesti?n de los botones de a?adir, eliminar fichero y
72
         * el combo de selecci?n de bandas.
73
         */
74
        public void actionPerformed(ActionEvent e) {
75
                if (e.getSource().equals(bandSetupPanel.getFileList().getJButtonAdd())) {
76
                        addFileBand();
77
                        return;
78
                }
79

    
80
                if (e.getSource().equals(bandSetupPanel.getFileList().getJButtonRemove())) {
81
                        delFileBand();
82
                        return;
83
                }
84
                if(e.getSource() instanceof JRadioButton) {
85
                        return;
86
                }
87
                apply();
88
        }
89

    
90
        public void actionButtonPressed(ButtonsPanelEvent e) {
91

    
92
        }
93

    
94
        @Override
95
        public void setNewBandsPositionInRendering() {
96

    
97
        }
98

    
99
        @Override
100
        public void apply() {
101
        }
102

    
103
        @Override
104
        public void addFileBand() {
105
                JFileChooser fileChooser = createJFileChooser();
106
                int result = fileChooser.showOpenDialog(bandSetupPanel);
107

    
108
                if (result == JFileChooser.APPROVE_OPTION) {
109
                        File[] files = fileChooser.getSelectedFiles();
110
                        if(files == null || files.length <= 0)
111
                                return;
112

    
113
                        JFileChooser.setLastPath(FilesystemExplorerWizardPanel.OPEN_LAYER_FILE_CHOOSER_ID, files[0]);
114
                        int initPosition = 0;
115

    
116
                        if(mainRasterStore == null) {
117
                                initPosition = 1;
118
                                MultiFileProvider provider = createMultiFileProvider(file, folder);
119
                                MultiFileDataParameters newParamsMultifile = (MultiFileDataParameters)provider.getDataParameters();
120

    
121
                                try {
122
                                        RasterDataStore firstDataStore = RasterLocator.getManager().getProviderServices().open(files[0].getAbsolutePath());
123
                                        newParamsMultifile.addProvider(firstDataStore);
124
                                        mainRasterStore = RasterLocator.getManager().getProviderServices().open(provider, provider.getDataStoreParameters());
125
                                } catch (RasterDriverException e) {
126
                                        log.debug("Error loading the main store", e);
127
                                } catch (LocatorException e) {
128
                                        log.debug("Error loading the main store", e);
129
                                } catch (NotSupportedExtensionException e) {
130
                                        log.debug("Error loading the main store", e);
131
                                } catch (InitializeException e) {
132
                                        log.debug("Error loading the main store", e);
133
                                } catch (ProviderNotRegisteredException e) {
134
                                        log.debug("Error loading the main store", e);
135
                                }
136
                        }
137

    
138
                        List<File> fileList = new ArrayList<File>();
139

    
140
                        for (int i = initPosition; i < files.length; i++) {
141
                                //Checks that the file does not exist
142
                                URI[] uris = mainRasterStore.getURIByProvider();
143
                                boolean exists = false;
144
                                for (int j = 0; j < uris.length; j++) {
145
                                        if (uris[j].getPath().endsWith(files[i].getName())) {
146
                                                RasterSwingLibrary.messageBoxError( Messages.getText("fichero_existe") + ": " + files[i].getAbsolutePath(), bandSetupPanel);
147
                                                exists = true;
148
                                                break;
149
                                        }
150
                                }
151
                                if(!exists)
152
                                        fileList.add(files[i]);
153
                        }
154

    
155
                        if(!checkStoresCompatibility(mainRasterStore, fileList))
156
                                return;
157

    
158
                        if(mainRasterStore.isMultiFile()) {
159
                                for (int i = 0; i < fileList.size(); i++) {
160
                                        try {
161
                                                mainRasterStore.addFile(fileList.get(i)); //.getAbsolutePath());
162
                                        } catch (InvalidSourceException e) {
163
                                                RasterSwingLibrary.messageBoxError(Messages.getText("addband_error"), bandSetupPanel, e);
164
                                        }
165
                                }
166

    
167
                                mainRasterStore.setProvider(mainRasterStore.getProvider());
168

    
169
                                //It shows the files and bands in the panel
170
                                try {
171
                                        bandSetupPanel.addFiles(mainRasterStore);
172
                                } catch (NotInitializeException e) {
173
                                        RasterSwingLibrary.messageBoxError("table_not_initialize", this, e);
174
                                }
175
                        }
176
                }
177
        }
178

    
179
        @Override
180
        public void delFileBand() {
181
                if(mainRasterStore == null)
182
                        return;
183

    
184
                Object[] objects = bandSetupPanel.getFileList().getJList().getSelectedValues();
185

    
186
                for (int i = objects.length - 1; i >= 0; i--) {
187
                        if (bandSetupPanel.getFileList().getNFiles() > 1) {
188
                                String pathName = objects[i].toString();
189
                                mainRasterStore.removeFile(new File(pathName));
190

    
191
                                String file = pathName.substring(pathName.lastIndexOf(File.separator) + 1);
192
                                file = file.substring(file.lastIndexOf("\\") + 1);
193
                                bandSetupPanel.removeFile(file);
194
                        }
195
                }
196

    
197
                setNewBandsPositionInRendering();
198
        }
199

    
200

    
201
}