diff options
author | Simon Rettberg | 2015-07-13 12:04:32 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-07-13 12:04:32 +0200 |
commit | edb3f4c93df393d9fb35880d69328d77e561283f (patch) | |
tree | f742678aa234f8ee2ff4d5565e8e98f1695eb5b6 /dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java | |
parent | [client] Cleaned up and fixed image list window layout (diff) | |
download | tutor-module-edb3f4c93df393d9fb35880d69328d77e561283f.tar.gz tutor-module-edb3f4c93df393d9fb35880d69328d77e561283f.tar.xz tutor-module-edb3f4c93df393d9fb35880d69328d77e561283f.zip |
[client] Common parent class for pages shown in the main window. Do not dispose and create pages every time, instead show and hide them as needed
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java | 66 |
1 files changed, 50 insertions, 16 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 aef307d6..2aed0ff5 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java @@ -1,6 +1,8 @@ package org.openslx.dozmod.gui; import java.lang.reflect.Constructor; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.apache.log4j.Logger; import org.eclipse.swt.SWT; @@ -15,10 +17,12 @@ import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Shell; import org.openslx.dozmod.Config; +import org.openslx.dozmod.gui.helper.CompositePage; 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.ImageListWindow; import org.openslx.dozmod.gui.window.LoginWindow; import org.openslx.dozmod.gui.window.MainMenuWindow; import org.openslx.dozmod.gui.window.VirtualizerNoticeWindow; @@ -31,26 +35,36 @@ public abstract class MainWindow { private final static Logger LOGGER = Logger.getLogger(MainWindow.class); private static final Shell mainShell; - private static Composite contentComposite; + private static CompositePage currentPage; + + private static final Map<Class<? extends CompositePage>, CompositePage> pages = new ConcurrentHashMap<>(); 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 + * Set the visible page of the main window. * - * @param The composite to add, should be a GUI + * @param clazz */ - public static void setContent(Composite contentComposite) { - - if (contentComposite == null) - return; - if (MainWindow.contentComposite != null) - MainWindow.contentComposite.dispose(); + public static void showPage(Class<? extends CompositePage> clazz) { + if (currentPage != null && currentPage.getLayoutData() != null) { + if (!currentPage.hide()) { + return; // Canceled by currently shown page + } + ((GridData) currentPage.getLayoutData()).exclude = true; + currentPage.setVisible(false); + } - MainWindow.contentComposite = contentComposite; + currentPage = pages.get(clazz); + if (currentPage == null) { + showMessageBox("Tried to show unknown page " + clazz.getSimpleName(), MessageType.ERROR, LOGGER, null); + System.exit(1); + } // sets the starting preferred size. - contentComposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true)); + currentPage.show(); + ((GridData)currentPage.getLayoutData()).exclude = false; + currentPage.setVisible(true); mainShell.layout(); } @@ -68,7 +82,8 @@ public abstract class MainWindow { con = clazz.getConstructor(Shell.class); con.newInstance(dialogShell); } catch (Exception e1) { - Gui.showMessageBox(mainShell, "Cannot show popup " + clazz.getName(), MessageType.DEBUG, LOGGER, e1); + Gui.showMessageBox(mainShell, "Cannot show popup " + clazz.getName(), MessageType.DEBUG, LOGGER, + e1); return; } @@ -96,8 +111,8 @@ public abstract class MainWindow { return Gui.syncExec(new GuiCallable<Boolean>() { @Override public Boolean run() { - return Gui.showMessageBox(mainShell, THRIFT_CONNECTION_ERROR, MessageType.ERROR_RETRY, - LOGGER, t); + return Gui.showMessageBox(mainShell, THRIFT_CONNECTION_ERROR, + MessageType.ERROR_RETRY, LOGGER, t); } }); } @@ -134,8 +149,11 @@ public abstract class MainWindow { mainShell.setLayout(new GridLayout(1, true)); mainShell.setMinimumSize(850, 650); - // always show the main menu - setContent(new MainMenuWindow(mainShell)); + // register all pages of the main window + registerPage(new MainMenuWindow(mainShell)); + registerPage(new ImageListWindow(mainShell)); + // Show main menu by default + showPage(MainMenuWindow.class); // center the window on the primary monitor Gui.centerShell(mainShell); @@ -160,6 +178,22 @@ public abstract class MainWindow { } } + /** + * Register a page that can be displayed in the main window. + * + * @param window + */ + private static synchronized void registerPage(CompositePage window) { + Class<? extends CompositePage> clazz = window.getClass(); + if (pages.containsKey(clazz)) + throw new IllegalArgumentException("Page " + clazz.getSimpleName() + " already registered!"); + pages.put(clazz, window); + GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true); + gd.exclude = true; + window.setLayoutData(gd); + window.setVisible(false); + } + private static void createMenu() { // the File menu button Menu menuBar = new Menu(mainShell, SWT.BAR); |