selection removed, now SelectionGroup does same job as id select and residue select
[jalview.git] / src / jalview / gui / Finder.java
1 package jalview.gui;\r
2 \r
3 import jalview.jbgui.GFinder;\r
4 import jalview.datamodel.*;\r
5 import java.awt.event.*;\r
6 import javax.swing.*;\r
7 import javax.swing.event.*;\r
8 import java.util.*;\r
9 import java.awt.*;\r
10 \r
11 public class Finder extends GFinder\r
12 {\r
13   AlignViewport av;\r
14   AlignmentPanel ap;\r
15   JInternalFrame frame;\r
16   String searchString;\r
17   SuperGroup searchGroup;\r
18 \r
19   int seqIndex = 0;\r
20   int resIndex = 0;\r
21   public Finder(AlignViewport av, AlignmentPanel ap, JInternalFrame f)\r
22   {\r
23     this.av = av;\r
24     this.ap = ap;\r
25     frame = f;\r
26 \r
27     // all a big pain, but we need to wait until the frame is visible before the textfield can\r
28     // obtain the focus/////////////////////////\r
29     frame.addInternalFrameListener(new InternalFrameAdapter()\r
30     {\r
31       public void internalFrameOpened(InternalFrameEvent evt)\r
32       {\r
33         SwingUtilities.invokeLater(new Runnable()\r
34         {\r
35           public void run()\r
36           {   textfield.requestFocus();      }\r
37        });\r
38      }\r
39 \r
40      public void internalFrameClosing(InternalFrameEvent evt)\r
41      {\r
42        cancel_actionPerformed(null);\r
43      }\r
44 });\r
45 \r
46   }\r
47 \r
48   public void textfield_actionPerformed(ActionEvent e)\r
49   {\r
50     doSearch(false);\r
51   }\r
52 \r
53   public void findNext_actionPerformed(ActionEvent e)\r
54   {\r
55     doSearch(false);\r
56   }\r
57 \r
58   public void findAll_actionPerformed(ActionEvent e)\r
59   {\r
60     resIndex=0;\r
61     seqIndex=0;\r
62     doSearch(true);\r
63   }\r
64 \r
65   public void cancel_actionPerformed(ActionEvent e)\r
66   {\r
67     try{\r
68       // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
69       ap.highlightSearchResults( null );\r
70      // frame.setClosed(true);\r
71     }catch(Exception ex){ }\r
72   }\r
73 \r
74 \r
75   public void createNewGroup_actionPerformed(ActionEvent e)\r
76   {\r
77     for(int i=0; i<searchGroup.getSize(); i++)\r
78       av.alignment.addGroup( searchGroup.getGroupeAt(i));\r
79 \r
80     ap.av.alignment.addSuperGroup(searchGroup);\r
81     ap.highlightSearchResults( null );\r
82   }\r
83 \r
84 \r
85   void doSearch(boolean findAll)\r
86   {\r
87     createNewGroup.setEnabled(false);\r
88     searchString = textfield.getText().toUpperCase();\r
89 \r
90     ArrayList searchResults = new ArrayList();\r
91     int [] allResults = null;\r
92 \r
93     Sequence seq;\r
94     String item=null;\r
95     boolean found = false;\r
96 \r
97     /// is the searchString a residue number?\r
98     try{\r
99       int res = Integer.parseInt(searchString);\r
100       found = true;\r
101 \r
102       if(av.getSelectionGroup().getSize()>0)\r
103         seq = (Sequence)(av.getSelectionGroup().getSequenceAt(0));\r
104       else\r
105         seq = (Sequence)av.getAlignment().getSequenceAt(0);\r
106 \r
107       searchResults.add( Integer.toString( av.getAlignment().findIndex(seq) ) );\r
108       searchResults.add( Integer.toString( seq.findIndex(res)-1 ) );\r
109       searchResults.add( Integer.toString( seq.findIndex(res)-1 ) );\r
110 \r
111     }catch(NumberFormatException ex){}\r
112 \r
113     jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();\r
114     Color [] newColors = new Color[24];\r
115     for(int i=0; i<24; i++)\r
116       newColors[i] = new Color(60,160,115);\r
117 \r
118     ucs.setColourScheme(newColors);\r
119 \r
120     searchGroup = new SuperGroup(searchString, ucs , true, true, false);\r
121 \r
122     int end = av.alignment.getHeight();\r
123 \r
124 \r
125     SequenceGroup rubberband = av.getSelectionGroup();\r
126     if(rubberband!=null)\r
127       end = rubberband.getEndRes();\r
128 \r
129 \r
130 \r
131     while( !found && seqIndex<end)\r
132     {\r
133       seq = (Sequence)av.getAlignment().getSequenceAt(seqIndex);\r
134 \r
135       if(   av.getSelectionGroup().getSize()>0 && !av.getSelectionGroup().sequences.contains(seq)\r
136             || (rubberband!=null && !rubberband.sequences.contains(seq))  )\r
137       {\r
138         // if a selection has been made, only search within that selection\r
139         seqIndex++;\r
140         resIndex=0;\r
141         continue;\r
142       }\r
143 \r
144       item = seq.getSequence();\r
145 \r
146       if(rubberband!=null)\r
147         item = item.substring(0, rubberband.getEndRes()+1);\r
148 \r
149       ///Shall we ignore gaps????\r
150       StringBuffer noGaps = new StringBuffer();\r
151       int insertCount=0;\r
152       ArrayList spaces = new ArrayList();\r
153 \r
154       for (int j=0; j < item.length(); j++)\r
155       {\r
156 \r
157         if(!jalview.util.Comparison.isGap(item.charAt(j)))\r
158          {\r
159            noGaps.append(item.charAt(j));\r
160            spaces.add(new Integer(insertCount));\r
161          }\r
162         else\r
163            insertCount++;\r
164       }\r
165 \r
166 \r
167       com.stevesoft.pat.Regex regex = new  com.stevesoft.pat.Regex(searchString);\r
168 \r
169       for(int r = resIndex; r<noGaps.length(); r++)\r
170       {\r
171 \r
172        if( regex.searchFrom( noGaps.toString(), r ) )\r
173        {\r
174          resIndex = regex.matchedFrom();\r
175 \r
176 \r
177          if(rubberband!=null && ( resIndex+ Integer.parseInt(spaces.get(resIndex).toString()) )<rubberband.getStartRes())\r
178            continue;\r
179 \r
180          searchResults.add( Integer.toString( seqIndex) );\r
181          searchResults.add( Integer.toString( resIndex+ Integer.parseInt(spaces.get(resIndex).toString()) ) );\r
182          searchResults.add( Integer.toString( regex.matchedTo()-1 + Integer.parseInt(spaces.get(regex.matchedTo()-1).toString()) ) );\r
183 \r
184          SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false,\r
185                                              resIndex+ Integer.parseInt(spaces.get(resIndex).toString()) ,\r
186                                       regex.matchedTo()-1 + Integer.parseInt(spaces.get(regex.matchedTo()-1).toString())\r
187             );\r
188          sg.addSequence((Sequence)av.getAlignment().getSequenceAt(seqIndex));\r
189          searchGroup.addGroup( sg );\r
190 \r
191          if(!findAll)\r
192          {\r
193            // thats enough, break and display the result\r
194            found = true;\r
195            resIndex++;\r
196            break;\r
197          }\r
198 \r
199          r=resIndex;\r
200        }\r
201       }\r
202       if(!found)\r
203       {\r
204         seqIndex++;\r
205         resIndex = 0;\r
206       }\r
207     }\r
208 \r
209     if(searchResults.size()>0)\r
210     {\r
211       allResults = new int[searchResults.size()];\r
212       for(int i=0; i<searchResults.size(); i++)\r
213         allResults[i] = Integer.parseInt(searchResults.get(i).toString());\r
214 \r
215       createNewGroup.setEnabled(true);\r
216     }\r
217     else\r
218     {\r
219       JOptionPane.showInternalMessageDialog(this, "Finished searching", null, JOptionPane.INFORMATION_MESSAGE);\r
220       resIndex=0;\r
221       seqIndex=0;\r
222     }\r
223 \r
224     // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
225     ap.highlightSearchResults( allResults );\r
226 \r
227     if (findAll)\r
228     {\r
229       String message =  (searchResults.size()/3) + " matches found.";\r
230       JOptionPane.showInternalMessageDialog(this, message, null,\r
231                                             JOptionPane.INFORMATION_MESSAGE);\r
232     }\r
233 \r
234   }\r
235 \r
236 }\r