f7ad81891b9f933ffba57b9f1d6edad56700adcb
[jalview.git] / src / jalview / appletgui / Finder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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
28 import java.awt.Button;
29 import java.awt.Checkbox;
30 import java.awt.Font;
31 import java.awt.Frame;
32 import java.awt.GridLayout;
33 import java.awt.Label;
34 import java.awt.Panel;
35 import java.awt.Rectangle;
36 import java.awt.TextField;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.KeyEvent;
40 import java.awt.event.WindowAdapter;
41 import java.awt.event.WindowEvent;
42 import java.util.Vector;
43
44 public class Finder extends Panel implements ActionListener
45 {
46   AlignViewport av;
47
48   AlignmentPanel ap;
49
50   Frame frame;
51
52   SearchResults searchResults;
53
54   int seqIndex = 0;
55
56   int resIndex = -1;
57
58   public Finder(final AlignmentPanel ap)
59   {
60     try
61     {
62       jbInit();
63
64     } catch (Exception e)
65     {
66       e.printStackTrace();
67     }
68
69     this.av = ap.av;
70     this.ap = ap;
71     frame = new Frame();
72     frame.add(this);
73     jalview.bin.JalviewLite.addFrame(frame,
74             MessageManager.getString("action.find"), 340, 120);
75     frame.repaint();
76     frame.addWindowListener(new WindowAdapter()
77     {
78       public void windowClosing(WindowEvent evt)
79       {
80         ap.highlightSearchResults(null);
81       }
82     });
83     textfield.requestFocus();
84   }
85
86   public void actionPerformed(ActionEvent evt)
87   {
88     if (evt.getSource() == textfield)
89     {
90       doSearch(false);
91     }
92
93     else if (evt.getSource() == findNext)
94     {
95       doSearch(false);
96     }
97
98     else if (evt.getSource() == findAll)
99     {
100       resIndex = -1;
101       seqIndex = 0;
102       doSearch(true);
103     }
104     else if (evt.getSource() == createNewGroup)
105     {
106       createNewGroup_actionPerformed();
107     }
108   }
109
110   public void createNewGroup_actionPerformed()
111   {
112     SequenceI[] seqs = new SequenceI[searchResults.getSize()];
113     SequenceFeature[] features = new SequenceFeature[searchResults
114             .getSize()];
115
116     for (int i = 0; i < searchResults.getSize(); i++)
117     {
118       seqs[i] = searchResults.getResultSequence(i);
119
120       features[i] = new SequenceFeature(textfield.getText().trim(),
121               "Search Results", null, searchResults.getResultStart(i),
122               searchResults.getResultEnd(i), "Search Results");
123     }
124
125     if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
126             features, true, ap))
127     {
128       ap.alignFrame.sequenceFeatures.setState(true);
129       av.setShowSequenceFeatures(true);
130       ap.highlightSearchResults(null);
131     }
132   }
133
134   void doSearch(boolean findAll)
135   {
136     if (ap.av.applet.currentAlignFrame != null)
137     {
138       ap = ap.av.applet.currentAlignFrame.alignPanel;
139       av = ap.av;
140     }
141     createNewGroup.setEnabled(false);
142     jalview.analysis.Finder finder = new jalview.analysis.Finder(
143             av.getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
144     finder.setCaseSensitive(caseSensitive.getState());
145     finder.setIncludeDescription(searchDescription.getState());
146     finder.setFindAll(findAll);
147
148     String searchString = textfield.getText();
149
150     finder.find(searchString);
151     seqIndex = finder.getSeqIndex();
152     resIndex = finder.getResIndex();
153     searchResults = finder.getSearchResults();
154     Vector idMatch = finder.getIdMatch();
155     boolean haveResults = false;
156     // set or reset the GUI
157     if ((idMatch.size() > 0))
158     {
159       haveResults = true;
160       ap.idPanel.highlightSearchResults(idMatch);
161     }
162     else
163     {
164       ap.idPanel.highlightSearchResults(null);
165     }
166
167     if (searchResults.getSize() > 0)
168     {
169       haveResults = true;
170       createNewGroup.setEnabled(true);
171
172     }
173     else
174     {
175       searchResults = null;
176     }
177
178     // if allResults is null, this effectively switches displaySearch flag in
179     // seqCanvas
180     ap.highlightSearchResults(searchResults);
181     // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
182     // 'SelectRegion' selection
183     if (!haveResults)
184     {
185       ap.alignFrame.statusBar.setText(MessageManager
186               .getString("label.finished_searching"));
187       resIndex = -1;
188       seqIndex = 0;
189     }
190     else
191     {
192       if (findAll)
193       {
194         String message = (idMatch.size() > 0) ? "" + idMatch.size()
195                 + " IDs" : "";
196         if (idMatch.size() > 0 && searchResults != null
197                 && searchResults.getSize() > 0)
198         {
199           message += " and ";
200         }
201         if (searchResults != null)
202         {
203           message += searchResults.getSize() + " subsequence matches.";
204         }
205         ap.alignFrame.statusBar.setText(MessageManager.formatMessage(
206                 "label.search_results", new String[]
207                 { searchString, message }));
208
209       }
210       else
211       {
212         // TODO: indicate sequence and matching position in status bar
213         ap.alignFrame.statusBar.setText(MessageManager.formatMessage(
214                 "label.found_match_for", new String[]
215                 { 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 }