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
49 SearchResults searchResults;
\r
52 * Creates a new Finder object.
\r
54 * @param av DOCUMENT ME!
\r
55 * @param ap DOCUMENT ME!
\r
56 * @param f DOCUMENT ME!
\r
58 public Finder(AlignViewport av, AlignmentPanel ap, JInternalFrame f)
\r
64 // all a big pain, but we need to wait until the frame is visible before the textfield can
\r
65 // obtain the focus/////////////////////////
\r
66 frame.addInternalFrameListener(new InternalFrameAdapter()
\r
68 public void internalFrameOpened(InternalFrameEvent evt)
\r
70 SwingUtilities.invokeLater(new Runnable()
\r
74 textfield.requestFocus();
\r
85 * @param e DOCUMENT ME!
\r
87 public void findNext_actionPerformed(ActionEvent e)
\r
95 * @param e DOCUMENT ME!
\r
97 public void findAll_actionPerformed(ActionEvent e)
\r
108 * @param e DOCUMENT ME!
\r
110 public void createNewGroup_actionPerformed(ActionEvent e)
\r
112 Color[] newColors = new Color[24];
\r
114 for (int i = 0; i < 24; i++)
\r
116 newColors[i] = new Color(60, 160, 115);
\r
119 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(newColors);
\r
120 String searchString = textfield.getText();
\r
122 searchGroup = new SuperGroup(searchString, ucs, true, true, false);
\r
124 for (int i = 0; i < searchResults.getSize(); i ++ )
\r
126 // its possible edits may have occurred since search was performed
\r
127 // account for this here
\r
128 SequenceI seq = searchResults.getResultSequence(i);
\r
129 int startRes = seq.findIndex(searchResults.getResultStart(i)) - 1;
\r
130 int endRes = seq.findIndex(searchResults.getResultEnd(i)) - 1;
\r
132 SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true,
\r
133 false, startRes, endRes);
\r
134 sg.addSequence(seq, false);
\r
135 av.alignment.addGroup(sg);
\r
136 searchGroup.addGroup(sg);
\r
139 ap.av.alignment.addSuperGroup(searchGroup);
\r
140 ap.highlightSearchResults(null);
\r
146 * @param findAll DOCUMENT ME!
\r
148 void doSearch(boolean findAll)
\r
150 createNewGroup.setEnabled(false);
\r
152 String searchString = textfield.getText().toUpperCase().trim();
\r
153 if(searchString.length()<1)
\r
156 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
\r
158 searchResults = new SearchResults();
\r
161 String item = null;
\r
162 boolean found = false;
\r
164 ////// is the searchString a residue number?
\r
167 int res = Integer.parseInt(searchString);
\r
169 if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize() < 1)
\r
171 seq = (Sequence) av.getAlignment().getSequenceAt(0);
\r
175 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));
\r
178 searchResults.addResult(seq, res, res);
\r
180 catch (NumberFormatException ex)
\r
184 ///////////////////////////////////////////////
\r
185 Color[] newColors = new Color[24];
\r
187 for (int i = 0; i < 24; i++)
\r
189 newColors[i] = new Color(60, 160, 115);
\r
192 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(newColors);
\r
194 searchGroup = new SuperGroup(searchString, ucs, true, true, false);
\r
196 int end = av.alignment.getHeight();
\r
198 SequenceGroup selection = av.getSelectionGroup();
\r
200 if (selection != null)
\r
202 if ((selection.getSize() < 1) ||
\r
203 ((selection.getEndRes() - selection.getStartRes()) < 2))
\r
209 while (!found && (seqIndex < end))
\r
211 seq = (Sequence) av.alignment.getSequenceAt(seqIndex);
\r
213 if ((selection != null) && !selection.sequences.contains(seq))
\r
221 item = seq.getSequence().toUpperCase();
\r
223 if ((selection != null) &&
\r
224 (selection.getEndRes() < av.alignment.getWidth()-1))
\r
226 item = item.substring(0, selection.getEndRes() + 1);
\r
229 ///Shall we ignore gaps????
\r
230 StringBuffer noGapsSB = new StringBuffer();
\r
231 int insertCount = 0;
\r
232 Vector spaces = new Vector();
\r
234 for (int j = 0; j < item.length(); j++)
\r
236 if (!jalview.util.Comparison.isGap(item.charAt(j)))
\r
238 noGapsSB.append(item.charAt(j));
\r
239 spaces.add(new Integer(insertCount));
\r
247 String noGaps = noGapsSB.toString();
\r
249 for (int r = resIndex; r < noGaps.length(); r++)
\r
252 if (regex.searchFrom(noGaps, r))
\r
254 resIndex = regex.matchedFrom();
\r
256 if ((selection != null) &&
\r
258 Integer.parseInt(spaces.get(resIndex).toString())) < selection.getStartRes()))
\r
264 int sres = seq.findPosition(resIndex +
\r
265 Integer.parseInt(spaces.elementAt(resIndex)
\r
267 int eres = seq.findPosition(regex.matchedTo() - 1 +
\r
268 Integer.parseInt(spaces.elementAt(regex.matchedTo() -
\r
271 searchResults.addResult(seq, sres, eres);
\r
275 // thats enough, break and display the result
\r
297 Vector idMatch = new Vector();
\r
299 for (int id = 0; id < av.alignment.getHeight(); id++)
\r
301 if (regex.search(av.alignment.getSequenceAt(id).getName()))
\r
303 idMatch.add(av.alignment.getSequenceAt(id));
\r
307 if ((searchResults.getSize() == 0) && (idMatch.size() > 0))
\r
309 ap.idPanel.highlightSearchResults(idMatch);
\r
313 int resultSize = searchResults.getSize();
\r
315 if (searchResults.getSize() > 0)
\r
316 createNewGroup.setEnabled(true);
\r
318 searchResults = null;
\r
320 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
321 ap.highlightSearchResults(searchResults);
\r
323 if(!findAll && resultSize==0)
\r
325 JOptionPane.showInternalMessageDialog(this, "Finished searching",
\r
326 null, JOptionPane.INFORMATION_MESSAGE);
\r
333 String message = resultSize + " matches found.";
\r
334 JOptionPane.showInternalMessageDialog(this, message, null,
\r
335 JOptionPane.INFORMATION_MESSAGE);
\r