2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.SearchResults;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.jbgui.GFinder;
27 import jalview.util.MessageManager;
29 import java.awt.event.ActionEvent;
30 import java.util.Vector;
31 import java.util.regex.Pattern;
32 import java.util.regex.PatternSyntaxException;
34 import javax.swing.JInternalFrame;
35 import javax.swing.JLayeredPane;
36 import javax.swing.JOptionPane;
39 * Performs the menu option for searching the alignment, for the next or all
40 * matches. If matches are found, they are highlighted, and the user has the
41 * option to create a new feature on the alignment for the matched positions.
43 * Searches can be for a simple base sequence, or may use a regular expression.
44 * Any gaps are ignored.
49 public class Finder extends GFinder
51 private static final int HEIGHT = 110;
53 private static final int WIDTH = 340;
65 SearchResults searchResults;
68 * Creates a new Finder object with no associated viewport or panel.
77 * Constructor given an associated viewport and alignment panel. Constructs
78 * and displays an internal frame where the user can enter a search string.
83 public Finder(AlignViewport viewport, AlignmentPanel alignPanel)
88 frame = new JInternalFrame();
89 frame.setContentPane(this);
90 frame.setLayer(JLayeredPane.PALETTE_LAYER);
91 Desktop.addInternalFrame(frame, MessageManager.getString("label.find"),
94 textfield.requestFocus();
98 * Performs the 'Find Next' action.
102 public void findNext_actionPerformed(ActionEvent e)
104 if (getFocusedViewport())
111 * Performs the 'Find All' action.
115 public void findAll_actionPerformed(ActionEvent e)
117 if (getFocusedViewport())
126 * do we only search a given alignment view ?
128 private boolean focusfixed;
131 * if !focusfixed and not in a desktop environment, checks that av and ap are
132 * valid. Otherwise, gets the topmost alignment window and sets av and ap
135 * @return false if no alignment window was found
137 boolean getFocusedViewport()
139 if (focusfixed || Desktop.desktop == null)
141 if (ap != null && av != null)
145 // we aren't in a desktop environment, so give up now.
148 // now checks further down the window stack to fix bug
149 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36008
150 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
151 for (int f = 0; f < frames.length; f++)
153 JInternalFrame frame = frames[f];
154 if (frame != null && frame instanceof AlignFrame)
156 av = ((AlignFrame) frame).viewport;
157 ap = ((AlignFrame) frame).alignPanel;
170 public void createNewGroup_actionPerformed(ActionEvent e)
172 SequenceI[] seqs = new SequenceI[searchResults.getSize()];
173 SequenceFeature[] features = new SequenceFeature[searchResults
176 for (int i = 0; i < searchResults.getSize(); i++)
178 seqs[i] = searchResults.getResultSequence(i).getDatasetSequence();
180 features[i] = new SequenceFeature(textfield.getText().trim(),
181 "Search Results", null, searchResults.getResultStart(i),
182 searchResults.getResultEnd(i), "Search Results");
185 if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
188 ap.alignFrame.showSeqFeatures.setSelected(true);
189 av.setShowSequenceFeatures(true);
190 ap.highlightSearchResults(null);
195 * Search the alignment for the next or all matches. If 'all matches', a
196 * dialog is shown with the number of sequence ids and subsequences matched.
200 void doSearch(boolean findAll)
202 createNewGroup.setEnabled(false);
204 String searchString = textfield.getText().trim();
206 if (isInvalidSearchString(searchString))
210 // TODO: extend finder to match descriptions, features and annotation, and
212 // TODO: add switches to control what is searched - sequences, IDS,
213 // descriptions, features
214 jalview.analysis.Finder finder = new jalview.analysis.Finder(
215 av.getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
216 finder.setCaseSensitive(caseSensitive.isSelected());
217 finder.setFindAll(findAll);
219 finder.find(searchString); // returns true if anything was actually found
221 seqIndex = finder.getSeqIndex();
222 resIndex = finder.getResIndex();
224 searchResults = finder.getSearchResults(); // find(regex,
225 // caseSensitive.isSelected(), )
226 Vector idMatch = finder.getIdMatch();
227 boolean haveResults = false;
228 // set or reset the GUI
229 if ((idMatch.size() > 0))
232 ap.idPanel.highlightSearchResults(idMatch);
236 ap.idPanel.highlightSearchResults(null);
239 if (searchResults.getSize() > 0)
242 createNewGroup.setEnabled(true);
246 searchResults = null;
249 // if allResults is null, this effectively switches displaySearch flag in
251 ap.highlightSearchResults(searchResults);
252 // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
253 // 'SelectRegion' selection
256 JOptionPane.showInternalMessageDialog(this,
257 MessageManager.getString("label.finished_searching"), null,
258 JOptionPane.INFORMATION_MESSAGE);
266 // then we report the matches that were found
267 String message = (idMatch.size() > 0) ? "" + idMatch.size()
269 if (searchResults != null)
271 if (idMatch.size() > 0 && searchResults.getSize() > 0)
275 message += searchResults.getSize()
276 + " subsequence matches found.";
278 JOptionPane.showInternalMessageDialog(this, message, null,
279 JOptionPane.INFORMATION_MESSAGE);
288 * Displays an error dialog, and answers false, if the search string is
289 * invalid, else answers true.
291 * @param searchString
294 protected boolean isInvalidSearchString(String searchString)
296 String error = getSearchValidationError(searchString);
301 JOptionPane.showInternalMessageDialog(this, error,
302 MessageManager.getString("label.invalid_search"), // $NON-NLS-1$
303 JOptionPane.ERROR_MESSAGE);
308 * Returns an error message string if the search string is invalid, else
311 * Currently validation is limited to checking the string is not empty, and is
312 * a valid regular expression (simple searches for base sub-sequences will
313 * pass this test). Additional validations may be added in future if the
314 * search syntax is expanded.
316 * @param searchString
319 protected String getSearchValidationError(String searchString)
322 if (searchString == null || searchString.length() == 0)
324 error = MessageManager.getString("label.invalid_search");
328 Pattern.compile(searchString);
329 } catch (PatternSyntaxException e)
331 error = MessageManager.getString("error.invalid_regex") + ": "
332 + e.getDescription();