From fded830f3d1aff4df4a639b6e5701c3668a61809 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Mon, 6 Apr 2020 16:49:29 -0400 Subject: Update UI controls Add an additional UI control (a button that has a keystroke bound to it), and add the ability to get a directory picker from SimpleFileChooser --- .../main/java/bjc/utils/gui/SimpleFileChooser.java | 22 ++++++ .../main/java/bjc/utils/gui/SimpleKeyedButton.java | 81 ++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 base/src/main/java/bjc/utils/gui/SimpleKeyedButton.java (limited to 'base/src/main/java') diff --git a/base/src/main/java/bjc/utils/gui/SimpleFileChooser.java b/base/src/main/java/bjc/utils/gui/SimpleFileChooser.java index 2bdd792..01cd37f 100644 --- a/base/src/main/java/bjc/utils/gui/SimpleFileChooser.java +++ b/base/src/main/java/bjc/utils/gui/SimpleFileChooser.java @@ -195,4 +195,26 @@ public class SimpleFileChooser { return files.getSelectedFile(); } + + /** + * Show a dialog box to pick a directory. + * + * @param parent + * The component to use as the parent for the dialog. + * @param title + * The title of the dialog for prompting. + * @return The directory picked, if the user picked one; null if they didn't. + */ + public static File pickDirectory(final Component parent, final String title) { + if (title == null) throw new NullPointerException("Title must not be null"); + + final JFileChooser files = new JFileChooser(); + files.setDialogType(JFileChooser.OPEN_DIALOG); + files.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + files.setDialogTitle(title); + + files.showOpenDialog(parent); + + return files.getSelectedFile(); + } } diff --git a/base/src/main/java/bjc/utils/gui/SimpleKeyedButton.java b/base/src/main/java/bjc/utils/gui/SimpleKeyedButton.java new file mode 100644 index 0000000..031cee3 --- /dev/null +++ b/base/src/main/java/bjc/utils/gui/SimpleKeyedButton.java @@ -0,0 +1,81 @@ +package bjc.utils.gui; + +import java.awt.event.ActionEvent; +import java.util.function.Consumer; + +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.JButton; +import javax.swing.KeyStroke; + +/** + * Simple {@link JButton} that provides an easy way to add a key-map to it. + * + * @author Ben Culkin + * + */ +public class SimpleKeyedButton extends JButton { + private final class KeyedButtonAction extends AbstractAction { + private static final long serialVersionUID = 5854121457339741391L; + + private final Consumer aevListener; + + private KeyedButtonAction(String name, Consumer aevListener) { + super(name); + this.aevListener = aevListener; + } + + @Override + public void actionPerformed(ActionEvent aev) { + aevListener.accept(aev); + } + } + + private static final long serialVersionUID = -8550504153221678178L; + + private String label; + + /** + * Create a new keyed button. + * + * @param label The label for the button. + */ + public SimpleKeyedButton(String label) { + super(label); + + this.label = label; + } + + /** + * Sets the default action for this button, and installs a global (WHEN_IN_FOCUSED_WINDOW) keystroke handler for it. + * + * @param eventName An unique internal name for the event. + * @param keystroke The keystroke for this event, passed to {@link KeyStroke#getKeyStroke(String)}. + * @param aevListener The listener that handles the implementation of the action. + */ + public void setGlobalDefaultKeystroke(String eventName, String keystroke, Consumer aevListener) { + Action act = new KeyedButtonAction(eventName, aevListener); + KeyStroke stroke = KeyStroke.getKeyStroke(keystroke); + + this.setAction(act); + this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(stroke, eventName); + this.getActionMap().put(eventName, act); + this.setText(label); + } + + /** + * Installs a global (WHEN_IN_FOCUSED_WINDOW) keystroke handler for an action. + * + * @param eventName An unique internal name for the event. + * @param keystroke The keystroke for this event, passed to {@link KeyStroke#getKeyStroke(String)}. + * @param aevListener The listener that handles the implementation of the action. + */ + public void addGlobalKeystroke(String eventName, String keystroke, Consumer aevListener) { + Action act = new KeyedButtonAction(eventName, aevListener); + KeyStroke stroke = KeyStroke.getKeyStroke(keystroke); + + this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(stroke, eventName); + this.getActionMap().put(eventName, act); + this.setText(label); + } +} -- cgit v1.2.3