JAL-2326 updated references of JOptionPane to JvOptionPane
[jalview.git] / src / jalview / gui / Finder.java
index 5a2271d..af23ceb 100755 (executable)
  */
 package jalview.gui;
 
-import jalview.datamodel.SearchResults;
+import jalview.datamodel.SearchResultMatchI;
+import jalview.datamodel.SearchResultsI;
 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.KeyEvent;
 import java.util.Vector;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 
+import javax.swing.AbstractAction;
+import javax.swing.JComponent;
 import javax.swing.JInternalFrame;
 import javax.swing.JLayeredPane;
 import javax.swing.JOptionPane;
+import javax.swing.KeyStroke;
 
 /**
  * Performs the menu option for searching the alignment, for the next or all
@@ -52,7 +58,7 @@ public class Finder extends GFinder
 
   private static final int WIDTH = 340;
 
-  AlignViewport av;
+  AlignmentViewport av;
 
   AlignmentPanel ap;
 
@@ -62,7 +68,7 @@ public class Finder extends GFinder
 
   int resIndex = -1;
 
-  SearchResults searchResults;
+  SearchResultsI searchResults;
 
   /**
    * Creates a new Finder object with no associated viewport or panel.
@@ -80,7 +86,7 @@ public class Finder extends GFinder
    * @param viewport
    * @param alignPanel
    */
-  public Finder(AlignViewport viewport, AlignmentPanel alignPanel)
+  public Finder(AlignmentViewport viewport, AlignmentPanel alignPanel)
   {
     av = viewport;
     ap = alignPanel;
@@ -88,6 +94,7 @@ public class Finder extends GFinder
     frame = new JInternalFrame();
     frame.setContentPane(this);
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
+    addEscapeHandler();
     Desktop.addInternalFrame(frame, MessageManager.getString("label.find"),
             WIDTH, HEIGHT);
 
@@ -95,10 +102,37 @@ public class Finder extends GFinder
   }
 
   /**
+   * Add a handler for the Escape key when the window has focus
+   */
+  private void addEscapeHandler()
+  {
+    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
+            KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
+    getRootPane().getActionMap().put("Cancel", new AbstractAction()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        escapeActionPerformed();
+      }
+    });
+  }
+
+  /**
+   * Close the panel on Escape key press
+   */
+  protected void escapeActionPerformed()
+  {
+    setVisible(false);
+    frame.dispose();
+  }
+
+  /**
    * Performs the 'Find Next' action.
    * 
    * @param e
    */
+  @Override
   public void findNext_actionPerformed(ActionEvent e)
   {
     if (getFocusedViewport())
@@ -112,6 +146,7 @@ public class Finder extends GFinder
    * 
    * @param e
    */
+  @Override
   public void findAll_actionPerformed(ActionEvent e)
   {
     if (getFocusedViewport())
@@ -167,19 +202,22 @@ public class Finder extends GFinder
    * @param e
    *          DOCUMENT ME!
    */
+  @Override
   public void createNewGroup_actionPerformed(ActionEvent e)
   {
     SequenceI[] seqs = new SequenceI[searchResults.getSize()];
     SequenceFeature[] features = new SequenceFeature[searchResults
             .getSize()];
 
-    for (int i = 0; i < searchResults.getSize(); i++)
+    int i = 0;
+    for (SearchResultMatchI match : searchResults.getResults())
     {
-      seqs[i] = searchResults.getResultSequence(i).getDatasetSequence();
+      seqs[i] = match.getSequence().getDatasetSequence();
 
       features[i] = new SequenceFeature(textfield.getText().trim(),
-              "Search Results", null, searchResults.getResultStart(i),
-              searchResults.getResultEnd(i), "Search Results");
+              "Search Results", null, match.getStart(), match.getEnd(),
+              "Search Results");
+      i++;
     }
 
     if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
@@ -225,7 +263,7 @@ public class Finder extends GFinder
 
     searchResults = finder.getSearchResults(); // find(regex,
     // caseSensitive.isSelected(), )
-    Vector idMatch = finder.getIdMatch();
+    Vector<SequenceI> idMatch = finder.getIdMatch();
     boolean haveResults = false;
     // set or reset the GUI
     if ((idMatch.size() > 0))
@@ -255,9 +293,9 @@ public class Finder extends GFinder
     // 'SelectRegion' selection
     if (!haveResults)
     {
-      JOptionPane.showInternalMessageDialog(this,
+      JvOptionPane.showInternalMessageDialog(this,
               MessageManager.getString("label.finished_searching"), null,
-              JOptionPane.INFORMATION_MESSAGE);
+              JvOptionPane.INFORMATION_MESSAGE);
       resIndex = -1;
       seqIndex = 0;
     }
@@ -277,8 +315,8 @@ public class Finder extends GFinder
           message += searchResults.getSize()
                   + " subsequence matches found.";
         }
-        JOptionPane.showInternalMessageDialog(this, message, null,
-                JOptionPane.INFORMATION_MESSAGE);
+        JvOptionPane.showInternalMessageDialog(this, message, null,
+                JvOptionPane.INFORMATION_MESSAGE);
         resIndex = -1;
         seqIndex = 0;
       }
@@ -300,9 +338,9 @@ public class Finder extends GFinder
     {
       return false;
     }
-    JOptionPane.showInternalMessageDialog(this, error,
+    JvOptionPane.showInternalMessageDialog(this, error,
             MessageManager.getString("label.invalid_search"), // $NON-NLS-1$
-            JOptionPane.ERROR_MESSAGE);
+            JvOptionPane.ERROR_MESSAGE);
     return true;
   }