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

History | View | Annotate | Download (6.35 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.util.ArrayList;
27
import java.util.List;
28

    
29
import javax.swing.JRadioButton;
30

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

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

    
56
        /**
57
         * Constructor
58
         * @param bs Panel del selector de bandas
59
         * @param lyr Capa raster
60
         */
61
        public BandSelectorNewLayerListener(BandSelectorPanel bs) {
62
                super(bs);
63
        }
64
        
65
        public RasterDataStore getResult() {
66
                return mainRasterStore;
67
        }
68
        
69
        /**
70
         * Listener para la gesti?n de los botones de a?adir, eliminar fichero y
71
         * el combo de selecci?n de bandas.
72
         */
73
        public void actionPerformed(ActionEvent e) {
74
                if (e.getSource().equals(bandSetupPanel.getFileList().getJButtonAdd())) {
75
                        addFileBand();
76
                        return;
77
                }
78

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

    
89
        public void actionButtonPressed(ButtonsPanelEvent e) {
90
                
91
        }
92

    
93
        @Override
94
        public void setNewBandsPositionInRendering() {
95
                
96
        }
97

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

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

    
107
                if (result == JFileChooser.APPROVE_OPTION) {
108
                        File[] files = fileChooser.getSelectedFiles();
109
                        if(files == null || files.length <= 0)
110
                                return;
111
                        
112
                        JFileChooser.setLastPath(FilesystemExplorerWizardPanel.OPEN_LAYER_FILE_CHOOSER_ID, files[0]);
113
                        int initPosition = 0;
114
                        
115
                        if(mainRasterStore == null) {
116
                                initPosition = 1;
117
                                MultiFileProvider provider = createMultiFileProvider(file, folder);
118
                                MultiFileDataParameters newParamsMultifile = (MultiFileDataParameters)provider.getDataParameters();
119
                                
120
                                try {
121
                                        RasterDataStore firstDataStore = RasterLocator.getManager().getProviderServices().open(files[0].getAbsolutePath());
122
                                        newParamsMultifile.addProvider(firstDataStore);
123
                                        mainRasterStore = RasterLocator.getManager().getProviderServices().open(provider, provider.getDataStoreParameters());
124
                                } catch (RasterDriverException e) {
125
                                        log.debug("Error loading the main store", e);
126
                                } catch (LocatorException e) {
127
                                        log.debug("Error loading the main store", e);
128
                                } catch (NotSupportedExtensionException e) {
129
                                        log.debug("Error loading the main store", e);
130
                                } catch (InitializeException e) {
131
                                        log.debug("Error loading the main store", e);
132
                                } catch (ProviderNotRegisteredException e) {
133
                                        log.debug("Error loading the main store", e);
134
                                }
135
                        } 
136

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

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

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

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

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

    
178
        @Override
179
        public void delFileBand() {
180
                if(mainRasterStore == null)
181
                        return;
182
                
183
                Object[] objects = bandSetupPanel.getFileList().getJList().getSelectedValues();
184
                
185
                for (int i = objects.length - 1; i >= 0; i--) {
186
                        if (bandSetupPanel.getFileList().getNFiles() > 1) {
187
                                String pathName = objects[i].toString();
188
                                mainRasterStore.removeFile(pathName);
189

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

    
199

    
200
}