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 | |
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')
6 files changed, 107 insertions, 33 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); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/CompositePage.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/CompositePage.java new file mode 100644 index 00000000..d9fed2e6 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/CompositePage.java @@ -0,0 +1,30 @@ +package org.openslx.dozmod.gui.helper; + +import org.eclipse.swt.widgets.Composite; + +public abstract class CompositePage extends Composite { + + public CompositePage(Composite parent, int style) { + super(parent, style); + } + + /** + * This page is requested to be closed (hidden). The page should save any + * data or unload resources, but not destroy the window, as it might be + * opened again at a later point in time. The page can request to cancel the + * operation, i.e. by asking the user first if he wants to cancel the + * operation and lose unsaved data, and then returning false if the user + * wants to stay on this page. + * + * @return true to allow hiding (and opening a new page afterwards), false + * to stay on this page. + */ + public abstract boolean hide(); + + /** + * Called when this page will be shown. The Page can initialize or reset + * internal data and control contents. + */ + public abstract void show(); + +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java index 3d3221d4..9a8de0c4 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java @@ -16,6 +16,7 @@ import org.openslx.bwlp.thrift.iface.ImagePermissions; import org.openslx.bwlp.thrift.iface.ImageSummaryRead; import org.openslx.dozmod.gui.window.layout.ImageListWindowLayout; import org.openslx.dozmod.gui.wizard.ImageWizard; +import org.openslx.dozmod.thrift.ImageCache; import org.openslx.dozmod.thrift.Session; import org.openslx.thrifthelper.ThriftManager; @@ -112,22 +113,23 @@ public class ImageListWindow extends ImageListWindowLayout { wd.open(); } }); - - refreshList(); } private boolean refreshList() { - List<ImageSummaryRead> imageList; - try { - imageList = ThriftManager.getSatClient().getImageList(Session.getSatelliteToken(), null, 0); - } catch (TException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } + List<ImageSummaryRead> imageList = ImageCache.get(false); tableViewer.setInput(imageList); tableViewer.refresh(); return true; } + @Override + public boolean hide() { + return true; + } + + @Override + public void show() { + refreshList(); + } + } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainMenuWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainMenuWindow.java index e1911f24..7b2fa94b 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainMenuWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainMenuWindow.java @@ -14,7 +14,7 @@ public class MainMenuWindow extends MainMenuWindowLayout { vmButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - MainWindow.setContent(new ImageListWindow(getShell())); + MainWindow.showPage(ImageListWindow.class); } }); @@ -27,4 +27,14 @@ public class MainMenuWindow extends MainMenuWindowLayout { }); } + @Override + public boolean hide() { + return true; + } + + @Override + public void show() { + // (void) + } + } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java index 4e2acb7c..9defdb34 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java @@ -5,22 +5,20 @@ import org.eclipse.jface.viewers.TableViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; -import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.Text; +import org.openslx.dozmod.gui.helper.CompositePage; import org.openslx.dozmod.gui.helper.Gui; import org.openslx.dozmod.gui.helper.TableHelper; -public abstract class ImageListWindowLayout extends Composite { +public abstract class ImageListWindowLayout extends CompositePage { protected String infoTitleString = "Übersicht Virtuelle Maschinen"; protected String newButtonLabel = "Neu"; diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/MainMenuWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/MainMenuWindowLayout.java index b8519a4f..46656667 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/MainMenuWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/MainMenuWindowLayout.java @@ -4,10 +4,10 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; -import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; +import org.openslx.dozmod.gui.helper.CompositePage; /* * Structure/workflow should be: @@ -35,7 +35,7 @@ import org.eclipse.swt.widgets.Shell; * will skip the login screen */ -public abstract class MainMenuWindowLayout extends Composite { +public abstract class MainMenuWindowLayout extends CompositePage { // text for info for the vms selection protected String vmInfo = "Infotext VMs."; |