look for current alignframe to search
[jalview.git] / src / jalview / appletgui / Finder.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 \r
27 import jalview.datamodel.*;\r
28 import java.awt.Rectangle;\r
29 \r
30 public class Finder extends Panel implements ActionListener\r
31 {\r
32   AlignViewport av;\r
33   AlignmentPanel ap;\r
34   Frame frame;\r
35 \r
36   SearchResults searchResults;\r
37 \r
38   int seqIndex = 0;\r
39   int resIndex = 0;\r
40   public Finder(final AlignmentPanel ap)\r
41   {\r
42     try\r
43     {\r
44       jbInit();\r
45     }\r
46     catch (Exception e)\r
47     {\r
48       e.printStackTrace();\r
49     }\r
50 \r
51     this.av = ap.av;\r
52     this.ap = ap;\r
53     frame = new Frame();\r
54     frame.add(this);\r
55     jalview.bin.JalviewLite.addFrame(frame, "Find", 340, 120);\r
56     frame.repaint();\r
57     frame.addWindowListener(new WindowAdapter()\r
58     {\r
59       public void windowClosing(WindowEvent evt)\r
60       {\r
61         ap.highlightSearchResults(null);\r
62       }\r
63     });\r
64   }\r
65 \r
66   public void actionPerformed(ActionEvent evt)\r
67   {\r
68     if (evt.getSource() == textfield)\r
69       doSearch(false);\r
70 \r
71     else if (evt.getSource() == findNext)\r
72       doSearch(false);\r
73 \r
74     else if (evt.getSource() == findAll)\r
75     {\r
76       resIndex = 0;\r
77       seqIndex = 0;\r
78       doSearch(true);\r
79     }\r
80     else if(evt.getSource() == createNewGroup)\r
81       createNewGroup_actionPerformed();\r
82   }\r
83 \r
84 \r
85   public void createNewGroup_actionPerformed()\r
86   {\r
87 \r
88     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, null);\r
89     Dialog dialog = new Dialog(ap.alignFrame, "Enter New Feature Name", true);\r
90     dialog.add(cap);\r
91 \r
92     cap.setText(textfield.getText());\r
93 \r
94     dialog.setBounds( frame.getLocation().x+frame.getSize().width+5,\r
95                       frame.getLocation().y+20,300,100);\r
96     dialog.show();\r
97 \r
98 \r
99     String featureName = cap.getText().trim();\r
100     if(featureName.length()<1)\r
101       return;\r
102 \r
103 \r
104     for (int i = 0; i < searchResults.getSize(); i ++ )\r
105     {\r
106         SequenceI seq = searchResults.getResultSequence(i);\r
107 \r
108         SequenceFeature sf = new SequenceFeature(featureName,\r
109             null, null,\r
110             searchResults.getResultStart(i),\r
111            searchResults.getResultEnd(i), "Search Results");\r
112 \r
113         ap.seqPanel.seqCanvas.getFeatureRenderer().addNewFeature(\r
114             featureName, new Color(60,160,115));\r
115         seq.addSequenceFeature(sf);\r
116     }\r
117 \r
118     ap.seqPanel.seqCanvas.getFeatureRenderer().findAllFeatures();\r
119     ap.alignFrame.sequenceFeatures.setState(true);\r
120     av.showSequenceFeatures(true);\r
121     ap.highlightSearchResults(null);\r
122   }\r
123 \r
124   void doSearch(boolean findAll)\r
125   {\r
126     if(jalview.bin.JalviewLite.currentAlignFrame!=null)\r
127     {\r
128       ap = jalview.bin.JalviewLite.currentAlignFrame.alignPanel;\r
129       av = ap.av;\r
130     }\r
131     createNewGroup.setEnabled(false);\r
132 \r
133     String searchString = textfield.getText();\r
134     if(!caseSensitive.getState())\r
135       searchString = searchString.toUpperCase();\r
136 \r
137     com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);\r
138 \r
139     searchResults = new SearchResults();\r
140 \r
141     Sequence seq;\r
142     String item = null;\r
143     boolean found = false;\r
144 \r
145     ////// is the searchString a residue number?\r
146     try\r
147     {\r
148       int res = Integer.parseInt(searchString);\r
149       found = true;\r
150 \r
151       if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize(false) < 1)\r
152       {\r
153         seq = (Sequence) av.getAlignment().getSequenceAt(0);\r
154       }\r
155       else\r
156       {\r
157         seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));\r
158       }\r
159 \r
160 \r
161       searchResults.addResult(seq, res, res);\r
162 \r
163     }\r
164     catch (NumberFormatException ex)\r
165     {}\r
166     ///////////////////////////////////////////////\r
167 \r
168 \r
169     int end = av.alignment.getHeight();\r
170 \r
171     SequenceGroup selection = av.getSelectionGroup();\r
172     if (selection != null)\r
173     {\r
174       if (selection.getSize(false) < 1 ||\r
175           (selection.getEndRes() - selection.getStartRes() < 2))\r
176       {\r
177         selection = null;\r
178       }\r
179     }\r
180 \r
181     while (!found && seqIndex < end)\r
182     {\r
183 \r
184       seq = (Sequence) av.alignment.getSequenceAt(seqIndex);\r
185 \r
186       if (selection != null && !selection.getSequences(false).contains(seq))\r
187       {\r
188         seqIndex++;\r
189         resIndex = 0;\r
190         continue;\r
191       }\r
192 \r
193       item = seq.getSequence();\r
194       if(!caseSensitive.getState())\r
195         item = item.toUpperCase();\r
196 \r
197       if (selection != null && selection.getEndRes() < av.alignment.getWidth())\r
198       {\r
199         item = item.substring(0, selection.getEndRes() + 1);\r
200       }\r
201 \r
202       ///Shall we ignore gaps????\r
203       StringBuffer noGapsSB = new StringBuffer();\r
204       int insertCount = 0;\r
205       Vector spaces = new Vector();\r
206 \r
207       for (int j = 0; j < item.length(); j++)\r
208       {\r
209 \r
210         if (!jalview.util.Comparison.isGap(item.charAt(j)))\r
211         {\r
212           noGapsSB.append(item.charAt(j));\r
213           spaces.addElement(new Integer(insertCount));\r
214         }\r
215         else\r
216         {\r
217           insertCount++;\r
218         }\r
219       }\r
220 \r
221       String noGaps = noGapsSB.toString();\r
222 \r
223       for (int r = resIndex; r < noGaps.length(); r++)\r
224       {\r
225 \r
226         if (regex.searchFrom(noGaps, r))\r
227         {\r
228           resIndex = regex.matchedFrom();\r
229           if (selection != null &&\r
230               (resIndex + Integer.parseInt(spaces.elementAt(resIndex).toString())) <\r
231               selection.getStartRes())\r
232           {\r
233             continue;\r
234           }\r
235 \r
236 \r
237           int sres = seq.findPosition(resIndex +\r
238                                       Integer.parseInt(spaces.\r
239               elementAt(resIndex).toString()));\r
240           int eres = seq.findPosition(regex.matchedTo() - 1 +\r
241                                       Integer.parseInt(\r
242                                       spaces.elementAt(regex.matchedTo() - 1).\r
243                                       toString()));\r
244 \r
245           searchResults.addResult(seq, sres, eres);\r
246 \r
247           if (!findAll)\r
248           {\r
249             // thats enough, break and display the result\r
250             found = true;\r
251             resIndex++;\r
252             break;\r
253           }\r
254 \r
255           r = resIndex;\r
256         }\r
257         else\r
258           break;\r
259       }\r
260       if (!found)\r
261       {\r
262         seqIndex++;\r
263         resIndex = 0;\r
264       }\r
265     }\r
266 \r
267     Vector idMatch = new Vector();\r
268     for (int id = 0; id < av.alignment.getHeight(); id++)\r
269     {\r
270       if (regex.search(av.alignment.getSequenceAt(id).getName()))\r
271       {\r
272         idMatch.addElement(av.alignment.getSequenceAt(id));\r
273       }\r
274     }\r
275 \r
276     if (searchResults.getSize() == 0 && idMatch.size() > 0)\r
277     {\r
278       ap.idPanel.highlightSearchResults(idMatch);\r
279     }\r
280 \r
281     if (searchResults.getSize() > 0)\r
282     {\r
283       createNewGroup.setEnabled(true);\r
284     }\r
285     else\r
286     {\r
287       searchResults = null;\r
288       resIndex = 0;\r
289       seqIndex = 0;\r
290     }\r
291 \r
292     // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
293     ap.highlightSearchResults(searchResults);\r
294 \r
295     if (findAll)\r
296     {\r
297       String message = (searchResults==null?0 : searchResults.getSize()) + " matches found.";\r
298       ap.alignFrame.statusBar.setText("Search results: "+searchString+" : "+message);\r
299     }\r
300 \r
301   }\r
302 \r
303   Label jLabel1 = new Label();\r
304   protected TextField textfield = new TextField();\r
305   protected Button findAll = new Button();\r
306   protected Button findNext = new Button();\r
307   Panel jPanel1 = new Panel();\r
308   GridLayout gridLayout1 = new GridLayout();\r
309   protected Button createNewGroup = new Button();\r
310   Checkbox caseSensitive = new Checkbox();\r
311 \r
312   private void jbInit() throws Exception {\r
313       jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));\r
314       jLabel1.setText("Find");\r
315       jLabel1.setBounds(new Rectangle(3, 30, 34, 15));\r
316       this.setLayout(null);\r
317       textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
318       textfield.setText("");\r
319       textfield.setBounds(new Rectangle(40, 27, 133, 21));\r
320       textfield.addKeyListener(new java.awt.event.KeyAdapter() {\r
321               public void keyTyped(KeyEvent e) {\r
322                   textfield_keyTyped(e);\r
323               }\r
324           });\r
325       textfield.addActionListener(this);\r
326       findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
327       findAll.setLabel("Find all");\r
328       findAll.addActionListener(this);\r
329       findNext.setEnabled(false);\r
330       findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
331       findNext.setLabel("Find Next");\r
332       findNext.addActionListener(this);\r
333       jPanel1.setBounds(new Rectangle(180, 5, 141, 64));\r
334       jPanel1.setLayout(gridLayout1);\r
335       gridLayout1.setHgap(0);\r
336       gridLayout1.setRows(3);\r
337       gridLayout1.setVgap(2);\r
338       createNewGroup.setEnabled(false);\r
339       createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
340       createNewGroup.setLabel("New Feature");\r
341       createNewGroup.addActionListener(this);\r
342     caseSensitive.setLabel("Match Case");\r
343     caseSensitive.setBounds(new Rectangle(40, 49, 126, 23));\r
344     jPanel1.add(findNext, null);\r
345       jPanel1.add(findAll, null);\r
346       jPanel1.add(createNewGroup, null);\r
347     this.add(caseSensitive);\r
348     this.add(textfield, null);\r
349       this.add(jLabel1, null);\r
350       this.add(jPanel1, null);\r
351   }\r
352 \r
353   void textfield_keyTyped(KeyEvent e) {\r
354       findNext.setEnabled(true);\r
355   }\r
356 \r
357 }\r