summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-07-11 16:49:52 +0200
committerSimon Rettberg2015-07-11 16:49:52 +0200
commita8edfc241edd1835edcfbef48e6575f7560121b6 (patch)
treeb0b233f4919600b99be56099a450d80c503677a3 /dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java
parentMerge branch 'v1.1' of git.openslx.org:openslx-ng/tutor-module into v1.1 (diff)
downloadtutor-module-a8edfc241edd1835edcfbef48e6575f7560121b6.tar.gz
tutor-module-a8edfc241edd1835edcfbef48e6575f7560121b6.tar.xz
tutor-module-a8edfc241edd1835edcfbef48e6575f7560121b6.zip
[client] Add helper methods for executing stuff in the gui thread from other threads.
Use those for error callbacks that want to display messages, as we can't know which context they're fired in.
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java39
1 files changed, 39 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java
index 25e456be..0868861d 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java
@@ -1,5 +1,8 @@
package org.openslx.dozmod.gui.helper;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.log4j.Logger;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
@@ -7,6 +10,8 @@ import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
public class Gui {
+
+ private static final Logger LOGGER = Logger.getLogger(Gui.class);
/**
* The one and only display to use throughout the application
@@ -117,5 +122,39 @@ public class Gui {
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 null;
+ }
+
+ 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();
+ }
}