Amend features/annotations in applet
[jalview.git] / src / jalview / appletgui / Finder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19
20 package jalview.appletgui;
21
22 import java.util.*;
23
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import jalview.datamodel.*;
28
29 public class Finder
30     extends Panel implements ActionListener
31 {
32   AlignViewport av;
33   AlignmentPanel ap;
34   Frame frame;
35
36   SearchResults searchResults;
37
38   int seqIndex = 0;
39   int resIndex = 0;
40   public Finder(final AlignmentPanel ap)
41   {
42     try
43     {
44       jbInit();
45
46     }
47     catch (Exception e)
48     {
49       e.printStackTrace();
50     }
51
52     this.av = ap.av;
53     this.ap = ap;
54     frame = new Frame();
55     frame.add(this);
56     jalview.bin.JalviewLite.addFrame(frame, "Find", 340, 120);
57     frame.repaint();
58     frame.addWindowListener(new WindowAdapter()
59     {
60       public void windowClosing(WindowEvent evt)
61       {
62         ap.highlightSearchResults(null);
63       }
64     });
65     textfield.requestFocus();
66   }
67
68   public void actionPerformed(ActionEvent evt)
69   {
70     if (evt.getSource() == textfield)
71     {
72       doSearch(false);
73     }
74
75     else if (evt.getSource() == findNext)
76     {
77       doSearch(false);
78     }
79
80     else if (evt.getSource() == findAll)
81     {
82       resIndex = 0;
83       seqIndex = 0;
84       doSearch(true);
85     }
86     else if (evt.getSource() == createNewGroup)
87     {
88       createNewGroup_actionPerformed();
89     }
90   }
91
92   public void createNewGroup_actionPerformed()
93   {
94     SequenceI[] seqs = new SequenceI[searchResults.getSize()];
95     SequenceFeature[] features = new SequenceFeature[searchResults.getSize()];
96
97     for (int i = 0; i < searchResults.getSize(); i++)
98     {
99       seqs[i] = searchResults.getResultSequence(i);
100
101       features[i] = new SequenceFeature(textfield.getText().trim(),
102                                         "Search Results", null,
103                                         searchResults.getResultStart(i),
104                                         searchResults.getResultEnd(i),
105                                         "Search Results");
106     }
107
108     if (ap.seqPanel.seqCanvas.getFeatureRenderer()
109         .createNewFeatures(seqs, features, ap))
110     {
111       ap.alignFrame.sequenceFeatures.setState(true);
112       av.showSequenceFeatures(true);
113       ap.highlightSearchResults(null);
114     }
115   }
116
117   void doSearch(boolean findAll)
118   {
119     if (jalview.bin.JalviewLite.currentAlignFrame != null)
120     {
121       ap = jalview.bin.JalviewLite.currentAlignFrame.alignPanel;
122       av = ap.av;
123     }
124     createNewGroup.setEnabled(false);
125     jalview.analysis.Finder finder = new jalview.analysis.Finder(av.
126         getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
127     finder.setCaseSensitive(caseSensitive.getState());
128     finder.setFindAll(findAll);
129
130     String searchString = textfield.getText();
131
132     finder.find(searchString);
133     seqIndex = finder.getSeqIndex();
134     resIndex = finder.getResIndex();
135     searchResults = finder.getSearchResults();
136     Vector idMatch = finder.getIdMatch();
137
138     if (searchResults.getSize() == 0 && idMatch.size() > 0)
139     {
140       ap.idPanel.highlightSearchResults(idMatch);
141     }
142
143     if (searchResults.getSize() > 0)
144     {
145       createNewGroup.setEnabled(true);
146
147     }
148     else
149     {
150       searchResults = null;
151       resIndex = 0;
152       seqIndex = 0;
153     }
154
155     // if allResults is null, this effectively switches displaySearch flag in seqCanvas
156     ap.highlightSearchResults(searchResults);
157
158     if (findAll)
159     {
160       String message = (searchResults == null ? 0 : searchResults.getSize()) +
161           " matches found.";
162       ap.alignFrame.statusBar.setText("Search results: " + searchString + " : " +
163                                       message);
164     }
165
166   }
167
168   Label jLabel1 = new Label();
169   protected TextField textfield = new TextField();
170   protected Button findAll = new Button();
171   protected Button findNext = new Button();
172   Panel jPanel1 = new Panel();
173   GridLayout gridLayout1 = new GridLayout();
174   protected Button createNewGroup = new Button();
175   Checkbox caseSensitive = new Checkbox();
176
177   private void jbInit()
178       throws Exception
179   {
180     jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
181     jLabel1.setText("Find");
182     jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
183     this.setLayout(null);
184     textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
185     textfield.setText("");
186     textfield.setBounds(new Rectangle(40, 27, 133, 21));
187     textfield.addKeyListener(new java.awt.event.KeyAdapter()
188     {
189       public void keyTyped(KeyEvent e)
190       {
191         textfield_keyTyped(e);
192       }
193     });
194     textfield.addActionListener(this);
195     findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
196     findAll.setLabel("Find all");
197     findAll.addActionListener(this);
198     findNext.setEnabled(false);
199     findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
200     findNext.setLabel("Find Next");
201     findNext.addActionListener(this);
202     jPanel1.setBounds(new Rectangle(180, 5, 141, 64));
203     jPanel1.setLayout(gridLayout1);
204     gridLayout1.setHgap(0);
205     gridLayout1.setRows(3);
206     gridLayout1.setVgap(2);
207     createNewGroup.setEnabled(false);
208     createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
209     createNewGroup.setLabel("New Feature");
210     createNewGroup.addActionListener(this);
211     caseSensitive.setLabel("Match Case");
212     caseSensitive.setBounds(new Rectangle(40, 49, 126, 23));
213     jPanel1.add(findNext, null);
214     jPanel1.add(findAll, null);
215     jPanel1.add(createNewGroup, null);
216     this.add(caseSensitive);
217     this.add(textfield, null);
218     this.add(jLabel1, null);
219     this.add(jPanel1, null);
220   }
221
222   void textfield_keyTyped(KeyEvent e)
223   {
224     findNext.setEnabled(true);
225   }
226
227 }