diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java | 159 |
1 files changed, 90 insertions, 69 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java index fd5492b..efbe74e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java @@ -7,6 +7,9 @@ import javax.swing.JFileChooser; /** * Utility class for easily prompting user for files. + * + * Built for Swing. + * * @author ben * */ @@ -20,7 +23,7 @@ public class SimpleFileChooser { while (!success) { try { maybeDoOpenFile(par, files); - + success = true; } catch (FileNotChosenException e) { SimpleDialogs.showError(par, "I/O Error", @@ -40,7 +43,7 @@ public class SimpleFileChooser { while (!success) { try { maybeDoSaveFile(par, files); - + return files.getSelectedFile(); } catch (FileNotChosenException e) { SimpleDialogs.showError(par, "I/O Error", @@ -51,31 +54,14 @@ public class SimpleFileChooser { return files.getSelectedFile(); } - private static void maybeDoSaveFile(Component par, JFileChooser files) - throws FileNotChosenException { - int res = files.showSaveDialog(par); - - System.out.println("Result: " + res); - - if (res != JFileChooser.APPROVE_OPTION) { - throw new FileNotChosenException(); - } - } - - private static void maybeDoOpenFile(Component par, JFileChooser files) - throws FileNotChosenException { - int res = files.showSaveDialog(par); - - if (res != JFileChooser.APPROVE_OPTION) { - throw new FileNotChosenException(); - } - } - /** - * Prompt the user with a "Open File..." dialog. - * Keeps prompting them until they pick a file. - * @param par The component to use as the parent for the dialog. - * @param title The title of the dialog to prompt with. + * Prompt the user with a "Open File..." dialog. Keeps prompting them + * until they pick a file. + * + * @param par + * The component to use as the parent for the dialog. + * @param title + * The title of the dialog to prompt with. * @return The file the user has chosen. */ public static File getOpenFile(Component par, String title) { @@ -85,11 +71,15 @@ public class SimpleFileChooser { } /** - * Prompt the user with a "Open File..." dialog. - * Keeps prompting them until they pick a file. - * @param par The component to use as the parent for the dialog. - * @param title The title of the dialog to prompt with. - * @param extensions The list of file extensions the file should have. + * Prompt the user with a "Open File..." dialog. Keeps prompting them + * until they pick a file. + * + * @param par + * The component to use as the parent for the dialog. + * @param title + * The title of the dialog to prompt with. + * @param extensions + * The list of file extensions the file should have. * @return The file the user has chosen. */ public static File getOpenFile(Component par, String title, @@ -101,67 +91,98 @@ public class SimpleFileChooser { } /** + * Prompt the user with a "Save File..." dialog. + * + * @param par + * The component to use as the parent for the dialog. + * @param title + * The title of the dialog to prompt with. + * @return The file the user chose. + */ + public static File getSaveFile(Component par, String title) { + JFileChooser files = new JFileChooser(); + + return doSaveFile(par, title, files); + } + + /** + * Prompt the user with a "Save File..." dialog. + * + * @param par + * The component to use as the parent for the dialog. + * @param title + * The title of the dialog to prompt with. + * @param extensions + * The extensions of the files the user can choose. + * @return The file the user chose. + */ + public static File getSaveFile(Component par, String title, + String... extensions) { + JFileChooser files = new JFileChooser(); + files.addChoosableFileFilter(new ExtensionFileFilter(extensions)); + + return doSaveFile(par, title, files); + } + + private static void maybeDoOpenFile(Component par, JFileChooser files) + throws FileNotChosenException { + int res = files.showSaveDialog(par); + + if (res != JFileChooser.APPROVE_OPTION) { + throw new FileNotChosenException(); + } + } + + private static void maybeDoSaveFile(Component par, JFileChooser files) + throws FileNotChosenException { + int res = files.showSaveDialog(par); + + System.out.println("Result: " + res); + + if (res != JFileChooser.APPROVE_OPTION) { + throw new FileNotChosenException(); + } + } + + /** * Prompt the user with a "Open File..." dialog. - * @param par The component to use as the parent for the dialog. - * @param title The title of the dialog to prompt with. + * + * @param par + * The component to use as the parent for the dialog. + * @param title + * The title of the dialog to prompt with. * @return The file if the user chose one or null if they didn't. */ public static File maybeOpenFile(Component par, String title) { JFileChooser files = new JFileChooser(); files.setDialogTitle(title); - + try { maybeDoOpenFile(par, files); } catch (FileNotChosenException e) { } - + return files.getSelectedFile(); } - + /** * Prompt the user with a "Save File..." dialog. - * @param par The component to use as the parent for the dialog. - * @param title The title of the dialog to prompt with. + * + * @param par + * The component to use as the parent for the dialog. + * @param title + * The title of the dialog to prompt with. * @return The file if the user chose one or null if they didn't. */ public static File maybeSaveFile(Component par, String title) { JFileChooser files = new JFileChooser(); files.setDialogTitle(title); - + try { maybeDoSaveFile(par, files); } catch (FileNotChosenException e) { } - - return files.getSelectedFile(); - } - - /** - * Prompt the user with a "Save File..." dialog. - * - * @param par The component to use as the parent for the dialog. - * @param title The title of the dialog to prompt with. - * @return The file the user chose. - */ - public static File getSaveFile(Component par, String title) { - JFileChooser files = new JFileChooser(); - - return doSaveFile(par, title, files); - } - - /** - * Prompt the user with a "Save File..." dialog. - * - * @param par The component to use as the parent for the dialog. - * @param title The title of the dialog to prompt with. - * @param extensions The extensions of the files the user can choose. - * @return The file the user chose. - */ - public static File getSaveFile(Component par, String title, - String... extensions) { - JFileChooser files = new JFileChooser(); - files.addChoosableFileFilter(new ExtensionFileFilter(extensions)); - return doSaveFile(par, title, files); + return files.getSelectedFile(); } } |
