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.io.cache.AppCache;
28 import jalview.jbgui.GFinder;
29 import jalview.util.MessageManager;
30 import jalview.viewmodel.AlignmentViewport;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.KeyEvent;
34 import java.util.Vector;
35 import java.util.regex.Pattern;
36 import java.util.regex.PatternSyntaxException;
38 import javax.swing.AbstractAction;
39 import javax.swing.JComponent;
40 import javax.swing.JInternalFrame;
41 import javax.swing.JLayeredPane;
42 import javax.swing.KeyStroke;
43 import javax.swing.event.InternalFrameEvent;
46 * Performs the menu option for searching the alignment, for the next or all
47 * matches. If matches are found, they are highlighted, and the user has the
48 * option to create a new feature on the alignment for the matched positions.
50 * Searches can be for a simple base sequence, or may use a regular expression.
51 * Any gaps are ignored.
56 public class Finder extends GFinder
58 private static final int HEIGHT = 110;
60 private static final int WIDTH = 340;
72 SearchResultsI searchResults;
75 * Creates a new Finder object with no associated viewport or panel.
84 * Constructor given an associated viewport and alignment panel. Constructs
85 * and displays an internal frame where the user can enter a search string.
90 public Finder(AlignmentViewport viewport, AlignmentPanel alignPanel)
95 frame = new JInternalFrame();
96 frame.setContentPane(this);
97 frame.setLayer(JLayeredPane.PALETTE_LAYER);
98 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
101 public void internalFrameClosing(InternalFrameEvent e)
107 Desktop.addInternalFrame(frame, MessageManager.getString("label.find"),
110 searchBox.requestFocus();
114 * Add a handler for the Escape key when the window has focus
116 private void addEscapeHandler()
118 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
119 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
120 getRootPane().getActionMap().put("Cancel", new AbstractAction()
123 public void actionPerformed(ActionEvent e)
125 escapeActionPerformed();
131 * Close the panel on Escape key press
133 protected void escapeActionPerformed()
140 * Performs the 'Find Next' action.
145 public void findNext_actionPerformed(ActionEvent e)
147 if (getFocusedViewport())
154 * Performs the 'Find All' action.
159 public void findAll_actionPerformed(ActionEvent e)
161 if (getFocusedViewport())
170 * do we only search a given alignment view ?
172 private boolean focusfixed;
175 * if !focusfixed and not in a desktop environment, checks that av and ap are
176 * valid. Otherwise, gets the topmost alignment window and sets av and ap
179 * @return false if no alignment window was found
181 boolean getFocusedViewport()
183 if (focusfixed || Desktop.desktop == null)
185 if (ap != null && av != null)
189 // we aren't in a desktop environment, so give up now.
192 // now checks further down the window stack to fix bug
193 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36008
194 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
195 for (int f = 0; f < frames.length; f++)
197 JInternalFrame frame = frames[f];
198 if (frame != null && frame instanceof AlignFrame)
200 av = ((AlignFrame) frame).viewport;
201 ap = ((AlignFrame) frame).alignPanel;
215 public void createNewGroup_actionPerformed(ActionEvent e)
217 SequenceI[] seqs = new SequenceI[searchResults.getSize()];
218 SequenceFeature[] features = new SequenceFeature[searchResults
222 for (SearchResultMatchI match : searchResults.getResults())
224 seqs[i] = match.getSequence().getDatasetSequence();
226 features[i] = new SequenceFeature(searchBox.getEditor().getItem()
228 "Search Results", null, match.getStart(), match.getEnd(),
233 if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
236 ap.alignFrame.showSeqFeatures.setSelected(true);
237 av.setShowSequenceFeatures(true);
238 ap.highlightSearchResults(null);
243 * Search the alignment for the next or all matches. If 'all matches', a
244 * dialog is shown with the number of sequence ids and subsequences matched.
248 void doSearch(boolean findAll)
250 createNewGroup.setEnabled(false);
252 String searchString = searchBox.getEditor().getItem().toString().trim();
254 if (isInvalidSearchString(searchString))
258 // TODO: extend finder to match descriptions, features and annotation, and
260 // TODO: add switches to control what is searched - sequences, IDS,
261 // descriptions, features
262 jalview.analysis.Finder finder = new jalview.analysis.Finder(
263 av.getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
264 finder.setCaseSensitive(caseSensitive.isSelected());
265 finder.setIncludeDescription(searchDescription.isSelected());
267 finder.setFindAll(findAll);
269 finder.find(searchString); // returns true if anything was actually found
271 seqIndex = finder.getSeqIndex();
272 resIndex = finder.getResIndex();
274 searchResults = finder.getSearchResults(); // find(regex,
275 // caseSensitive.isSelected(), )
276 Vector<SequenceI> idMatch = finder.getIdMatch();
277 boolean haveResults = false;
278 // set or reset the GUI
279 if ((idMatch.size() > 0))
282 ap.getIdPanel().highlightSearchResults(idMatch);
286 ap.getIdPanel().highlightSearchResults(null);
289 if (searchResults.getSize() > 0)
292 createNewGroup.setEnabled(true);
296 searchResults = null;
299 // if allResults is null, this effectively switches displaySearch flag in
301 ap.highlightSearchResults(searchResults);
302 // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
303 // 'SelectRegion' selection
306 JvOptionPane.showInternalMessageDialog(this,
307 MessageManager.getString("label.finished_searching"), null,
308 JvOptionPane.INFORMATION_MESSAGE);
316 // then we report the matches that were found
317 String message = (idMatch.size() > 0) ? "" + idMatch.size()
319 if (searchResults != null)
321 if (idMatch.size() > 0 && searchResults.getSize() > 0)
325 message += searchResults.getSize()
326 + " subsequence matches found.";
328 JvOptionPane.showInternalMessageDialog(this, message, null,
329 JvOptionPane.INFORMATION_MESSAGE);
334 searchBox.updateCache();
338 * Displays an error dialog, and answers false, if the search string is
339 * invalid, else answers true.
341 * @param searchString
344 protected boolean isInvalidSearchString(String searchString)
346 String error = getSearchValidationError(searchString);
351 JvOptionPane.showInternalMessageDialog(this, error,
352 MessageManager.getString("label.invalid_search"), // $NON-NLS-1$
353 JvOptionPane.ERROR_MESSAGE);
358 * Returns an error message string if the search string is invalid, else
361 * Currently validation is limited to checking the string is not empty, and is
362 * a valid regular expression (simple searches for base sub-sequences will
363 * pass this test). Additional validations may be added in future if the
364 * search syntax is expanded.
366 * @param searchString
369 protected String getSearchValidationError(String searchString)
372 if (searchString == null || searchString.length() == 0)
374 error = MessageManager.getString("label.invalid_search");
378 Pattern.compile(searchString);
379 } catch (PatternSyntaxException e)
381 error = MessageManager.getString("error.invalid_regex") + ": "
382 + e.getDescription();
387 protected void closeAction()
390 AppCache.getInstance().persistCache(getCacheKey());