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 java.awt.Button;
24 import java.awt.Checkbox;
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;
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;
50 public class Finder extends Panel implements ActionListener
52 private AlignViewportI av;
54 private AlignmentPanel ap;
56 private TextField textfield = new TextField();
58 private Button findAll = new Button();
60 private Button findNext = new Button();
62 private Button createFeatures = new Button();
64 private Checkbox caseSensitive = new Checkbox();
66 private Checkbox searchDescription = new Checkbox();
68 private SearchResultsI searchResults;
71 * Finder agent per viewport searched
73 Map<AlignViewportI, FinderI> finders;
75 public Finder(final AlignmentPanel ap)
77 finders = new HashMap<>();
90 Frame frame = new Frame();
92 jalview.bin.JalviewLite.addFrame(frame,
93 MessageManager.getString("action.find"), 340, 120);
95 frame.addWindowListener(new WindowAdapter()
98 public void windowClosing(WindowEvent evt)
100 ap.highlightSearchResults(null);
103 textfield.requestFocus();
107 public void actionPerformed(ActionEvent evt)
109 if (evt.getSource() == textfield)
114 else if (evt.getSource() == findNext)
119 else if (evt.getSource() == findAll)
123 else if (evt.getSource() == createFeatures)
125 createFeatures_actionPerformed();
129 public void createFeatures_actionPerformed()
131 List<SequenceI> seqs = new ArrayList<>();
132 List<SequenceFeature> features = new ArrayList<>();
133 String searchString = textfield.getText().trim();
135 for (SearchResultMatchI match : searchResults.getResults())
137 seqs.add(match.getSequence().getDatasetSequence());
138 features.add(new SequenceFeature(searchString, "Search Results",
139 match.getStart(), match.getEnd(), "Search Results"));
142 if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
145 ap.alignFrame.sequenceFeatures.setState(true);
146 av.setShowSequenceFeatures(true);
147 ap.highlightSearchResults(null);
151 void doSearch(boolean doFindAll)
153 if (ap.av.applet.currentAlignFrame != null)
155 ap = ap.av.applet.currentAlignFrame.alignPanel;
158 createFeatures.setEnabled(false);
159 FinderI finder = finders.get(av);
163 * first time we searched this viewport
165 finder = new jalview.analysis.Finder(av);
166 finders.put(av, finder);
169 String searchString = textfield.getText();
170 boolean isCaseSensitive = caseSensitive.getState();
171 boolean doSearchDescription = searchDescription.getState();
174 finder.findAll(searchString, isCaseSensitive, doSearchDescription,
179 finder.findNext(searchString, isCaseSensitive, doSearchDescription,
183 searchResults = finder.getSearchResults();
185 List<SequenceI> idMatches = finder.getIdMatches();
186 ap.idPanel.highlightSearchResults(idMatches);
188 if (searchResults.isEmpty())
190 searchResults = null;
194 createFeatures.setEnabled(true);
197 // if allResults is null, this effectively switches displaySearch flag in
199 ap.highlightSearchResults(searchResults);
200 // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
201 // 'SelectRegion' selection
202 if (idMatches.isEmpty() && searchResults == null)
204 ap.alignFrame.statusBar.setText(
205 MessageManager.getString("label.finished_searching"));
211 String message = (idMatches.size() > 0)
212 ? "" + idMatches.size() + " IDs"
214 if (idMatches.size() > 0 && searchResults != null
215 && searchResults.getCount() > 0)
219 if (searchResults != null)
221 message += searchResults.getCount() + " subsequence matches.";
223 ap.alignFrame.statusBar.setText(MessageManager
224 .formatMessage("label.search_results", new String[]
225 { searchString, message }));
230 // TODO: indicate sequence and matching position in status bar
231 ap.alignFrame.statusBar.setText(MessageManager
232 .formatMessage("label.found_match_for", new String[]
238 private void jbInit() throws Exception
240 Label jLabel1 = new Label(MessageManager.getString("action.find"));
241 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
242 jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
243 this.setLayout(null);
244 textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
245 textfield.setText("");
246 textfield.setBounds(new Rectangle(40, 17, 133, 21));
247 textfield.addKeyListener(new java.awt.event.KeyAdapter()
250 public void keyTyped(KeyEvent e)
252 textfield_keyTyped();
255 textfield.addActionListener(this);
256 findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
257 findAll.setLabel(MessageManager.getString("action.find_all"));
258 findAll.addActionListener(this);
259 findNext.setEnabled(false);
260 findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
261 findNext.setLabel(MessageManager.getString("action.find_next"));
262 findNext.addActionListener(this);
264 Panel actionsPanel = new Panel();
265 actionsPanel.setBounds(new Rectangle(195, 5, 141, 64));
266 GridLayout gridLayout1 = new GridLayout();
267 actionsPanel.setLayout(gridLayout1);
268 gridLayout1.setHgap(0);
269 gridLayout1.setRows(3);
270 gridLayout1.setVgap(2);
271 createFeatures.setEnabled(false);
272 createFeatures.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
273 createFeatures.setLabel(MessageManager.getString("label.new_feature"));
274 createFeatures.addActionListener(this);
275 caseSensitive.setLabel(MessageManager.getString("label.match_case"));
276 caseSensitive.setBounds(new Rectangle(30, 39, 126, 23));
278 searchDescription.setLabel(
279 MessageManager.getString("label.include_description"));
280 searchDescription.setBounds(new Rectangle(30, 59, 170, 23));
281 actionsPanel.add(findNext, null);
282 actionsPanel.add(findAll, null);
283 actionsPanel.add(createFeatures, null);
284 this.add(caseSensitive);
285 this.add(textfield, null);
286 this.add(jLabel1, null);
287 this.add(actionsPanel, null);
288 this.add(searchDescription);
291 void textfield_keyTyped()
293 findNext.setEnabled(true);