diff options
author | Jonathan Bauer | 2015-07-10 09:25:24 +0200 |
---|---|---|
committer | Jonathan Bauer | 2015-07-10 09:25:24 +0200 |
commit | 4f2026d557d5a5c27b0dcac0881d6c4b57e3dcae (patch) | |
tree | 14d6e7460e6b7fc9943ef505282acb9d595a7546 /dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainWindow.java | |
parent | [client] Only allow Composites to be passed to openPopup; make Ctrl-Q quit th... (diff) | |
download | tutor-module-4f2026d557d5a5c27b0dcac0881d6c4b57e3dcae.tar.gz tutor-module-4f2026d557d5a5c27b0dcac0881d6c4b57e3dcae.tar.xz tutor-module-4f2026d557d5a5c27b0dcac0881d6c4b57e3dcae.zip |
[client] GuiManager is now MainWindow, old MainWindow is now MainMenuWindow
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainWindow.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainWindow.java | 195 |
1 files changed, 182 insertions, 13 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainWindow.java index 195f748f..43156049 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainWindow.java @@ -1,31 +1,200 @@ package org.openslx.dozmod.gui.window; +import java.lang.reflect.Constructor; + +import org.apache.log4j.Logger; +import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +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.Monitor; import org.eclipse.swt.widgets.Shell; -import org.openslx.dozmod.gui.helper.GuiManager; -import org.openslx.dozmod.gui.window.layout.MainWindowLayout; +import org.openslx.dozmod.gui.helper.MessageType; +import org.openslx.thrifthelper.ThriftManager; +import org.openslx.thrifthelper.ThriftManager.ErrorCallback; + +public abstract class MainWindow { + + private final static Logger LOGGER = Logger.getLogger(MainWindow.class); + + private static Shell mainShell; + private static Composite contentComposite; + private static Display display; + + private static final String THRIFT_CONNECTION_ERROR = "Lost connection to the masterserver. Do you want to retry?"; + + /** + * Add a new composite with content to the main Shell + * + * @param The composite to add, should be a GUI + */ + public static void addContent(Composite contentComposite) { + removeContent(); + + MainWindow.contentComposite = contentComposite; + + // sets the starting preferred size. + GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true); + gridData.widthHint = 800; + gridData.heightHint = 600; + contentComposite.setLayoutData(gridData); + mainShell.setMinimumSize(850, 650); + mainShell.layout(); + + } + + /** + * Remove the current content of the main shell + */ + private static void removeContent() { + if (contentComposite != null) { + MainWindow.contentComposite.dispose(); + } + } + + /** + * @param clazz Class to open as a popup over the main window + */ + public static void openPopup(Class<? extends Composite> clazz, boolean modal) { + Shell dialogShell = new Shell(mainShell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); + // populate dialogShell + dialogShell.setLayout(new GridLayout(1, false)); + LOGGER.debug(clazz.getDeclaredClasses()); + Constructor<?> con = null; + try { + con = clazz.getConstructor(Shell.class); + con.newInstance(dialogShell); + } catch (Exception e1) { + showMessageBox(mainShell, "Cannot show popup " + clazz.getName(), MessageType.DEBUG, LOGGER, e1); + return; + } + + dialogShell.layout(); + dialogShell.pack(); + dialogShell.open(); + if (modal) { + while (!dialogShell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + } + } + + /** + * @return The instance of SWT display currently in use + */ + public static Display getDisplay() { + return display; + } + + /** + * Initialises the GUI by creating the main window, adding the menu and + * creating the login mask as the first content window. + * Further sets up the global thrift error callback to catch any + * connection errors during the communication with the servers. + */ + public static void initialise() { + // init SWT stuffs + display = new Display(); + mainShell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); -public class MainWindow extends MainWindowLayout { + // Set up thrift error message displaying + ThriftManager.setErrorCallback(new ErrorCallback() { - public MainWindow(Shell mainShell) { - super(mainShell); - // TODO Auto-generated constructor stub - // function for vmButton - vmButton.addSelectionListener(new SelectionAdapter() { @Override - public void widgetSelected(SelectionEvent e) { - GuiManager.addContent(new ImageListWindow(getShell())); + public boolean thriftError(int failCount, String method, Throwable t) { + // Ask user if we should retry + return showMessageBox(mainShell, THRIFT_CONNECTION_ERROR, MessageType.ERROR_RETRY, LOGGER, t); } }); - // function for lecturesButton - lecturesButton.addSelectionListener(new SelectionAdapter() { + // Global key listener + display.addFilter(SWT.KeyDown, new Listener() { + @Override + public void handleEvent(Event event) { + if (event.character == 17) // Ctrl-Q = Quit + System.exit(0); + } + }); + + Menu menuBar = new Menu(mainShell, SWT.BAR); + MenuItem cascadeFileMenu = new MenuItem(menuBar, SWT.CASCADE); + cascadeFileMenu.setText("&File"); + + Menu fileMenu = new Menu(mainShell, SWT.DROP_DOWN); + cascadeFileMenu.setMenu(fileMenu); + + MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH); + exitItem.setText("&Exit"); + exitItem.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - // + System.exit(0); } }); + + mainShell.setText("bwSuite"); + mainShell.setMenuBar(menuBar); + + // Set layout for the mainshell, items added to the shell should get a gridData + mainShell.setLayout(new GridLayout(1, true)); + + // Add LoginWindow as the first window to be shown + addContent(new LoginWindow(mainShell)); + + // center the window on the primary monitor + Monitor primary = display.getPrimaryMonitor(); + Rectangle bounds = primary.getBounds(); + Rectangle rect = mainShell.getBounds(); + + int x = bounds.x + (bounds.width - rect.width) / 2; + int y = bounds.y + (bounds.height - rect.height) / 2; + + mainShell.setLocation(x, y); + + mainShell.pack(); + mainShell.open(); + + LOGGER.info("GUI initialised."); + + while (!mainShell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + } + + /** + * 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; } } |