2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import jalview.datamodel.*;
23 import jalview.jbgui.*;
26 import java.awt.event.*;
31 import javax.swing.event.*;
40 public class Finder extends GFinder
48 SearchResults searchResults;
51 * Creates a new Finder object.
53 * @param av DOCUMENT ME!
54 * @param ap DOCUMENT ME!
55 * @param f DOCUMENT ME!
57 public Finder(AlignViewport av, AlignmentPanel ap, JInternalFrame f)
63 // all a big pain, but we need to wait until the frame is visible before the textfield can
64 // obtain the focus/////////////////////////
65 frame.addInternalFrameListener(new InternalFrameAdapter()
67 public void internalFrameOpened(InternalFrameEvent evt)
69 SwingUtilities.invokeLater(new Runnable()
73 textfield.requestFocus();
84 * @param e DOCUMENT ME!
86 public void findNext_actionPerformed(ActionEvent e)
94 * @param e DOCUMENT ME!
96 public void findAll_actionPerformed(ActionEvent e)
107 * @param e DOCUMENT ME!
109 public void createNewGroup_actionPerformed(ActionEvent e)
111 JLabel label = new JLabel("Enter name of new sequence feature");
112 JTextField textinput = new JTextField(textfield.getText());
113 JPanel panel = new JPanel(new BorderLayout());
114 panel.add(label, BorderLayout.NORTH);
115 panel.add(textinput, BorderLayout.SOUTH);
117 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
118 panel, "New Sequence Feature Name",
119 JOptionPane.OK_CANCEL_OPTION );
121 if(reply != JOptionPane.OK_OPTION)
124 for (int i = 0; i < searchResults.getSize(); i ++ )
126 SequenceI seq = searchResults.getResultSequence(i);
128 SequenceFeature sf = new SequenceFeature(textinput.getText(),
129 "Search Results", null,
130 searchResults.getResultStart(i),
131 searchResults.getResultEnd(i),
134 ap.seqPanel.seqCanvas.getFeatureRenderer().addNewFeature(
135 textinput.getText(), new Color(60,160,115),
138 seq.getDatasetSequence().addSequenceFeature(sf);
141 ap.seqPanel.seqCanvas.getFeatureRenderer().findAllFeatures();
142 ap.alignFrame.showSeqFeatures.setSelected(true);
143 av.setShowSequenceFeatures(true);
144 ap.highlightSearchResults(null);
150 * @param findAll DOCUMENT ME!
152 void doSearch(boolean findAll)
154 createNewGroup.setEnabled(false);
156 String searchString = textfield.getText().trim();
157 if(!caseSensitive.isSelected())
158 searchString = searchString.toUpperCase();
160 if(searchString.length()<1)
163 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
165 searchResults = new SearchResults();
169 boolean found = false;
171 ////// is the searchString a residue number?
174 int res = Integer.parseInt(searchString);
176 if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize(false) < 1)
178 seq = (Sequence) av.getAlignment().getSequenceAt(0);
182 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));
185 searchResults.addResult(seq, res, res);
187 catch (NumberFormatException ex)
191 ///////////////////////////////////////////////
193 int end = av.alignment.getHeight();
195 SequenceGroup selection = av.getSelectionGroup();
197 if (selection != null)
199 if ((selection.getSize(false) < 1) ||
200 ((selection.getEndRes() - selection.getStartRes()) < 2))
206 while (!found && (seqIndex < end))
208 seq = (Sequence) av.alignment.getSequenceAt(seqIndex);
210 if ((selection != null) && !selection.getSequences(false).contains(seq))
218 item = seq.getSequence();
219 if(!caseSensitive.isSelected())
220 item = item.toUpperCase();
222 if ((selection != null) &&
223 (selection.getEndRes() < av.alignment.getWidth()-1))
225 item = item.substring(0, selection.getEndRes() + 1);
228 ///Shall we ignore gaps????
229 StringBuffer noGapsSB = new StringBuffer();
231 Vector spaces = new Vector();
233 for (int j = 0; j < item.length(); j++)
235 if (!jalview.util.Comparison.isGap(item.charAt(j)))
237 noGapsSB.append(item.charAt(j));
238 spaces.add(new Integer(insertCount));
246 String noGaps = noGapsSB.toString();
248 for (int r = resIndex; r < noGaps.length(); r++)
251 if (regex.searchFrom(noGaps, r))
253 resIndex = regex.matchedFrom();
255 if ((selection != null) &&
257 Integer.parseInt(spaces.get(resIndex).toString())) < selection.getStartRes()))
263 int sres = seq.findPosition(resIndex +
264 Integer.parseInt(spaces.elementAt(resIndex)
266 int eres = seq.findPosition(regex.matchedTo() - 1 +
267 Integer.parseInt(spaces.elementAt(regex.matchedTo() -
270 searchResults.addResult(seq, sres, eres);
274 // thats enough, break and display the result
296 Vector idMatch = new Vector();
298 for (int id = 0; id < av.alignment.getHeight(); id++)
300 if (regex.search(av.alignment.getSequenceAt(id).getName()))
302 idMatch.add(av.alignment.getSequenceAt(id));
306 if ((searchResults.getSize() == 0) && (idMatch.size() > 0))
308 ap.idPanel.highlightSearchResults(idMatch);
312 int resultSize = searchResults.getSize();
314 if (searchResults.getSize() > 0)
315 createNewGroup.setEnabled(true);
317 searchResults = null;
319 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
320 ap.highlightSearchResults(searchResults);
322 if(!findAll && resultSize==0)
324 JOptionPane.showInternalMessageDialog(this, "Finished searching",
325 null, JOptionPane.INFORMATION_MESSAGE);
332 String message = resultSize + " matches found.";
333 JOptionPane.showInternalMessageDialog(this, message, null,
334 JOptionPane.INFORMATION_MESSAGE);