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