/*
* JFileChooserOverwriteDialog.java
*
* Created on 11 November 2004, 10:51
*/
package ffg.gui;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileSystemView;
/**
* A JFileChooser that takes into account instances when the user chooses
* to save over an existing file name.
*
* @author eugene
*/
public class JFileChooserOverwriteDialog extends JFileChooser {
/**
* Creates a new instance of JFileChooserOverwriteDialog with the default home directory selected
*/
public JFileChooserOverwriteDialog() {
super();
}
/**
* Creates a new instance of JFileChooserOverwriteDialog with the specified directory selected
* @param currentDirectory The selected directory
*/
public JFileChooserOverwriteDialog(File currentDirectory) {
super(currentDirectory);
}
/**
* Creates a new instance of JFileChooserOverwriteDialog with the specified directory selected and the given {@link FileSystemView}
* @param currentDirectory The selected directory
* @param fsv The specified {@link FileSystemView}
*/
public JFileChooserOverwriteDialog(File currentDirectory, FileSystemView fsv) {
super(currentDirectory, fsv);
}
/**
* Creates a new instance of JFileChooserOverwriteDialog with the specified directory selected
* @param currentDirectoryPath The selected directory
*/
public JFileChooserOverwriteDialog(String currentDirectoryPath) {
super(currentDirectoryPath);
}
/**
* Creates a new instance of JFileChooserOverwriteDialog with the specified directory selected and the given {@link FileSystemView}
* @param currentDirectoryPath The selected directory
* @param fsv The specified {@link FileSystemView}
*/
public JFileChooserOverwriteDialog(String currentDirectoryPath, FileSystemView fsv) {
super(currentDirectoryPath, fsv);
}
/**
* Creates a new instance of JFileChooserOverwriteDialog with the given {@link FileSystemView}
* @param fsv The specified {@link FileSystemView}
*/
public JFileChooserOverwriteDialog(FileSystemView fsv) {
super(fsv);
}
/**
* If a the dialog is in a save mode, and the selected file already exists,
* prompt the user whether to overwrite the existing file.
*/
@Override
public void approveSelection() {
if (getDialogType() == SAVE_DIALOG) {
File selectedFile = getSelectedFile();
if (selectedFile.exists()) {
int result = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(this), "The file " + selectedFile.getAbsolutePath() + " already exists."
+ System.getProperty("line.separator") + "Do you wish to overwrite this file?", "Overwrite File?" , JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.NO_OPTION) {
return;
}
}
}
super.approveSelection();
}
}
|