From 6481f37ec5a68050d4dab8aebdfcb99a3dc15d86 Mon Sep 17 00:00:00 2001 From: kiramt Date: Wed, 10 Jan 2018 09:21:54 +0000 Subject: [PATCH] JAL-797: Added keybindings so that ctrl- or cmd-W close internal frames --- src/jalview/gui/Desktop.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 128481c..36bf551 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -68,6 +68,7 @@ import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -920,6 +921,8 @@ public class Desktop extends jalview.jbgui.GDesktop } }); + setKeyBindings(frame); + desktop.add(frame); windowMenu.add(menuItem); @@ -939,6 +942,45 @@ public class Desktop extends jalview.jbgui.GDesktop } } + /** + * 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 + { + 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 + 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); + } + @Override public void lostOwnership(Clipboard clipboard, Transferable contents) { -- 1.7.10.2