d328452950575333ad76b13f7a7b6a276b490061
[jalview.git] / src / jalview / gui / Finder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.FinderI;
25 import jalview.datamodel.SearchResultMatchI;
26 import jalview.datamodel.SearchResultsI;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.jbgui.GFinder;
30 import jalview.util.MessageManager;
31
32 import java.awt.Dimension;
33 import java.awt.Graphics;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.KeyEvent;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.regex.Pattern;
41 import java.util.regex.PatternSyntaxException;
42
43 import javax.swing.AbstractAction;
44 import javax.swing.JComponent;
45 import javax.swing.JInternalFrame;
46 import javax.swing.JLayeredPane;
47 import javax.swing.KeyStroke;
48 import javax.swing.event.InternalFrameAdapter;
49 import javax.swing.event.InternalFrameEvent;
50
51 /**
52  * Performs the menu option for searching the alignment, for the next or all
53  * matches. If matches are found, they are highlighted, and the user has the
54  * option to create a new feature on the alignment for the matched positions.
55  * 
56  * Searches can be for a simple base sequence, or may use a regular expression.
57  * Any gaps are ignored.
58  * 
59  * @author $author$
60  * @version $Revision$
61  */
62 public class Finder extends GFinder
63 {
64   private static final int MIN_WIDTH = 350;
65
66   private static final int MIN_HEIGHT = 120;
67
68   private static final int MY_HEIGHT = 150;
69
70   private static final int MY_WIDTH = 400;
71
72   private AlignViewportI av;
73
74   private AlignmentPanel ap;
75
76   private JInternalFrame frame;
77
78   /*
79    * Finder agent per viewport searched
80    */
81   private Map<AlignViewportI, FinderI> finders;
82
83   private SearchResultsI searchResults;
84
85   /**
86    * Constructor given an associated alignment panel. Constructs and displays an
87    * internal frame where the user can enter a search string.
88    * 
89    * @param alignPanel
90    */
91   public Finder(AlignmentPanel alignPanel)
92   {
93     av = alignPanel.getAlignViewport();
94     ap = alignPanel;
95     finders = new HashMap<>();
96     frame = new JInternalFrame();
97     frame.setContentPane(this);
98     frame.setLayer(JLayeredPane.PALETTE_LAYER);
99     frame.addInternalFrameListener(
100             new InternalFrameAdapter()
101             {
102               @Override
103               public void internalFrameClosing(InternalFrameEvent e)
104               {
105                 closeAction();
106               }
107             });
108     addEscapeHandler();
109
110     Desktop.addInternalFrame(frame, MessageManager.getString("label.find"),
111             true, MY_WIDTH, MY_HEIGHT, true, true);
112     searchBox.getComponent().requestFocus();
113   }
114
115   /**
116    * Add a handler for the Escape key when the window has focus
117    */
118   private void addEscapeHandler()
119   {
120     getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
121             .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
122     getRootPane().getActionMap().put("Cancel", new AbstractAction()
123     {
124       @Override
125       public void actionPerformed(ActionEvent e)
126       {
127         closeAction();
128       }
129     });
130   }
131
132   /**
133    * Performs the 'Find Next' action on the alignment panel with focus
134    */
135   @Override
136   public void findNext_actionPerformed()
137   {
138     if (getFocusedViewport())
139     {
140       doSearch(false);
141     }
142   }
143
144   /**
145    * Performs the 'Find All' action on the alignment panel with focus
146    */
147   @Override
148   public void findAll_actionPerformed()
149   {
150     if (getFocusedViewport())
151     {
152       doSearch(true);
153     }
154   }
155
156   /**
157    * if !focusfixed and not in a desktop environment, checks that av and ap are
158    * valid. Otherwise, gets the topmost alignment window and sets av and ap
159    * accordingly. Also sets the 'ignore hidden' checkbox disabled if the viewport
160    * has no hidden columns.
161    * 
162    * @return false if no alignment window was found
163    */
164   boolean getFocusedViewport()
165   {
166     if (Desktop.desktop == null)
167     {
168       if (ap != null && av != null)
169       {
170         return true;
171       }
172       // we aren't in a desktop environment, so give up now.
173       return false;
174     }
175     // now checks further down the window stack to fix bug
176     // https://mantis.lifesci.dundee.ac.uk/view.php?id=36008
177     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
178     for (int f = 0; f < frames.length; f++)
179     {
180       JInternalFrame alignFrame = frames[f];
181       if (alignFrame != null && alignFrame instanceof AlignFrame
182               && !alignFrame.isIcon())
183       {
184         av = ((AlignFrame) alignFrame).viewport;
185         ap = ((AlignFrame) alignFrame).alignPanel;
186         ignoreHidden.setEnabled(av.hasHiddenColumns());
187         return true;
188       }
189     }
190     return false;
191   }
192
193   /**
194    * Opens a dialog that allows the user to create sequence features for the
195    * find match results
196    */
197   @Override
198   public void createFeatures_actionPerformed()
199   {
200     if (searchResults.isEmpty())
201     {
202       return; // shouldn't happen
203     }
204     List<SequenceI> seqs = new ArrayList<>();
205     List<SequenceFeature> features = new ArrayList<>();
206
207     String searchString = searchBox.getUserInput();
208     String desc = "Search Results";
209
210     /*
211      * assemble dataset sequences, and template new sequence features,
212      * for the amend features dialog
213      */
214     for (SearchResultMatchI match : searchResults.getResults())
215     {
216       seqs.add(match.getSequence().getDatasetSequence());
217       features.add(new SequenceFeature(searchString, desc, match.getStart(),
218               match.getEnd(), desc));
219     }
220
221     new FeatureEditor(ap, seqs, features, true).showDialog();
222   }
223
224   /**
225    * Search the alignment for the next or all matches. If 'all matches', a
226    * dialog is shown with the number of sequence ids and subsequences matched.
227    * 
228    * @param doFindAll
229    */
230   void doSearch(boolean doFindAll)
231   {
232     createFeatures.setEnabled(false);
233
234     String searchString = searchBox.getUserInput();
235
236     if (isInvalidSearchString(searchString))
237     {
238       return;
239     }
240     // TODO: extend finder to match descriptions, features and annotation, and
241     // other stuff
242     // TODO: add switches to control what is searched - sequences, IDS,
243     // descriptions, features
244     FinderI finder = finders.get(av);
245     if (finder == null)
246     {
247       /*
248        * first time we've searched this viewport
249        */
250       finder = new jalview.analysis.Finder(av);
251       finders.put(av, finder);
252     }
253
254     boolean isCaseSensitive = caseSensitive.isSelected();
255     boolean doSearchDescription = searchDescription.isSelected();
256     boolean skipHidden = ignoreHidden.isSelected();
257     if (doFindAll)
258     {
259       finder.findAll(searchString, isCaseSensitive, doSearchDescription,
260               skipHidden);
261     }
262     else
263     {
264       finder.findNext(searchString, isCaseSensitive, doSearchDescription,
265               skipHidden);
266     }
267
268     searchResults = finder.getSearchResults();
269     List<SequenceI> idMatch = finder.getIdMatches();
270     ap.getIdPanel().highlightSearchResults(idMatch);
271
272     if (searchResults.isEmpty())
273     {
274       searchResults = null;
275     }
276     else
277     {
278       createFeatures.setEnabled(true);
279     }
280
281     searchBox.updateCache();
282
283     ap.highlightSearchResults(searchResults);
284     // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
285     // 'SelectRegion' selection
286     if (idMatch.isEmpty() && searchResults == null)
287     {
288       JvOptionPane.showInternalMessageDialog(this,
289               MessageManager.getString("label.finished_searching"), null,
290               JvOptionPane.PLAIN_MESSAGE);
291     }
292     else
293     {
294       if (doFindAll)
295       {
296         // then we report the matches that were found
297         String message = (idMatch.size() > 0) ? "" + idMatch.size() + " IDs"
298                 : "";
299         if (searchResults != null)
300         {
301           if (idMatch.size() > 0 && searchResults.getCount() > 0)
302           {
303             message += " and ";
304           }
305           message += searchResults.getCount()
306                   + " subsequence matches found.";
307         }
308         JvOptionPane.showInternalMessageDialog(this, message, null,
309                 JvOptionPane.PLAIN_MESSAGE);
310       }
311     }
312   }
313
314   /**
315    * Displays an error dialog, and answers false, if the search string is
316    * invalid, else answers true.
317    * 
318    * @param searchString
319    * @return
320    */
321   protected boolean isInvalidSearchString(String searchString)
322   {
323     String error = getSearchValidationError(searchString);
324     if (error == null)
325     {
326       return false;
327     }
328     JvOptionPane.showInternalMessageDialog(this, error,
329             MessageManager.getString("label.invalid_search"), // $NON-NLS-1$
330             JvOptionPane.ERROR_MESSAGE);
331     return true;
332   }
333
334   /**
335    * Returns an error message string if the search string is invalid, else
336    * returns null.
337    * 
338    * Currently validation is limited to checking the string is not empty, and is
339    * a valid regular expression (simple searches for base sub-sequences will
340    * pass this test). Additional validations may be added in future if the
341    * search syntax is expanded.
342    * 
343    * @param searchString
344    * @return
345    */
346   protected String getSearchValidationError(String searchString)
347   {
348     String error = null;
349     if (searchString == null || searchString.length() == 0)
350     {
351       error = MessageManager.getString("label.invalid_search");
352     }
353     try
354     {
355       Pattern.compile(searchString);
356     } catch (PatternSyntaxException e)
357     {
358       error = MessageManager.getString("error.invalid_regex") + ": "
359               + e.getDescription();
360     }
361     return error;
362   }
363
364   protected void closeAction()
365   {
366     frame.setVisible(false);
367     frame.dispose();
368     searchBox.persistCache();
369     if (getFocusedViewport())
370     {
371       ap.alignFrame.requestFocus();
372     }
373   }
374
375   @Override
376   protected void paintComponent(Graphics g)
377   {
378     /*
379      * enable 'hidden regions' option only if
380      * 'top' viewport has hidden columns
381      */
382     getFocusedViewport();
383     super.paintComponent(g);
384   }
385 }