Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / util / BaseOpenErrorHandler.java @ 41004

History | View | Annotate | Download (6.71 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.util;
25

    
26
import java.io.File;
27
import java.io.FilenameFilter;
28

    
29
import javax.swing.JOptionPane;
30

    
31
import org.apache.commons.io.FilenameUtils;
32
import org.gvsig.fmap.dal.DataParameters;
33
import org.gvsig.fmap.dal.OpenErrorHandler;
34
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
35
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
36
import org.gvsig.gui.beans.swing.JFileChooser;
37
import org.gvsig.i18n.Messages;
38
import org.gvsig.utils.SimpleFileFilter;
39

    
40
public class BaseOpenErrorHandler implements OpenErrorHandler{
41
        
42
        
43
        private DataParameters parameters;
44
//        private Exception exception;
45

    
46
        public BaseOpenErrorHandler() {
47
//                exception=null;
48
                parameters=null;
49
                
50
        }
51

    
52
        public boolean canRetryOpen(Exception e, DataParameters parameters) {
53
//                this.exception=e;
54
                this.parameters=parameters;
55
//                Throwable cause = null;
56
                boolean retry = false;
57

    
58
                /*if (this.exception instanceof InvocationTargetException){
59
                        cause = this.exception.getCause();
60
                } else {
61
                        cause = this.exception;
62
                }*/
63
                //if (cause instanceof InitializeException){
64
                        //if (cause.getCause() instanceof OpenException){
65
                                if ( this.parameters instanceof FilesystemStoreParameters) {
66
                                        FilesystemStoreParameters filesystemStoreParameters = (FilesystemStoreParameters)this.parameters;
67
                                        File searchedFile = filesystemStoreParameters.getFile();
68
                                        File file = null;
69
                                        if( searchedFile != null && !searchedFile.exists() ) {
70
                                                file = chooseAlternateFile(searchedFile);
71
                                                if( file!=null){
72
                                                        filesystemStoreParameters.setFile(file);
73
                                                        retry = true;
74
                                                } else {
75
                                                        return false;
76
                                                }
77
                                        }
78

    
79
                                        if (this.parameters.getDynClass().getName().equalsIgnoreCase("SHPStoreParameters") ) {
80
                                                //SHPStoreParameters shpStoreParameters = (SHPStoreParameters)this.parameters;
81
//                                                File searchedDBFFile = shpStoreParameters.getDBFFile();
82
                                                File searchedDBFFile =  (File) this.parameters.getDynValue("dbfFile");
83
                                                if (searchedDBFFile!=null && !searchedDBFFile.exists()) {
84
                                                        File possibleDbfFile = null;
85
                                                        if (file != null){
86
//                                                                possibleDbfFile = SHP.getDbfFile(file);
87
                                                                possibleDbfFile = findFile(file, "dbf");
88
                                                        }
89
                                                        if (possibleDbfFile != null && possibleDbfFile.exists()) {
90
//                                                                shpStoreParameters.setDBFFile(possibleDbfFile);
91
                                                                this.parameters.setDynValue("dbfFile", possibleDbfFile);
92
                                                                retry = true;
93
                                                        } else {
94
                                                                File dbfFile = chooseAlternateFile(searchedDBFFile);
95
                                                                if (dbfFile != null){
96
//                                                                        shpStoreParameters.setDBFFile(dbfFile);
97
                                                                        this.parameters.setDynValue("dbfFile", dbfFile);
98
                                                                        retry = true;
99
                                                                } else {
100
                                                                        return false;
101
                                                                }
102
                                                        }
103
                                                }
104
//                                                File searchedSHXFile = shpStoreParameters.getSHXFile();
105
                                                File searchedSHXFile = (File) this.parameters.getDynValue("shxfile");;
106
                                                if (searchedDBFFile!=null && !searchedSHXFile.exists()) {
107
                                                        File possibleShxFile = null;
108
                                                        if (file != null){
109
//                                                                possibleShxFile = SHP.getShxFile(file);
110
                                                                possibleShxFile = findFile(file, "shx");
111
                                                        }
112
                                                        if (possibleShxFile != null && possibleShxFile.exists()) {
113
//                                                                shpStoreParameters.setSHXFile(possibleShxFile);
114
                                                                this.parameters.setDynValue("shxfile", possibleShxFile);
115
                                                                retry = true;
116
                                                        } else {
117
                                                                File shxFile = chooseAlternateFile(searchedSHXFile);
118
                                                                if (shxFile != null) {
119
//                                                                        shpStoreParameters.setSHXFile(shxFile);
120
                                                                        this.parameters.setDynValue("shxfile", shxFile);
121
                                                                        retry = true;
122
                                                                } else {
123
                                                                        return false;
124
                                                                }
125
                                                        }
126
                                                }
127
                                        }
128
                                }
129
                        //}
130
                //}
131
                return retry;
132
        }
133
        
134
        private File chooseAlternateFile(File searchedFile){
135
                        int resp = JOptionPane.showConfirmDialog(
136
                                        null,
137
                                        Messages.getText("dont_find_the_file")+"\n"+
138
                                        searchedFile.getName()+"\n"+
139
                                        Messages.getText("do_you_want_to_locate_the_file"),
140
                                        Messages.getText("dont_find_the_file"),
141
                                        JOptionPane.YES_NO_OPTION,
142
                                        JOptionPane.QUESTION_MESSAGE);
143
                        if (resp == JOptionPane.OK_OPTION){
144
                                JFileChooser fileChooser = new JFileChooser(FilesystemExplorerWizardPanel.OPEN_LAYER_FILE_CHOOSER_ID,
145
                                                searchedFile.getParentFile());
146
                                File parentFile = searchedFile.getParentFile();
147
                                if (parentFile.exists()){
148
                                        fileChooser.setLastPath(parentFile);
149
                                }
150
                                fileChooser.setMultiSelectionEnabled(false);
151
                                fileChooser.setAcceptAllFileFilterUsed(false);
152
                                fileChooser.setDialogTitle("dont_find_the_file: "+searchedFile.getAbsolutePath());
153

    
154
                                String searchedFileName = searchedFile.getName();
155
                                String ext = searchedFileName.substring(searchedFileName.lastIndexOf(".")+1);
156
                                SimpleFileFilter fileFilter = new SimpleFileFilter(ext, ext);
157
                                fileChooser.addChoosableFileFilter(fileFilter);
158
                                fileChooser.setFileFilter(fileFilter);
159
                                
160
                                int result = fileChooser.showOpenDialog(null);
161
                                if (result == JFileChooser.APPROVE_OPTION) {
162
                            File file = fileChooser.getSelectedFile();
163
                            return file;
164
                        }
165
                        }
166
                return null;
167
        }
168
        
169
        private File findFile(File file, String extension) {
170
                File[] files = file.getParentFile().listFiles(new ExtensionFilenameFilter(file, extension));
171
                if( files == null || files.length<1 ) {
172
                        return new File(file.getParentFile(),FilenameUtils.getBaseName(file.getName())+"."+extension);
173
                }
174
                return files[0];
175
        }
176
        
177
        private class ExtensionFilenameFilter implements FilenameFilter {
178
                private String extension;
179
                private String baseName;
180
                ExtensionFilenameFilter(File f, String extension) {
181
                        this.extension = extension;
182
                        this.baseName = FilenameUtils.getBaseName(f.getName());
183
                }
184
                public boolean accept(File arg0, String filename) {
185
                        if( FilenameUtils.getBaseName(filename).equals(baseName) ) {
186
                                if( FilenameUtils.getExtension(filename).equalsIgnoreCase(extension) ) {
187
                                        return true;
188
                                }
189
                        }
190
                        return false;
191
                }
192
        }
193

    
194
}
195

    
196