JAL-797 Cmd-W act WHEN_IN_FOCUSED_WINDOW bug/JAL-797
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 21 May 2018 14:31:34 +0000 (15:31 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 21 May 2018 14:31:34 +0000 (15:31 +0100)
src/jalview/gui/Desktop.java

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