1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.misc.gui;
17
18 import james.SimSystem;
19
20 import java.awt.BorderLayout;
21 import java.awt.Component;
22 import java.awt.Font;
23 import java.awt.Toolkit;
24 import java.awt.Window;
25 import java.util.List;
26 import java.util.logging.Level;
27
28 import javax.swing.DefaultListModel;
29 import javax.swing.JComponent;
30 import javax.swing.JFileChooser;
31 import javax.swing.JLabel;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34
35 import p3j.gui.P3J;
36
37 import com.jgoodies.forms.layout.CellConstraints;
38
39
40
41
42
43
44
45
46 public final class GUI {
47
48
49 public static final int STD_LAYOUT_GAP = 5;
50
51
52 private static final int FONT_SIZE_WAIT_MSG = 25;
53
54
55
56
57
58 public static final int ROW_SKIP_LAYOUT = 2;
59
60
61
62
63
64 public static final int KEYS_COLUMN_INDEX = 2;
65
66
67
68
69
70 public static final int INPUT_COLUMN_INDEX = 4;
71
72
73
74
75
76
77 private static boolean headless = false;
78
79
80
81
82 private GUI() {
83 }
84
85
86
87
88
89
90 public static BorderLayout getStdBorderLayout() {
91 return new BorderLayout(STD_LAYOUT_GAP, STD_LAYOUT_GAP);
92 }
93
94
95
96
97
98
99
100 public static void centerOnScreen(Window window) {
101 int locationX = (int) Math.round((Toolkit.getDefaultToolkit()
102 .getScreenSize().getWidth() - window.getWidth()) / 2);
103 int locationY = (int) Math.round((Toolkit.getDefaultToolkit()
104 .getScreenSize().getHeight() - window.getHeight()) / 2);
105 window.setLocation(locationX, locationY);
106 }
107
108
109
110
111
112
113
114 public static void setHeadless(boolean headless) {
115 GUI.headless = headless;
116 }
117
118
119
120
121
122
123 public static boolean isHeadless() {
124 return GUI.headless;
125 }
126
127
128
129
130
131
132
133
134
135
136
137 public static void printErrorMessage(Component parent, String title,
138 Object message) {
139 if (isHeadless()) {
140 return;
141 }
142 JOptionPane.showMessageDialog(parent, message, title,
143 JOptionPane.ERROR_MESSAGE);
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public static void printErrorMessage(Component parent, String title,
159 Object message, Throwable throwable) {
160 SimSystem.report(Level.SEVERE, title, throwable);
161 printErrorMessage(parent, title, message);
162 }
163
164
165
166
167
168
169
170
171
172
173 public static void printErrorMessage(String title, Throwable throwable) {
174 SimSystem.report(Level.SEVERE, title, throwable);
175 printErrorMessage(P3J.getInstance(), title, throwable.getMessage(),
176 throwable);
177 }
178
179
180
181
182
183
184
185
186
187
188
189 public static void printMessage(Component parent, String title, Object message) {
190 if (isHeadless()) {
191 return;
192 }
193 JOptionPane.showMessageDialog(parent, message, title,
194 JOptionPane.INFORMATION_MESSAGE);
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209 public static boolean printQuestion(Component parent, String title,
210 Object message) {
211
212 if (isHeadless()) {
213 throw new UnsupportedOperationException(
214 "Cannot print question while in head-less mode.");
215 }
216
217 int decision = JOptionPane.showConfirmDialog(parent, message, title,
218 JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
219
220 if (decision == JOptionPane.YES_OPTION) {
221 return true;
222 }
223
224 return false;
225 }
226
227
228
229
230
231
232
233
234
235 public static void replaceListContents(DefaultListModel model,
236 List<?> newContent) {
237 model.clear();
238 if (newContent != null) {
239 for (Object element : newContent) {
240 model.addElement(element);
241 }
242 }
243 }
244
245
246
247
248
249
250
251
252
253 public static JFileChooser getDirectoryChooser(String dialogTitle) {
254 JFileChooser fileChooser = new JFileChooser();
255 fileChooser.setMultiSelectionEnabled(false);
256 fileChooser.setDialogTitle(dialogTitle);
257 fileChooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
258 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
259 return fileChooser;
260 }
261
262
263
264
265
266
267 public static JLabel getLabelToWait() {
268 JLabel msg = new JLabel("This may take a while. Please be patient...");
269 msg.setFont(new Font("Sans", Font.BOLD, FONT_SIZE_WAIT_MSG));
270 return msg;
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 public static int addRowToPanel(JPanel panel, String key, JComponent input,
287 int currentRow) {
288 CellConstraints c = new CellConstraints();
289 panel.add(new JLabel(key), c.xy(KEYS_COLUMN_INDEX, currentRow));
290 panel.add(input, c.xy(INPUT_COLUMN_INDEX, currentRow));
291 return currentRow + ROW_SKIP_LAYOUT;
292 }
293 }