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

History | View | Annotate | Download (5.53 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.lang.reflect.InvocationTargetException;
28

    
29
import javax.swing.JOptionPane;
30

    
31
import org.gvsig.fmap.dal.DataParameters;
32
import org.gvsig.fmap.dal.OpenErrorHandler;
33
import org.gvsig.fmap.dal.exception.InitializeException;
34
import org.gvsig.fmap.dal.exception.OpenException;
35
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
36
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
37
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters;
38
import org.gvsig.fmap.dal.store.shp.utils.SHP;
39
import org.gvsig.gui.beans.swing.JFileChooser;
40
import org.gvsig.i18n.Messages;
41
import org.gvsig.utils.SimpleFileFilter;
42

    
43
public class BaseOpenErrorHandler implements OpenErrorHandler{
44
        
45
        
46
        private DataParameters parameters;
47
        private Exception exception;
48

    
49
        public BaseOpenErrorHandler() {
50
                exception=null;
51
                parameters=null;
52
                
53
        }
54

    
55
        public boolean canRetryOpen(Exception e, DataParameters parameters) {
56
                this.exception=e;
57
                this.parameters=parameters;
58
                Throwable cause = null;
59
                boolean retry = false;
60

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

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

    
149
                                String searchedFileName = searchedFile.getName();
150
                                String ext = searchedFileName.substring(searchedFileName.lastIndexOf(".")+1);
151
                                SimpleFileFilter fileFilter = new SimpleFileFilter(ext, ext);
152
                                fileChooser.addChoosableFileFilter(fileFilter);
153
                                fileChooser.setFileFilter(fileFilter);
154
                                
155
                                int result = fileChooser.showOpenDialog(null);
156
                                if (result == JFileChooser.APPROVE_OPTION) {
157
                            File file = fileChooser.getSelectedFile();
158
                            return file;
159
                        }
160
                        }
161
                return null;
162
        }
163
}
164

    
165