2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.util.Locale;
25 import java.awt.Dimension;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.FocusAdapter;
28 import java.awt.event.FocusEvent;
29 import java.awt.event.KeyEvent;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.regex.Pattern;
35 import java.util.regex.PatternSyntaxException;
37 import javax.swing.AbstractAction;
38 import javax.swing.JComponent;
39 import javax.swing.JInternalFrame;
40 import javax.swing.JLayeredPane;
41 import javax.swing.KeyStroke;
42 import javax.swing.event.InternalFrameAdapter;
43 import javax.swing.event.InternalFrameEvent;
45 import jalview.api.AlignViewportI;
46 import jalview.api.FinderI;
47 import jalview.datamodel.SearchResultMatchI;
48 import jalview.datamodel.SearchResultsI;
49 import jalview.datamodel.SequenceFeature;
50 import jalview.datamodel.SequenceI;
51 import jalview.jbgui.GFinder;
52 import jalview.util.MessageManager;
55 * Performs the menu option for searching the alignment, for the next or all
56 * matches. If matches are found, they are highlighted, and the user has the
57 * option to create a new feature on the alignment for the matched positions.
59 * Searches can be for a simple base sequence, or may use a regular expression.
60 * Any gaps are ignored.
65 public class Finder extends GFinder
67 private static final int MIN_WIDTH = 350;
69 private static final int MIN_HEIGHT = 120;
71 private static final int MY_HEIGHT = 150;
73 private static final int MY_WIDTH = 400;
75 private AlignViewportI av;
77 private AlignmentPanel ap;
79 private JInternalFrame frame;
82 * Finder agent per viewport searched
84 private Map<AlignViewportI, FinderI> finders;
86 private SearchResultsI searchResults;
89 * true if Finder always acts on the same alignment,
90 * false if it acts on the alignment with focus
92 private boolean focusFixed;
95 * Constructor given an associated alignment panel. Constructs and displays an
96 * internal frame where the user can enter a search string. The Finder may
97 * have 'fixed focus' (always act the panel for which it is constructed), or
98 * not (acts on the alignment that has focus). An optional 'scope' may be
99 * added to be shown in the title of the Finder frame.
105 public Finder(AlignmentPanel alignPanel, boolean fixedFocus, String scope)
107 av = alignPanel.getAlignViewport();
109 focusFixed = fixedFocus;
110 finders = new HashMap<>();
111 frame = new JInternalFrame();
112 frame.setContentPane(this);
113 frame.setLayer(JLayeredPane.PALETTE_LAYER);
114 frame.addInternalFrameListener(new InternalFrameAdapter()
117 public void internalFrameClosing(InternalFrameEvent e)
122 frame.addFocusListener(new FocusAdapter()
125 public void focusGained(FocusEvent e)
128 * ensure 'ignore hidden columns' is only enabled
129 * if the alignment with focus has hidden columns
131 getFocusedViewport();
137 String title = MessageManager.getString("label.find");
140 title += " " + scope;
142 Desktop.addInternalFrame(frame, title, Desktop.FRAME_MAKE_VISIBLE, MY_WIDTH, MY_HEIGHT, Desktop.FRAME_ALLOW_RESIZE, Desktop.FRAME_ALLOW_ANY_SIZE);
143 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
144 searchBox.getComponent().requestFocus();
148 * Add a handler for the Escape key when the window has focus
150 private void addEscapeHandler()
152 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
153 .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
154 getRootPane().getActionMap().put("Cancel", new AbstractAction()
157 public void actionPerformed(ActionEvent e)
165 * Performs the 'Find Next' action on the alignment panel with focus
168 public void findNext_actionPerformed()
170 if (getFocusedViewport())
177 * Performs the 'Find All' action on the alignment panel with focus
180 public void findAll_actionPerformed()
182 if (getFocusedViewport())
189 * if !focusfixed and not in a desktop environment, checks that av and ap are
190 * valid. Otherwise, gets the topmost alignment window and sets av and ap
191 * accordingly. Also sets the 'ignore hidden' checkbox disabled if the
192 * viewport has no hidden columns.
194 * @return false if no alignment window was found
196 boolean getFocusedViewport()
198 if (focusFixed || Desktop.getDesktopPane() == null)
200 if (ap != null && av != null)
202 ignoreHidden.setEnabled(av.hasHiddenColumns());
205 // we aren't in a desktop environment, so give up now.
208 // now checks further down the window stack to fix bug
209 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36008
210 JInternalFrame[] frames = Desktop.getDesktopPane().getAllFrames();
211 for (int f = 0; f < frames.length; f++)
213 JInternalFrame alignFrame = frames[f];
214 if (alignFrame != null && alignFrame instanceof AlignFrame
215 && !alignFrame.isIcon())
217 av = ((AlignFrame) alignFrame).viewport;
218 ap = ((AlignFrame) alignFrame).alignPanel;
219 ignoreHidden.setEnabled(av.hasHiddenColumns());
227 * Opens a dialog that allows the user to create sequence features for the
231 public void createFeatures_actionPerformed()
233 if (searchResults.isEmpty())
235 return; // shouldn't happen
237 List<SequenceI> seqs = new ArrayList<>();
238 List<SequenceFeature> features = new ArrayList<>();
240 String searchString = searchBox.getUserInput();
241 String desc = "Search Results";
244 * assemble dataset sequences, and template new sequence features,
245 * for the amend features dialog
247 for (SearchResultMatchI match : searchResults.getResults())
249 seqs.add(match.getSequence().getDatasetSequence());
250 features.add(new SequenceFeature(searchString, desc, match.getStart(),
251 match.getEnd(), desc));
254 new FeatureEditor(ap, seqs, features, true).showDialog();
258 * Search the alignment for the next or all matches. If 'all matches', a
259 * dialog is shown with the number of sequence ids and subsequences matched.
263 void doSearch(boolean doFindAll)
265 createFeatures.setEnabled(false);
267 String searchString = searchBox.getUserInput();
269 if (isInvalidSearchString(searchString))
273 // TODO: extend finder to match descriptions, features and annotation, and
275 // TODO: add switches to control what is searched - sequences, IDS,
276 // descriptions, features
277 FinderI finder = finders.get(av);
281 * first time we've searched this viewport
283 finder = new jalview.analysis.Finder(av);
284 finders.put(av, finder);
287 boolean isCaseSensitive = caseSensitive.isSelected();
288 boolean doSearchDescription = searchDescription.isSelected();
289 boolean skipHidden = ignoreHidden.isSelected();
292 finder.findAll(searchString, isCaseSensitive, doSearchDescription,
297 finder.findNext(searchString, isCaseSensitive, doSearchDescription,
301 searchResults = finder.getSearchResults();
302 List<SequenceI> idMatch = finder.getIdMatches();
303 ap.getIdPanel().highlightSearchResults(idMatch);
305 if (searchResults.isEmpty())
307 searchResults = null;
311 createFeatures.setEnabled(true);
314 searchBox.updateCache();
316 ap.highlightSearchResults(searchResults);
317 // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
318 // 'SelectRegion' selection
319 if (idMatch.isEmpty() && searchResults == null)
321 JvOptionPane.showInternalMessageDialog(this,
322 MessageManager.getString("label.finished_searching"), null,
323 JvOptionPane.PLAIN_MESSAGE);
329 // then we report the matches that were found
330 StringBuilder message = new StringBuilder();
331 if (idMatch.size() > 0)
333 message.append(idMatch.size()).append(" IDs");
335 if (searchResults != null)
337 if (idMatch.size() > 0 && searchResults.getCount() > 0)
339 message.append(" ").append(MessageManager.getString("label.and")
340 .toLowerCase(Locale.ROOT)).append(" ");
342 message.append(MessageManager.formatMessage(
343 "label.subsequence_matches_found",
344 searchResults.getCount()));
346 JvOptionPane.showInternalMessageDialog(this, message.toString(),
347 null, JvOptionPane.INFORMATION_MESSAGE);
353 * Displays an error dialog, and answers false, if the search string is
354 * invalid, else answers true.
356 * @param searchString
359 protected boolean isInvalidSearchString(String searchString)
361 String error = getSearchValidationError(searchString);
366 JvOptionPane.showInternalMessageDialog(this, error,
367 MessageManager.getString("label.invalid_search"), // $NON-NLS-1$
368 JvOptionPane.ERROR_MESSAGE);
373 * Returns an error message string if the search string is invalid, else
376 * Currently validation is limited to checking the string is not empty, and is
377 * a valid regular expression (simple searches for base sub-sequences will
378 * pass this test). Additional validations may be added in future if the
379 * search syntax is expanded.
381 * @param searchString
384 protected String getSearchValidationError(String searchString)
387 if (searchString == null || searchString.length() == 0)
389 error = MessageManager.getString("label.invalid_search");
393 Pattern.compile(searchString);
394 } catch (PatternSyntaxException e)
396 error = MessageManager.getString("error.invalid_regex") + ": "
397 + e.getDescription();
402 protected void closeAction()
404 frame.setVisible(false);
406 searchBox.persistCache();
407 if (getFocusedViewport())
409 ap.alignFrame.requestFocus();