find incrementally searches ID then subsequence matches. Dialog reports number of...
[jalview.git] / src / jalview / appletgui / Finder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.appletgui;
20
21 import java.util.*;
22
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import jalview.datamodel.*;
27
28 public class Finder extends Panel implements ActionListener
29 {
30   AlignViewport av;
31
32   AlignmentPanel ap;
33
34   Frame frame;
35
36   SearchResults searchResults;
37
38   int seqIndex = 0;
39
40   int resIndex = -1;
41
42   public Finder(final AlignmentPanel ap)
43   {
44     try
45     {
46       jbInit();
47
48     } catch (Exception e)
49     {
50       e.printStackTrace();
51     }
52
53     this.av = ap.av;
54     this.ap = ap;
55     frame = new Frame();
56     frame.add(this);
57     jalview.bin.JalviewLite.addFrame(frame, "Find", 340, 120);
58     frame.repaint();
59     frame.addWindowListener(new WindowAdapter()
60     {
61       public void windowClosing(WindowEvent evt)
62       {
63         ap.highlightSearchResults(null);
64       }
65     });
66     textfield.requestFocus();
67   }
68
69   public void actionPerformed(ActionEvent evt)
70   {
71     if (evt.getSource() == textfield)
72     {
73       doSearch(false);
74     }
75
76     else if (evt.getSource() == findNext)
77     {
78       doSearch(false);
79     }
80
81     else if (evt.getSource() == findAll)
82     {
83       resIndex = -1;
84       seqIndex = 0;
85       doSearch(true);
86     }
87     else if (evt.getSource() == createNewGroup)
88     {
89       createNewGroup_actionPerformed();
90     }
91   }
92
93   public void createNewGroup_actionPerformed()
94   {
95     SequenceI[] seqs = new SequenceI[searchResults.getSize()];
96     SequenceFeature[] features = new SequenceFeature[searchResults
97             .getSize()];
98
99     for (int i = 0; i < searchResults.getSize(); i++)
100     {
101       seqs[i] = searchResults.getResultSequence(i);
102
103       features[i] = new SequenceFeature(textfield.getText().trim(),
104               "Search Results", null, searchResults.getResultStart(i),
105               searchResults.getResultEnd(i), "Search Results");
106     }
107
108     if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
109             features, true, ap))
110     {
111       ap.alignFrame.sequenceFeatures.setState(true);
112       av.showSequenceFeatures(true);
113       ap.highlightSearchResults(null);
114     }
115   }
116
117   void doSearch(boolean findAll)
118   {
119     if (jalview.bin.JalviewLite.currentAlignFrame != null)
120     {
121       ap = jalview.bin.JalviewLite.currentAlignFrame.alignPanel;
122       av = ap.av;
123     }
124     createNewGroup.setEnabled(false);
125     jalview.analysis.Finder finder = new jalview.analysis.Finder(av
126             .getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
127     finder.setCaseSensitive(caseSensitive.getState());
128     finder.setFindAll(findAll);
129
130     String searchString = textfield.getText();
131
132     finder.find(searchString);
133     seqIndex = finder.getSeqIndex();
134     resIndex = finder.getResIndex();
135     searchResults = finder.getSearchResults();
136     Vector idMatch = finder.getIdMatch();
137     boolean haveResults=false;
138     // set or reset the GUI
139     if ((idMatch.size() > 0))
140     {
141       haveResults=true;
142       ap.idPanel.highlightSearchResults(idMatch);
143     } else {
144       ap.idPanel.highlightSearchResults(null);
145     }
146
147     if (searchResults.getSize() > 0)
148     {
149       haveResults=true;
150       createNewGroup.setEnabled(true);
151
152     }
153     else
154     {
155       searchResults = null;
156     }
157
158     // if allResults is null, this effectively switches displaySearch flag in
159     // seqCanvas
160     ap.highlightSearchResults(searchResults);
161     // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
162     // 'SelectRegion' selection
163     if (!haveResults)
164     {
165     ap.alignFrame.statusBar.setText("Finished searching.");
166     resIndex = -1;
167     seqIndex = 0;
168     }
169     if (findAll)
170     {
171       String message = (idMatch.size()>0) ? ""+idMatch.size()+" IDs" : "";
172       if (idMatch.size()>0 && searchResults!=null && searchResults.getSize()>0) {
173         message += " and ";
174       }
175       if (searchResults!=null) {
176         message += searchResults.getSize() + " subsequence matches.";
177       }
178       ap.alignFrame.statusBar.setText("Search results: " + searchString
179               + " : " + message);
180               
181     }
182
183   }
184
185   Label jLabel1 = new Label();
186
187   protected TextField textfield = new TextField();
188
189   protected Button findAll = new Button();
190
191   protected Button findNext = new Button();
192
193   Panel jPanel1 = new Panel();
194
195   GridLayout gridLayout1 = new GridLayout();
196
197   protected Button createNewGroup = new Button();
198
199   Checkbox caseSensitive = new Checkbox();
200
201   private void jbInit() throws Exception
202   {
203     jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
204     jLabel1.setText("Find");
205     jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
206     this.setLayout(null);
207     textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
208     textfield.setText("");
209     textfield.setBounds(new Rectangle(40, 27, 133, 21));
210     textfield.addKeyListener(new java.awt.event.KeyAdapter()
211     {
212       public void keyTyped(KeyEvent e)
213       {
214         textfield_keyTyped(e);
215       }
216     });
217     textfield.addActionListener(this);
218     findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
219     findAll.setLabel("Find all");
220     findAll.addActionListener(this);
221     findNext.setEnabled(false);
222     findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
223     findNext.setLabel("Find Next");
224     findNext.addActionListener(this);
225     jPanel1.setBounds(new Rectangle(180, 5, 141, 64));
226     jPanel1.setLayout(gridLayout1);
227     gridLayout1.setHgap(0);
228     gridLayout1.setRows(3);
229     gridLayout1.setVgap(2);
230     createNewGroup.setEnabled(false);
231     createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
232     createNewGroup.setLabel("New Feature");
233     createNewGroup.addActionListener(this);
234     caseSensitive.setLabel("Match Case");
235     caseSensitive.setBounds(new Rectangle(40, 49, 126, 23));
236     jPanel1.add(findNext, null);
237     jPanel1.add(findAll, null);
238     jPanel1.add(createNewGroup, null);
239     this.add(caseSensitive);
240     this.add(textfield, null);
241     this.add(jLabel1, null);
242     this.add(jPanel1, null);
243   }
244
245   void textfield_keyTyped(KeyEvent e)
246   {
247     findNext.setEnabled(true);
248   }
249
250 }