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