summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-10-08 22:39:59 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-10-08 22:39:59 -0300
commitc82e3b3b2de0633317ec8fc85925e91422820597 (patch)
tree96567416ce23c5ce85601f9cedc3a94bb1c55cba /base/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java
parentb3ac1c8690c3e14c879913e5dcc03a5f5e14876e (diff)
Start splitting into maven modules
Diffstat (limited to 'base/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java')
-rw-r--r--base/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java144
1 files changed, 144 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java b/base/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java
new file mode 100644
index 0000000..77a4a59
--- /dev/null
+++ b/base/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java
@@ -0,0 +1,144 @@
+package bjc.utils.gui.awt;
+
+import java.awt.FileDialog;
+import java.awt.Frame;
+import java.io.File;
+import java.io.FilenameFilter;
+
+import bjc.utils.gui.SimpleDialogs;
+
+/**
+ * A simple way to get the user to pick a file
+ *
+ * Built for AWT.
+ *
+ * @author ben
+ *
+ */
+public class SimpleFileDialog {
+ /**
+ * Prompt the user to pick a file to open
+ *
+ * @param parent
+ * The parent of the file picker
+ * @param title
+ * The title of the file picker
+ * @return The file the user picked
+ */
+ public static File getOpenFile(final Frame parent, final String title) {
+ return getOpenFile(parent, title, (String[]) null);
+ }
+
+ /**
+ * Prompt the user to pick a file to open
+ *
+ * @param parent
+ * The parent of the file picker
+ * @param title
+ * The title of the file picker
+ * @param extensions
+ * The extensions to accept as valid
+ * @return The file the user picked
+ */
+ public static File getOpenFile(final Frame parent, final String title, final String... extensions) {
+ if (parent == null)
+ throw new NullPointerException("Parent must not be null");
+ else if (title == null) throw new NullPointerException("Title must not be null");
+
+ final FileDialog chooser = new FileDialog(parent, title, FileDialog.LOAD);
+
+ if (extensions != null) {
+ final FilenameFilter filter = new ExtensionFileFilter(extensions);
+ chooser.setFilenameFilter(filter);
+ }
+
+ chooser.setVisible(true);
+
+ while (chooser.getFile() == null) {
+ SimpleDialogs.showError(parent, "File I/O Error", "Please choose a file to open.");
+ chooser.setVisible(true);
+ }
+
+ return chooser.getFiles()[0];
+ }
+
+ /**
+ * Prompt the user to pick a file to open
+ *
+ * @param parent
+ * The parent of the file picker
+ * @param title
+ * The title of the file picker
+ * @param extensions
+ * The extensions to accept as valid
+ * @return The file the user picked
+ */
+ public static File[] getOpenFiles(final Frame parent, final String title, final String... extensions) {
+ if (parent == null)
+ throw new NullPointerException("Parent must not be null");
+ else if (title == null) throw new NullPointerException("Title must not be null");
+
+ final FileDialog chooser = new FileDialog(parent, title, FileDialog.LOAD);
+
+ if (extensions != null) {
+ final FilenameFilter filter = new ExtensionFileFilter(extensions);
+ chooser.setFilenameFilter(filter);
+ }
+
+ chooser.setMultipleMode(true);
+ chooser.setVisible(true);
+
+ while (chooser.getFile() == null) {
+ SimpleDialogs.showError(parent, "File I/O Error", "Please choose a file to open.");
+ chooser.setVisible(true);
+ }
+
+ return chooser.getFiles();
+ }
+
+ /**
+ * Prompt the user to pick a file to save
+ *
+ * @param parent
+ * The parent of the file picker
+ * @param title
+ * The title of the file picker
+ * @return The file the user picked
+ */
+ public static File getSaveFile(final Frame parent, final String title) {
+ return getSaveFile(parent, title, (String[]) null);
+ }
+
+ /**
+ * Prompt the user to pick a file to save
+ *
+ * @param parent
+ * The parent of the file picker
+ * @param title
+ * The title of the file picker
+ * @param extensions
+ * The extensions to accept as valid
+ * @return The file the user picked
+ */
+ public static File getSaveFile(final Frame parent, final String title, final String... extensions) {
+ if (parent == null)
+ throw new NullPointerException("Parent must not be null");
+ else if (title == null) throw new NullPointerException("Title must not be null");
+
+ final FileDialog chooser = new FileDialog(parent, title, FileDialog.SAVE);
+
+ if (extensions != null) {
+ final FilenameFilter filter = new ExtensionFileFilter(extensions);
+ chooser.setFilenameFilter(filter);
+ }
+
+ chooser.setVisible(true);
+
+ while (chooser.getFile() == null) {
+ SimpleDialogs.showError(parent, "File I/O Error", "Please choose a file to save to.");
+ chooser.setVisible(true);
+ }
+
+ return chooser.getFiles()[0];
+ }
+}