Revision 43168 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/util/BaseOpenErrorHandler.java

View differences:

BaseOpenErrorHandler.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 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.
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 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.
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 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.
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.app.util;
25 24

  
......
29 28
import javax.swing.JOptionPane;
30 29

  
31 30
import org.apache.commons.io.FilenameUtils;
31
import org.gvsig.app.ApplicationLocator;
32
import org.gvsig.app.ApplicationManager;
32 33
import org.gvsig.fmap.dal.DataParameters;
33 34
import org.gvsig.fmap.dal.OpenErrorHandler;
34 35
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
35 36
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
36 37
import org.gvsig.gui.beans.swing.JFileChooser;
37
import org.gvsig.i18n.Messages;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.i18n.I18nManager;
38 40
import org.gvsig.utils.SimpleFileFilter;
39 41

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

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

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

  
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
					}
49
    }
78 50

  
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());
51
    public boolean canRetryOpen(Exception e, DataParameters parameters) {
52
        this.parameters = parameters;
53
        boolean retry = false;
153 54

  
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
	}
55
        if (this.parameters instanceof FilesystemStoreParameters) {
56
            FilesystemStoreParameters filesystemStoreParameters = (FilesystemStoreParameters) this.parameters;
57
            File searchedFile = filesystemStoreParameters.getFile();
58
            File file = null;
59
            if (searchedFile != null && !searchedFile.exists()) {
60
                file = chooseAlternateFile(searchedFile);
61
                if (file != null) {
62
                    filesystemStoreParameters.setFile(file);
63
                    retry = true;
64
                } else {
65
                    return false;
66
                }
67
            }
193 68

  
194
}
69
            if (this.parameters.getDynClass().getName().equalsIgnoreCase("SHPStoreParameters")) {
70
                File searchedDBFFile = (File) this.parameters.getDynValue("dbfFile");
71
                if (searchedDBFFile != null && !searchedDBFFile.exists()) {
72
                    File possibleDbfFile = null;
73
                    if (file != null) {
74
                        possibleDbfFile = findFile(file, "dbf");
75
                    }
76
                    if (possibleDbfFile != null && possibleDbfFile.exists()) {
77
                        this.parameters.setDynValue("dbfFile", possibleDbfFile);
78
                        retry = true;
79
                    } else {
80
                        File dbfFile = chooseAlternateFile(searchedDBFFile);
81
                        if (dbfFile != null) {
82
                            this.parameters.setDynValue("dbfFile", dbfFile);
83
                            retry = true;
84
                        } else {
85
                            return false;
86
                        }
87
                    }
88
                }
89
                File searchedSHXFile = (File) this.parameters.getDynValue("shxfile");;
90
                if (searchedDBFFile != null && !searchedSHXFile.exists()) {
91
                    File possibleShxFile = null;
92
                    if (file != null) {
93
                        possibleShxFile = findFile(file, "shx");
94
                    }
95
                    if (possibleShxFile != null && possibleShxFile.exists()) {
96
                        this.parameters.setDynValue("shxfile", possibleShxFile);
97
                        retry = true;
98
                    } else {
99
                        File shxFile = chooseAlternateFile(searchedSHXFile);
100
                        if (shxFile != null) {
101
                            this.parameters.setDynValue("shxfile", shxFile);
102
                            retry = true;
103
                        } else {
104
                            return false;
105
                        }
106
                    }
107
                }
108
            }
109
        }
110
        return retry;
111
    }
195 112

  
113
    private File chooseAlternateFile(File searchedFile) {
114
        ApplicationManager application = ApplicationLocator.getManager();
115
        I18nManager i18nManager = ToolsLocator.getI18nManager();
116
        int resp = application.confirmDialog(
117
                i18nManager.getTranslation("dont_find_the_file") + "\n"
118
                + searchedFile.getName() + "\n"
119
                + i18nManager.getTranslation("do_you_want_to_locate_the_file"),
120
                i18nManager.getTranslation("dont_find_the_file"),
121
                JOptionPane.YES_NO_OPTION,
122
                JOptionPane.QUESTION_MESSAGE,
123
                "File not found when open data source"
124
        );
125
        if (resp == JOptionPane.OK_OPTION) {
126
            JFileChooser fileChooser = new JFileChooser(FilesystemExplorerWizardPanel.OPEN_LAYER_FILE_CHOOSER_ID,
127
                    searchedFile.getParentFile());
128
            File parentFile = searchedFile.getParentFile();
129
            if (parentFile.exists()) {
130
                fileChooser.setLastPath(parentFile);
131
            }
132
            fileChooser.setMultiSelectionEnabled(false);
133
            fileChooser.setAcceptAllFileFilterUsed(false);
134
            fileChooser.setDialogTitle("dont_find_the_file: " + searchedFile.getAbsolutePath());
196 135

  
136
            String searchedFileName = searchedFile.getName();
137
            String ext = searchedFileName.substring(searchedFileName.lastIndexOf(".") + 1);
138
            SimpleFileFilter fileFilter = new SimpleFileFilter(ext, ext);
139
            fileChooser.addChoosableFileFilter(fileFilter);
140
            fileChooser.setFileFilter(fileFilter);
141

  
142
            int result = fileChooser.showOpenDialog(null);
143
            if (result == JFileChooser.APPROVE_OPTION) {
144
                File file = fileChooser.getSelectedFile();
145
                return file;
146
            }
147
        }
148
        return null;
149
    }
150

  
151
    private File findFile(File file, String extension) {
152
        File[] files = file.getParentFile().listFiles(new ExtensionFilenameFilter(file, extension));
153
        if (files == null || files.length < 1) {
154
            return new File(file.getParentFile(), FilenameUtils.getBaseName(file.getName()) + "." + extension);
155
        }
156
        return files[0];
157
    }
158

  
159
    private class ExtensionFilenameFilter implements FilenameFilter {
160

  
161
        private String extension;
162
        private String baseName;
163

  
164
        ExtensionFilenameFilter(File f, String extension) {
165
            this.extension = extension;
166
            this.baseName = FilenameUtils.getBaseName(f.getName());
167
        }
168

  
169
        public boolean accept(File arg0, String filename) {
170
            if (FilenameUtils.getBaseName(filename).equals(baseName)) {
171
                if (FilenameUtils.getExtension(filename).equalsIgnoreCase(extension)) {
172
                    return true;
173
                }
174
            }
175
            return false;
176
        }
177
    }
178

  
179
}

Also available in: Unified diff