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