diff options
| author | Simon Rettberg | 2015-07-13 10:45:52 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-07-13 10:45:52 +0200 |
| commit | 404b4b414aad1a1bd8c07bddba2a96c023070060 (patch) | |
| tree | 329856fa4168558412d807a261a74aa3ea6e821d | |
| parent | [client] Add helper methods for executing stuff in the gui thread from other ... (diff) | |
| download | tutor-module-404b4b414aad1a1bd8c07bddba2a96c023070060.tar.gz tutor-module-404b4b414aad1a1bd8c07bddba2a96c023070060.tar.xz tutor-module-404b4b414aad1a1bd8c07bddba2a96c023070060.zip | |
[client] Formatting, moved generic showMessageBox to Gui class
4 files changed, 59 insertions, 48 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 080d21ed..aef307d6 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java @@ -13,7 +13,6 @@ import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; -import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.openslx.dozmod.Config; import org.openslx.dozmod.gui.helper.Gui; @@ -69,7 +68,7 @@ public abstract class MainWindow { con = clazz.getConstructor(Shell.class); con.newInstance(dialogShell); } catch (Exception e1) { - showMessageBox(mainShell, "Cannot show popup " + clazz.getName(), MessageType.DEBUG, LOGGER, e1); + Gui.showMessageBox(mainShell, "Cannot show popup " + clazz.getName(), MessageType.DEBUG, LOGGER, e1); return; } @@ -97,7 +96,8 @@ public abstract class MainWindow { return Gui.syncExec(new GuiCallable<Boolean>() { @Override public Boolean run() { - return showMessageBox(mainShell, THRIFT_CONNECTION_ERROR, MessageType.ERROR_RETRY, LOGGER, t); + return Gui.showMessageBox(mainShell, THRIFT_CONNECTION_ERROR, MessageType.ERROR_RETRY, + LOGGER, t); } }); } @@ -110,7 +110,8 @@ public abstract class MainWindow { Gui.asyncExec(new Runnable() { @Override public void run() { - showMessageBox("Konnte Programmeinstellungen nicht speichern", MessageType.WARNING, LOGGER, t); + showMessageBox("Konnte Programmeinstellungen nicht speichern", MessageType.WARNING, + LOGGER, t); } }); } @@ -139,6 +140,7 @@ public abstract class MainWindow { // center the window on the primary monitor Gui.centerShell(mainShell); + Gui.limitShellSize(mainShell); mainShell.open(); // here we can check for Session information @@ -216,31 +218,7 @@ public abstract class MainWindow { */ public static boolean showMessageBox(String message, MessageType messageType, Logger logger, Throwable exception) { - return showMessageBox(mainShell, message, messageType, logger, exception); - } - - /** - * 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() + " (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; + return Gui.showMessageBox(mainShell, message, messageType, logger, exception); } } 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 0868861d..386a06ae 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 @@ -3,14 +3,16 @@ package org.openslx.dozmod.gui.helper; 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; public class Gui { - + private static final Logger LOGGER = Logger.getLogger(Gui.class); /** @@ -122,10 +124,11 @@ 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. - * + * 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 */ @@ -143,18 +146,48 @@ public class Gui { }); return null; } - + + /** + * 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() + " (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; + } + } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LoginWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LoginWindow.java index 66e1f030..5645a212 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LoginWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LoginWindow.java @@ -250,7 +250,7 @@ public class LoginWindow extends LoginWindowLayout { private void doLogin() { // sanity check on loginType. if (loginType == null) { - MainWindow.showMessageBox(this.getShell(), + Gui.showMessageBox(this.getShell(), "No login type set, a default should be set! Ignoring...", MessageType.ERROR, LOGGER, null); return; @@ -260,11 +260,11 @@ public class LoginWindow extends LoginWindowLayout { String password = passwordText.getText(); // login clicked, lets first read the fields if (username.isEmpty()) { - MainWindow.showMessageBox(this.getShell(), NO_USERNAME, MessageType.ERROR, null, null); + Gui.showMessageBox(this.getShell(), NO_USERNAME, MessageType.ERROR, null, null); return; } if (password.isEmpty()) { - MainWindow.showMessageBox(this.getShell(), NO_PASSWORD, MessageType.ERROR, null, null); + Gui.showMessageBox(this.getShell(), NO_PASSWORD, MessageType.ERROR, null, null); return; } @@ -279,7 +279,7 @@ public class LoginWindow extends LoginWindowLayout { public void postLogin(ReturnCode returnCode, UserInfo user, Throwable t) { // TODO finish this if (user == null) { - MainWindow.showMessageBox(me.getShell(), + Gui.showMessageBox(me.getShell(), "User information received from the masterserver is corrupt!", MessageType.ERROR, LOGGER, null); return; @@ -289,23 +289,23 @@ public class LoginWindow extends LoginWindowLayout { postSuccessfulLogin(); break; case IDENTITY_PROVIDER_ERROR: - MainWindow.showMessageBox(me.getShell(), "IdP Error", MessageType.ERROR, null, null); + Gui.showMessageBox(me.getShell(), "IdP Error", MessageType.ERROR, null, null); break; case SERVICE_PROVIDER_ERROR: // here if we have t != null then we have not received a token // if we have t, then the token is invalid. - MainWindow.showMessageBox(me.getShell(), "Invalid token from the service provider!", + Gui.showMessageBox(me.getShell(), "Invalid token from the service provider!", MessageType.ERROR, LOGGER, t); break; case UNREGISTERED_ERROR: - MainWindow.showMessageBox( + Gui.showMessageBox( me.getShell(), "You are not registered to bwLehrpool. Please visit " + ShibbolethEcp.getRegistrationUrl() + " and register first.", MessageType.ERROR, LOGGER, t); break; default: - MainWindow.showMessageBox(me.getShell(), "Internal error!", MessageType.ERROR, null, null); + Gui.showMessageBox(me.getShell(), "Internal error!", MessageType.ERROR, null, null); break; } } @@ -321,10 +321,10 @@ public class LoginWindow extends LoginWindowLayout { authenticator = new TestAccountAuthenticator(); break; case DIRECT_CONNECT: - MainWindow.showMessageBox(this.getShell(), "Not yet implemented", MessageType.ERROR, null, null); + Gui.showMessageBox(this.getShell(), "Not yet implemented", MessageType.ERROR, null, null); return; default: - MainWindow.showMessageBox(this.getShell(), "No login type selected!", MessageType.ERROR, null, + Gui.showMessageBox(this.getShell(), "No login type selected!", MessageType.ERROR, null, null); return; } @@ -333,7 +333,7 @@ public class LoginWindow extends LoginWindowLayout { try { authenticator.login(username, password, authenticatorCallback); } catch (TAuthenticationException e) { - MainWindow.showMessageBox(me.getShell(), "Authentication failed: " + e.getMessage(), + Gui.showMessageBox(me.getShell(), "Authentication failed: " + e.getMessage(), MessageType.ERROR, LOGGER, null); return; } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageWizard.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageWizard.java index dc25aad9..9b979ea9 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageWizard.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageWizard.java @@ -42,7 +42,7 @@ public class ImageWizard extends Wizard { @Override public boolean performFinish() { - MainWindow.showMessageBox("Would try to upload now :)", MessageType.INFO, LOGGER, null); + MainWindow.showMessageBox("Would continue uploading now :)", MessageType.INFO, LOGGER, null); return true; } } |
