1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.misc.gui;
17
18 import java.awt.BorderLayout;
19 import java.awt.Component;
20 import java.awt.Font;
21 import java.awt.Toolkit;
22 import java.awt.Window;
23 import java.util.List;
24 import java.util.logging.Level;
25
26 import javax.swing.AbstractButton;
27 import javax.swing.DefaultListModel;
28 import javax.swing.ImageIcon;
29 import javax.swing.JButton;
30 import javax.swing.JComponent;
31 import javax.swing.JDialog;
32 import javax.swing.JFileChooser;
33 import javax.swing.JLabel;
34 import javax.swing.JOptionPane;
35 import javax.swing.JPanel;
36 import javax.swing.JRadioButton;
37
38 import org.jamesii.SimSystem;
39 import org.jamesii.gui.utils.BasicUtilities;
40
41 import p3j.gui.P3J;
42
43 import com.jgoodies.forms.layout.CellConstraints;
44
45
46
47
48
49
50
51
52 public final class GUI {
53
54
55 public static final int STD_LAYOUT_GAP = 5;
56
57
58 private static final int FONT_SIZE_WAIT_MSG = 25;
59
60
61 private static final int FONT_SIZE_MEDIUM = 15;
62
63
64
65
66
67 public static final int ROW_SKIP_LAYOUT = 2;
68
69
70
71
72
73 public static final int KEYS_COLUMN_INDEX = 2;
74
75
76
77
78
79 public static final int INPUT_COLUMN_INDEX = 4;
80
81
82
83
84
85
86 private static boolean headless = false;
87
88
89
90
91 private GUI() {
92 }
93
94
95
96
97
98
99 public static BorderLayout getStdBorderLayout() {
100 return new BorderLayout(STD_LAYOUT_GAP, STD_LAYOUT_GAP);
101 }
102
103
104
105
106
107
108
109 public static void centerOnScreen(Window window) {
110 int locationX = (int) Math.round((Toolkit.getDefaultToolkit()
111 .getScreenSize().getWidth() - window.getWidth()) / 2);
112 int locationY = (int) Math.round((Toolkit.getDefaultToolkit()
113 .getScreenSize().getHeight() - window.getHeight()) / 2);
114 window.setLocation(locationX, locationY);
115 }
116
117
118
119
120
121
122
123 public static void setHeadless(boolean headless) {
124 GUI.headless = headless;
125 }
126
127
128
129
130
131
132 public static boolean isHeadless() {
133 return GUI.headless;
134 }
135
136
137
138
139
140
141
142
143
144
145
146 public static void printErrorMessage(Component parent, String title,
147 Object message) {
148 if (isHeadless()) {
149 return;
150 }
151 JOptionPane.showMessageDialog(parent, message, title,
152 JOptionPane.ERROR_MESSAGE);
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167 public static void printErrorMessage(Component parent, String title,
168 Object message, Throwable throwable) {
169 SimSystem.report(Level.SEVERE, title, throwable);
170 throwable.printStackTrace();
171 printErrorMessage(parent, title, message);
172 }
173
174
175
176
177
178
179
180
181
182
183 public static void printErrorMessage(String title, Throwable throwable) {
184 SimSystem.report(Level.SEVERE, title, throwable);
185 printErrorMessage(P3J.getInstance(), title, throwable.getMessage(),
186 throwable);
187 }
188
189
190
191
192
193
194
195
196
197
198 public static JButton createIconButton(String iconFileName, String defaultText) {
199 return decorateButtonWithIconOrText(new JButton(),
200 retrieveIcon(iconFileName), defaultText);
201 }
202
203
204
205
206
207
208
209
210
211
212 public static JRadioButton createIconRadioButton(String iconFileName,
213 String defaultText) {
214 return decorateButtonWithIconOrText(new JRadioButton(),
215 retrieveIcon(iconFileName), defaultText);
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 public static <B extends AbstractButton> B decorateButtonWithIconOrText(
232 B button, ImageIcon icon, String defaultText) {
233 if (icon.getImage() != null) {
234 button.setIcon(icon);
235 } else {
236 button.setText(defaultText);
237 }
238 return button;
239 }
240
241
242
243
244
245
246
247
248 public static ImageIcon retrieveIcon(String iconFileName) {
249 return new ImageIcon(GUI.class.getResource("/p3j/icons/" + iconFileName));
250 }
251
252
253
254
255
256
257
258
259
260
261
262 public static void printMessage(Component parent, String title, Object message) {
263 if (isHeadless()) {
264 return;
265 }
266 JOptionPane.showMessageDialog(parent, message, title,
267 JOptionPane.INFORMATION_MESSAGE);
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281
282 public static boolean printQuestion(Component parent, String title,
283 Object message) {
284
285 if (isHeadless()) {
286 throw new UnsupportedOperationException(
287 "Cannot print question while in head-less mode.");
288 }
289
290 int decision = JOptionPane.showConfirmDialog(parent, message, title,
291 JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
292
293 if (decision == JOptionPane.YES_OPTION) {
294 return true;
295 }
296
297 return false;
298 }
299
300
301
302
303
304
305
306
307
308 public static void replaceListContents(DefaultListModel<Object> model,
309 List<?> newContent) {
310 model.clear();
311 if (newContent != null) {
312 for (Object element : newContent) {
313 model.addElement(element);
314 }
315 }
316 }
317
318
319
320
321
322
323
324
325 public static void showModalDialog(final JDialog dialogToShow) {
326 BasicUtilities.invokeLaterOnEDT(new Runnable() {
327 @Override
328 public void run() {
329 dialogToShow.setVisible(true);
330 BasicUtilities.repaintOnEDT(dialogToShow);
331 }
332 });
333 }
334
335
336
337
338
339
340
341
342
343 public static JFileChooser getDirectoryChooser(String dialogTitle) {
344 JFileChooser fileChooser = new JFileChooser();
345 fileChooser.setMultiSelectionEnabled(false);
346 fileChooser.setDialogTitle(dialogTitle);
347 fileChooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
348 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
349 return fileChooser;
350 }
351
352
353
354
355
356
357 public static JLabel getLabelToWait() {
358 JLabel msg = new JLabel("This may take a while. Please be patient...");
359 msg.setFont(getDefaultFontLarge());
360 return msg;
361 }
362
363
364
365
366
367
368 public static Font getDefaultFontLarge() {
369 return new Font("Sans", Font.BOLD, FONT_SIZE_WAIT_MSG);
370 }
371
372
373
374
375
376
377 public static Font getDefaultFontBold() {
378 return new Font("Sans", Font.BOLD, FONT_SIZE_MEDIUM);
379 }
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 public static int addRowToPanel(JPanel panel, String key, JComponent input,
395 int currentRow) {
396 CellConstraints c = new CellConstraints();
397 panel.add(new JLabel(key), c.xy(KEYS_COLUMN_INDEX, currentRow));
398 panel.add(input, c.xy(INPUT_COLUMN_INDEX, currentRow));
399 return currentRow + ROW_SKIP_LAYOUT;
400 }
401 }