From: kiramt Date: Wed, 10 Jan 2018 09:21:54 +0000 (+0000) Subject: JAL-797: Added keybindings so that ctrl- or cmd-W close internal frames X-Git-Tag: Release_2_11_0~36^2~1 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=6481f37ec5a68050d4dab8aebdfcb99a3dc15d86;p=jalview.git JAL-797: Added keybindings so that ctrl- or cmd-W close internal frames --- 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) {