From e0f5dee96b23105f325334d7af5f826ac3469bb0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 10 Jul 2015 13:27:56 +0200 Subject: [client] Minor refactoring --- .../src/main/java/org/openslx/dozmod/App.java | 4 +- .../java/org/openslx/dozmod/gui/MainWindow.java | 223 ++++++++++++++++++++ .../org/openslx/dozmod/gui/window/LoginWindow.java | 1 + .../openslx/dozmod/gui/window/MainMenuWindow.java | 1 + .../org/openslx/dozmod/gui/window/MainWindow.java | 225 --------------------- .../gui/window/layout/ImageListWindowLayout.java | 2 +- .../gui/window/layout/LoginWindowLayout.java | 2 +- .../layout/VirtualizerNoticeWindowLayout.java | 2 +- .../org/openslx/dozmod/util/ResourceLoader.java | 2 +- 9 files changed, 231 insertions(+), 231 deletions(-) create mode 100644 dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java delete mode 100644 dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainWindow.java (limited to 'dozentenmodul/src/main/java') diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/App.java b/dozentenmodul/src/main/java/org/openslx/dozmod/App.java index b940e18c..37e93434 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/App.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/App.java @@ -11,7 +11,7 @@ import org.apache.log4j.FileAppender; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.spi.LoggingEvent; -import org.openslx.dozmod.gui.window.MainWindow; +import org.openslx.dozmod.gui.MainWindow; import org.openslx.dozmod.util.ProxyConfigurator; import org.openslx.thrifthelper.ThriftManager; @@ -114,7 +114,7 @@ public class App { } // start the main window - MainWindow.initialise(); + MainWindow.mainloop(); } } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java new file mode 100644 index 00000000..72b19293 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java @@ -0,0 +1,223 @@ +package org.openslx.dozmod.gui; + +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.MessageType; +import org.openslx.dozmod.gui.window.DisclaimerWindow; +import org.openslx.dozmod.gui.window.LoginWindow; +import org.openslx.dozmod.gui.window.MainMenuWindow; +import org.openslx.dozmod.gui.window.VirtualizerNoticeWindow; +import org.openslx.dozmod.thrift.Session; +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 final Shell mainShell; + private static Composite contentComposite; + private static final 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 setContent(Composite contentComposite) { + + if (contentComposite == null) return; + if (MainWindow.contentComposite != null) + MainWindow.contentComposite.dispose(); + + MainWindow.contentComposite = contentComposite; + + // sets the starting preferred size. + contentComposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true)); + mainShell.layout(); + } + + /** + * @param clazz Class to open as a popup over the main window. + * MUST be a subclass of Composite. + */ + public static void openPopup(Class clazz, boolean modal) { + Shell dialogShell = new Shell(mainShell, SWT.DIALOG_TRIM | (modal ? SWT.APPLICATION_MODAL : 0)); + // 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(); + } + + /** + * @return The instance of SWT display currently in use + */ + public static Display getDisplay() { + return display; + } + + static { + // init SWT stuff + display = new Display(); + mainShell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); + + // Set up thrift error message displaying + ThriftManager.setErrorCallback(new ErrorCallback() { + + @Override + 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); + } + }); + + // 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); + } + }); + + createMenu(); + + mainShell.setText("bwSuite"); + + // Set layout for the mainshell, items added to the shell should get a gridData + mainShell.setLayout(new GridLayout(1, true)); + mainShell.setMinimumSize(850, 650); + + // always show the main menu + setContent(new MainMenuWindow(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.open(); + + // here we can check for Session information + if (Session.getSatelliteToken() == null) { + // User did not login, show the login mask + openPopup(LoginWindow.class, true); + } + } + + /** + * 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 mainloop() { + while (!mainShell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + } + + private static void createMenu() { + // the File menu button + 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); + } + }); + + // the About menu button + MenuItem cascadeAboutMenu = new MenuItem(menuBar, SWT.CASCADE); + cascadeAboutMenu.setText("&About"); + + Menu aboutMenu = new Menu(mainShell, SWT.DROP_DOWN); + cascadeAboutMenu.setMenu(aboutMenu); + + MenuItem disclaimerItem = new MenuItem(aboutMenu, SWT.PUSH); + disclaimerItem.setText("&Disclaimer"); + disclaimerItem.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + MainWindow.openPopup(DisclaimerWindow.class, false); + } + }); + + MenuItem virtualizerNoticeItem = new MenuItem(aboutMenu, SWT.PUSH); + virtualizerNoticeItem.setText("&Virtualizer"); + virtualizerNoticeItem.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + MainWindow.openPopup(VirtualizerNoticeWindow.class, false); + } + }); + mainShell.setMenuBar(menuBar); + } + + /** + * 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 6896835a..7ebf8a51 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 @@ -21,6 +21,7 @@ import org.openslx.dozmod.authentication.EcpAuthenticator; import org.openslx.dozmod.authentication.ShibbolethEcp; import org.openslx.dozmod.authentication.ShibbolethEcp.ReturnCode; import org.openslx.dozmod.authentication.TestAccountAuthenticator; +import org.openslx.dozmod.gui.MainWindow; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.gui.window.layout.LoginWindowLayout; import org.openslx.dozmod.thrift.OrganizationCache; 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 a6337fb8..e1911f24 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 @@ -3,6 +3,7 @@ package org.openslx.dozmod.gui.window; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Shell; +import org.openslx.dozmod.gui.MainWindow; import org.openslx.dozmod.gui.window.layout.MainMenuWindowLayout; public class MainMenuWindow extends MainMenuWindowLayout { 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 deleted file mode 100644 index 3f16211a..00000000 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainWindow.java +++ /dev/null @@ -1,225 +0,0 @@ -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.MessageType; -import org.openslx.dozmod.thrift.Session; -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 setContent(Composite contentComposite) { - - if (contentComposite == null) return; - if (MainWindow.contentComposite != null) - MainWindow.contentComposite.dispose(); - - 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(); - } - - /** - * @param clazz Class to open as a popup over the main window. - * MUST be a subclass of Composite. - */ - public static void openPopup(Class 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); - - // Set up thrift error message displaying - ThriftManager.setErrorCallback(new ErrorCallback() { - - @Override - 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); - } - }); - - // 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); - } - }); - - // the File menu button - 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); - } - }); - - // the About menu button - MenuItem cascadeAboutMenu = new MenuItem(menuBar, SWT.CASCADE); - cascadeAboutMenu.setText("&About"); - - Menu aboutMenu = new Menu(mainShell, SWT.DROP_DOWN); - cascadeAboutMenu.setMenu(aboutMenu); - - MenuItem disclaimerItem = new MenuItem(aboutMenu, SWT.PUSH); - disclaimerItem.setText("&Disclaimer"); - disclaimerItem.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - MainWindow.openPopup(DisclaimerWindow.class, false); - } - }); - - MenuItem virtualizerNoticeItem = new MenuItem(aboutMenu, SWT.PUSH); - virtualizerNoticeItem.setText("&Virtualizer"); - virtualizerNoticeItem.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - MainWindow.openPopup(VirtualizerNoticeWindow.class, false); - } - }); - - 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)); - - // always show the main menu - setContent(new MainMenuWindow(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(); - - // here we can check for Session information - if (Session.getSatelliteToken() == null) { - // User did not login, show the login mask - openPopup(LoginWindow.class, true); - } - - 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; - } - -} 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 c9e7c1da..a66d47ff 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 @@ -15,8 +15,8 @@ import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.Text; +import org.openslx.dozmod.gui.MainWindow; import org.openslx.dozmod.gui.helper.TableHelper; -import org.openslx.dozmod.gui.window.MainWindow; public abstract class ImageListWindowLayout extends Composite { diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java index 63ee21c2..d52ca940 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java @@ -12,7 +12,7 @@ import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; -import org.openslx.dozmod.gui.window.MainWindow; +import org.openslx.dozmod.gui.MainWindow; public abstract class LoginWindowLayout extends Composite { diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/VirtualizerNoticeWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/VirtualizerNoticeWindowLayout.java index e367d468..0c4c14c8 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/VirtualizerNoticeWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/VirtualizerNoticeWindowLayout.java @@ -9,7 +9,7 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; -import org.openslx.dozmod.gui.window.MainWindow; +import org.openslx.dozmod.gui.MainWindow; public abstract class VirtualizerNoticeWindowLayout extends Composite { private final String title = "Hinweis VMWare Player"; diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java index 64a416c8..337cdc9d 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java @@ -9,7 +9,7 @@ import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; -import org.openslx.dozmod.gui.window.MainWindow; +import org.openslx.dozmod.gui.MainWindow; /** * Helper class for loading resources. This should be error safe loaders with a -- cgit v1.2.3-55-g7522