summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java
blob: 0d14a3b9e2dae5bbc5acfedf2945a2ce75313091 (plain) (blame)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package org.openslx.dozmod.gui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.openslx.dozmod.gui.helper.MessageType;

public class Gui {

	private static final Logger LOGGER = Logger.getLogger(Gui.class);

	/**
	 * The one and only display to use throughout the application
	 */
	public static final Display display = new Display();

	/**
	 * All active shells - we don't use display.getShells() as it is slow...
	 */
	private static final List<Shell> shells = new ArrayList<>();

	private static volatile int exitCode = 0;

	/**
	 * Center the given shell on the {@link Monitor} it is displayed on.
	 * WARNING: Seems broken on Linux (depending on DE or WM)
	 * 
	 * @param shell Some shell
	 */
	public static void centerShell(Shell shell) {
		Monitor activeMonitor = getMonitorFromRectangle(shell.getBounds(), true);
		Rectangle bounds = activeMonitor.getClientArea();
		Rectangle rect = shell.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shell.setLocation(x, y);
	}

	/**
	 * Take a shell and center it relative to another shell
	 * 
	 * @param parent Parent shell, used as reference
	 * @param shellToCenter The shell that should be repositioned
	 */
	public static void centerShellOverShell(Shell parent, Shell shellToCenter) {
		Rectangle bounds = parent.getBounds();
		Rectangle rect = shellToCenter.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		if (x < bounds.x)
			x = bounds.x;
		if (y < bounds.y)
			y = bounds.y;
		shellToCenter.setLocation(x, y);
	}

	/**
	 * Make sure the given shell fits the {@link Monitor} it is displayed on.
	 * 
	 * @param shell Some shell
	 */
	public static void limitShellSize(Shell shell) {
		Monitor activeMonitor = getMonitorFromRectangle(shell.getBounds(), true);
		Rectangle bounds = activeMonitor.getClientArea();
		Rectangle rect = shell.getBounds();
		boolean changed = false;
		if (rect.width + 20 > bounds.width) {
			rect.width = bounds.width - 20;
			changed = true;
		}
		if (rect.height + 20 > bounds.height) {
			rect.height = bounds.height - 20;
			changed = true;
		}
		if (changed) {
			shell.setSize(rect.width, rect.height);
			rect = shell.getBounds();
		}
		changed = false;
		if (rect.x + rect.width >= bounds.x + bounds.width) {
			rect.x = 5;
			changed = true;
		}
		if (rect.y + rect.height >= bounds.y + bounds.height) {
			rect.y = 5;
			changed = true;
		}
		if (changed) {
			shell.setLocation(rect.x, rect.y);
		}
	}

	/**
	 * Get the {@link Monitor} which the given {@link Point} lies in.
	 * 
	 * @param point The point in question
	 * @param defaultToPrimary if no monitor matches the check, return the
	 *            primary monitor if true, <code>null</code> otherwise
	 * @return the {@link Monitor}
	 */
	public static Monitor getMonitorFromPoint(Point point, boolean defaultToPrimary) {
		LOGGER.debug("Finding monitor for point " + point.toString());
		Monitor[] monitors = display.getMonitors();
		for (Monitor monitor : monitors) {
			Rectangle bounds = monitor.getBounds();
			LOGGER.debug("Checking monitor " + bounds.toString());
			if (bounds.contains(point))
				return monitor;
		}
		if (defaultToPrimary) {
			LOGGER.debug("Defaulting to primary...");
			return display.getPrimaryMonitor();
		}
		return null;
	}

	/**
	 * Get the {@link Monitor} which most of the given rectangle overlaps.
	 * 
	 * @param rect The rectangle to check
	 * @param defaultToPrimary if no monitor matches the check, return the
	 *            primary monitor if true, <code>null</code> otherwise
	 * @return the {@link Monitor}
	 */
	public static Monitor getMonitorFromRectangle(Rectangle rect, boolean defaultToPrimary) {
		// Make sure rectangle is in bounds. This is not completely accurate
		// in case there are multiple monitors that have different resolutions.
		Rectangle bounds = display.getBounds();
		LOGGER.debug("Display bounds are " + bounds.toString() + ", rect is " + rect.toString());
		if (rect.x + rect.width >= bounds.x + bounds.width) {
			rect.width -= (rect.x + rect.width) - (bounds.x + bounds.width);
			if (rect.width < 1)
				rect.width = 1;
		}
		if (rect.y + rect.height >= bounds.y + bounds.height) {
			rect.height -= (rect.y + rect.height) - (bounds.y + bounds.height);
			if (rect.height < 1)
				rect.height = 1;
		}
		if (rect.x < bounds.x) {
			rect.width -= bounds.x - rect.x;
			rect.x = bounds.x;
		}
		if (rect.y < bounds.y) {
			rect.height -= bounds.y - rect.y;
			rect.y = bounds.y;
		}
		LOGGER.debug("After correction: " + rect.toString());
		// Now just use the same code as *FromPoint by using the rectangle's center
		return getMonitorFromPoint(new Point(rect.x + rect.width / 2, rect.y + rect.height / 2),
				defaultToPrimary);
	}

