Clover icon

Coverage Report

  1. Project Clover database Wed Nov 13 2024 16:21:17 GMT
  2. Package jalview.appletgui

File Finder.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
59% of files have more coverage

Code metrics

28
103
8
1
296
231
26
0.25
12.88
8
3.25

Classes

Class Line # Actions
Finder 50 103 26
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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