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 jalview.datamodel.SearchResultMatchI;
24 import jalview.datamodel.SearchResultsI;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.jbgui.GFinder;
28 import jalview.util.MessageManager;
29 import jalview.viewmodel.AlignmentViewport;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.KeyEvent;
33 import java.util.Vector;
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.JOptionPane;
42 import javax.swing.KeyStroke;
45 * Performs the menu option for searching the alignment, for the next or all
46 * matches. If matches are found, they are highlighted, and the user has the
47 * option to create a new feature on the alignment for the matched positions.
49 * Searches can be for a simple base sequence, or may use a regular expression.
50 * Any gaps are ignored.
55 public class Finder extends GFinder
57 private static final int HEIGHT = 110;
59 private static final int WIDTH = 340;
71 SearchResultsI searchResults;
74 * Creates a new Finder object with no associated viewport or panel.
83 * Constructor given an associated viewport and alignment panel. Constructs
84 * and displays an internal frame where the user can enter a search string.
89 public Finder(AlignmentViewport viewport, AlignmentPanel alignPanel)
94 frame = new JInternalFrame();
95 frame.setContentPane(this);
96 frame.setLayer(JLayeredPane.PALETTE_LAYER);
98 Desktop.addInternalFrame(frame, MessageManager.getString("label.find"),
101 textfield.requestFocus();
105 * Add a handler for the Escape key when the window has focus
107 private void addEscapeHandler()
109 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
110 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
111 getRootPane().getActionMap().put("Cancel", new AbstractAction()
114 public void actionPerformed(ActionEvent e)
116 escapeActionPerformed();
122 * Close the panel on Escape key press
124 protected void escapeActionPerformed()
131 * Performs the 'Find Next' action.
136 public void findNext_actionPerformed(ActionEvent e)
138 if (getFocusedViewport())
145 * Performs the 'Find All' action.
150 public void findAll_actionPerformed(ActionEvent e)
152 if (getFocusedViewport())
161 * do we only search a given alignment view ?
163 private boolean focusfixed;
166 * if !focusfixed and not in a desktop environment, checks that av and ap are
167 * valid. Otherwise, gets the topmost alignment window and sets av and ap
170 * @return false if no alignment window was found
172 boolean getFocusedViewport()
174 if (focusfixed || Desktop.desktop == null)
176 if (ap != null && av != null)
180 // we aren't in a desktop environment, so give up now.
183 // now checks further down the window stack to fix bug
184 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36008
185 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
186 for (int f = 0; f < frames.length; f++)
188 JInternalFrame frame = frames[f];
189 if (frame != null && frame instanceof AlignFrame)
191 av = ((AlignFrame) frame).viewport;
192 ap = ((AlignFrame) frame).alignPanel;
206 public void createNewGroup_actionPerformed(ActionEvent e)
208 SequenceI[] seqs = new SequenceI[searchResults.getSize()];
209 SequenceFeature[] features = new SequenceFeature[searchResults
213 for (SearchResultMatchI match : searchResults.getResults())
215 seqs[i] = match.getSequence().getDatasetSequence();
217 features[i] = new SequenceFeature(textfield.getText().trim(),
218 "Search Results", null, match.getStart(), match.getEnd(),
223 if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
226 ap.alignFrame.showSeqFeatures.setSelected(true);
227 av.setShowSequenceFeatures(true);
228 ap.highlightSearchResults(null);
233 * Search the alignment for the next or all matches. If 'all matches', a
234 * dialog is shown with the number of sequence ids and subsequences matched.
238 void doSearch(boolean findAll)
240 createNewGroup.setEnabled(false);
242 String searchString = textfield.getText().trim();
244 if (isInvalidSearchString(searchString))
248 // TODO: extend finder to match descriptions, features and annotation, and
250 // TODO: add switches to control what is searched - sequences, IDS,
251 // descriptions, features
252 jalview.analysis.Finder finder = new jalview.analysis.Finder(
253 av.getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
254 finder.setCaseSensitive(caseSensitive.isSelected());
255 finder.setIncludeDescription(searchDescription.isSelected());
257 finder.setFindAll(findAll);
259 finder.find(searchString); // returns true if anything was actually found
261 seqIndex = finder.getSeqIndex();
262 resIndex = finder.getResIndex();
264 searchResults = finder.getSearchResults(); // find(regex,
265 // caseSensitive.isSelected(), )
266 Vector<SequenceI> idMatch = finder.getIdMatch();
267 boolean haveResults = false;
268 // set or reset the GUI
269 if ((idMatch.size() > 0))
272 ap.getIdPanel().highlightSearchResults(idMatch);
276 ap.getIdPanel().highlightSearchResults(null);
279 if (searchResults.getSize() > 0)
282 createNewGroup.setEnabled(true);
286 searchResults = null;
289 // if allResults is null, this effectively switches displaySearch flag in
291 ap.highlightSearchResults(searchResults);
292 // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
293 // 'SelectRegion' selection
296 JvOptionPane.showInternalMessageDialog(this,
297 MessageManager.getString("label.finished_searching"), null,
298 JvOptionPane.INFORMATION_MESSAGE);
306 // then we report the matches that were found
307 String message = (idMatch.size() > 0) ? "" + idMatch.size()
309 if (searchResults != null)
311 if (idMatch.size() > 0 && searchResults.getSize() > 0)
315 message += searchResults.getSize()
316 + " subsequence matches found.";
318 JvOptionPane.showInternalMessageDialog(this, message, null,
319 JvOptionPane.INFORMATION_MESSAGE);
328 * Displays an error dialog, and answers false, if the search string is
329 * invalid, else answers true.
331 * @param searchString
334 protected boolean isInvalidSearchString(String searchString)
336 String error = getSearchValidationError(searchString);
341 JvOptionPane.showInternalMessageDialog(this, error,
342 MessageManager.getString("label.invalid_search"), // $NON-NLS-1$
343 JvOptionPane.ERROR_MESSAGE);
348 * Returns an error message string if the search string is invalid, else
351 * Currently validation is limited to checking the string is not empty, and is
352 * a valid regular expression (simple searches for base sub-sequences will
353 * pass this test). Additional validations may be added in future if the
354 * search syntax is expanded.
356 * @param searchString
359 protected String getSearchValidationError(String searchString)
362 if (searchString == null || searchString.length() == 0)
364 error = MessageManager.getString("label.invalid_search");
368 Pattern.compile(searchString);
369 } catch (PatternSyntaxException e)
371 error = MessageManager.getString("error.invalid_regex") + ": "
372 + e.getDescription();