	/**
	 * Run given task in the GUI thread, blocking the calling thread until the
	 * task is done.
	 * 
	 * @param task Task to run
	 * @return return value of the task
	 */
	public static <T> T syncExec(final GuiCallable<T> task) {
		final AtomicReference<T> instance = new AtomicReference<>();
		display.syncExec(new Runnable() {
			@Override
			public void run() {
				try {
					instance.set(task.run());
				} catch (Throwable e) {
					LOGGER.warn("syncExec failed!", e);
				}
			}
		});
		return instance.get();
	}

	/**
	 * Run given task as soon as possible in the GUI thread, but don't block
	 * calling thread.
	 * 
	 * @param task Task to run
	 */
	public static void asyncExec(final Runnable task) {
		display.asyncExec(task);
	}

	/**
	 * Pretty much the same as Callable, but no exceptions are allowed.
	 * 
	 * @param <T> return value
	 */
	public static interface GuiCallable<T> {
		T run();
	}

	/**
	 * Generic helper to show a message box to the user, and optionally log the
	 * message to the log file.
	 * 
	 * @param parent parent shell this message box belongs to
	 * @param message Message to display. Can be multiline.
	 * @param messageType Type of message (warning, information)
	 * @param logger Logger instance to log to. Can be null.
	 * @param exception Exception related to this message. Can be null.
	 * @return true if OK, YES or RETRY was clicked, false for CANCEL or NO
	 */
	public static boolean showMessageBox(Shell parent, String message, MessageType messageType,
			Logger logger, Throwable exception) {
		if (logger != null)
			logger.log(messageType.logPriority, message, exception);
		if (exception != null)
			message += "\n\n" + exception.getClass().getSimpleName() + "\n" + exception.getMessage() + "\n"
					+ " (Siehe Logdatei)";
		MessageBox box = new MessageBox(parent, messageType.style);
		box.setMessage(message);
		box.setText(messageType.title);
		int ret = box.open();
		return ret == SWT.OK || ret == SWT.RETRY || ret == SWT.YES;
	}

	/**
	 * Run the GUI mainloop as long as a window exists. This method does not
	 * return.
	 */
	public static void mainloop() {
		do {
			Iterator<Shell> it = shells.iterator();
			while (it.hasNext()) {
				if (it.next().isDisposed())
					it.remove();
			}
			if (!display.readAndDispatch())
				display.sleep();
		} while (!shells.isEmpty());
		display.dispose();
		System.exit(exitCode);
	}

	/**
	 * Exit application - dispose all shells, so mainloop will terminate.
	 * 
	 * @param code
	 */
	public static void exit(int code) {
		exitCode = code;
		asyncExec(new Runnable() {
			@Override
			public void run() {
				for (Shell shell : shells) {
					shell.dispose();
				}
			}
		});
	}

	/**
	 * Wrapper to get a new shell. This adds the shell to the list of shells,
	 * which we need to determine whether the app should still be running.
	 * 
	 * @param style the style of the {@link Shell}
	 * @return a new {@link Shell} that has no other {@link Shell} as its
	 *         parent.
	 */
	public static Shell newShell(int style) {
		Shell shell = new Shell(display, style);
		shells.add(shell);
		return shell;
	}

	/**
	 * Wrapper to get a new shell that is a child of another shell. This adds
	 * the shell to the list of shells, which we need to determine whether the
	 * app should still be running.
	 * 
	 * @param parent the parent {@link Shell}
	 * @param style the style of the {@link Shell}
	 * @return a new {@link Shell} that has no other {@link Shell} as its
	 *         parent.
	 */
	public static Shell newShell(Shell parent, int style) {
		Shell shell = new Shell(parent, style);
		shells.add(shell);
		return shell;
	}

}