2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
21 import jalview.datamodel.*;
\r
23 import jalview.jbgui.*;
\r
26 import java.awt.event.*;
\r
30 import javax.swing.*;
\r
31 import javax.swing.event.*;
\r
38 * @version $Revision$
\r
40 public class Finder extends GFinder
\r
44 JInternalFrame frame;
\r
45 SuperGroup searchGroup;
\r
46 Vector searchResults;
\r
51 * Creates a new Finder object.
\r
53 * @param av DOCUMENT ME!
\r
54 * @param ap DOCUMENT ME!
\r
55 * @param f DOCUMENT ME!
\r
57 public Finder(AlignViewport av, AlignmentPanel ap, JInternalFrame f)
\r
63 // all a big pain, but we need to wait until the frame is visible before the textfield can
\r
64 // obtain the focus/////////////////////////
\r
65 frame.addInternalFrameListener(new InternalFrameAdapter()
\r
67 public void internalFrameOpened(InternalFrameEvent evt)
\r
69 SwingUtilities.invokeLater(new Runnable()
\r
73 textfield.requestFocus();
\r
81 * @param evt DOCUMENT ME!
\r
83 public void internalFrameClosing(InternalFrameEvent evt)
\r
85 cancel_actionPerformed(null);
\r
94 * @param e DOCUMENT ME!
\r
96 public void findNext_actionPerformed(ActionEvent e)
\r
104 * @param e DOCUMENT ME!
\r
106 public void findAll_actionPerformed(ActionEvent e)
\r
116 * @param e DOCUMENT ME!
\r
118 public void cancel_actionPerformed(ActionEvent e)
\r
122 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
123 ap.highlightSearchResults(null);
\r
124 ap.idPanel.highlightSearchResults(null);
\r
126 // frame.setClosed(true);
\r
128 catch (Exception ex)
\r
136 * @param e DOCUMENT ME!
\r
138 public void createNewGroup_actionPerformed(ActionEvent e)
\r
140 Color[] newColors = new Color[24];
\r
142 for (int i = 0; i < 24; i++)
\r
144 newColors[i] = new Color(60, 160, 115);
\r
147 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(newColors);
\r
148 String searchString = textfield.getText();
\r
150 searchGroup = new SuperGroup(searchString, ucs, true, true, false);
\r
152 for (int i = 0; i < searchResults.size(); i += 3)
\r
154 // its possible edits may have occurred since search was performed
\r
155 // account for this here
\r
156 SequenceI seq = av.alignment.getSequenceAt(Integer.parseInt(
\r
157 searchResults.elementAt(i).toString()));
\r
158 int startRes = seq.findIndex(Integer.parseInt(
\r
159 searchResults.elementAt(i + 1).toString())) - 1;
\r
160 int endRes = seq.findIndex(Integer.parseInt(
\r
161 searchResults.elementAt(i + 2).toString())) - 1;
\r
163 SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true,
\r
164 false, startRes, endRes);
\r
165 sg.addSequence(seq, false);
\r
166 av.alignment.addGroup(sg);
\r
167 searchGroup.addGroup(sg);
\r
170 ap.av.alignment.addSuperGroup(searchGroup);
\r
171 ap.highlightSearchResults(null);
\r
177 * @param findAll DOCUMENT ME!
\r
179 void doSearch(boolean findAll)
\r
181 createNewGroup.setEnabled(false);
\r
183 String searchString = textfield.getText().toUpperCase().trim();
\r
185 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
\r
187 searchResults = new Vector();
\r
189 int[] allResults = null;
\r
192 String item = null;
\r
193 boolean found = false;
\r
195 ////// is the searchString a residue number?
\r
198 int res = Integer.parseInt(searchString);
\r
200 if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize() < 1)
\r
202 seq = (Sequence) av.getAlignment().getSequenceAt(0);
\r
206 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));
\r
209 searchResults.add(Integer.toString(av.getAlignment().findIndex(seq)));
\r
210 searchResults.add(res+"");
\r
211 searchResults.add(res+"");
\r
213 catch (NumberFormatException ex)
\r
217 ///////////////////////////////////////////////
\r
218 Color[] newColors = new Color[24];
\r
220 for (int i = 0; i < 24; i++)
\r
222 newColors[i] = new Color(60, 160, 115);
\r
225 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(newColors);
\r
227 searchGroup = new SuperGroup(searchString, ucs, true, true, false);
\r
229 int end = av.alignment.getHeight();
\r
231 SequenceGroup selection = av.getSelectionGroup();
\r
233 if (selection != null)
\r
235 if ((selection.getSize() < 1) ||
\r
236 ((selection.getEndRes() - selection.getStartRes()) < 2))
\r
242 while (!found && (seqIndex < end))
\r
244 seq = (Sequence) av.alignment.getSequenceAt(seqIndex);
\r
246 if ((selection != null) && !selection.sequences.contains(seq))
\r
254 item = seq.getSequence().toUpperCase();
\r
256 if ((selection != null) &&
\r
257 (selection.getEndRes() < av.alignment.getWidth()))
\r
259 item = item.substring(0, selection.getEndRes() + 1);
\r
262 ///Shall we ignore gaps????
\r
263 StringBuffer noGaps = new StringBuffer();
\r
264 int insertCount = 0;
\r
265 Vector spaces = new Vector();
\r
267 for (int j = 0; j < item.length(); j++)
\r
269 if (!jalview.util.Comparison.isGap(item.charAt(j)))
\r
271 noGaps.append(item.charAt(j));
\r
272 spaces.add(new Integer(insertCount));
\r
280 for (int r = resIndex; r < noGaps.length(); r++)
\r
282 if (regex.searchFrom(noGaps.toString(), r))
\r
284 resIndex = regex.matchedFrom();
\r
286 if ((selection != null) &&
\r
288 Integer.parseInt(spaces.get(resIndex).toString())) < selection.getStartRes()))
\r
293 searchResults.add(Integer.toString(seqIndex));
\r
295 int sres = seq.findPosition(resIndex +
\r
296 Integer.parseInt(spaces.elementAt(resIndex)
\r
298 int eres = seq.findPosition(regex.matchedTo() - 1 +
\r
299 Integer.parseInt(spaces.elementAt(regex.matchedTo() -
\r
302 searchResults.addElement(sres + "");
\r
303 searchResults.addElement(eres + "");
\r
307 // thats enough, break and display the result
\r
325 Vector idMatch = new Vector();
\r
327 for (int id = 0; id < av.alignment.getHeight(); id++)
\r
329 if (regex.search(av.alignment.getSequenceAt(id).getName()))
\r
331 idMatch.add(av.alignment.getSequenceAt(id));
\r
335 if ((searchResults.size() == 0) && (idMatch.size() > 0))
\r
337 ap.idPanel.highlightSearchResults(idMatch);
\r
340 if (searchResults.size() > 0)
\r
342 allResults = new int[searchResults.size()];
\r
344 for (int i = 0; i < searchResults.size(); i++)
\r
346 allResults[i] = Integer.parseInt(searchResults.get(i).toString());
\r
349 createNewGroup.setEnabled(true);
\r
353 JOptionPane.showInternalMessageDialog(this, "Finished searching",
\r
354 null, JOptionPane.INFORMATION_MESSAGE);
\r
359 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
360 ap.highlightSearchResults(allResults);
\r
364 String message = (searchResults.size() / 3) + " matches found.";
\r
365 JOptionPane.showInternalMessageDialog(this, message, null,
\r
366 JOptionPane.INFORMATION_MESSAGE);
\r