svn-gvsig-desktop / tags / v2_0_0_Build_2030 / applications / appgvSIG / src / org / gvsig / app / util / BaseOpenErrorHandler.java @ 35872
History | View | Annotate | Download (4.47 KB)
1 |
package org.gvsig.app.util; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.lang.reflect.InvocationTargetException; |
5 |
|
6 |
import javax.swing.JOptionPane; |
7 |
|
8 |
import org.gvsig.fmap.dal.DataParameters; |
9 |
import org.gvsig.fmap.dal.OpenErrorHandler; |
10 |
import org.gvsig.fmap.dal.exception.InitializeException; |
11 |
import org.gvsig.fmap.dal.exception.OpenException; |
12 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
13 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel; |
14 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
15 |
import org.gvsig.fmap.dal.store.shp.utils.SHP; |
16 |
import org.gvsig.gui.beans.swing.JFileChooser; |
17 |
import org.gvsig.i18n.Messages; |
18 |
import org.gvsig.utils.SimpleFileFilter; |
19 |
|
20 |
public class BaseOpenErrorHandler implements OpenErrorHandler{ |
21 |
|
22 |
|
23 |
private DataParameters parameters;
|
24 |
private Exception exception; |
25 |
|
26 |
public BaseOpenErrorHandler() {
|
27 |
exception=null;
|
28 |
parameters=null;
|
29 |
|
30 |
} |
31 |
|
32 |
public boolean canRetryOpen(Exception e, DataParameters parameters) { |
33 |
this.exception=e;
|
34 |
this.parameters=parameters;
|
35 |
|
36 |
if (this.exception instanceof InvocationTargetException){ |
37 |
Throwable cause = this.exception.getCause(); |
38 |
if (cause instanceof InitializeException){ |
39 |
if (cause.getCause() instanceof OpenException){ |
40 |
if ( this.parameters instanceof FilesystemStoreParameters){ |
41 |
FilesystemStoreParameters filesystemStoreParameters = (FilesystemStoreParameters)this.parameters;
|
42 |
File searchedFile = filesystemStoreParameters.getFile();
|
43 |
File file = null; |
44 |
if( searchedFile != null && !searchedFile.exists() ){ |
45 |
file = chooseAlternateFile(searchedFile); |
46 |
if( file!=null){ |
47 |
filesystemStoreParameters.setFile(file); |
48 |
} else {
|
49 |
return false; |
50 |
} |
51 |
} |
52 |
|
53 |
if (this.parameters instanceof SHPStoreParameters){ |
54 |
SHPStoreParameters shpStoreParameters = (SHPStoreParameters)this.parameters;
|
55 |
File searchedDBFFile = shpStoreParameters.getDBFFile();
|
56 |
if (searchedDBFFile!=null && !searchedDBFFile.exists()){ |
57 |
File possibleDbfFile = null; |
58 |
if (file != null){ |
59 |
possibleDbfFile = SHP.getDbfFile(file); |
60 |
} |
61 |
if (possibleDbfFile != null && possibleDbfFile.exists()){ |
62 |
shpStoreParameters.setDBFFile(possibleDbfFile); |
63 |
} else {
|
64 |
File dbfFile = chooseAlternateFile(searchedDBFFile);
|
65 |
if (dbfFile != null){ |
66 |
shpStoreParameters.setDBFFile(dbfFile); |
67 |
} else {
|
68 |
return false; |
69 |
} |
70 |
} |
71 |
} |
72 |
File searchedSHXFile = shpStoreParameters.getSHXFile();
|
73 |
if (searchedDBFFile!=null && !searchedSHXFile.exists()){ |
74 |
File possibleShxFile = null; |
75 |
if (file != null){ |
76 |
possibleShxFile = SHP.getShxFile(file); |
77 |
} |
78 |
if (possibleShxFile != null && possibleShxFile.exists()){ |
79 |
shpStoreParameters.setSHXFile(possibleShxFile); |
80 |
} else {
|
81 |
File shxFile = chooseAlternateFile(searchedSHXFile);
|
82 |
if (shxFile != null){ |
83 |
shpStoreParameters.setSHXFile(shxFile); |
84 |
} else {
|
85 |
return false; |
86 |
} |
87 |
} |
88 |
} |
89 |
} |
90 |
return true; |
91 |
} |
92 |
} |
93 |
} |
94 |
} |
95 |
return false; |
96 |
} |
97 |
|
98 |
private File chooseAlternateFile(File searchedFile){ |
99 |
int resp = JOptionPane.showConfirmDialog( |
100 |
null,
|
101 |
Messages.getText("dont_find_the_file")+"\n"+ |
102 |
searchedFile.getName()+"\n"+
|
103 |
Messages.getText("do_you_want_to_locate_the_file"),
|
104 |
Messages.getText("dont_find_the_file"),
|
105 |
JOptionPane.YES_NO_OPTION,
|
106 |
JOptionPane.QUESTION_MESSAGE);
|
107 |
if (resp == JOptionPane.OK_OPTION){ |
108 |
JFileChooser fileChooser = new JFileChooser(FilesystemExplorerWizardPanel.OPEN_LAYER_FILE_CHOOSER_ID, |
109 |
searchedFile.getParentFile()); |
110 |
File parentFile = searchedFile.getParentFile();
|
111 |
if (parentFile.exists()){
|
112 |
fileChooser.setLastPath(parentFile); |
113 |
} |
114 |
fileChooser.setMultiSelectionEnabled(false);
|
115 |
fileChooser.setAcceptAllFileFilterUsed(false);
|
116 |
fileChooser.setDialogTitle("dont_find_the_file: "+searchedFile.getAbsolutePath());
|
117 |
|
118 |
String searchedFileName = searchedFile.getName();
|
119 |
String ext = searchedFileName.substring(searchedFileName.lastIndexOf(".")+1); |
120 |
SimpleFileFilter fileFilter = new SimpleFileFilter(ext, ext);
|
121 |
fileChooser.addChoosableFileFilter(fileFilter); |
122 |
fileChooser.setFileFilter(fileFilter); |
123 |
|
124 |
int result = fileChooser.showOpenDialog(null); |
125 |
if (result == JFileChooser.APPROVE_OPTION) { |
126 |
File file = fileChooser.getSelectedFile();
|
127 |
return file;
|
128 |
} |
129 |
} |
130 |
return null; |
131 |
} |
132 |
} |
133 |
|
134 |
|