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