summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/gui/SimpleDialogs.java
blob: b7763a26ea36a6e906bfa2e43f1ff2368c2bd328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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 lowerBound
	 *        The lower integer bound to accept.
	 * @param upperBound
	 *        The upper integer bound to accept.
	 * @return A int within the specified bounds.
	 */
	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 {
				final int value = Integer.parseInt(strang);

				return value < upperBound && value > lowerBound;
			} catch(final NumberFormatException nfex) {
				// We don't care about the specifics of the
				// exception, just
				// that this value isn't good
				return false;
			}
		}, Integer::parseInt);
	}

	/**
	 * Asks the user to pick an option from a series of choices.
	 *
	 * @param <E>
	 *        The type of choices for the user to pick
	 *
	 * @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(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)
			throw new NullPointerException("Title must not be null");
		else if(question == null) throw new NullPointerException("Question must not be null");

		final JDialog chooser = new JDialog(parent, title, true);
		chooser.setLayout(new VLayout(2));

		final JPanel questionPane = new JPanel();

		final JLabel questionText = new JLabel(question);
		final JComboBox<E> questionChoices = new JComboBox<>(choices);

		questionPane.add(questionText);
		questionPane.add(questionChoices);

		final JPanel buttonPane = new JPanel();

		final JButton okButton = new JButton("Ok");
		final JButton cancelButton = new JButton("Cancel");

		okButton.addActionListener((event) -> chooser.dispose());
		cancelButton.addActionListener((event) -> chooser.dispose());

		buttonPane.add(cancelButton);
		buttonPane.add(okButton);

		chooser.add(questionPane);
		chooser.add(buttonPane);

		chooser.pack();
		chooser.setVisible(true);

		return (E) questionChoices.getSelectedItem();
	}

	/**
	 * 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(final Component parent, final String title, final String prompt) {
		return getValue(parent, title, prompt, strang -> {
			try {
				Integer.parseInt(strang);
				return true;
			} catch(final NumberFormatException nfex) {
				// We don't care about this exception, just mark
				// the value
				// as not good
				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(final Component parent, final String title, final String prompt) {
		if(parent == null)
			throw new NullPointerException("Parent must not be 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");

		return JOptionPane.showInputDialog(parent, prompt, title, JOptionPane.QUESTION_MESSAGE);
	}

	/**
	 * Get a value parsable from a string from the user.
	 *
	 * @param <E>
	 *        The type of the value parsed from the string
	 *
	 * @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 validator
	 *        A predicate to determine if a input is valid.
	 * @param transformer
	 *        The function to transform the string into a value.
	 * @return The value parsed from a string.
	 */
	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");

		String input = getString(parent, title, prompt);

		while(!validator.test(input)) {
			showError(parent, "I/O Error", "Please enter a valid value");

			input = getString(parent, title, prompt);
		}

		return transformer.apply(input);
	}

	/**
	 * 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(final Component parent, final String title, final 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(final Component parent, final String title, final String question) {
		if(parent == null)
			throw new NullPointerException("Parent must not be 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");

		final int result = JOptionPane.showConfirmDialog(parent, question, title, JOptionPane.YES_NO_OPTION);

		return result == 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 message
	 *        The error to show the user.
	 */
	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)
			throw new NullPointerException("Title 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);
	}

	/**
	 * Show an informative message to the user
	 *
	 * @param parent
	 *        The parent for this dialog
	 * @param title
	 *        Show the title for this dialog
	 * @param message
	 *        Show the message for this dialog
	 */
	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)
			throw new NullPointerException("Title must not be null");
		else if(message == null) throw new NullPointerException("Message must not be null");

		JOptionPane.showMessageDialog(parent, title, message, JOptionPane.INFORMATION_MESSAGE);
	}
}