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