summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java84
1 files changed, 43 insertions, 41 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 bb14327..59eb1c3 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java
@@ -1,7 +1,5 @@
package bjc.utils.gui;
-import bjc.utils.gui.layout.VLayout;
-
import java.awt.Component;
import java.awt.Frame;
import java.util.function.Function;
@@ -14,6 +12,8 @@ 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.
*
@@ -36,13 +36,14 @@ public class SimpleDialogs {
* The upper integer bound to accept.
* @return A int within the specified bounds.
*/
- public static int getBoundedInt(Component parent, String title, String prompt, int lowerBound, int upperBound) {
+ public static int getBoundedInt(final Component parent, final String title, final String prompt,
+ final int lowerBound, final int upperBound) {
return getValue(parent, title, prompt, (strang) -> {
try {
- int value = Integer.parseInt(strang);
+ final int value = Integer.parseInt(strang);
return value < upperBound && value > lowerBound;
- } catch(NumberFormatException nfex) {
+ } catch (final NumberFormatException nfex) {
// We don't care about the specifics of the
// exception, just
// that this value isn't good
@@ -68,28 +69,29 @@ public class SimpleDialogs {
* @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) {
- if(parent == null)
+ public static <E> E getChoice(final Frame parent, final String title, final String question,
+ final E... choices) {
+ if (parent == null)
throw new NullPointerException("Parent must not be null");
- else if(title == null)
+ else if (title == null)
throw new NullPointerException("Title must not be null");
- else if(question == null) throw new NullPointerException("Question must not be null");
+ else if (question == null) throw new NullPointerException("Question must not be null");
- JDialog chooser = new JDialog(parent, title, true);
+ final JDialog chooser = new JDialog(parent, title, true);
chooser.setLayout(new VLayout(2));
- JPanel questionPane = new JPanel();
+ final JPanel questionPane = new JPanel();
- JLabel questionText = new JLabel(question);
- JComboBox<E> questionChoices = new JComboBox<>(choices);
+ final JLabel questionText = new JLabel(question);
+ final JComboBox<E> questionChoices = new JComboBox<>(choices);
questionPane.add(questionText);
questionPane.add(questionChoices);
- JPanel buttonPane = new JPanel();
+ final JPanel buttonPane = new JPanel();
- JButton okButton = new JButton("Ok");
- JButton cancelButton = new JButton("Cancel");
+ final JButton okButton = new JButton("Ok");
+ final JButton cancelButton = new JButton("Cancel");
okButton.addActionListener((event) -> chooser.dispose());
cancelButton.addActionListener((event) -> chooser.dispose());
@@ -117,12 +119,12 @@ public class SimpleDialogs {
* The prompt to tell the user what to enter.
* @return A int.
*/
- public static int getInt(Component parent, String title, String prompt) {
+ public static int getInt(final Component parent, final String title, final String prompt) {
return getValue(parent, title, prompt, strang -> {
try {
Integer.parseInt(strang);
return true;
- } catch(NumberFormatException nfex) {
+ } catch (final NumberFormatException nfex) {
// We don't care about this exception, just mark
// the value
// as not good
@@ -142,12 +144,12 @@ public class SimpleDialogs {
* The prompt to tell the user what to enter.
* @return A string.
*/
- public static String getString(Component parent, String title, String prompt) {
- if(parent == null)
+ public static String getString(final Component parent, final String title, final String prompt) {
+ if (parent == null)
throw new NullPointerException("Parent must not be null");
- else if(title == null)
+ else if (title == null)
throw new NullPointerException("Title must not be null");
- else if(prompt == null) throw new NullPointerException("Prompt must not be null");
+ else if (prompt == null) throw new NullPointerException("Prompt must not be null");
return JOptionPane.showInputDialog(parent, prompt, title, JOptionPane.QUESTION_MESSAGE);
}
@@ -170,15 +172,15 @@ public class SimpleDialogs {
* 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> validator,
- Function<String, E> transformer) {
- if(validator == null)
+ public static <E> E getValue(final Component parent, final String title, final String prompt,
+ final Predicate<String> validator, final Function<String, E> transformer) {
+ if (validator == null)
throw new NullPointerException("Validator must not be null");
- else if(transformer == null) throw new NullPointerException("Transformer must not be null");
+ else if (transformer == null) throw new NullPointerException("Transformer must not be null");
String input = getString(parent, title, prompt);
- while(!validator.test(input)) {
+ while (!validator.test(input)) {
showError(parent, "I/O Error", "Please enter a valid value");
input = getString(parent, title, prompt);
@@ -198,7 +200,7 @@ public class SimpleDialogs {
* The prompt to tell the user what to enter.
* @return A whole number.
*/
- public static int getWhole(Component parent, String title, String prompt) {
+ public static int getWhole(final Component parent, final String title, final String prompt) {
return getBoundedInt(parent, title, prompt, 0, Integer.MAX_VALUE);
}
@@ -213,14 +215,14 @@ public class SimpleDialogs {
* 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) {
- if(parent == null)
+ public static boolean getYesNo(final Component parent, final String title, final String question) {
+ if (parent == null)
throw new NullPointerException("Parent must not be null");
- else if(title == null)
+ else if (title == null)
throw new NullPointerException("Title must not be null");
- else if(question == null) throw new NullPointerException("Question must not be null");
+ else if (question == null) throw new NullPointerException("Question must not be null");
- int result = JOptionPane.showConfirmDialog(parent, question, title, JOptionPane.YES_NO_OPTION);
+ final int result = JOptionPane.showConfirmDialog(parent, question, title, JOptionPane.YES_NO_OPTION);
return result == JOptionPane.YES_OPTION ? true : false;
}
@@ -235,12 +237,12 @@ public class SimpleDialogs {
* @param message
* The error to show the user.
*/
- public static void showError(Component parent, String title, String message) {
- if(parent == null)
+ public static void showError(final Component parent, final String title, final String message) {
+ if (parent == null)
throw new NullPointerException("Parent must not be null");
- else if(title == null)
+ else if (title == null)
throw new NullPointerException("Title must not be null");
- else if(message == null) throw new NullPointerException("Error message must not be null");
+ else if (message == null) throw new NullPointerException("Error message must not be null");
JOptionPane.showMessageDialog(parent, message, title, JOptionPane.ERROR_MESSAGE);
}
@@ -255,12 +257,12 @@ public class SimpleDialogs {
* @param message
* Show the message for this dialog
*/
- public static void showMessage(Component parent, String title, String message) {
- if(parent == null)
+ public static void showMessage(final Component parent, final String title, final String message) {
+ if (parent == null)
throw new NullPointerException("Parent must not be null");
- else if(title == null)
+ else if (title == null)
throw new NullPointerException("Title must not be null");
- else if(message == null) throw new NullPointerException("Message must not be null");
+ else if (message == null) throw new NullPointerException("Message must not be null");
JOptionPane.showMessageDialog(parent, title, message, JOptionPane.INFORMATION_MESSAGE);
}