diff options
author | Simon Rettberg | 2015-07-13 15:16:27 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-07-13 15:16:27 +0200 |
commit | 37da9a30923fd64449dc86d3864d55e1b0509076 (patch) | |
tree | 07111cbed1acd068b72208953b8e87833c7533c2 /dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java | |
parent | [client] Fix freezing of mainWindow when clicking close button. (diff) | |
download | tutor-module-37da9a30923fd64449dc86d3864d55e1b0509076.tar.gz tutor-module-37da9a30923fd64449dc86d3864d55e1b0509076.tar.xz tutor-module-37da9a30923fd64449dc86d3864d55e1b0509076.zip |
[client] Add confirmation dialog when quitting
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 | 75 |
1 files changed, 47 insertions, 28 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 0c4cb15a..07cfd292 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java @@ -17,7 +17,6 @@ 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.Shell; import org.openslx.dozmod.Config; import org.openslx.dozmod.gui.helper.CompositePage; @@ -60,13 +59,14 @@ public abstract class MainWindow { currentPage = pages.get(clazz); if (currentPage == null) { - showMessageBox("Tried to show unknown page " + clazz.getSimpleName(), MessageType.ERROR, LOGGER, null); + showMessageBox("Tried to show unknown page " + clazz.getSimpleName(), MessageType.ERROR, LOGGER, + null); System.exit(1); } // sets the starting preferred size. currentPage.show(); - ((GridData)currentPage.getLayoutData()).exclude = false; + ((GridData) currentPage.getLayoutData()).exclude = false; currentPage.setVisible(true); mainShell.layout(); } @@ -75,8 +75,13 @@ public abstract class MainWindow { * @param clazz Class to open as a popup over the main window. * MUST be a subclass of Composite. */ - public static void openPopup(Class<? extends Composite> clazz, boolean modal) { - Shell dialogShell = new Shell(mainShell, SWT.DIALOG_TRIM | (modal ? SWT.APPLICATION_MODAL : 0)); + public static void openPopup(Class<? extends Composite> clazz, boolean modal, boolean noclose) { + int style = SWT.TITLE | SWT.BORDER | SWT.RESIZE; + if (modal) + style |= SWT.APPLICATION_MODAL; + if (!noclose) + style |= SWT.CLOSE; + Shell dialogShell = new Shell(mainShell, style); // populate dialogShell dialogShell.setLayout(new GridLayout(1, false)); LOGGER.debug(clazz.getDeclaredClasses()); @@ -90,6 +95,17 @@ public abstract class MainWindow { return; } + if (noclose) { + // Add listener that will cancel a close request via the X. + // (Some platforms don't allow disabling it via style, so this is used to be safe) + dialogShell.addListener(SWT.Close, new Listener() { + @Override + public void handleEvent(Event event) { + event.doit = false; + } + }); + } + dialogShell.layout(); dialogShell.pack(); Gui.limitShellSize(dialogShell); @@ -105,19 +121,14 @@ public abstract class MainWindow { static { // init SWT stuff mainShell = new Shell(Gui.display, SWT.SHELL_TRIM | SWT.CENTER); - - // TODO catch the close button to cleanup -// mainShell.addListener(SWT.Close, new Listener() -// { -// public void handleEvent(Event event) -// { -// int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO; -// MessageBox messageBox = new MessageBox(mainShell, style); -// messageBox.setText("Information"); -// messageBox.setMessage("Close the shell?"); -// event.doit = messageBox.open() == SWT.YES; -// } -// }); + + // Catch the close button (X) + mainShell.addListener(SWT.Close, new Listener() { + public void handleEvent(Event event) { + MainWindow.askApplicationQuit(); + event.doit = false; + } + }); // Set up thrift error message displaying ThriftManager.setErrorCallback(new ErrorCallback() { @@ -153,15 +164,15 @@ public abstract class MainWindow { @Override public void handleEvent(Event event) { if (event.character == 17) // Ctrl-Q = Quit - System.exit(0); + askApplicationQuit(); } }); - + mainShell.addDisposeListener(new DisposeListener() { - + @Override public void widgetDisposed(DisposeEvent e) { - System.exit(0); + askApplicationQuit(); } }); @@ -188,7 +199,7 @@ public abstract class MainWindow { // here we can check for Session information if (Session.getSatelliteToken() == null) { // User did not login, show the login mask - openPopup(LoginWindow.class, true); + openPopup(LoginWindow.class, true, true); } } @@ -203,6 +214,16 @@ public abstract class MainWindow { } /** + * Request application quit. Will show a message box asking the user for + * confirmation. + */ + protected static void askApplicationQuit() { + // TODO: Only ask if an upload or download is running,, wizard is open etc.. + if (showMessageBox("Are you sure you want to quit?", MessageType.QUESTION_YESNO, null, null)) + System.exit(0); + } + + /** * Register a page that can be displayed in the main window. * * @param window @@ -217,8 +238,6 @@ public abstract class MainWindow { window.setLayoutData(gd); window.setVisible(false); } - - private static void createMenu() { // the File menu button @@ -234,7 +253,7 @@ public abstract class MainWindow { exitItem.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - System.exit(0); + askApplicationQuit(); } }); @@ -250,7 +269,7 @@ public abstract class MainWindow { disclaimerItem.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - MainWindow.openPopup(DisclaimerWindow.class, false); + MainWindow.openPopup(DisclaimerWindow.class, false, false); } }); @@ -259,7 +278,7 @@ public abstract class MainWindow { virtualizerNoticeItem.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - MainWindow.openPopup(VirtualizerNoticeWindow.class, false); + MainWindow.openPopup(VirtualizerNoticeWindow.class, false, false); } }); mainShell.setMenuBar(menuBar); |