diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/MPGuiConfig.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/MPGuiConfig.java index b58c5a86..926d7e0b 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/MPGuiConfig.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/MPGuiConfig.java @@ -35,6 +35,7 @@ public class MPGuiConfig extends MPConfig { Boolean checkForUpdates; Boolean stayResident; + Boolean logoutOnClose; public boolean checkForUpdates() { return (checkForUpdates != null)? checkForUpdates: @@ -55,4 +56,13 @@ public class MPGuiConfig extends MPConfig { this.stayResident = stayResident; setChanged(); } + + public boolean logoutOnClose() { + return (logoutOnClose != null)? logoutOnClose: true; + } + + public void setLogoutOnClose(final boolean logoutOnClose) { + this.logoutOnClose = logoutOnClose; + setChanged(); + } } diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/MPGuiConstants.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/MPGuiConstants.java index 54b48735..924167df 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/MPGuiConstants.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/MPGuiConstants.java @@ -11,4 +11,6 @@ import javax.swing.*; public final class MPGuiConstants { public static final KeyStroke ui_hotkey = KeyStroke.getKeyStroke( KeyEvent.VK_P, InputEvent.CTRL_DOWN_MASK | InputEvent.META_DOWN_MASK ); + + public static final int FORCE_CLOSE_KEY = KeyEvent.VK_ALT; } diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/MasterPasswordFrame.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/MasterPasswordFrame.java index 50cc607d..643ad381 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/MasterPasswordFrame.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/MasterPasswordFrame.java @@ -2,6 +2,7 @@ package com.lyndir.masterpassword.gui.view; import com.lyndir.lhunath.opal.system.logging.Logger; import com.lyndir.masterpassword.gui.MPGuiConfig; +import com.lyndir.masterpassword.gui.MPGuiConstants; import com.lyndir.masterpassword.gui.util.Components; import com.lyndir.masterpassword.gui.util.Res; import com.lyndir.masterpassword.model.impl.MPFileUserManager; @@ -19,8 +20,6 @@ public class MasterPasswordFrame extends JFrame { private static final Logger logger = Logger.get( MasterPasswordFrame.class ); - private final UserContentPanel userContent; - @SuppressWarnings("MagicNumber") public MasterPasswordFrame() { super( "Master Password" ); @@ -32,14 +31,16 @@ public class MasterPasswordFrame extends JFrame { root.add( Components.strut() ); root.add( userPanel = Components.panel( new BorderLayout( 0, 0 ) ) ); + final UserContentPanel userContent = new UserContentPanel(); userPanel.add( Components.borderPanel( BorderFactory.createBevelBorder( BevelBorder.RAISED, Res.colors().controlBorder(), Res.colors().frameBg() ), - Res.colors().controlBg(), BoxLayout.PAGE_AXIS, userContent = new UserContentPanel() ), BorderLayout.CENTER ); + Res.colors().controlBg(), BoxLayout.PAGE_AXIS, userContent), BorderLayout.CENTER ); userPanel.add( userContent.getUserToolbar(), BorderLayout.LINE_START ); userPanel.add( userContent.getSiteToolbar(), BorderLayout.LINE_END ); - addComponentListener( new ComponentHandler() ); - addWindowListener( new WindowHandler() ); + final WindowHandler windowHandler = new WindowHandler(); + addWindowListener(windowHandler); + KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(windowHandler); setPreferredSize( new Dimension( 800, 560 ) ); setDefaultCloseOperation( DISPOSE_ON_CLOSE ); pack(); @@ -48,22 +49,30 @@ public class MasterPasswordFrame extends JFrame { setLocationByPlatform( true ); } - private class ComponentHandler extends ComponentAdapter { - @Override - public void componentShown(final ComponentEvent e) { - MPFileUserManager.get().reload(); - userContent.transferFocus(); - } - } + private static class WindowHandler extends WindowAdapter implements KeyEventDispatcher { - - private static class WindowHandler extends WindowAdapter { + private boolean forceClose = false; @Override public void windowClosed(final WindowEvent e) { - if (!MPGuiConfig.get().stayResident()) + if (!MPGuiConfig.get().stayResident() || forceClose) { + System.exit( 0 ); + } + else if (MPGuiConfig.get().logoutOnClose()) { + + MPFileUserManager.get().reload(); + } + } + + @Override + public boolean dispatchKeyEvent(KeyEvent e) { + + if (e.getKeyCode() == MPGuiConstants.FORCE_CLOSE_KEY) + forceClose = (e.getID() == KeyEvent.KEY_PRESSED); + + return false; } } } diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java index 8ba63405..fcff949d 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java @@ -253,12 +253,19 @@ public class UserContentPanel extends JPanel implements MasterPassword.Listener, private void showAppPreferences() { Component checkForUpdates = Components.checkBox("Check For Updates", MPGuiConfig.get().checkForUpdates(), MPGuiConfig.get()::setCheckForUpdates); - Component stayResident = Components.checkBox(strf("Stay Resident (reactivate with %s+%s)", + JCheckBox stayResident = Components.checkBox(strf("Stay running in background when closed (reactivate with %s+%s)", InputEvent.getModifiersExText(MPGuiConstants.ui_hotkey.getModifiers()), KeyEvent.getKeyText(MPGuiConstants.ui_hotkey.getKeyCode())), MPGuiConfig.get().stayResident(), MPGuiConfig.get()::setStayResident); + stayResident.setToolTipText(strf("Hold %s while closing to force exit", KeyEvent.getKeyText(MPGuiConstants.FORCE_CLOSE_KEY))); - Components.showDialog(this, "Application Preferences", new JOptionPane(Components.panel(BoxLayout.PAGE_AXIS, checkForUpdates, stayResident))); + Component logoutOnClose = Components.checkBox("Logout current user on close", MPGuiConfig.get().logoutOnClose(), MPGuiConfig.get()::setLogoutOnClose); + + stayResident.addItemListener(e -> logoutOnClose.setEnabled(e.getStateChange() == ItemEvent.SELECTED)); + logoutOnClose.setEnabled(stayResident.isSelected()); + + Components.showDialog(this, "Application Preferences", + new JOptionPane(Components.panel(BoxLayout.PAGE_AXIS, checkForUpdates, stayResident, logoutOnClose))); } private enum ContentMode {