diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc')
12 files changed, 369 insertions, 146 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java b/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java index 9520492..7212e61 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java @@ -9,15 +9,35 @@ import bjc.utils.funcdata.FunctionalList; /** * A file filter based on extensions. + * + * Built for Swing. + * * @author ben * */ public class ExtensionFileFilter extends FileFilter { + /** + * The list holding all filtered extensions + */ private FunctionalList<String> extensions; /** - * Create a new filter only showing files with the specified extensions. - * @param exts The extensions to show in this filter. + * 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); + } + + /** + * 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)); @@ -27,14 +47,6 @@ public class ExtensionFileFilter extends FileFilter { } } - /** - * 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); diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java b/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java index 565d705..b972596 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java @@ -3,7 +3,7 @@ package bjc.utils.gui; import java.io.IOException; /** - * Represents the user not choosing a file. + * Represents the user failing to choose a file. * @author ben * */ diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java new file mode 100644 index 0000000..3cdfd47 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java @@ -0,0 +1,95 @@ +package bjc.utils.gui; + +import java.util.function.Consumer; +import java.util.function.Supplier; + +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.ListSelectionModel; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.gui.layout.HLayout; +import bjc.utils.gui.layout.VLayout; + +/** + * A panel that has a list of objects and ways of manipulating that list + * + * @author ben + * + * @param <E> + * The type of data stored in the list + */ +public class ListParameterPanel<E> extends JPanel { + /** + * Version id for serialization + */ + private static final long serialVersionUID = 3442971104975491571L; + + /** + * Create a new panel using the specified actions for doing things + * + * @param addAct + * The action that provides items + * @param editAct + * The action that edits items + * @param removeAct + * The action that removes items + */ + public ListParameterPanel(Supplier<E> addAct, Consumer<E> editAct, + Consumer<E> removeAct) { + this(addAct, editAct, removeAct, null); + } + + /** + * Create a new panel using the specified actions for doing things + * + * @param addAct + * The action that provides items + * @param editAct + * The action that edits items + * @param removeAct + * The action that removes items + * @param defVals + * The default values to put in the list + */ + public ListParameterPanel(Supplier<E> addAct, Consumer<E> editAct, + Consumer<E> removeAct, FunctionalList<E> defVals) { + setLayout(new VLayout(2)); + + JList<E> list; + + if (defVals != null) { + list = SimpleJList.buildFromList(defVals.toIterable()); + } else { + list = new JList<>(new DefaultListModel<>()); + } + + list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + JPanel buttonPanel = new JPanel(); + buttonPanel.setLayout(new HLayout(3)); + + JButton addParam = new JButton("Add..."); + addParam.addActionListener( + (ev) -> ((DefaultListModel<E>) list.getModel()) + .addElement(addAct.get())); + + JButton editParam = new JButton("Edit..."); + editParam.addActionListener( + (ev) -> editAct.accept(list.getSelectedValue())); + + JButton removeParam = new JButton("Remove..."); + removeParam.addActionListener((ev) -> removeAct + .accept(((DefaultListModel<E>) list.getModel()) + .remove(list.getSelectedIndex()))); + + buttonPanel.add(addParam); + buttonPanel.add(editParam); + buttonPanel.add(removeParam); + + add(list); + add(buttonPanel); + } +} 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 85b9fa9..ea48977 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java @@ -49,6 +49,52 @@ public class SimpleDialogs { } /** + * 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 available 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(); + } + + /** * Get a integer from the user * * @param parent @@ -152,52 +198,6 @@ public class SimpleDialogs { } /** - * 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 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(); } } 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 518805b..8bca490 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java @@ -6,13 +6,16 @@ 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. + * + * @param ls + * The list to populate the JList with. * @return A JList populated with the elements from ls. */ public static <E> JList<E> buildFromList(Iterable<E> ls) { @@ -21,14 +24,16 @@ public class SimpleJList { /** * Create a new list model from a given list. - * @param ls The list to fill the list model from. + * + * @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(Iterable<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 index aa19566..9c6feef 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java @@ -5,15 +5,21 @@ import javax.swing.border.TitledBorder; /** * A simple border with a title attached to it. + * * @author ben * */ public class SimpleTitledBorder extends TitledBorder { + /** + * Version ID for serialization + */ private static final long serialVersionUID = -5655969079949148487L; /** * Create a new border with the specified title. - * @param title The title for the border. + * + * @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/awt/ExtensionFileFilter.java b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java index fb857eb..6f43ba9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java @@ -7,12 +7,37 @@ import java.util.List; import bjc.utils.funcdata.FunctionalList; +/** + * Filter a set of filenames by extension. + * + * Built for AWT + * + * @author ben + * + */ public class ExtensionFileFilter implements FilenameFilter { + /** + * The list of extensions to filter + */ private FunctionalList<String> extensions; /** - * Create a new filter only showing files with the specified extensions. - * @param exts The extensions to show in this filter. + * 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); + } + + /** + * 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)); @@ -22,14 +47,6 @@ public class ExtensionFileFilter implements FilenameFilter { } } - /** - * 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); 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 index 1d14903..2e13949 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java @@ -7,7 +7,26 @@ 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 par + * 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(Frame par, String title, String... extensions) { FileDialog fd = new FileDialog(par, title, FileDialog.LOAD); @@ -28,10 +47,30 @@ public class SimpleFileDialog { return fd.getFiles()[0]; } + /** + * Prompt the user to pick a file to open + * + * @param par + * The parent of the file picker + * @param title + * The title of the file picker + * @return The file the user picked + */ public static File getOpenFile(Frame par, String title) { return getOpenFile(par, title, (String[]) null); } - + + /** + * Prompt the user to pick a file to save + * + * @param par + * 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(Frame par, String title, String... extensions) { FileDialog fd = new FileDialog(par, title, FileDialog.SAVE); @@ -52,6 +91,15 @@ public class SimpleFileDialog { return fd.getFiles()[0]; } + /** + * Prompt the user to pick a file to save + * + * @param par + * The parent of the file picker + * @param title + * The title of the file picker + * @return The file the user picked + */ public static File getSaveFile(Frame par, String title) { return getSaveFile(par, title, (String[]) null); } 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 index ae470d5..0130eee 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/layout/AutosizeLayout.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/layout/AutosizeLayout.java @@ -3,11 +3,16 @@ package bjc.utils.gui.layout; import java.awt.GridLayout; /** - * A layout that simply holds one component that it auto-resizes whenever it is resized. + * A layout that simply holds one component that it auto-resizes whenever + * it is resized. + * * @author ben * */ public class AutosizeLayout extends GridLayout { + /** + * Version id for serialization + */ private static final long serialVersionUID = -2495693595953396924L; /** 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 index fd049fc..83b3793 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/layout/HLayout.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/layout/HLayout.java @@ -3,16 +3,23 @@ package bjc.utils.gui.layout; import java.awt.GridLayout; /** - * A layout manager that lays out its components horizontally, evenly sizing them. + * A layout manager that lays out its components horizontally, evenly + * sizing them. + * * @author ben * */ public class HLayout extends GridLayout { + /** + * Version id for serialization + */ 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. + * + * @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 index 953e522..23ab61e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/layout/VLayout.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/layout/VLayout.java @@ -3,16 +3,23 @@ package bjc.utils.gui.layout; import java.awt.GridLayout; /** - * A layout that lays out its components vertically, evenly sharing space among them. + * A layout that lays out its components vertically, evenly sharing space + * among them. + * * @author ben * */ public class VLayout extends GridLayout { + /** + * Version id for serialization + */ private static final long serialVersionUID = -6417962941602322663L; /** * Create a new vertical layout with the specified number of rows. - * @param rows The number of rows. + * + * @param rows + * The number of rows. */ public VLayout(int rows) { super(rows, 1); |
