diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-01-26 11:32:41 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-01-26 11:32:41 -0500 |
| commit | d8b3b3c5e4441cecec98c06a36fc81570008c888 (patch) | |
| tree | f71e260819ef4fdf1297ae0cc43c6a1dc4092eb9 /BJC-Utils2/src/main/java/bjc/utils/gui | |
| parent | 6de1845151db750c8dbbc6b12964c4d6e6144eaf (diff) | |
Updates to various things, and addition of a graph class.
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui')
4 files changed, 212 insertions, 31 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java index 7731529..85b9fa9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java @@ -1,24 +1,39 @@ package bjc.utils.gui; import java.awt.Component; +import java.awt.Frame; import java.util.function.Function; import java.util.function.Predicate; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JLabel; import javax.swing.JOptionPane; +import javax.swing.JPanel; + +import bjc.utils.gui.layout.VLayout; /** * Utility class for getting simple input from the user. + * * @author ben * */ public class SimpleDialogs { /** * Get a bounded integer from the user. - * @param parent The parent component for the dialogs. - * @param title The title for the dialogs. - * @param prompt The prompt to tell the user what to enter. - * @param lower The lower integer bound to accept. - * @param upper The upper integer bound to accept. + * + * @param parent + * The parent component for the dialogs. + * @param title + * The title for the dialogs. + * @param prompt + * The prompt to tell the user what to enter. + * @param lower + * The lower integer bound to accept. + * @param upper + * The upper integer bound to accept. * @return A int within the specified bounds. */ public static int getBoundedInt(Component parent, String title, @@ -35,9 +50,13 @@ public class SimpleDialogs { /** * Get a integer from the user - * @param parent The parent component for dialogs. - * @param title The title for dialogs. - * @param prompt The prompt to tell the user what to enter. + * + * @param parent + * The parent component for dialogs. + * @param title + * The title for dialogs. + * @param prompt + * The prompt to tell the user what to enter. * @return A int. */ public static int getInt(Component parent, String title, @@ -54,23 +73,34 @@ public class SimpleDialogs { /** * Get a string from the user - * @param parent The parent component for dialogs. - * @param title The title for the dialogs. - * @param prompt The prompt to tell the user what to enter. + * + * @param parent + * The parent component for dialogs. + * @param title + * The title for the dialogs. + * @param prompt + * The prompt to tell the user what to enter. * @return A string. */ - public static String getString(Component parent, String title, String prompt) { + public static String getString(Component parent, String title, + String prompt) { return JOptionPane.showInputDialog(parent, prompt, title, JOptionPane.QUESTION_MESSAGE); } /** * Get a value parsable from a string from the user. - * @param parent The parent component for dialogs. - * @param title The title for dialogs. - * @param prompt The prompt to tell the user what to enter. - * @param p A predicate to determine if a input is valid. - * @param f The function to transform the string into a value. + * + * @param parent + * The parent component for dialogs. + * @param title + * The title for dialogs. + * @param prompt + * The prompt to tell the user what to enter. + * @param p + * A predicate to determine if a input is valid. + * @param f + * The function to transform the string into a value. * @return The value parsed from a string. */ public static <E> E getValue(Component parent, String title, @@ -88,9 +118,13 @@ public class SimpleDialogs { /** * Get a whole number from the user. - * @param parent The parent component for dialogs. - * @param title The title for dialogs. - * @param prompt The prompt to tell the user what to enter. + * + * @param parent + * The parent component for dialogs. + * @param title + * The title for dialogs. + * @param prompt + * The prompt to tell the user what to enter. * @return A whole number. */ public static int getWhole(Component parent, String title, @@ -100,9 +134,13 @@ public class SimpleDialogs { /** * Ask the user a Yes/No question. - * @param parent The parent component for dialogs. - * @param title The title for dialogs. - * @param question The question to ask the user. + * + * @param parent + * The parent component for dialogs. + * @param title + * The title for dialogs. + * @param question + * The question to ask the user. * @return True if the user said yes, false otherwise. */ public static boolean getYesNo(Component parent, String title, @@ -112,12 +150,62 @@ public class SimpleDialogs { return (res == JOptionPane.YES_OPTION ? true : false); } - + + /** + * Asks the user to pick an option from a series of choices. + * + * @param parent + * The parent frame for this dialog + * @param title + * The title of this dialog + * @param question + * The question being asked + * @param choices + * The availible choices for the question + * @return The choice the user picked, or null if they didn't pick one + */ + @SuppressWarnings("unchecked") + public static <E> E getChoice(Frame parent, String title, + String question, E... choices) { + JDialog jd = new JDialog(parent, title, true); + jd.setLayout(new VLayout(2)); + + JPanel questionPane = new JPanel(); + + JLabel questionText = new JLabel(question); + JComboBox<E> questionChoices = new JComboBox<>(choices); + + questionPane.add(questionText); + questionPane.add(questionChoices); + + JPanel buttonPane = new JPanel(); + + JButton okButton = new JButton("Ok"); + okButton.addActionListener(e -> jd.dispose()); + JButton cancelButton = new JButton("Cancel"); + cancelButton.addActionListener(e -> jd.dispose()); + + buttonPane.add(cancelButton); + buttonPane.add(okButton); + + jd.add(questionPane); + jd.add(buttonPane); + + jd.pack(); + jd.setVisible(true); + + return (E) questionChoices.getSelectedItem(); + } + /** * Show a error message to the user - * @param parent The parent component for dialogs. - * @param title The title for dialogs. - * @param err The error to show the user. + * + * @param parent + * The parent component for dialogs. + * @param title + * The title for dialogs. + * @param err + * The error to show the user. */ public static void showError(Component parent, String title, String err) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java index 220ac7d..518805b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java @@ -1,7 +1,5 @@ package bjc.utils.gui; -import java.util.List; - import javax.swing.DefaultListModel; import javax.swing.JList; import javax.swing.ListModel; @@ -17,7 +15,7 @@ public class SimpleJList { * @param ls The list to populate the JList with. * @return A JList populated with the elements from ls. */ - public static <E> JList<E> buildFromList(List<E> ls) { + public static <E> JList<E> buildFromList(Iterable<E> ls) { return new JList<E>(buildModel(ls)); } @@ -26,7 +24,7 @@ public class SimpleJList { * @param ls The list to fill the list model from. * @return A list model populated with the elements from ls. */ - public static <E> ListModel<E> buildModel(List<E> ls) { + public static <E> ListModel<E> buildModel(Iterable<E> ls) { DefaultListModel<E> dlm = new DefaultListModel<>(); ls.forEach(dlm::addElement); diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java new file mode 100644 index 0000000..fb857eb --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java @@ -0,0 +1,37 @@ +package bjc.utils.gui.awt; + +import java.io.File; +import java.io.FilenameFilter; +import java.util.ArrayList; +import java.util.List; + +import bjc.utils.funcdata.FunctionalList; + +public class ExtensionFileFilter implements FilenameFilter { + private FunctionalList<String> extensions; + + /** + * Create a new filter only showing files with the specified extensions. + * @param exts The extensions to show in this filter. + */ + public ExtensionFileFilter(String... exts) { + extensions = new FunctionalList<>(new ArrayList<>(exts.length)); + + for (String ext : exts) { + extensions.add(ext); + } + } + + /** + * Create a new filter only showing files with the specified extensions. + * @param exts The extensions to show in this filter. + */ + public ExtensionFileFilter(List<String> exts) { + extensions = new FunctionalList<>(exts); + } + + @Override + public boolean accept(File dir, String name) { + return extensions.anyMatch(name::endsWith); + } +}
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java new file mode 100644 index 0000000..1d14903 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java @@ -0,0 +1,58 @@ +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; + +public class SimpleFileDialog { + public static File getOpenFile(Frame par, String title, + String... extensions) { + FileDialog fd = new FileDialog(par, title, FileDialog.LOAD); + + if (extensions != null) { + FilenameFilter filter = new ExtensionFileFilter(extensions); + fd.setFilenameFilter(filter); + } + + fd.setVisible(true); + + while (fd.getFile() == null) { + SimpleDialogs.showError(par, "File I/O Error", + "Please choose a file to open."); + fd.setVisible(true); + } + + return fd.getFiles()[0]; + } + + public static File getOpenFile(Frame par, String title) { + return getOpenFile(par, title, (String[]) null); + } + + public static File getSaveFile(Frame par, String title, + String... extensions) { + FileDialog fd = new FileDialog(par, title, FileDialog.SAVE); + + if (extensions != null) { + FilenameFilter filter = new ExtensionFileFilter(extensions); + fd.setFilenameFilter(filter); + } + + fd.setVisible(true); + + while (fd.getFile() == null) { + SimpleDialogs.showError(par, "File I/O Error", + "Please choose a file to save to."); + fd.setVisible(true); + } + + return fd.getFiles()[0]; + } + + public static File getSaveFile(Frame par, String title) { + return getSaveFile(par, title, (String[]) null); + } +} |
