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