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
20 package jalview.appletgui;
\r
25 import java.awt.event.*;
\r
27 import jalview.datamodel.*;
\r
29 public class Finder extends Panel implements ActionListener
\r
35 SearchResults searchResults;
\r
39 public Finder(final AlignmentPanel ap)
\r
47 e.printStackTrace();
\r
52 frame = new Frame();
\r
54 jalview.bin.JalviewLite.addFrame(frame, "Find", 340, 120);
\r
56 frame.addWindowListener(new WindowAdapter()
\r
58 public void windowClosing(WindowEvent evt)
\r
60 ap.highlightSearchResults(null);
\r
65 public void actionPerformed(ActionEvent evt)
\r
67 if (evt.getSource() == textfield)
\r
70 else if (evt.getSource() == findNext)
\r
73 else if (evt.getSource() == findAll)
\r
79 else if(evt.getSource() == createNewGroup)
\r
80 createNewGroup_actionPerformed();
\r
84 public void createNewGroup_actionPerformed()
\r
87 for (int i = 0; i < searchResults.getSize(); i ++ )
\r
89 SequenceI seq = searchResults.getResultSequence(i);
\r
91 SequenceFeature sf = new SequenceFeature(textfield.getText(),
\r
92 "Search Results", null,
\r
93 searchResults.getResultStart(i),
\r
94 searchResults.getResultEnd(i));
\r
96 ap.seqPanel.seqCanvas.getFeatureRenderer().addNewFeature(
\r
97 textfield.getText(), new Color(60,160,115));
\r
98 seq.addSequenceFeature(sf);
\r
101 ap.alignFrame.sequenceFeatures.setState(true);
\r
102 av.showSequenceFeatures(true);
\r
103 ap.highlightSearchResults(null);
\r
106 void doSearch(boolean findAll)
\r
108 createNewGroup.setEnabled(false);
\r
110 String searchString = textfield.getText().toUpperCase();
\r
112 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
\r
114 searchResults = new SearchResults();
\r
117 String item = null;
\r
118 boolean found = false;
\r
120 ////// is the searchString a residue number?
\r
123 int res = Integer.parseInt(searchString);
\r
126 if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize() < 1)
\r
128 seq = (Sequence) av.getAlignment().getSequenceAt(0);
\r
132 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));
\r
136 searchResults.addResult(seq, res, res);
\r
139 catch (NumberFormatException ex)
\r
141 ///////////////////////////////////////////////
\r
144 int end = av.alignment.getHeight();
\r
146 SequenceGroup selection = av.getSelectionGroup();
\r
147 if (selection != null)
\r
149 if (selection.getSize() < 1 ||
\r
150 (selection.getEndRes() - selection.getStartRes() < 2))
\r
156 while (!found && seqIndex < end)
\r
158 seq = (Sequence) av.alignment.getSequenceAt(seqIndex);
\r
160 if (selection != null && !selection.sequences.contains(seq))
\r
167 item = seq.getSequence().toUpperCase();
\r
169 if (selection != null && selection.getEndRes() < av.alignment.getWidth())
\r
171 item = item.substring(0, selection.getEndRes() + 1);
\r
174 ///Shall we ignore gaps????
\r
175 StringBuffer noGaps = new StringBuffer();
\r
176 int insertCount = 0;
\r
177 Vector spaces = new Vector();
\r
179 for (int j = 0; j < item.length(); j++)
\r
182 if (!jalview.util.Comparison.isGap(item.charAt(j)))
\r
184 noGaps.append(item.charAt(j));
\r
185 spaces.addElement(new Integer(insertCount));
\r
193 for (int r = resIndex; r < noGaps.length(); r++)
\r
196 if (regex.searchFrom(noGaps.toString(), r))
\r
198 resIndex = regex.matchedFrom();
\r
199 if (selection != null &&
\r
200 (resIndex + Integer.parseInt(spaces.elementAt(resIndex).toString())) <
\r
201 selection.getStartRes())
\r
207 int sres = seq.findPosition(resIndex +
\r
208 Integer.parseInt(spaces.
\r
209 elementAt(resIndex).toString()));
\r
210 int eres = seq.findPosition(regex.matchedTo() - 1 +
\r
212 spaces.elementAt(regex.matchedTo() - 1).
\r
215 searchResults.addResult(seq, sres, eres);
\r
219 // thats enough, break and display the result
\r
237 Vector idMatch = new Vector();
\r
238 for (int id = 0; id < av.alignment.getHeight(); id++)
\r
240 if (regex.search(av.alignment.getSequenceAt(id).getName()))
\r
242 idMatch.addElement(av.alignment.getSequenceAt(id));
\r
246 if (searchResults.getSize() == 0 && idMatch.size() > 0)
\r
248 ap.idPanel.highlightSearchResults(idMatch);
\r
251 if (searchResults.getSize() > 0)
\r
253 createNewGroup.setEnabled(true);
\r
257 searchResults = null;
\r
262 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
263 ap.highlightSearchResults(searchResults);
\r
267 String message = (searchResults.getSize()) + " matches found.";
\r
268 System.out.println(message);
\r
273 Label jLabel1 = new Label();
\r
274 protected TextField textfield = new TextField();
\r
275 protected Button findAll = new Button();
\r
276 protected Button findNext = new Button();
\r
277 Panel jPanel1 = new Panel();
\r
278 GridLayout gridLayout1 = new GridLayout();
\r
279 protected Button createNewGroup = new Button();
\r
282 private void jbInit() throws Exception {
\r
283 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
\r
284 jLabel1.setText("Find");
\r
285 jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
\r
286 this.setLayout(null);
\r
287 textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
288 textfield.setText("");
\r
289 textfield.setBounds(new Rectangle(40, 27, 133, 21));
\r
290 textfield.addKeyListener(new java.awt.event.KeyAdapter() {
\r
291 public void keyTyped(KeyEvent e) {
\r
292 textfield_keyTyped(e);
\r
295 textfield.addActionListener(this);
\r
296 findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
297 findAll.setLabel("Find all");
\r
298 findAll.addActionListener(this);
\r
299 findNext.setEnabled(false);
\r
300 findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
301 findNext.setLabel("Find Next");
\r
302 findNext.addActionListener(this);
\r
303 jPanel1.setBounds(new Rectangle(180, 5, 141, 64));
\r
304 jPanel1.setLayout(gridLayout1);
\r
305 gridLayout1.setHgap(0);
\r
306 gridLayout1.setRows(3);
\r
307 gridLayout1.setVgap(2);
\r
308 createNewGroup.setEnabled(false);
\r
309 createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
310 createNewGroup.setLabel("New Feature");
\r
311 createNewGroup.addActionListener(this);
\r
312 jPanel1.add(findNext, null);
\r
313 jPanel1.add(findAll, null);
\r
314 jPanel1.add(createNewGroup, null);
\r
315 this.add(textfield, null);
\r
316 this.add(jLabel1, null);
\r
317 this.add(jPanel1, null);
\r
320 void textfield_keyTyped(KeyEvent e) {
\r
321 findNext.setEnabled(true);
\r