2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.appletgui;
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.SequenceGroup;
29 import jalview.datamodel.SequenceI;
30 import jalview.util.MessageManager;
32 import java.awt.Button;
33 import java.awt.Checkbox;
35 import java.awt.Frame;
36 import java.awt.GridLayout;
37 import java.awt.Label;
38 import java.awt.Panel;
39 import java.awt.Rectangle;
40 import java.awt.TextField;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.KeyEvent;
44 import java.awt.event.WindowAdapter;
45 import java.awt.event.WindowEvent;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
51 public class Finder extends Panel implements ActionListener
53 private AlignViewportI av;
55 private AlignmentPanel ap;
57 private TextField textfield = new TextField();
59 private Button findAll = new Button();
61 private Button findNext = new Button();
63 private Button createFeatures = new Button();
65 private Checkbox caseSensitive = new Checkbox();
67 private Checkbox searchDescription = new Checkbox();
69 private SearchResultsI searchResults;
72 * Finder agent per viewport searched
74 Map<AlignViewportI, FinderI> finders;
76 public Finder(final AlignmentPanel ap)
78 finders = new HashMap<>();
91 Frame frame = new Frame();
93 jalview.bin.JalviewLite.addFrame(frame,
94 MessageManager.getString("action.find"), 340, 120);
96 frame.addWindowListener(new WindowAdapter()
99 public void windowClosing(WindowEvent evt)
101 ap.highlightSearchResults(null);
104 textfield.requestFocus();
108 public void actionPerformed(ActionEvent evt)
110 if (evt.getSource() == textfield)
115 else if (evt.getSource() == findNext)
120 else if (evt.getSource() == findAll)
124 else if (evt.getSource() == createFeatures)
126 createFeatures_actionPerformed();
130 public void createFeatures_actionPerformed()
132 List<SequenceI> seqs = new ArrayList<>();
133 List<SequenceFeature> features = new ArrayList<>();
134 String searchString = textfield.getText().trim();
136 for (SearchResultMatchI match : searchResults.getResults())
138 seqs.add(match.getSequence().getDatasetSequence());
139 features.add(new SequenceFeature(searchString, "Search Results",
140 match.getStart(), match.getEnd(), "Search Results"));
143 if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
146 ap.alignFrame.sequenceFeatures.setState(true);
147 av.setShowSequenceFeatures(true);
148 ap.highlightSearchResults(null);
152 void doSearch(boolean doFindAll)
154 if (ap.av.applet.currentAlignFrame != null)
156 ap = ap.av.applet.currentAlignFrame.alignPanel;
159 createFeatures.setEnabled(false);
160 FinderI finder = finders.get(av);
164 * first time we searched this viewport
166 finder = new jalview.analysis.Finder(av.getAlignment());
167 finders.put(av, finder);
170 String searchString = textfield.getText();
171 SequenceGroup selectionGroup = av.getSelectionGroup();
172 boolean isCaseSensitive = caseSensitive.getState();
173 boolean doSearchDescription = searchDescription.getState();
176 finder.findAll(searchString, selectionGroup, isCaseSensitive,
177 doSearchDescription);
181 finder.findNext(searchString, selectionGroup, isCaseSensitive,
182 doSearchDescription);
185 searchResults = finder.getSearchResults();
187 List<SequenceI> idMatches = finder.getIdMatches();
188 ap.idPanel.highlightSearchResults(idMatches);
190 if (searchResults.isEmpty())
192 searchResults = null;
196 createFeatures.setEnabled(true);
199 // if allResults is null, this effectively switches displaySearch flag in
201 ap.highlightSearchResults(searchResults);
202 // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
203 // 'SelectRegion' selection
204 if (idMatches.isEmpty() && searchResults == null)
206 ap.alignFrame.statusBar.setText(
207 MessageManager.getString("label.finished_searching"));
213 String message = (idMatches.size() > 0) ? "" + idMatches.size() + " IDs"
215 if (idMatches.size() > 0 && searchResults != null
216 && searchResults.getSize() > 0)
220 if (searchResults != null)
222 message += searchResults.getSize() + " subsequence matches.";
224 ap.alignFrame.statusBar.setText(MessageManager
225 .formatMessage("label.search_results", new String[]
226 { searchString, message }));
231 // TODO: indicate sequence and matching position in status bar
232 ap.alignFrame.statusBar.setText(MessageManager
233 .formatMessage("label.found_match_for", new String[]
239 private void jbInit() throws Exception
241 Label jLabel1 = new Label(MessageManager.getString("action.find"));
242 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
243 jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
244 this.setLayout(null);
245 textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
246 textfield.setText("");
247 textfield.setBounds(new Rectangle(40, 17, 133, 21));
248 textfield.addKeyListener(new java.awt.event.KeyAdapter()
251 public void keyTyped(KeyEvent e)
253 textfield_keyTyped();
256 textfield.addActionListener(this);
257 findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
258 findAll.setLabel(MessageManager.getString("action.find_all"));
259 findAll.addActionListener(this);
260 findNext.setEnabled(false);
261 findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
262 findNext.setLabel(MessageManager.getString("action.find_next"));
263 findNext.addActionListener(this);
265 Panel actionsPanel = new Panel();
266 actionsPanel.setBounds(new Rectangle(195, 5, 141, 64));
267 GridLayout gridLayout1 = new GridLayout();
268 actionsPanel.setLayout(gridLayout1);
269 gridLayout1.setHgap(0);
270 gridLayout1.setRows(3);
271 gridLayout1.setVgap(2);
272 createFeatures.setEnabled(false);
273 createFeatures.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
274 createFeatures.setLabel(MessageManager.getString("label.new_feature"));
275 createFeatures.addActionListener(this);
276 caseSensitive.setLabel(MessageManager.getString("label.match_case"));
277 caseSensitive.setBounds(new Rectangle(30, 39, 126, 23));
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(createFeatures, null);
285 this.add(caseSensitive);
286 this.add(textfield, null);
287 this.add(jLabel1, null);
288 this.add(actionsPanel, null);
289 this.add(searchDescription);
292 void textfield_keyTyped()
294 findNext.setEnabled(true);