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.Dimension;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.KeyEvent;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Vector;
37 import java.util.regex.Pattern;
38 import java.util.regex.PatternSyntaxException;
40 import javax.swing.AbstractAction;
41 import javax.swing.JComponent;
42 import javax.swing.JInternalFrame;
43 import javax.swing.JLayeredPane;
44 import javax.swing.KeyStroke;
45 import javax.swing.event.InternalFrameEvent;
48 * Performs the menu option for searching the alignment, for the next or all
49 * matches. If matches are found, they are highlighted, and the user has the
50 * option to create a new feature on the alignment for the matched positions.
52 * Searches can be for a simple base sequence, or may use a regular expression.
53 * Any gaps are ignored.
58 public class Finder extends GFinder
60 private static final int MY_HEIGHT = 120;
62 private static final int MY_WIDTH = 400;
68 private static final int MIN_WIDTH = 350;
70 private static final int MIN_HEIGHT = 120;
78 SearchResultsI searchResults;
81 * Creates a new Finder object with no associated viewport or panel.
90 * Constructor given an associated viewport and alignment panel. Constructs
91 * and displays an internal frame where the user can enter a search string.
96 public Finder(AlignmentViewport viewport, AlignmentPanel alignPanel)
101 frame = new JInternalFrame();
102 frame.setContentPane(this);
103 frame.setLayer(JLayeredPane.PALETTE_LAYER);
104 frame.addInternalFrameListener(
105 new javax.swing.event.InternalFrameAdapter()
108 public void internalFrameClosing(InternalFrameEvent e)
114 Desktop.addInternalFrame(frame, MessageManager.getString("label.find"),
115 MY_WIDTH, MY_HEIGHT);
116 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
117 searchBox.requestFocus();
121 * Add a handler for the Escape key when the window has focus
123 private void addEscapeHandler()
125 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
126 .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
127 getRootPane().getActionMap().put("Cancel", new AbstractAction()
130 public void actionPerformed(ActionEvent e)
138 * Performs the 'Find Next' action.
143 public void findNext_actionPerformed(ActionEvent e)
145 if (getFocusedViewport())
152 * Performs the 'Find All' action.
157 public void findAll_actionPerformed(ActionEvent e)
159 if (getFocusedViewport())
168 * do we only search a given alignment view ?
170 private boolean focusfixed;
173 * if !focusfixed and not in a desktop environment, checks that av and ap are
174 * valid. Otherwise, gets the topmost alignment window and sets av and ap
177 * @return false if no alignment window was found
179 boolean getFocusedViewport()
181 if (focusfixed || Desktop.desktop == null)
183 if (ap != null && av != null)
187 // we aren't in a desktop environment, so give up now.
190 // now checks further down the window stack to fix bug
191 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36008
192 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
193 for (int f = 0; f < frames.length; f++)
195 JInternalFrame alignFrame = frames[f];
196 if (alignFrame != null && alignFrame instanceof AlignFrame)
198 av = ((AlignFrame) alignFrame).viewport;
199 ap = ((AlignFrame) alignFrame).alignPanel;
207 * Opens a dialog that allows the user to create sequence features for the
208 * find match results.
211 public void createFeatures_actionPerformed()
213 List<SequenceI> seqs = new ArrayList<SequenceI>();
214 List<SequenceFeature> features = new ArrayList<SequenceFeature>();
216 String searchString = searchBox.getEditor().getItem().toString().trim();
217 String desc = "Search Results";
220 * assemble dataset sequences, and template new sequence features,
221 * for the amend features dialog
223 for (SearchResultMatchI match : searchResults.getResults())
225 seqs.add(match.getSequence().getDatasetSequence());
226 features.add(new SequenceFeature(searchString, desc,
228 .getStart(), match.getEnd(), desc));
231 if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
235 * ensure feature display is turned on to show the new features,
236 * and remove them as highlighted regions
238 ap.alignFrame.showSeqFeatures.setSelected(true);
239 av.setShowSequenceFeatures(true);
240 ap.highlightSearchResults(null);
245 * Search the alignment for the next or all matches. If 'all matches', a
246 * dialog is shown with the number of sequence ids and subsequences matched.
250 void doSearch(boolean doFindAll)
252 createFeatures.setEnabled(false);
254 String searchString = searchBox.getUserInput().trim();
256 if (isInvalidSearchString(searchString))
260 // TODO: extend finder to match descriptions, features and annotation, and
262 // TODO: add switches to control what is searched - sequences, IDS,
263 // descriptions, features
264 jalview.analysis.Finder finder = new jalview.analysis.Finder(
265 av.getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
266 finder.setCaseSensitive(caseSensitive.isSelected());
267 finder.setIncludeDescription(searchDescription.isSelected());
269 finder.setFindAll(doFindAll);
271 finder.find(searchString); // returns true if anything was actually found
273 seqIndex = finder.getSeqIndex();
274 resIndex = finder.getResIndex();
276 searchResults = finder.getSearchResults(); // find(regex,
277 // caseSensitive.isSelected(), )
278 Vector<SequenceI> idMatch = finder.getIdMatch();
279 boolean haveResults = false;
280 // set or reset the GUI
281 if ((idMatch.size() > 0))
284 ap.getIdPanel().highlightSearchResults(idMatch);
288 ap.getIdPanel().highlightSearchResults(null);
291 if (searchResults.getSize() > 0)
294 createFeatures.setEnabled(true);
298 searchResults = null;
301 // if allResults is null, this effectively switches displaySearch flag in
303 ap.highlightSearchResults(searchResults);
304 // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
305 // 'SelectRegion' selection
308 JvOptionPane.showInternalMessageDialog(this,
309 MessageManager.getString("label.finished_searching"), null,
310 JvOptionPane.INFORMATION_MESSAGE);
318 // then we report the matches that were found
319 String message = (idMatch.size() > 0) ? "" + idMatch.size() + " IDs"
321 if (searchResults != null)
323 if (idMatch.size() > 0 && searchResults.getSize() > 0)
327 message += searchResults.getSize()
328 + " subsequence matches found.";
330 JvOptionPane.showInternalMessageDialog(this, message, null,
331 JvOptionPane.INFORMATION_MESSAGE);
336 searchBox.updateCache();
340 * Displays an error dialog, and answers false, if the search string is
341 * invalid, else answers true.
343 * @param searchString
346 protected boolean isInvalidSearchString(String searchString)
348 String error = getSearchValidationError(searchString);
353 JvOptionPane.showInternalMessageDialog(this, error,
354 MessageManager.getString("label.invalid_search"), // $NON-NLS-1$
355 JvOptionPane.ERROR_MESSAGE);
360 * Returns an error message string if the search string is invalid, else
363 * Currently validation is limited to checking the string is not empty, and is
364 * a valid regular expression (simple searches for base sub-sequences will
365 * pass this test). Additional validations may be added in future if the
366 * search syntax is expanded.
368 * @param searchString
371 protected String getSearchValidationError(String searchString)
374 if (searchString == null || searchString.length() == 0)
376 error = MessageManager.getString("label.invalid_search");
380 Pattern.compile(searchString);
381 } catch (PatternSyntaxException e)
383 error = MessageManager.getString("error.invalid_regex") + ": "
384 + e.getDescription();
389 protected void closeAction()
391 frame.setVisible(false);
393 searchBox.persistCache();
394 if (getFocusedViewport())
396 ap.alignFrame.requestFocus();