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
28 import java.awt.Rectangle;
\r
30 public class Finder extends Panel implements ActionListener
\r
36 SearchResults searchResults;
\r
40 public Finder(final AlignmentPanel ap)
\r
48 e.printStackTrace();
\r
53 frame = new Frame();
\r
55 jalview.bin.JalviewLite.addFrame(frame, "Find", 340, 120);
\r
57 frame.addWindowListener(new WindowAdapter()
\r
59 public void windowClosing(WindowEvent evt)
\r
61 ap.highlightSearchResults(null);
\r
66 public void actionPerformed(ActionEvent evt)
\r
68 if (evt.getSource() == textfield)
\r
71 else if (evt.getSource() == findNext)
\r
74 else if (evt.getSource() == findAll)
\r
80 else if(evt.getSource() == createNewGroup)
\r
81 createNewGroup_actionPerformed();
\r
85 public void createNewGroup_actionPerformed()
\r
88 CutAndPasteTransfer cap = new CutAndPasteTransfer(true, null);
\r
89 Dialog dialog = new Dialog(ap.alignFrame, "Enter New Feature Name", true);
\r
92 cap.setText(textfield.getText());
\r
94 dialog.setBounds( frame.getLocation().x+frame.getSize().width+5,
\r
95 frame.getLocation().y+20,300,100);
\r
99 String featureName = cap.getText().trim();
\r
100 if(featureName.length()<1)
\r
104 for (int i = 0; i < searchResults.getSize(); i ++ )
\r
106 SequenceI seq = searchResults.getResultSequence(i);
\r
108 SequenceFeature sf = new SequenceFeature(featureName,
\r
110 searchResults.getResultStart(i),
\r
111 searchResults.getResultEnd(i), "Search Results");
\r
113 ap.seqPanel.seqCanvas.getFeatureRenderer().addNewFeature(
\r
114 featureName, new Color(60,160,115));
\r
115 seq.addSequenceFeature(sf);
\r
118 ap.seqPanel.seqCanvas.getFeatureRenderer().findAllFeatures();
\r
119 ap.alignFrame.sequenceFeatures.setState(true);
\r
120 av.showSequenceFeatures(true);
\r
121 ap.highlightSearchResults(null);
\r
124 void doSearch(boolean findAll)
\r
126 createNewGroup.setEnabled(false);
\r
128 String searchString = textfield.getText();
\r
129 if(!caseSensitive.getState())
\r
130 searchString = searchString.toUpperCase();
\r
132 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
\r
134 searchResults = new SearchResults();
\r
137 String item = null;
\r
138 boolean found = false;
\r
140 ////// is the searchString a residue number?
\r
143 int res = Integer.parseInt(searchString);
\r
146 if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize(false) < 1)
\r
148 seq = (Sequence) av.getAlignment().getSequenceAt(0);
\r
152 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));
\r
156 searchResults.addResult(seq, res, res);
\r
159 catch (NumberFormatException ex)
\r
161 ///////////////////////////////////////////////
\r
164 int end = av.alignment.getHeight();
\r
166 SequenceGroup selection = av.getSelectionGroup();
\r
167 if (selection != null)
\r
169 if (selection.getSize(false) < 1 ||
\r
170 (selection.getEndRes() - selection.getStartRes() < 2))
\r
176 while (!found && seqIndex < end)
\r
179 seq = (Sequence) av.alignment.getSequenceAt(seqIndex);
\r
181 if (selection != null && !selection.getSequences(false).contains(seq))
\r
188 item = seq.getSequence();
\r
189 if(!caseSensitive.getState())
\r
190 item = item.toUpperCase();
\r
192 if (selection != null && selection.getEndRes() < av.alignment.getWidth())
\r
194 item = item.substring(0, selection.getEndRes() + 1);
\r
197 ///Shall we ignore gaps????
\r
198 StringBuffer noGapsSB = new StringBuffer();
\r
199 int insertCount = 0;
\r
200 Vector spaces = new Vector();
\r
202 for (int j = 0; j < item.length(); j++)
\r
205 if (!jalview.util.Comparison.isGap(item.charAt(j)))
\r
207 noGapsSB.append(item.charAt(j));
\r
208 spaces.addElement(new Integer(insertCount));
\r
216 String noGaps = noGapsSB.toString();
\r
218 for (int r = resIndex; r < noGaps.length(); r++)
\r
221 if (regex.searchFrom(noGaps, r))
\r
223 resIndex = regex.matchedFrom();
\r
224 if (selection != null &&
\r
225 (resIndex + Integer.parseInt(spaces.elementAt(resIndex).toString())) <
\r
226 selection.getStartRes())
\r
232 int sres = seq.findPosition(resIndex +
\r
233 Integer.parseInt(spaces.
\r
234 elementAt(resIndex).toString()));
\r
235 int eres = seq.findPosition(regex.matchedTo() - 1 +
\r
237 spaces.elementAt(regex.matchedTo() - 1).
\r
240 searchResults.addResult(seq, sres, eres);
\r
244 // thats enough, break and display the result
\r
262 Vector idMatch = new Vector();
\r
263 for (int id = 0; id < av.alignment.getHeight(); id++)
\r
265 if (regex.search(av.alignment.getSequenceAt(id).getName()))
\r
267 idMatch.addElement(av.alignment.getSequenceAt(id));
\r
271 if (searchResults.getSize() == 0 && idMatch.size() > 0)
\r
273 ap.idPanel.highlightSearchResults(idMatch);
\r
276 if (searchResults.getSize() > 0)
\r
278 createNewGroup.setEnabled(true);
\r
282 searchResults = null;
\r
287 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
288 ap.highlightSearchResults(searchResults);
\r
292 String message = (searchResults==null?0 : searchResults.getSize()) + " matches found.";
\r
293 ap.alignFrame.statusBar.setText("Search results: "+searchString+" : "+message);
\r
298 Label jLabel1 = new Label();
\r
299 protected TextField textfield = new TextField();
\r
300 protected Button findAll = new Button();
\r
301 protected Button findNext = new Button();
\r
302 Panel jPanel1 = new Panel();
\r
303 GridLayout gridLayout1 = new GridLayout();
\r
304 protected Button createNewGroup = new Button();
\r
305 Checkbox caseSensitive = new Checkbox();
\r
307 private void jbInit() throws Exception {
\r
308 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
\r
309 jLabel1.setText("Find");
\r
310 jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
\r
311 this.setLayout(null);
\r
312 textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
313 textfield.setText("");
\r
314 textfield.setBounds(new Rectangle(40, 27, 133, 21));
\r
315 textfield.addKeyListener(new java.awt.event.KeyAdapter() {
\r
316 public void keyTyped(KeyEvent e) {
\r
317 textfield_keyTyped(e);
\r
320 textfield.addActionListener(this);
\r
321 findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
322 findAll.setLabel("Find all");
\r
323 findAll.addActionListener(this);
\r
324 findNext.setEnabled(false);
\r
325 findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
326 findNext.setLabel("Find Next");
\r
327 findNext.addActionListener(this);
\r
328 jPanel1.setBounds(new Rectangle(180, 5, 141, 64));
\r
329 jPanel1.setLayout(gridLayout1);
\r
330 gridLayout1.setHgap(0);
\r
331 gridLayout1.setRows(3);
\r
332 gridLayout1.setVgap(2);
\r
333 createNewGroup.setEnabled(false);
\r
334 createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
335 createNewGroup.setLabel("New Feature");
\r
336 createNewGroup.addActionListener(this);
\r
337 caseSensitive.setLabel("Match Case");
\r
338 caseSensitive.setBounds(new Rectangle(40, 49, 126, 23));
\r
339 jPanel1.add(findNext, null);
\r
340 jPanel1.add(findAll, null);
\r
341 jPanel1.add(createNewGroup, null);
\r
342 this.add(caseSensitive);
\r
343 this.add(textfield, null);
\r
344 this.add(jLabel1, null);
\r
345 this.add(jPanel1, null);
\r
348 void textfield_keyTyped(KeyEvent e) {
\r
349 findNext.setEnabled(true);
\r