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