Formatting
[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     catch (Exception e)
47     {
48       e.printStackTrace();
49     }
50
51     this.av = ap.av;
52     this.ap = ap;
53     frame = new Frame();
54     frame.add(this);
55     jalview.bin.JalviewLite.addFrame(frame, "Find", 340, 120);
56     frame.repaint();
57     frame.addWindowListener(new WindowAdapter()
58     {
59       public void windowClosing(WindowEvent evt)
60       {
61         ap.highlightSearchResults(null);
62       }
63     });
64   }
65
66   public void actionPerformed(ActionEvent evt)
67   {
68     if (evt.getSource() == textfield)
69     {
70       doSearch(false);
71     }
72
73     else if (evt.getSource() == findNext)
74     {
75       doSearch(false);
76     }
77
78     else if (evt.getSource() == findAll)
79     {
80       resIndex = 0;
81       seqIndex = 0;
82       doSearch(true);
83     }
84     else if (evt.getSource() == createNewGroup)
85     {
86       createNewGroup_actionPerformed();
87     }
88   }
89
90   public void createNewGroup_actionPerformed()
91   {
92
93     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, null);
94     cap.accept.setLabel("Accept");
95     Dialog dialog = new Dialog(ap.alignFrame, "Enter New Feature Name", true);
96     dialog.add(cap);
97
98     cap.setText(textfield.getText());
99
100     dialog.setBounds(frame.getLocation().x + frame.getSize().width + 5,
101                      frame.getLocation().y + 20, 300, 100);
102     dialog.show();
103
104     String featureName = cap.getText().trim();
105     if (featureName.length() < 1)
106     {
107       return;
108     }
109
110     for (int i = 0; i < searchResults.getSize(); i++)
111     {
112       SequenceI seq = searchResults.getResultSequence(i);
113
114       SequenceFeature sf = new SequenceFeature(featureName,
115                                                null, null,
116                                                searchResults.getResultStart(i),
117                                                searchResults.getResultEnd(i),
118                                                "Search Results");
119
120       ap.seqPanel.seqCanvas.getFeatureRenderer().addNewFeature(
121           featureName, new Color(60, 160, 115));
122       seq.addSequenceFeature(sf);
123     }
124
125     ap.seqPanel.seqCanvas.getFeatureRenderer().findAllFeatures();
126     ap.alignFrame.sequenceFeatures.setState(true);
127     av.showSequenceFeatures(true);
128     ap.highlightSearchResults(null);
129   }
130
131   void doSearch(boolean findAll)
132   {
133     if (jalview.bin.JalviewLite.currentAlignFrame != null)
134     {
135       ap = jalview.bin.JalviewLite.currentAlignFrame.alignPanel;
136       av = ap.av;
137     }
138     createNewGroup.setEnabled(false);
139     jalview.analysis.Finder finder = new jalview.analysis.Finder(av.
140         getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
141     finder.setCaseSensitive(caseSensitive.getState());
142     finder.setFindAll(findAll);
143
144     String searchString = textfield.getText();
145
146     finder.find(searchString);
147     seqIndex = finder.getSeqIndex();
148     resIndex = finder.getResIndex();
149     searchResults = finder.getSearchResults();
150     Vector idMatch = finder.getIdMatch();
151
152     if (searchResults.getSize() == 0 && idMatch.size() > 0)
153     {
154       ap.idPanel.highlightSearchResults(idMatch);
155     }
156
157     if (searchResults.getSize() > 0)
158     {
159       createNewGroup.setEnabled(true);
160
161     }
162     else
163     {
164       searchResults = null;
165       resIndex = 0;
166       seqIndex = 0;
167     }
168
169     // if allResults is null, this effectively switches displaySearch flag in seqCanvas
170     ap.highlightSearchResults(searchResults);
171
172     if (findAll)
173     {
174       String message = (searchResults == null ? 0 : searchResults.getSize()) +
175           " matches found.";
176       ap.alignFrame.statusBar.setText("Search results: " + searchString + " : " +
177                                       message);
178     }
179
180   }
181
182   Label jLabel1 = new Label();
183   protected TextField textfield = new TextField();
184   protected Button findAll = new Button();
185   protected Button findNext = new Button();
186   Panel jPanel1 = new Panel();
187   GridLayout gridLayout1 = new GridLayout();
188   protected Button createNewGroup = new Button();
189   Checkbox caseSensitive = new Checkbox();
190
191   private void jbInit()
192       throws Exception
193   {
194     jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
195     jLabel1.setText("Find");
196     jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
197     this.setLayout(null);
198     textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
199     textfield.setText("");
200     textfield.setBounds(new Rectangle(40, 27, 133, 21));
201     textfield.addKeyListener(new java.awt.event.KeyAdapter()
202     {
203       public void keyTyped(KeyEvent e)
204       {
205         textfield_keyTyped(e);
206       }
207     });
208     textfield.addActionListener(this);
209     findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
210     findAll.setLabel("Find all");
211     findAll.addActionListener(this);
212     findNext.setEnabled(false);
213     findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
214     findNext.setLabel("Find Next");
215     findNext.addActionListener(this);
216     jPanel1.setBounds(new Rectangle(180, 5, 141, 64));
217     jPanel1.setLayout(gridLayout1);
218     gridLayout1.setHgap(0);
219     gridLayout1.setRows(3);
220     gridLayout1.setVgap(2);
221     createNewGroup.setEnabled(false);
222     createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
223     createNewGroup.setLabel("New Feature");
224     createNewGroup.addActionListener(this);
225     caseSensitive.setLabel("Match Case");
226     caseSensitive.setBounds(new Rectangle(40, 49, 126, 23));
227     jPanel1.add(findNext, null);
228     jPanel1.add(findAll, null);
229     jPanel1.add(createNewGroup, null);
230     this.add(caseSensitive);
231     this.add(textfield, null);
232     this.add(jLabel1, null);
233     this.add(jPanel1, null);
234   }
235
236   void textfield_keyTyped(KeyEvent e)
237   {
238     findNext.setEnabled(true);
239   }
240
241 }