获取完整的JFileChooser输入并阻止采取其他操作

时间:2015-04-21 17:54:10

标签: java swing filesystems filenames jfilechooser

我有一个FileSaver类,它扩展了JFileChooser类,以便我可以控制文件过滤器并验证输入。这是代码:

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

private class FileSaver extends JFileChooser {

    private String pngDescription = "*.png";
    private String jpgDescription = "*.png";

    public FileSaver() {
        super();
        FileNameExtensionFilter jpgFilter = new FileNameExtensionFilter(jpgDescription, "jpg");
        addChoosableFileFilter(jpgFilter);
        addChoosableFileFilter(new FileNameExtensionFilter(pngDescription, "png"));
        setAcceptAllFileFilterUsed(false);
        setFileFilter(jpgFilter);
    }

    @Override
    public void approveSelection() {
        boolean approveFileName = false;
        File newFile = getSelectedFile();
        System.out.println(newFile.getName());
        if (newFile.getName().matches(".*[\\\\/?<>:|\"%*].*")) { // Regex which matches if invalid characters present
            JOptionPane.showMessageDialog(this, "The following characters are not allowed: \\ / ? < > : | \" % *");
        } else if (newFile.exists()) {
            int overwriteReturn = JOptionPane.showConfirmDialog(this, "A file with this name already exists, would you like to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION);
            if (overwriteReturn == JOptionPane.YES_OPTION) { // If the user chose to overwrite the file
                approveFileName = true;
            }
        } else {
            approveFileName = true;
        }
        if (approveFileName) {
            try {
                if (newFile.createNewFile()) {
                    super.approveSelection(); // Allow the dialog to close and the showSaveDialog() method to return APPROVE_OPTION
                }
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, "There was a problem saving the image; you might not have permission to save to the directory you chose.");
            }
        }
    }

}

我想要使用此类的唯一实例是在其他类中使用此方法:

public void someMethod() {
    FileSaver fileSaver = new FileSaver();
    int saveFileReturn = fileSaver.showSaveDialog(window);
    if (saveFileReturn == FileSaver.APPROVE_OPTION) {
        File newFile = fileSaver.getSelectedFile();
        saveFile(newFile);
    }
}

对于输入的大多数文件名都可以,但如果文件名包含问号(“?”)或星号(“*”),则不会调用approveSelection()并创建新的文件过滤器(并显示当程序主要将文件过滤器视为其先前的设置时设置,文件名为描述。是否可以将这些文件名视为任何其他文件名(例如调用approveSelection()并相应地选择所选文件)?

e.g。此图显示了尝试保存“nameWithQuestionMark?”的结果。在桌面目录中:

Save Dialog

此外,似乎包含正斜杠(“/”)的文件名被视为相对于当前目录的路径,因此getSelectedFile().getName()仅在斜杠(或删除)后返回文件名的一部分文件名末尾的斜杠)和附加到目录路径之前的部分。 approveSelection()是否可以检索验证的完整文件名而不是更改目录?

e.g。在/ home目录中输入文件名“dir / name”应该为选定的文件提供路径“/ home”并命名为“dir / name”,并将filename字段的内容保留为“dir / name”。相反,所选文件的路径为“/ home / dir”,名称为“name”,文件名字段包含“name”。

0 个答案:

没有答案