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