a342736bd6f9c7d732cc8cbc5711059c3d5e6de9
[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,
127               "Search Results", null, match.getStart(), match.getEnd(),
128               "Search Results");
129       i++;
130     }
131
132     if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
133             features, true, ap))
134     {
135       ap.alignFrame.sequenceFeatures.setState(true);
136       av.setShowSequenceFeatures(true);
137       ap.highlightSearchResults(null);
138     }
139   }
140
141   void doSearch(boolean findAll)
142   {
143     if (ap.av.applet.currentAlignFrame != null)
144     {
145       ap = ap.av.applet.currentAlignFrame.alignPanel;
146       av = ap.av;
147     }
148     createNewGroup.setEnabled(false);
149     jalview.analysis.Finder finder = new jalview.analysis.Finder(
150             av.getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
151     finder.setCaseSensitive(caseSensitive.getState());
152     finder.setIncludeDescription(searchDescription.getState());
153     finder.setFindAll(findAll);
154
155     String searchString = textfield.getText();
156
157     finder.find(searchString);
158     seqIndex = finder.getSeqIndex();
159     resIndex = finder.getResIndex();
160     searchResults = finder.getSearchResults();
161     Vector<SequenceI> idMatch = finder.getIdMatch();
162     boolean haveResults = false;
163     // set or reset the GUI
164     if ((idMatch.size() > 0))
165     {
166       haveResults = true;
167       ap.idPanel.highlightSearchResults(idMatch);
168     }
169     else
170     {
171       ap.idPanel.highlightSearchResults(null);
172     }
173
174     if (searchResults.getSize() > 0)
175     {
176       haveResults = true;
177       createNewGroup.setEnabled(true);
178
179     }
180     else
181     {
182       searchResults = null;
183     }
184
185     // if allResults is null, this effectively switches displaySearch flag in
186     // seqCanvas
187     ap.highlightSearchResults(searchResults);
188     // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
189     // 'SelectRegion' selection
190     if (!haveResults)
191     {
192       ap.alignFrame.statusBar.setText(MessageManager
193               .getString("label.finished_searching"));
194       resIndex = -1;
195       seqIndex = 0;
196     }
197     else
198     {
199       if (findAll)
200       {
201         String message = (idMatch.size() > 0) ? "" + idMatch.size()
202                 + " IDs" : "";
203         if (idMatch.size() > 0 && searchResults != null
204                 && searchResults.getSize() > 0)
205         {
206           message += " and ";
207         }
208         if (searchResults != null)
209         {
210           message += searchResults.getSize() + " subsequence matches.";
211         }
212         ap.alignFrame.statusBar.setText(MessageManager.formatMessage(
213                 "label.search_results", new String[] { searchString,
214                     message }));
215
216       }
217       else
218       {
219         // TODO: indicate sequence and matching position in status bar
220         ap.alignFrame.statusBar.setText(MessageManager.formatMessage(
221                 "label.found_match_for", new String[] { searchString }));
222       }
223     }
224   }
225
226   Label jLabel1 = new Label();
227
228   protected TextField textfield = new TextField();
229
230   protected Button findAll = new Button();
231
232   protected Button findNext = new Button();
233
234   Panel actionsPanel = new Panel();
235
236   GridLayout gridLayout1 = new GridLayout();
237
238   protected Button createNewGroup = new Button();
239
240   Checkbox caseSensitive = new Checkbox();
241
242   Checkbox searchDescription = new Checkbox();
243
244   private void jbInit() throws Exception
245   {
246     jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
247     jLabel1.setText(MessageManager.getString("action.find"));
248     jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
249     this.setLayout(null);
250     textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
251     textfield.setText("");
252     textfield.setBounds(new Rectangle(40, 17, 133, 21));
253     textfield.addKeyListener(new java.awt.event.KeyAdapter()
254     {
255       @Override
256       public void keyTyped(KeyEvent e)
257       {
258         textfield_keyTyped(e);
259       }
260     });
261     textfield.addActionListener(this);
262     findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
263     findAll.setLabel(MessageManager.getString("action.find_all"));
264     findAll.addActionListener(this);
265     findNext.setEnabled(false);
266     findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
267     findNext.setLabel(MessageManager.getString("action.find_next"));
268     findNext.addActionListener(this);
269     actionsPanel.setBounds(new Rectangle(195, 5, 141, 64));
270     actionsPanel.setLayout(gridLayout1);
271     gridLayout1.setHgap(0);
272     gridLayout1.setRows(3);
273     gridLayout1.setVgap(2);
274     createNewGroup.setEnabled(false);
275     createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
276     createNewGroup.setLabel(MessageManager.getString("label.new_feature"));
277     createNewGroup.addActionListener(this);
278     caseSensitive.setLabel(MessageManager.getString("label.match_case"));
279     caseSensitive.setBounds(new Rectangle(30, 39, 126, 23));
280
281     searchDescription.setLabel(MessageManager
282             .getString("label.include_description"));
283     searchDescription.setBounds(new Rectangle(30, 59, 170, 23));
284     actionsPanel.add(findNext, null);
285     actionsPanel.add(findAll, null);
286     actionsPanel.add(createNewGroup, null);
287     this.add(caseSensitive);
288     this.add(textfield, null);
289     this.add(jLabel1, null);
290     this.add(actionsPanel, null);
291     this.add(searchDescription);
292   }
293
294   void textfield_keyTyped(KeyEvent e)
295   {
296     findNext.setEnabled(true);
297   }
298
299 }