jsugar

Added new methods, geared towards presenting dialogs to the user. Consult the JavaDoc for detailed explanation of their behavior and functionality.

Author
Vngngdn
Date
July 14, 2016, 2:35 p.m.
Hash
f17774d8fa11121751d81e9bc6a701e3e00fe276
Parent
eeef80237f004b52370f8eef5dabe6c5abc2dc67
Modified file
Main.java

Main.java

85 additions and 0 deletions.

View changes Hide changes
1
1
 * Main.java - (Temporary) Main file in which the functionality is put.
2
2
 * Copyright © 2016 Maarten "Vngngdn" Vangeneugden
3
3
 * 
4
4
 * This program is free software: you can redistribute it and/or modify
5
5
 * it under the terms of the GNU General Public License as published by
6
6
 * the Free Software Foundation, either version 3 of the License, or
7
7
 * (at your option) any later version.
8
8
 * 
9
9
 * This program is distributed in the hope that it will be useful,
10
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
12
 * GNU General Public License for more details.
13
13
 * 
14
14
 * You should have received a copy of the GNU General Public License
15
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
16
 */
17
17
18
18
/**
19
19
 * Main class for the program.
20
20
 *
21
21
 * In this class, all parts of the program are being brought together, and their
22
22
 * functionality is 'activated', if you will.
23
23
 * @author Maarten Vangeneugden
24
24
 */
