1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| if (tmsFile == null) {
//Get selected input file and create window for it.
int returnVal = exportFileChooser.showSaveDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) return null;
//Retreive file types allowed for the export function
FileFilter[] fileFilters = exportFileChooser.getChoosableFileFilters();
//Retrieve file name typed in by the user, in order to verify that it has a correct extension
String selectedFile = exportFileChooser.getSelectedFile().getAbsolutePath();
String[] extensions = new String[fileFilters.length-1]; //Save allowed extension for further display
int nbExt = 0;
boolean hasExtension=false;
//Check, if the file name is one of the file type allowed, making comparison between extensions.
for(int i=0;i<fileFilters.length && !hasExtension && selectedFile!=null;i++){
//Do not compare extension with the "All file" filter.
if(!fileFilters[i].equals(exportFileChooser.getAcceptAllFileFilter())){
extensions[nbExt++]=((CommonFileFilter)fileFilters[i]).getExtension();
if(selectedFile.toLowerCase().endsWith(((CommonFileFilter)fileFilters[i]).getExtension()))
hasExtension=true;
}
}
//If the file does not have the correct extension, or has no extension...
if (!hasExtension) {
//...add the one defined by the filter type selected.
if(!exportFileChooser.getFileFilter().equals(exportFileChooser.getAcceptAllFileFilter())){
CommonFileFilter cff = (CommonFileFilter)exportFileChooser.getFileFilter();
selectedFile+= cff.getExtension();
}else{
//If the filter selected is "All files", then inform that the user
//have to enter an extension.
StringBuffer allowedExtensions=new StringBuffer(extensions[0]);
for(int i=1; i<extensions.length;i++){
allowedExtensions.append(" or "+extensions[i]);
}
JOptionPane.showMessageDialog(
this,
"MySoft could not proceed with EXPORT...."
+ "\nPlease make sure that the file name ends with"
+ allowedExtensions,
"Notice", JOptionPane.ERROR_MESSAGE);
return null;
}
}
exptFile = new File(selectedFile);
//Check if the file does not already exist
File parentFile = exptFile.getParentFile(); //Work directory
File[] otherFiles = null; //List all the files of the work directory
boolean alreadyExist = false;
boolean overwriteIt = true;
if(parentFile!=null){
otherFiles = parentFile.listFiles(); //Retrieve the list of all files contained into the work directory
for(int i=0;i<otherFiles.length && !alreadyExist;i++){
//If the file already exist in the work directory,
//ask the user if he wants to overwrite or not the file.
if(otherFiles[i].equals(exptFile)){
int chosen = JOptionPane.showOptionDialog(null,
"The file "+exptFile.getName()+" already exists."+
"\nAre you sure you want to overwrite it?",
"File already exists",
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
null, null, null);
switch(chosen) {
case JOptionPane.YES_OPTION:
overwriteIt=true;
break;
case JOptionPane.NO_OPTION:
overwriteIt=false;
break;
case JOptionPane.CLOSED_OPTION:
overwriteIt=false;
break;
}
alreadyExist=true;
}
}
}
//Exit the export function if the user does not want to overwrite
//the file, or if it is impossible to create the file.
if (exptFile == null || !overwriteIt) return null;
} |
Partager