2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
48 SearchResults 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
59 frame = new JInternalFrame();
\r
60 frame.setContentPane(this);
\r
61 frame.setLayer(JLayeredPane.PALETTE_LAYER);
\r
62 Desktop.addInternalFrame(frame, "Find", 340, 110);
\r
65 // all a big pain, but we need to wait until the frame is visible before the textfield can
\r
66 // obtain the focus/////////////////////////
\r
67 frame.addInternalFrameListener(new InternalFrameAdapter()
\r
69 public void internalFrameOpened(InternalFrameEvent evt)
\r
71 SwingUtilities.invokeLater(new Runnable()
\r
75 textfield.requestFocus();
\r
86 * @param e DOCUMENT ME!
\r
88 public void findNext_actionPerformed(ActionEvent e)
\r
90 if (getFocusedViewport())
\r
97 * @param e DOCUMENT ME!
\r
99 public void findAll_actionPerformed(ActionEvent e)
\r
101 if(getFocusedViewport())
\r
109 boolean getFocusedViewport()
\r
111 JInternalFrame frame = Desktop.desktop.getAllFrames()[1];
\r
113 if(frame!=null && frame instanceof AlignFrame)
\r
115 av = ((AlignFrame)frame).viewport;
\r
116 ap = ((AlignFrame)frame).alignPanel;
\r
126 * @param e DOCUMENT ME!
\r
128 public void createNewGroup_actionPerformed(ActionEvent e)
\r
130 JLabel label = new JLabel("Enter name of new sequence feature");
\r
131 JTextField textinput = new JTextField(textfield.getText());
\r
132 JPanel panel = new JPanel(new BorderLayout());
\r
133 panel.add(label, BorderLayout.NORTH);
\r
134 panel.add(textinput, BorderLayout.SOUTH);
\r
137 SequenceI [] seqs = new SequenceI[searchResults.getSize()];
\r
138 SequenceFeature [] features = new SequenceFeature[searchResults.getSize()];
\r
141 for (int i = 0; i < searchResults.getSize(); i ++ )
\r
143 seqs[i] = searchResults.getResultSequence(i).getDatasetSequence();
\r
145 features[i] = new SequenceFeature(textinput.getText(),
\r
146 "Search Results", null,
\r
147 searchResults.getResultStart(i),
\r
148 searchResults.getResultEnd(i),
\r
152 if( ap.seqPanel.seqCanvas.getFeatureRenderer()
\r
153 .createNewFeatures(seqs, features))
\r
155 ap.alignFrame.showSeqFeatures.setSelected(true);
\r
156 av.setShowSequenceFeatures(true);
\r
157 ap.highlightSearchResults(null);
\r
164 * @param findAll DOCUMENT ME!
\r
166 void doSearch(boolean findAll)
\r
168 createNewGroup.setEnabled(false);
\r
170 String searchString = textfield.getText().trim();
\r
171 if(!caseSensitive.isSelected())
\r
172 searchString = searchString.toUpperCase();
\r
174 if(searchString.length()<1)
\r
177 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
\r
179 searchResults = new SearchResults();
\r
182 String item = null;
\r
183 boolean found = false;
\r
185 ////// is the searchString a residue number?
\r
188 int res = Integer.parseInt(searchString);
\r
190 if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize(false) < 1)
\r
192 seq = (Sequence) av.getAlignment().getSequenceAt(0);
\r
196 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));
\r
199 searchResults.addResult(seq, res, res);
\r
201 catch (NumberFormatException ex)
\r
205 ///////////////////////////////////////////////
\r
207 int end = av.alignment.getHeight();
\r
209 SequenceGroup selection = av.getSelectionGroup();
\r
211 if (selection != null)
\r
213 if ((selection.getSize(false) < 1) ||
\r
214 ((selection.getEndRes() - selection.getStartRes()) < 2))
\r
220 while (!found && (seqIndex < end))
\r
222 seq = (Sequence) av.alignment.getSequenceAt(seqIndex);
\r
224 if ((selection != null) && !selection.getSequences(false).contains(seq))
\r
232 item = seq.getSequence();
\r
233 if(!caseSensitive.isSelected())
\r
234 item = item.toUpperCase();
\r
236 if ((selection != null) &&
\r
237 (selection.getEndRes() < av.alignment.getWidth()-1))
\r
239 item = item.substring(0, selection.getEndRes() + 1);
\r
242 ///Shall we ignore gaps????
\r
243 StringBuffer noGapsSB = new StringBuffer();
\r
244 int insertCount = 0;
\r
245 Vector spaces = new Vector();
\r
247 for (int j = 0; j < item.length(); j++)
\r
249 if (!jalview.util.Comparison.isGap(item.charAt(j)))
\r
251 noGapsSB.append(item.charAt(j));
\r
252 spaces.add(new Integer(insertCount));
\r
260 String noGaps = noGapsSB.toString();
\r
262 for (int r = resIndex; r < noGaps.length(); r++)
\r
265 if (regex.searchFrom(noGaps, r))
\r
267 resIndex = regex.matchedFrom();
\r
269 if ((selection != null) &&
\r
271 Integer.parseInt(spaces.get(resIndex).toString())) < selection.getStartRes()))
\r
277 int sres = seq.findPosition(resIndex +
\r
278 Integer.parseInt(spaces.elementAt(resIndex)
\r
280 int eres = seq.findPosition(regex.matchedTo() - 1 +
\r
281 Integer.parseInt(spaces.elementAt(regex.matchedTo() -
\r
284 searchResults.addResult(seq, sres, eres);
\r
288 // thats enough, break and display the result
\r
310 Vector idMatch = new Vector();
\r
312 for (int id = 0; id < av.alignment.getHeight(); id++)
\r
314 if (regex.search(av.alignment.getSequenceAt(id).getName()))
\r
316 idMatch.add(av.alignment.getSequenceAt(id));
\r
320 if ((searchResults.getSize() == 0) && (idMatch.size() > 0))
\r
322 ap.idPanel.highlightSearchResults(idMatch);
\r
326 int resultSize = searchResults.getSize();
\r
328 if (searchResults.getSize() > 0)
\r
329 createNewGroup.setEnabled(true);
\r
331 searchResults = null;
\r
333 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
334 ap.highlightSearchResults(searchResults);
\r
336 if(!findAll && resultSize==0)
\r
338 JOptionPane.showInternalMessageDialog(this, "Finished searching",
\r
339 null, JOptionPane.INFORMATION_MESSAGE);
\r
346 String message = resultSize + " matches found.";
\r
347 JOptionPane.showInternalMessageDialog(this, message, null,
\r
348 JOptionPane.INFORMATION_MESSAGE);
\r