diff options
| -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(); + } } |
