diff options
| author | Simon Rettberg | 2015-07-10 15:48:26 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-07-10 15:48:26 +0200 |
| commit | 2ab01861e771592c238d22f623f01ce0967887e6 (patch) | |
| tree | aba296455b078f55cb0f248492f4869e8de92533 /dozentenmodul/src/main/java | |
| parent | Merge branch 'v1.1' of git.openslx.org:openslx-ng/tutor-module into v1.1 (diff) | |
| download | tutor-module-2ab01861e771592c238d22f623f01ce0967887e6.tar.gz tutor-module-2ab01861e771592c238d22f623f01ce0967887e6.tar.xz tutor-module-2ab01861e771592c238d22f623f01ce0967887e6.zip | |
[client] Use AWT to show an error message in case initializing SWT fails
Diffstat (limited to 'dozentenmodul/src/main/java')
3 files changed, 126 insertions, 11 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/App.java b/dozentenmodul/src/main/java/org/openslx/dozmod/App.java index 37e93434..93f58edf 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/App.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/App.java @@ -1,10 +1,11 @@ package org.openslx.dozmod; + import java.io.File; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.log4j.AppenderSkeleton; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.FileAppender; @@ -12,6 +13,8 @@ import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.spi.LoggingEvent; import org.openslx.dozmod.gui.MainWindow; +import org.openslx.dozmod.gui.helper.Gui; +import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.util.ProxyConfigurator; import org.openslx.thrifthelper.ThriftManager; @@ -32,9 +35,9 @@ public class App { LOGGER.info("renaming old log file"); try { File oldFile = new File(logFilePath + ".old"); - FileUtils.forceDelete(oldFile); + oldFile.delete(); logFile.renameTo(oldFile); - FileUtils.forceDelete(logFile); + logFile.delete(); } catch (Exception e) { LOGGER.error("Could not move '" + logFilePath + "' to '" + logFilePath + ".old'", e); } @@ -91,12 +94,20 @@ public class App { } public static void main(final String[] args) { - - // Pruefe und Erzeuge gegebenfalls Config try { Config.init(); - } catch (IOException e) { - e.printStackTrace(); + } catch (Exception e) { + showAwtMessage("Error loading configuration", e); + return; + } + + setupLogger(); + + // Check if we can load SWT by calling some library function + try { + Gui.display.getActiveShell(); + } catch (NoClassDefFoundError e) { + showAwtMessage("Error loading SWT libraries", e); return; } @@ -104,17 +115,24 @@ public class App { // Set master server to use (TODO: make configurable via command line) ThriftManager.setMasterServerAddress("bwlp-masterserver.ruf.uni-freiburg.de"); - setupLogger(); - - // initialise the proxy settings + // Initialize the proxy settings try { ProxyConfigurator.init(); } catch (IOException e) { - LOGGER.error("IOException when trying to initialise the proxy, see trace: ", e); + MainWindow.showMessageBox( + "Could not detect proxy server automatically. No proxy server will be used", + MessageType.WARNING, LOGGER, e); } // start the main window MainWindow.mainloop(); + } + private static void showAwtMessage(String message, Throwable e) { + LOGGER.error(message, e); + if (e != null) { + message += "\n\n" + ExceptionUtils.getStackTrace(e); + } + new AwtBox(null, message); } } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/AwtBox.java b/dozentenmodul/src/main/java/org/openslx/dozmod/AwtBox.java new file mode 100644 index 00000000..9ac9b87b --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/AwtBox.java @@ -0,0 +1,81 @@ +package org.openslx.dozmod; + +import java.awt.BorderLayout; +import java.awt.Button; +import java.awt.Dialog; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.Frame; +import java.awt.Panel; +import java.awt.TextArea; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * Show a message box using AWT - used for when SWT fails. + */ +public class AwtBox extends Dialog implements ActionListener { + + private static final long serialVersionUID = 6254641637349233126L; + private Button okButton, cancelButton; + public boolean okClicked = false; + + /** + * @param frame parent frame + * @param message message to be displayed + * @param addCancelButton whether we want a cancel button too, instead of + * just OK + */ + AwtBox(Frame frame, String message, boolean addCancelButton) { + super(frame, "Message", true); + setLayout(new BorderLayout()); + TextArea ta = new TextArea(message, 1 + (message.length() - message.replace("\n", "").length()), 90, + TextArea.SCROLLBARS_VERTICAL_ONLY); + ta.setEditable(false); + add("Center", ta); + addOKCancelPanel(addCancelButton); + createFrame(); + pack(); + setVisible(true); + } + + AwtBox(Frame frame, String msg) { + this(frame, msg, false); + } + + private void addOKCancelPanel(boolean okcan) { + Panel p = new Panel(); + p.setLayout(new FlowLayout()); + createOKButton(p); + if (okcan == true) + createCancelButton(p); + add("South", p); + } + + private void createOKButton(Panel p) { + p.add(okButton = new Button("OK")); + okButton.addActionListener(this); + } + + private void createCancelButton(Panel p) { + p.add(cancelButton = new Button("Cancel")); + cancelButton.addActionListener(this); + } + + private void createFrame() { + Dimension d = getToolkit().getScreenSize(); + setLocation(d.width / 3, d.height / 3); + } + + @Override + public void actionPerformed(ActionEvent ae) { + if (ae.getSource() == okButton) { + okClicked = true; + setVisible(false); + } else if (ae.getSource() == cancelButton) { + setVisible(false); + } + this.dispose(); + } + +} 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 c8ba577f..b3517403 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java @@ -186,6 +186,22 @@ public abstract class MainWindow { /** * Generic helper to show a message box to the user, and optionally log the + * message to the log file. The main window will be the parent of the + * message box. + * + * @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(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 |