25
25
import javax.swing.*; // FIXME: Maybe namespacing it to "javax.swing;" is a better idea.
26
26
import java.util.NoSuchElementException;
27
27
28
28
public class Main {
29
29
	private JPanel panel;
30
30
31
31
	/**
32
32
	 * Creates a button in the GUI for interaction.
33
33
	 * This function offers a convenient way to create a button, that can be
34
34
	 * directly interacted with by the user. After creation, the button itself
35
35
	 * is returned to the caller, if he wishes to do something else with it.
36
36
	 * @param text The text that will be displayed in the button.
37
37
	 * @param action The action that will be returned to the action listener.
38
38
	 * @return The button that was created.
39
39
	 */
40
40
	public JButton createButton(String text, String action) {
41
41
		JButton button = new JButton(text);
42
42
		button.setActionCommand(action);
43
43
		this.panel.add(button);
44
44
		return button;
45
45
	}
46
46
47
47
	/**
+
48
	 * Ask the user for input through a dialog box.
+
49
	 * This method presents the user with an input field, that can accept
+
50
	 * textual input. The method will return the given input after the user's
+
51
	 * clicked a button to send.
+
52
	 * @param text The text/question to be asked to the user.
+
53
	 * @return A String, equal to what the user entered.
+
54
	 * @throws NullPointerException if text is a null pointer.
+
55
	 */
+
56
	public String inputDialog(String text) {
+
57
		if (text == null) {
+
58
			throw new NullPointerException("The given text/question was a null pointer.");
+
59
		}
+
60
		return JOptionPane.showInputDialog(text);
+
61
	}
+
62
+
63
	/**
+
64
	 * Give the user a dialog box.
+
65
	 * This method can be used to provide a simple dialog to the user.
+
66
	 * This will show the user the given question, after which a boolean value
+
67
	 * is returned, holding the choice.
+
68
	 * @param text The text/question to be asked to the user.
+
69
	 * @return True if the user confirms, False if he denies.
+
70
	 * @throws NullPointerException if text is a null pointer.
+
71
	 */
+
72
	public boolean confirmDialog(String text) {
+
73
		if (text == null) {
+
74
			throw new NullPointerException("The given text/question was a null pointer.");
+
75
		}
+
76
		final int ACCEPTED = 0;
+
77
		final int DENIED = 1;
+
78
		int result = this.choiceDialog(text, new String[]{"Confirm", "Deny"});
+
79
		if (result == ACCEPTED) {
+
80
			return true;
+
81
		}
+
82
		else {
+
83
			return false;
+
84
		}
+
85
	}
+
86
+
87
	/**
+
88
	 * Give the user a choice dialog box.
+
89
	 * This method gives the user a simple dialog with predefined choices.
+
90
	 * These choices are to be provided by the caller in a simple array.
+
91
	 * Tip: This method works extremely well with arbitrary created choices.
+
92
	 * That is: if the outcome of the dialog is trivial (e.g. Only 1 choice),
+
93
	 * then that value is immediately returned.
+
94
	 * @param text The text/question to be asked to the user.
+
95
	 * @param choices An array of Strings, containing the choices the user can
+
96
	 * pick.
+
97
	 * @return The index value of the picked choice, or -1 if no choices were
+
98
	 * given.
+
99
	 * @throws NullPointerException if text is a null pointer.
+
100
	 */
+
101
	public int choiceDialog(String text, String[] choices) {
+
102
		if (text == null) {
+
103
			throw new NullPointerException("The given text/question was a null pointer.");
+
104
		}
+
105
		// First: handling the trivial cases:
+
106
		if (choices.length == 0) {
+
107
			return -1;
+
108
		}
+
109
		else if (choices.length == 1) {
+
110
			return 0;
+
111
		}
+
112
		int answer = JOptionPane.CLOSED_OPTION;
+
113
		// The dialog needs to be shown again until the user has made a possible
+
114
		// choice, i.e. Chickening out using the close button is not possible
+
115
		// (Because that returns CLOSED_OPTION).
+
116
		while (answer == JOptionPane.CLOSED_OPTION) {
+
117
				JOptionPane.showOptionDialog(
+
118
					null, // The parent component. May become the panel?
+
119
					text, // The text/question to describe the goal
+
120
					"Dialog", // The text in the title bar
+
121
					JOptionPane.DEFAULT_OPTION, // The kind of available options
+
122
					JOptionPane.QUESTION_MESSAGE, // The type of message
+
123
					null, // The icon to show
+
124
					choices, // The possible choices
+
125
					choices[0] // The standard choice
+
126
					);
+
127
		}
+
128
		return answer;
+
129
	}
+
130
		
+
131
+
132
	/**
48
133
	 * Creates a label in the GUI for interaction.
49
134
	 * This function offers a convenient way to create a label, that can be
50
135
	 * directly interacted with by the user. After creation, the label itself
51
136
	 * is returned to the caller, if he wishes to do something else with it.
52
137
	 * @param text The text that will be displayed in the label.
53
138
	 * @return The label that was created.
54
139
	 */
55
140
	public JLabel createLabel(String text) {
56
141
		JLabel label = new JLabel(text);
57
142
		this.panel.add(label);
58
143
		return label;
59
144
	}
60
145
61
146
	/**
62
147
	 * Removes the given component from the GUI.
63
148
	 * This method allows its caller to remove a component from the GUI.
64
149
	 * @param component The component to be removed.
65
150
	 * @throws NoSuchElementException if the given component does not exist in
66
151
	 * the GUI.
67
152
	 * @throws NullPointerException if the given component is a null pointer.
68
153
	 */
69
154
	public void removeComponent(JComponent component) {
70
155
		int originalSize = this.panel.getComponentCount();
71
156
		this.panel.remove(component);
72
157
		int newSize = this.panel.getComponentCount();
73
158
		if (originalSize != newSize+1) {
74
159
			throw new NoSuchElementException("The given component does not exist in the GUI.");
75
160
		}
76
161
	}
77
162
	/**
78
163
	 * Main function.
79
164
	 * 
80
165
	 * This is the first function that is being called in the program.
81
166
	 */
82
167
	public static void main(String[] args) {
83
168
		// Creat©s2016eyournamewith a double buffer and flow layout.
84
169
		JPanel panel = new JPanel();
85
170
		JFrame frame = new JFrame("Hello world!");
86
171
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
87
172
		JLabel lblHelloWorld = new JLabel("Hello world... :D");
88
173
		//frame.getContentPane().add(lblHelloWorld); // So you use a get() in order to set() data? #JavaWTF
89
174
		panel.add(lblHelloWorld);
90
175
		frame.setContentPane(panel);
91
176
92
177
		frame.pack();
93
178
		frame.setVisible(true);
94
179
	}
95
180
}
96
181