[Jreepad-CVS] jreepad/src/jreepad/ui SaveFileChooser.java, NONE, 1.1 ExtensionFileFilter.java, NONE
Brought to you by:
danstowell
From: PeWu <pe...@us...> - 2007-03-20 10:18:22
|
Update of /cvsroot/jreepad/jreepad/src/jreepad/ui In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv2400/src/jreepad/ui Added Files: SaveFileChooser.java ExtensionFileFilter.java Log Message: Use Action interface for saveAction. Added possibility to choose file format in the save dialog. --- NEW FILE: ExtensionFileFilter.java --- package jreepad.ui; import java.io.File; import javax.swing.filechooser.FileFilter; /** * File filter, which accepts only files with the given extension. */ public class ExtensionFileFilter extends FileFilter { /** * Description of this filter. */ private String description; /** * Allowed extension. */ private String extension; /** * Lowercase version of the extension. */ private String lowercaseExtension; public ExtensionFileFilter(String description, String extension) { this.description = description; this.extension = extension; this.lowercaseExtension = extension.toLowerCase(); } /** * Tests whether the given file has the appropriate extension. */ public boolean accept(File f) { if (f == null) return false; if (f.isDirectory()) return true; String fileName = f.getName(); int i = fileName.lastIndexOf('.'); if (i <= 0 || i >= fileName.length() - 1) return false; String fileExtension = fileName.substring(i + 1).toLowerCase(); if (fileExtension.equals(lowercaseExtension)) return true; return false; } /** * Returns the description of this filter. */ public String getDescription() { return description; } /** * Returns the filtered file extension. */ public String getExtension() { return extension; } } --- NEW FILE: SaveFileChooser.java --- package jreepad.ui; import java.io.File; import java.util.ResourceBundle; import javax.swing.JFileChooser; import javax.swing.JOptionPane; public class SaveFileChooser extends JFileChooser { protected static final ResourceBundle LANG = ResourceBundle.getBundle("jreepad.lang.JreepadStrings"); public void approveSelection() { if (!checkOverwrite(getSelectedFile())) return; // User doesn't want to overwrite the file if (getSelectedFile().isFile() && !getSelectedFile().canWrite()) { JOptionPane.showMessageDialog(this, LANG.getString("MSG_FILE_NOT_WRITEABLE"), LANG.getString("TITLE_FILE_ERROR"), JOptionPane.ERROR_MESSAGE); return; // Can't write selected file } super.approveSelection(); } private boolean checkOverwrite(File theFile) { // If file doesn't already exist then fine if (theFile == null || !theFile.exists()) return true; // Else we need to confirm return (JOptionPane.showConfirmDialog(this, LANG.getString("PROMPT_CONFIRM_OVERWRITE1") + " " + theFile.getName() + " " + LANG.getString("PROMPT_CONFIRM_OVERWRITE2"), LANG.getString("TITLE_CONFIRM_OVERWRITE"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION); } } |