From: gmungoc Date: Mon, 21 May 2018 14:31:34 +0000 (+0100) Subject: JAL-797 Cmd-W act WHEN_IN_FOCUSED_WINDOW X-Git-Tag: Release_2_11_0~36^2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=56b390641b07da301f3591785141ff09a261b22d;p=jalview.git JAL-797 Cmd-W act WHEN_IN_FOCUSED_WINDOW --- 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