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 | |
| parent | e528aec6d2d277338d7ddfdceba38d62eff08657 (diff) | |
GUI stuff.
Almost finished with imports from version one.
Diffstat (limited to 'BJC-Utils2/src/main')
10 files changed, 473 insertions, 1 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java index dc57fb5..73ace88 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -99,7 +99,7 @@ public class FunctionalList<E> { * @param p The predicate to use for checking. * @return Whether any element in the list matches the provided predicate. */ - public boolean anyMatches(Predicate<E> p) { + public boolean anyMatch(Predicate<E> p) { for (E item : wrap) { if (p.test(item)) { return true; diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java b/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java new file mode 100644 index 0000000..9520492 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java @@ -0,0 +1,48 @@ +package bjc.utils.gui; + +import java.io.File; +import javax.swing.filechooser.FileFilter; +import java.util.ArrayList; +import java.util.List; + +import bjc.utils.funcdata.FunctionalList; + +/** + * A file filter based on extensions. + * @author ben + * + */ +public class ExtensionFileFilter extends FileFilter { + 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 pathname) { + return extensions.anyMatch(pathname.getName()::endsWith); + } + + @Override + public String getDescription() { + return extensions.toString(); + } + +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java b/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java new file mode 100644 index 0000000..565d705 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java @@ -0,0 +1,13 @@ +package bjc.utils.gui; + +import java.io.IOException; + +/** + * Represents the user not choosing a file. + * @author ben + * + */ +public class FileNotChosenException extends IOException { + private static final long serialVersionUID = -8753348705210831096L; + +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java new file mode 100644 index 0000000..7731529 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java @@ -0,0 +1,127 @@ +package bjc.utils.gui; + +import java.awt.Component; +import java.util.function.Function; +import java.util.function.Predicate; + +import javax.swing.JOptionPane; + +/** + * 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. + * @return A int within the specified bounds. + */ + public static int getBoundedInt(Component parent, String title, + String prompt, int lower, int upper) { + return getValue(parent, title, prompt, s -> { + try { + int n = Integer.parseInt(s); + return (n < upper) && (n > lower); + } catch (NumberFormatException nfe) { + return false; + } + } , Integer::parseInt); + } + + /** + * 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. + * @return A int. + */ + public static int getInt(Component parent, String title, + String prompt) { + return getValue(parent, title, prompt, s -> { + try { + Integer.parseInt(s); + return true; + } catch (NumberFormatException nfe) { + return false; + } + } , Integer::parseInt); + } + + /** + * 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. + * @return A string. + */ + 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. + * @return The value parsed from a string. + */ + public static <E> E getValue(Component parent, String title, + String prompt, Predicate<String> p, Function<String, E> f) { + String inp = getString(parent, title, prompt); + + while (!p.test(inp)) { + showError(parent, "I/O Error", "Please enter a valid value"); + + inp = getString(parent, title, prompt); + } + + return f.apply(inp); + } + + /** + * 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. + * @return A whole number. + */ + public static int getWhole(Component parent, String title, + String prompt) { + return getBoundedInt(parent, title, prompt, 0, Integer.MAX_VALUE); + } + + /** + * 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. + * @return True if the user said yes, false otherwise. + */ + public static boolean getYesNo(Component parent, String title, + String question) { + int res = JOptionPane.showConfirmDialog(parent, question, title, + JOptionPane.YES_NO_OPTION); + + return (res == JOptionPane.YES_OPTION ? true : false); + } + + /** + * 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. + */ + public static void showError(Component parent, String title, + String err) { + JOptionPane.showMessageDialog(parent, err, title, + JOptionPane.ERROR_MESSAGE); + } +} 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); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java new file mode 100644 index 0000000..220ac7d --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java @@ -0,0 +1,36 @@ +package bjc.utils.gui; + +import java.util.List; + +import javax.swing.DefaultListModel; +import javax.swing.JList; +import javax.swing.ListModel; + +/** + * Utility class for making JLists and their models. + * @author ben + * + */ +public class SimpleJList { + /** + * Create a new JList from a given list. + * @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) { + return new JList<E>(buildModel(ls)); + } + + /** + * Create a new list model from a given list. + * @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) { + DefaultListModel<E> dlm = new DefaultListModel<>(); + + ls.forEach(dlm::addElement); + + return dlm; + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java new file mode 100644 index 0000000..aa19566 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java @@ -0,0 +1,22 @@ +package bjc.utils.gui; + +import javax.swing.border.EtchedBorder; +import javax.swing.border.TitledBorder; + +/** + * A simple border with a title attached to it. + * @author ben + * + */ +public class SimpleTitledBorder extends TitledBorder { + private static final long serialVersionUID = -5655969079949148487L; + + /** + * Create a new border with the specified title. + * @param title The title for the border. + */ + public SimpleTitledBorder(String title) { + super(new EtchedBorder(), title); + } + +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/layout/AutosizeLayout.java b/BJC-Utils2/src/main/java/bjc/utils/gui/layout/AutosizeLayout.java new file mode 100644 index 0000000..ae470d5 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/layout/AutosizeLayout.java @@ -0,0 +1,19 @@ +package bjc.utils.gui.layout; + +import java.awt.GridLayout; + +/** + * A layout that simply holds one component that it auto-resizes whenever it is resized. + * @author ben + * + */ +public class AutosizeLayout extends GridLayout { + private static final long serialVersionUID = -2495693595953396924L; + + /** + * Create a new auto-size layout. + */ + public AutosizeLayout() { + super(1, 1); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/layout/HLayout.java b/BJC-Utils2/src/main/java/bjc/utils/gui/layout/HLayout.java new file mode 100644 index 0000000..fd049fc --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/layout/HLayout.java @@ -0,0 +1,20 @@ +package bjc.utils.gui.layout; + +import java.awt.GridLayout; + +/** + * A layout manager that lays out its components horizontally, evenly sizing them. + * @author ben + * + */ +public class HLayout extends GridLayout { + private static final long serialVersionUID = 1244964456966270026L; + + /** + * Create a new horizontal layout with the specified number of columns. + * @param cols The number of columns in this layout. + */ + public HLayout(int cols) { + super(1, cols); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/layout/VLayout.java b/BJC-Utils2/src/main/java/bjc/utils/gui/layout/VLayout.java new file mode 100644 index 0000000..953e522 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/layout/VLayout.java @@ -0,0 +1,20 @@ +package bjc.utils.gui.layout; + +import java.awt.GridLayout; + +/** + * A layout that lays out its components vertically, evenly sharing space among them. + * @author ben + * + */ +public class VLayout extends GridLayout { + private static final long serialVersionUID = -6417962941602322663L; + + /** + * Create a new vertical layout with the specified number of rows. + * @param rows The number of rows. + */ + public VLayout(int rows) { + super(rows, 1); + } +} |
