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.datamodel.SearchResultMatchI;
25 import jalview.datamodel.SearchResultsI;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.MessageManager;
30 import java.awt.Button;
31 import java.awt.Checkbox;
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;
48 import java.util.Vector;
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 * sequence index and column position of last match,
72 * for current search and per viewport
74 private int seqIndex = 0;
76 private int colIndex = -1;
78 Map<AlignViewportI, Integer> seqIndices;
80 Map<AlignViewportI, Integer> colIndices;
82 public Finder(final AlignmentPanel ap)
84 seqIndices = new HashMap<>();
85 colIndices = new HashMap<>();
98 Frame frame = new Frame();
100 jalview.bin.JalviewLite.addFrame(frame,
101 MessageManager.getString("action.find"), 340, 120);
103 frame.addWindowListener(new WindowAdapter()
106 public void windowClosing(WindowEvent evt)
108 ap.highlightSearchResults(null);
111 textfield.requestFocus();
115 public void actionPerformed(ActionEvent evt)
117 if (evt.getSource() == textfield)
122 else if (evt.getSource() == findNext)
127 else if (evt.getSource() == findAll)
133 else if (evt.getSource() == createFeatures)
135 createFeatures_actionPerformed();
139 public void createFeatures_actionPerformed()
141 List<SequenceI> seqs = new ArrayList<>();
142 List<SequenceFeature> features = new ArrayList<>();
143 String searchString = textfield.getText().trim();
145 for (SearchResultMatchI match : searchResults.getResults())
147 seqs.add(match.getSequence().getDatasetSequence());
148 features.add(new SequenceFeature(searchString, "Search Results",
149 match.getStart(), match.getEnd(), "Search Results"));
152 if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
155 ap.alignFrame.sequenceFeatures.setState(true);
156 av.setShowSequenceFeatures(true);
157 ap.highlightSearchResults(null);
161 void doSearch(boolean doFindAll)
163 if (ap.av.applet.currentAlignFrame != null)
165 ap = ap.av.applet.currentAlignFrame.alignPanel;
169 if (seqIndices.containsKey(av))
171 seqIndex = seqIndices.get(av).intValue();
173 if (colIndices.containsKey(av))
175 colIndex = colIndices.get(av).intValue();
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);
185 String searchString = textfield.getText();
187 finder.find(searchString);
189 seqIndex = finder.getSequenceIndex();
190 colIndex = finder.getColumnIndex();
191 seqIndices.put(av, seqIndex);
192 colIndices.put(av, colIndex);
193 searchResults = finder.getSearchResults();
195 Vector<SequenceI> idMatch = finder.getIdMatch();
196 ap.idPanel.highlightSearchResults(idMatch);
198 if (searchResults.isEmpty())
200 searchResults = null;
204 createFeatures.setEnabled(true);
207 // if allResults is null, this effectively switches displaySearch flag in
209 ap.highlightSearchResults(searchResults);
210 // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
211 // 'SelectRegion' selection
212 if (idMatch.isEmpty() && searchResults == null)
214 ap.alignFrame.statusBar.setText(
215 MessageManager.getString("label.finished_searching"));
223 String message = (idMatch.size() > 0) ? "" + idMatch.size() + " IDs"
225 if (idMatch.size() > 0 && searchResults != null
226 && searchResults.getSize() > 0)
230 if (searchResults != null)
232 message += searchResults.getSize() + " subsequence matches.";
234 ap.alignFrame.statusBar.setText(MessageManager
235 .formatMessage("label.search_results", new String[]
236 { searchString, message }));
241 // TODO: indicate sequence and matching position in status bar
242 ap.alignFrame.statusBar.setText(MessageManager
243 .formatMessage("label.found_match_for", new String[]
249 private void jbInit() throws Exception
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()
261 public void keyTyped(KeyEvent e)
263 textfield_keyTyped();
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);
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));
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);
302 void textfield_keyTyped()
304 findNext.setEnabled(true);