diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2015-09-29 13:10:53 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2015-09-29 13:10:53 -0400 |
| commit | 47577a40bc0e298041ab0915fa4bdc3355d53349 (patch) | |
| tree | 73bb298636935d37ef31d39acef6de59ebc5f85d /BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java | |
| parent | e528aec6d2d277338d7ddfdceba38d62eff08657 (diff) | |
GUI stuff.
Almost finished with imports from version one.
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 | 167 |
1 files changed, 167 insertions, 0 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 new file mode 100644 index 0000000..fd5492b --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java @@ -0,0 +1,167 @@ +package bjc.utils.gui; + +import java.awt.Component; +import java.io.File; + +import javax.swing.JFileChooser; + +/** + * Utility class for easily prompting user for files. + * @author ben + * + */ +public class SimpleFileChooser { + private static File doOpenFile(Component par, String title, + JFileChooser files) { + files.setDialogTitle(title); + + boolean success = false; + + while (!success) { + try { + maybeDoOpenFile(par, files); + + success = true; + } catch (FileNotChosenException e) { + SimpleDialogs.showError(par, "I/O Error", + "Please pick a file to open"); + } + } + + return files.getSelectedFile(); + } + + private static File doSaveFile(Component par, String title, + JFileChooser files) { + files.setDialogTitle(title); + + boolean success = false; + + while (!success) { + try { + maybeDoSaveFile(par, files); + + return files.getSelectedFile(); + } catch (FileNotChosenException e) { + SimpleDialogs.showError(par, "I/O Error", + "Please pick a file to save to"); + } + } + + 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. + * @return The file the user has chosen. + */ + public static File getOpenFile(Component par, String title) { + JFileChooser files = new JFileChooser(); + + return doOpenFile(par, title, files); + } + + /** + * 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, + String... extensions) { + JFileChooser files = new JFileChooser(); + files.addChoosableFileFilter(new ExtensionFileFilter(extensions)); + + return doOpenFile(par, title, files); + } + + /** + * 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. + * @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. + * @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); + } +} |
