From 56b390641b07da301f3591785141ff09a261b22d Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 21 May 2018 15:31:34 +0100 Subject: [PATCH] JAL-797 Cmd-W act WHEN_IN_FOCUSED_WINDOW --- src/jalview/gui/Desktop.java | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 36bf551..e2735d3 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -92,10 +92,13 @@ import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.ActionMap; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.DefaultDesktopManager; import javax.swing.DesktopManager; +import javax.swing.InputMap; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; @@ -943,42 +946,39 @@ public class Desktop extends jalview.jbgui.GDesktop } /** - * Add key bindings to a JInternalFrame so that ctrl-W and cmd-W will close + * Add key bindings to a JInternalFrame so that Ctrl-W and Cmd-W will close * the window * * @param frame - * the JInternalFrame to set the key bindings for */ private static void setKeyBindings(JInternalFrame frame) { - // create a close action - class CloseAction extends AbstractAction + @SuppressWarnings("serial") + final Action closeAction = new AbstractAction() { - private JInternalFrame frame; - - CloseAction(JInternalFrame frame) - { - this.frame = frame; - } - @Override public void actionPerformed(ActionEvent e) { frame.dispose(); } - } + }; - final CloseAction closeAction = new CloseAction(frame); - - // set up keybindings for ctrl-W and cmd-W + /* + * set up key bindings for Ctrl-W and Cmd-W, with the same (Close) action + */ KeyStroke ctrlWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_DOWN_MASK); KeyStroke cmdWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); - frame.getInputMap().put(ctrlWKey, ctrlWKey.toString()); - frame.getActionMap().put(ctrlWKey.toString(), closeAction); - frame.getInputMap().put(cmdWKey, ctrlWKey.toString()); - frame.getActionMap().put(cmdWKey.toString(), closeAction); + + InputMap inputMap = frame + .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); + String ctrlW = ctrlWKey.toString(); + inputMap.put(ctrlWKey, ctrlW); + inputMap.put(cmdWKey, ctrlW); + + ActionMap actionMap = frame.getActionMap(); + actionMap.put(ctrlW, closeAction); } @Override -- 1.7.10.2