JAL-3060 extract FeatureEditor class
[jalview.git] / src / jalview / gui / Finder.java
index 81de48f..244e6df 100755 (executable)
@@ -42,6 +42,7 @@ import javax.swing.JComponent;
 import javax.swing.JInternalFrame;
 import javax.swing.JLayeredPane;
 import javax.swing.KeyStroke;
+import javax.swing.event.InternalFrameEvent;
 
 /**
  * Performs the menu option for searching the alignment, for the next or all
@@ -100,11 +101,20 @@ public class Finder extends GFinder
     frame = new JInternalFrame();
     frame.setContentPane(this);
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
+    frame.addInternalFrameListener(
+            new javax.swing.event.InternalFrameAdapter()
+            {
+              @Override
+              public void internalFrameClosing(InternalFrameEvent e)
+              {
+                closeAction();
+              }
+            });
     addEscapeHandler();
     Desktop.addInternalFrame(frame, MessageManager.getString("label.find"),
             MY_WIDTH, MY_HEIGHT);
     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
-    textfield.requestFocus();
+    searchBox.requestFocus();
   }
 
   /**
@@ -112,28 +122,19 @@ public class Finder extends GFinder
    */
   private void addEscapeHandler()
   {
-    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
-            KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
+    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();
+        closeAction();
       }
     });
   }
 
   /**
-   * Close the panel on Escape key press
-   */
-  protected void escapeActionPerformed()
-  {
-    setVisible(false);
-    frame.dispose();
-  }
-
-  /**
    * Performs the 'Find Next' action.
    * 
    * @param e
@@ -212,7 +213,7 @@ public class Finder extends GFinder
     List<SequenceI> seqs = new ArrayList<SequenceI>();
     List<SequenceFeature> features = new ArrayList<SequenceFeature>();
 
-    String searchString = textfield.getText().trim();
+    String searchString = searchBox.getEditor().getItem().toString().trim();
     String desc = "Search Results";
 
     /*
@@ -222,22 +223,25 @@ public class Finder extends GFinder
     for (SearchResultMatchI match : searchResults.getResults())
     {
       seqs.add(match.getSequence().getDatasetSequence());
-      features.add(new SequenceFeature(searchString, desc,
-              match
-              .getStart(), match.getEnd(), desc));
+      features.add(new SequenceFeature(searchString, desc, match.getStart(),
+              match.getEnd(), desc));
     }
 
-    if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
-            features, true, ap))
+    new FeatureEditor(ap).amendFeatures(seqs, features, true, new Runnable()
     {
-      /*
-       * ensure feature display is turned on to show the new features,
-       * and remove them as highlighted regions
-       */
-      ap.alignFrame.showSeqFeatures.setSelected(true);
-      av.setShowSequenceFeatures(true);
-      ap.highlightSearchResults(null);
-    }
+      @Override
+              public void run()
+              {
+
+                /*
+                 * ensure feature display is turned on to show the new features,
+                 * and remove them as highlighted regions
+                 */
+                ap.alignFrame.showSeqFeatures.setSelected(true);
+                av.setShowSequenceFeatures(true);
+                ap.highlightSearchResults(null);
+              }
+            });
   }
 
   /**
@@ -250,7 +254,7 @@ public class Finder extends GFinder
   {
     createFeatures.setEnabled(false);
 
-    String searchString = textfield.getText().trim();
+    String searchString = searchBox.getUserInput().trim();
 
     if (isInvalidSearchString(searchString))
     {
@@ -315,8 +319,8 @@ public class Finder extends GFinder
       if (doFindAll)
       {
         // then we report the matches that were found
-        String message = (idMatch.size() > 0) ? "" + idMatch.size()
-                + " IDs" : "";
+        String message = (idMatch.size() > 0) ? "" + idMatch.size() + " IDs"
+                : "";
         if (searchResults != null)
         {
           if (idMatch.size() > 0 && searchResults.getSize() > 0)
@@ -332,7 +336,7 @@ public class Finder extends GFinder
         seqIndex = 0;
       }
     }
-
+    searchBox.updateCache();
   }
 
   /**
@@ -384,4 +388,15 @@ public class Finder extends GFinder
     }
     return error;
   }
+
+  protected void closeAction()
+  {
+    frame.setVisible(false);
+    frame.dispose();
+    searchBox.persistCache();
+    if (getFocusedViewport())
+    {
+      ap.alignFrame.requestFocus();
+    }
+  }
 }