JAL-797: Added keybindings so that ctrl- or cmd-W close internal frames
authorkiramt <k.mourao@dundee.ac.uk>
Wed, 10 Jan 2018 09:21:54 +0000 (09:21 +0000)
committerkiramt <k.mourao@dundee.ac.uk>
Wed, 10 Jan 2018 09:21:54 +0000 (09:21 +0000)
src/jalview/gui/Desktop.java

index 128481c..36bf551 100644 (file)
@@ -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)
   {