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