diff options
| author | Simon Rettberg | 2015-07-11 16:49:52 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-07-11 16:49:52 +0200 |
| commit | a8edfc241edd1835edcfbef48e6575f7560121b6 (patch) | |
| tree | b0b233f4919600b99be56099a450d80c503677a3 | |
| parent | Merge branch 'v1.1' of git.openslx.org:openslx-ng/tutor-module into v1.1 (diff) | |
| download | tutor-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.
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java | 19 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java | 39 |
2 files changed, 54 insertions, 4 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java index 1bdb3220..080d21ed 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java @@ -17,6 +17,7 @@ import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.openslx.dozmod.Config; import org.openslx.dozmod.gui.helper.Gui; +import org.openslx.dozmod.gui.helper.Gui.GuiCallable; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.gui.window.DisclaimerWindow; import org.openslx.dozmod.gui.window.LoginWindow; @@ -91,17 +92,27 @@ public abstract class MainWindow { // Set up thrift error message displaying ThriftManager.setErrorCallback(new ErrorCallback() { @Override - public boolean thriftError(int failCount, String method, Throwable t) { + public boolean thriftError(int failCount, String method, final Throwable t) { // Ask user if we should retry - return showMessageBox(mainShell, THRIFT_CONNECTION_ERROR, MessageType.ERROR_RETRY, LOGGER, t); + return Gui.syncExec(new GuiCallable<Boolean>() { + @Override + public Boolean run() { + return showMessageBox(mainShell, THRIFT_CONNECTION_ERROR, MessageType.ERROR_RETRY, LOGGER, t); + } + }); } }); // Same for config errors Config.setErrorCallback(new Config.ErrorCallback() { @Override - public void writeError(Throwable t) { - showMessageBox("Konnte Programmeinstellungen nicht speichern", MessageType.WARNING, LOGGER, t); + public void writeError(final Throwable t) { + Gui.asyncExec(new Runnable() { + @Override + public void run() { + showMessageBox("Konnte Programmeinstellungen nicht speichern", MessageType.WARNING, LOGGER, t); + } + }); } }); 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(); + } } |
