JAL-2933 remember Find position per viewport
[jalview.git] / src / jalview / appletgui / Finder.java
index 75d9b9e..f44387f 100644 (file)
  */
 package jalview.appletgui;
 
-import jalview.datamodel.SearchResults;
+import jalview.api.AlignViewportI;
+import jalview.datamodel.SearchResultMatchI;
+import jalview.datamodel.SearchResultsI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.util.MessageManager;
-import jalview.viewmodel.AlignmentViewport;
 
 import java.awt.Button;
 import java.awt.Checkbox;
@@ -40,24 +41,49 @@ import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Vector;
 
 public class Finder extends Panel implements ActionListener
 {
-  AlignmentViewport av;
+  private AlignViewportI av;
 
-  AlignmentPanel ap;
+  private AlignmentPanel ap;
 
-  Frame frame;
+  private TextField textfield = new TextField();
 
-  SearchResults searchResults;
+  private Button findAll = new Button();
 
-  int seqIndex = 0;
+  private Button findNext = new Button();
 
-  int resIndex = -1;
+  private Button createFeatures = new Button();
+
+  private Checkbox caseSensitive = new Checkbox();
+
+  private Checkbox searchDescription = new Checkbox();
+
+  private SearchResultsI searchResults;
+
+  /*
+   * sequence index and residue position of last match,
+   * for current search and per viewport
+   */
+  private int seqIndex = 0;
+
+  private int resIndex = -1;
+
+  Map<AlignViewportI, Integer> seqIndices;
+
+  Map<AlignViewportI, Integer> resIndices;
 
   public Finder(final AlignmentPanel ap)
   {
+    seqIndices = new HashMap<>();
+    resIndices = new HashMap<>();
+
     try
     {
       jbInit();
@@ -69,13 +95,14 @@ public class Finder extends Panel implements ActionListener
 
     this.av = ap.av;
     this.ap = ap;
-    frame = new Frame();
+    Frame frame = new Frame();
     frame.add(this);
     jalview.bin.JalviewLite.addFrame(frame,
             MessageManager.getString("action.find"), 340, 120);
     frame.repaint();
     frame.addWindowListener(new WindowAdapter()
     {
+      @Override
       public void windowClosing(WindowEvent evt)
       {
         ap.highlightSearchResults(null);
@@ -84,6 +111,7 @@ public class Finder extends Panel implements ActionListener
     textfield.requestFocus();
   }
 
+  @Override
   public void actionPerformed(ActionEvent evt)
   {
     if (evt.getSource() == textfield)
@@ -102,25 +130,23 @@ public class Finder extends Panel implements ActionListener
       seqIndex = 0;
       doSearch(true);
     }
-    else if (evt.getSource() == createNewGroup)
+    else if (evt.getSource() == createFeatures)
     {
-      createNewGroup_actionPerformed();
+      createFeatures_actionPerformed();
     }
   }
 
-  public void createNewGroup_actionPerformed()
+  public void createFeatures_actionPerformed()
   {
-    SequenceI[] seqs = new SequenceI[searchResults.getSize()];
-    SequenceFeature[] features = new SequenceFeature[searchResults
-            .getSize()];
+    List<SequenceI> seqs = new ArrayList<>();
+    List<SequenceFeature> features = new ArrayList<>();
+    String searchString = textfield.getText().trim();
 
-    for (int i = 0; i < searchResults.getSize(); i++)
+    for (SearchResultMatchI match : searchResults.getResults())
     {
-      seqs[i] = searchResults.getResultSequence(i);
-
-      features[i] = new SequenceFeature(textfield.getText().trim(),
-              "Search Results", null, searchResults.getResultStart(i),
-              searchResults.getResultEnd(i), "Search Results");
+      seqs.add(match.getSequence().getDatasetSequence());
+      features.add(new SequenceFeature(searchString, "Search Results",
+              match.getStart(), match.getEnd(), "Search Results"));
     }
 
     if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
@@ -132,48 +158,50 @@ public class Finder extends Panel implements ActionListener
     }
   }
 
-  void doSearch(boolean findAll)
+  void doSearch(boolean doFindAll)
   {
     if (ap.av.applet.currentAlignFrame != null)
     {
       ap = ap.av.applet.currentAlignFrame.alignPanel;
       av = ap.av;
+      seqIndex = 0;
+      resIndex = -1;
+      if (seqIndices.containsKey(av))
+      {
+        seqIndex = seqIndices.get(av).intValue();
+      }
+      if (resIndices.containsKey(av))
+      {
+        resIndex = resIndices.get(av).intValue();
+      }
     }
-    createNewGroup.setEnabled(false);
+    createFeatures.setEnabled(false);
     jalview.analysis.Finder finder = new jalview.analysis.Finder(
             av.getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
     finder.setCaseSensitive(caseSensitive.getState());
     finder.setIncludeDescription(searchDescription.getState());
-    finder.setFindAll(findAll);
+    finder.setFindAll(doFindAll);
 
     String searchString = textfield.getText();
 
     finder.find(searchString);
+
     seqIndex = finder.getSeqIndex();
     resIndex = finder.getResIndex();
+    seqIndices.put(av, seqIndex);
+    resIndices.put(av, resIndex);
     searchResults = finder.getSearchResults();
-    Vector idMatch = finder.getIdMatch();
-    boolean haveResults = false;
-    // set or reset the GUI
-    if ((idMatch.size() > 0))
-    {
-      haveResults = true;
-      ap.idPanel.highlightSearchResults(idMatch);
-    }
-    else
-    {
-      ap.idPanel.highlightSearchResults(null);
-    }
 
-    if (searchResults.getSize() > 0)
-    {
-      haveResults = true;
-      createNewGroup.setEnabled(true);
+    Vector<SequenceI> idMatch = finder.getIdMatch();
+    ap.idPanel.highlightSearchResults(idMatch);
 
+    if (searchResults.isEmpty())
+    {
+      searchResults = null;
     }
     else
     {
-      searchResults = null;
+      createFeatures.setEnabled(true);
     }
 
     // if allResults is null, this effectively switches displaySearch flag in
@@ -181,19 +209,19 @@ public class Finder extends Panel implements ActionListener
     ap.highlightSearchResults(searchResults);
     // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
     // 'SelectRegion' selection
-    if (!haveResults)
+    if (idMatch.isEmpty() && searchResults == null)
     {
-      ap.alignFrame.statusBar.setText(MessageManager
-              .getString("label.finished_searching"));
+      ap.alignFrame.statusBar.setText(
+              MessageManager.getString("label.finished_searching"));
       resIndex = -1;
       seqIndex = 0;
     }
     else
     {
-      if (findAll)
+      if (doFindAll)
       {
-        String message = (idMatch.size() > 0) ? "" + idMatch.size()
-                + " IDs" : "";
+        String message = (idMatch.size() > 0) ? "" + idMatch.size() + " IDs"
+                : "";
         if (idMatch.size() > 0 && searchResults != null
                 && searchResults.getSize() > 0)
         {
@@ -203,42 +231,25 @@ public class Finder extends Panel implements ActionListener
         {
           message += searchResults.getSize() + " subsequence matches.";
         }
-        ap.alignFrame.statusBar.setText(MessageManager.formatMessage(
-                "label.search_results", new String[] { searchString,
-                    message }));
+        ap.alignFrame.statusBar.setText(MessageManager
+                .formatMessage("label.search_results", new String[]
+                { searchString, message }));
 
       }
       else
       {
         // TODO: indicate sequence and matching position in status bar
-        ap.alignFrame.statusBar.setText(MessageManager.formatMessage(
-                "label.found_match_for", new String[] { searchString }));
+        ap.alignFrame.statusBar.setText(MessageManager
+                .formatMessage("label.found_match_for", new String[]
+                { searchString }));
       }
     }
   }
 
-  Label jLabel1 = new Label();
-
-  protected TextField textfield = new TextField();
-
-  protected Button findAll = new Button();
-
-  protected Button findNext = new Button();
-
-  Panel actionsPanel = new Panel();
-
-  GridLayout gridLayout1 = new GridLayout();
-
-  protected Button createNewGroup = new Button();
-
-  Checkbox caseSensitive = new Checkbox();
-
-  Checkbox searchDescription = new Checkbox();
-
   private void jbInit() throws Exception
   {
+    Label jLabel1 = new Label(MessageManager.getString("action.find"));
     jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
-    jLabel1.setText(MessageManager.getString("action.find"));
     jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
     this.setLayout(null);
     textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
@@ -246,9 +257,10 @@ public class Finder extends Panel implements ActionListener
     textfield.setBounds(new Rectangle(40, 17, 133, 21));
     textfield.addKeyListener(new java.awt.event.KeyAdapter()
     {
+      @Override
       public void keyTyped(KeyEvent e)
       {
-        textfield_keyTyped(e);
+        textfield_keyTyped();
       }
     });
     textfield.addActionListener(this);
@@ -259,24 +271,27 @@ public class Finder extends Panel implements ActionListener
     findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
     findNext.setLabel(MessageManager.getString("action.find_next"));
     findNext.addActionListener(this);
+
+    Panel actionsPanel = new Panel();
     actionsPanel.setBounds(new Rectangle(195, 5, 141, 64));
+    GridLayout gridLayout1 = new GridLayout();
     actionsPanel.setLayout(gridLayout1);
     gridLayout1.setHgap(0);
     gridLayout1.setRows(3);
     gridLayout1.setVgap(2);
-    createNewGroup.setEnabled(false);
-    createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
-    createNewGroup.setLabel(MessageManager.getString("label.new_feature"));
-    createNewGroup.addActionListener(this);
+    createFeatures.setEnabled(false);
+    createFeatures.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
+    createFeatures.setLabel(MessageManager.getString("label.new_feature"));
+    createFeatures.addActionListener(this);
     caseSensitive.setLabel(MessageManager.getString("label.match_case"));
     caseSensitive.setBounds(new Rectangle(30, 39, 126, 23));
 
-    searchDescription.setLabel(MessageManager
-            .getString("label.include_description"));
+    searchDescription.setLabel(
+            MessageManager.getString("label.include_description"));
     searchDescription.setBounds(new Rectangle(30, 59, 170, 23));
     actionsPanel.add(findNext, null);
     actionsPanel.add(findAll, null);
-    actionsPanel.add(createNewGroup, null);
+    actionsPanel.add(createFeatures, null);
     this.add(caseSensitive);
     this.add(textfield, null);
     this.add(jLabel1, null);
@@ -284,7 +299,7 @@ public class Finder extends Panel implements ActionListener
     this.add(searchDescription);
   }
 
-  void textfield_keyTyped(KeyEvent e)
+  void textfield_keyTyped()
   {
     findNext.setEnabled(true);
   }