ad99983010291f8d2ec7b9a976d39f5beb2dc78f
[jalview.git] / src / jalview / appletgui / Finder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.appletgui;
20
21 import java.util.*;
22
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import jalview.datamodel.*;
27 import jalview.util.MessageManager;
28
29 public class Finder extends Panel implements ActionListener
30 {
31   AlignViewport av;
32
33   AlignmentPanel ap;
34
35   Frame frame;
36
37   SearchResults searchResults;
38
39   int seqIndex = 0;
40
41   int resIndex = -1;
42
43   public Finder(final AlignmentPanel ap)
44   {
45     try
46     {
47       jbInit();
48
49     } catch (Exception e)
50     {
51       e.printStackTrace();
52     }
53
54     this.av = ap.av;
55     this.ap = ap;
56     frame = new Frame();
57     frame.add(this);
58     jalview.bin.JalviewLite.addFrame(frame, MessageManager.getString("action.find"), 340, 120);
59     frame.repaint();
60     frame.addWindowListener(new WindowAdapter()
61     {
62       public void windowClosing(WindowEvent evt)
63       {
64         ap.highlightSearchResults(null);
65       }
66     });
67     textfield.requestFocus();
68   }
69
70   public void actionPerformed(ActionEvent evt)
71   {
72     if (evt.getSource() == textfield)
73     {
74       doSearch(false);
75     }
76
77     else if (evt.getSource() == findNext)
78     {
79       doSearch(false);
80     }
81
82     else if (evt.getSource() == findAll)
83     {
84       resIndex = -1;
85       seqIndex = 0;
86       doSearch(true);
87     }
88     else if (evt.getSource() == createNewGroup)
89     {
90       createNewGroup_actionPerformed();
91     }
92   }
93
94   public void createNewGroup_actionPerformed()
95   {
96     SequenceI[] seqs = new SequenceI[searchResults.getSize()];
97     SequenceFeature[] features = new SequenceFeature[searchResults
98             .getSize()];
99
100     for (int i = 0; i < searchResults.getSize(); i++)
101     {
102       seqs[i] = searchResults.getResultSequence(i);
103
104       features[i] = new SequenceFeature(textfield.getText().trim(),
105               "Search Results", null, searchResults.getResultStart(i),
106               searchResults.getResultEnd(i), "Search Results");
107     }
108
109     if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
110             features, true, ap))
111     {
112       ap.alignFrame.sequenceFeatures.setState(true);
113       av.showSequenceFeatures(true);
114       ap.highlightSearchResults(null);
115     }
116   }
117
118   void doSearch(boolean findAll)
119   {
120     if (ap.av.applet.currentAlignFrame != null)
121     {
122       ap = ap.av.applet.currentAlignFrame.alignPanel;
123       av = ap.av;
124     }
125     createNewGroup.setEnabled(false);
126     jalview.analysis.Finder finder = new jalview.analysis.Finder(
127             av.getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
128     finder.setCaseSensitive(caseSensitive.getState());
129     finder.setFindAll(findAll);
130
131     String searchString = textfield.getText();
132
133     finder.find(searchString);
134     seqIndex = finder.getSeqIndex();
135     resIndex = finder.getResIndex();
136     searchResults = finder.getSearchResults();
137     Vector idMatch = finder.getIdMatch();
138     boolean haveResults = false;
139     // set or reset the GUI
140     if ((idMatch.size() > 0))
141     {
142       haveResults = true;
143       ap.idPanel.highlightSearchResults(idMatch);
144     }
145     else
146     {
147       ap.idPanel.highlightSearchResults(null);
148     }
149
150     if (searchResults.getSize() > 0)
151     {
152       haveResults = true;
153       createNewGroup.setEnabled(true);
154
155     }
156     else
157     {
158       searchResults = null;
159     }
160
161     // if allResults is null, this effectively switches displaySearch flag in
162     // seqCanvas
163     ap.highlightSearchResults(searchResults);
164     // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
165     // 'SelectRegion' selection
166     if (!haveResults)
167     {
168       ap.alignFrame.statusBar.setText(MessageManager.getString("label.finished_searching"));
169       resIndex = -1;
170       seqIndex = 0;
171     }
172     else
173     {
174       if (findAll)
175       {
176         String message = (idMatch.size() > 0) ? "" + idMatch.size()
177                 + " IDs" : "";
178         if (idMatch.size() > 0 && searchResults != null
179                 && searchResults.getSize() > 0)
180         {
181           message += " and ";
182         }
183         if (searchResults != null)
184         {
185           message += searchResults.getSize() + " subsequence matches.";
186         }
187         ap.alignFrame.statusBar.setText(MessageManager.formatMessage("label.search_results", new String[] {searchString, message}));
188
189       }
190       else
191       {
192         // TODO: indicate sequence and matching position in status bar
193         ap.alignFrame.statusBar.setText(MessageManager.formatMessage("label.found_match_for", new String[]{searchString}));
194       }
195     }
196   }
197
198   Label jLabel1 = new Label();
199
200   protected TextField textfield = new TextField();
201
202   protected Button findAll = new Button();
203
204   protected Button findNext = new Button();
205
206   Panel jPanel1 = new Panel();
207
208   GridLayout gridLayout1 = new GridLayout();
209
210   protected Button createNewGroup = new Button();
211
212   Checkbox caseSensitive = new Checkbox();
213
214   private void jbInit() throws Exception
215   {
216     jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
217     jLabel1.setText(MessageManager.getString("action.find"));
218     jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
219     this.setLayout(null);
220     textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
221     textfield.setText("");
222     textfield.setBounds(new Rectangle(40, 27, 133, 21));
223     textfield.addKeyListener(new java.awt.event.KeyAdapter()
224     {
225       public void keyTyped(KeyEvent e)
226       {
227         textfield_keyTyped(e);
228       }
229     });
230     textfield.addActionListener(this);
231     findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
232     findAll.setLabel(MessageManager.getString("action.find_all"));
233     findAll.addActionListener(this);
234     findNext.setEnabled(false);
235     findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
236     findNext.setLabel(MessageManager.getString("action.find_next"));
237     findNext.addActionListener(this);
238     jPanel1.setBounds(new Rectangle(180, 5, 141, 64));
239     jPanel1.setLayout(gridLayout1);
240     gridLayout1.setHgap(0);
241     gridLayout1.setRows(3);
242     gridLayout1.setVgap(2);
243     createNewGroup.setEnabled(false);
244     createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
245     createNewGroup.setLabel(MessageManager.getString("label.new_feature"));
246     createNewGroup.addActionListener(this);
247     caseSensitive.setLabel(MessageManager.getString("label.match_case"));
248     caseSensitive.setBounds(new Rectangle(40, 49, 126, 23));
249     jPanel1.add(findNext, null);
250     jPanel1.add(findAll, null);
251     jPanel1.add(createNewGroup, null);
252     this.add(caseSensitive);
253     this.add(textfield, null);
254     this.add(jLabel1, null);
255     this.add(jPanel1, null);
256   }
257
258   void textfield_keyTyped(KeyEvent e)
259   {
260     findNext.setEnabled(true);
261   }
262
263 }