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