JAL-1652 Escape key now closes Find dialog
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 18 May 2015 10:29:18 +0000 (11:29 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 18 May 2015 10:29:18 +0000 (11:29 +0100)
src/jalview/gui/Finder.java

index 3cce82c..4397a48 100755 (executable)
  */
 package jalview.gui;
 
-import jalview.datamodel.SearchResults;
-import jalview.datamodel.SequenceFeature;
-import jalview.datamodel.SequenceI;
-import jalview.jbgui.GFinder;
-import jalview.util.MessageManager;
-import jalview.viewmodel.AlignmentViewport;
-
 import java.awt.event.ActionEvent;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.beans.PropertyVetoException;
 import java.util.Vector;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
@@ -36,6 +32,13 @@ import javax.swing.JInternalFrame;
 import javax.swing.JLayeredPane;
 import javax.swing.JOptionPane;
 
+import jalview.datamodel.SearchResults;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceI;
+import jalview.jbgui.GFinder;
+import jalview.util.MessageManager;
+import jalview.viewmodel.AlignmentViewport;
+
 /**
  * Performs the menu option for searching the alignment, for the next or all
  * matches. If matches are found, they are highlighted, and the user has the
@@ -89,6 +92,7 @@ public class Finder extends GFinder
     frame = new JInternalFrame();
     frame.setContentPane(this);
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
+    addKeyListener();
     Desktop.addInternalFrame(frame, MessageManager.getString("label.find"),
             WIDTH, HEIGHT);
 
@@ -96,6 +100,39 @@ public class Finder extends GFinder
   }
 
   /**
+   * Add a key listener that closes the find dialog on 'Esc' key press
+   */
+  private void addKeyListener()
+  {
+    /*
+     * add the listener to the field with focus
+     */
+    textfield.addKeyListener(new KeyAdapter()
+    {
+      @Override
+      public void keyPressed(KeyEvent evt)
+      {
+        switch (evt.getKeyCode())
+        {
+        case 27:
+          // close this window on Esc key
+          Finder.this.setVisible(false);
+          try
+          {
+            Finder.this.frame.setClosed(true);
+          } catch (PropertyVetoException e)
+          {
+            // ignore
+          }
+          break;
+        default:
+          break;
+        }
+      }
+    });
+  }
+
+  /**
    * Performs the 'Find Next' action.
    * 
    * @param e