licencing and format applied (eclipse)
[jalview.git] / src / jalview / gui / Finder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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 package jalview.gui;
20
21 import java.util.*;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import javax.swing.*;
26
27 import jalview.datamodel.*;
28 import jalview.jbgui.*;
29
30 /**
31  * DOCUMENT ME!
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36 public class Finder extends GFinder
37 {
38   AlignViewport av;
39
40   AlignmentPanel ap;
41
42   JInternalFrame frame;
43
44   int seqIndex = 0;
45
46   int resIndex = 0;
47
48   SearchResults searchResults;
49
50   /**
51    * Creates a new Finder object.
52    * 
53    * @param av
54    *                DOCUMENT ME!
55    * @param ap
56    *                DOCUMENT ME!
57    * @param f
58    *                DOCUMENT ME!
59    */
60   public Finder()
61   {
62     frame = new JInternalFrame();
63     frame.setContentPane(this);
64     frame.setLayer(JLayeredPane.PALETTE_LAYER);
65     Desktop.addInternalFrame(frame, "Find", 340, 110);
66
67     textfield.requestFocus();
68   }
69
70   /**
71    * DOCUMENT ME!
72    * 
73    * @param e
74    *                DOCUMENT ME!
75    */
76   public void findNext_actionPerformed(ActionEvent e)
77   {
78     if (getFocusedViewport())
79     {
80       doSearch(false);
81     }
82   }
83
84   /**
85    * DOCUMENT ME!
86    * 
87    * @param e
88    *                DOCUMENT ME!
89    */
90   public void findAll_actionPerformed(ActionEvent e)
91   {
92     if (getFocusedViewport())
93     {
94       resIndex = 0;
95       seqIndex = 0;
96       doSearch(true);
97     }
98   }
99
100   boolean getFocusedViewport()
101   {
102     JInternalFrame frame = Desktop.desktop.getAllFrames()[1];
103
104     if (frame != null && frame instanceof AlignFrame)
105     {
106       av = ((AlignFrame) frame).viewport;
107       ap = ((AlignFrame) frame).alignPanel;
108       return true;
109     }
110     return false;
111   }
112
113   /**
114    * DOCUMENT ME!
115    * 
116    * @param e
117    *                DOCUMENT ME!
118    */
119   public void createNewGroup_actionPerformed(ActionEvent e)
120   {
121     SequenceI[] seqs = new SequenceI[searchResults.getSize()];
122     SequenceFeature[] features = new SequenceFeature[searchResults
123             .getSize()];
124
125     for (int i = 0; i < searchResults.getSize(); i++)
126     {
127       seqs[i] = searchResults.getResultSequence(i).getDatasetSequence();
128
129       features[i] = new SequenceFeature(textfield.getText().trim(),
130               "Search Results", null, searchResults.getResultStart(i),
131               searchResults.getResultEnd(i), "Search Results");
132     }
133
134     if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
135             features, true, ap))
136     {
137       ap.alignFrame.showSeqFeatures.setSelected(true);
138       av.setShowSequenceFeatures(true);
139       ap.highlightSearchResults(null);
140     }
141   }
142
143   /**
144    * DOCUMENT ME!
145    * 
146    * @param findAll
147    *                DOCUMENT ME!
148    */
149   void doSearch(boolean findAll)
150   {
151     createNewGroup.setEnabled(false);
152
153     String searchString = textfield.getText().trim();
154
155     if (searchString.length() < 1)
156     {
157       return;
158     }
159     // TODO: extend finder to match descriptions, features and annotation, and
160     // other stuff
161     // TODO: add switches to control what is searched - sequences, IDS,
162     // descriptions, features
163     jalview.analysis.Finder finder = new jalview.analysis.Finder(
164             av.alignment, av.getSelectionGroup(), seqIndex, resIndex);
165     finder.setCaseSensitive(caseSensitive.isSelected());
166     finder.setFindAll(findAll);
167
168     finder.find(searchString); // returns true if anything was actually found
169
170     seqIndex = finder.getSeqIndex();
171     resIndex = finder.getResIndex();
172
173     searchResults = finder.getSearchResults(); // find(regex,
174                                                 // caseSensitive.isSelected(), )
175     Vector idMatch = finder.getIdMatch();
176     // set or reset the GUI
177     if ((searchResults.getSize() == 0) && (idMatch.size() > 0))
178     {
179       ap.idPanel.highlightSearchResults(idMatch);
180     }
181
182     int resultSize = searchResults.getSize();
183
184     if (searchResults.getSize() > 0)
185     {
186       createNewGroup.setEnabled(true);
187     }
188     else
189     {
190       searchResults = null;
191     }
192
193     // if allResults is null, this effectively switches displaySearch flag in
194     // seqCanvas
195     ap.highlightSearchResults(searchResults);
196     // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
197     // 'SelectRegion' selection
198     if (!findAll && resultSize == 0)
199     {
200       JOptionPane.showInternalMessageDialog(this, "Finished searching",
201               null, JOptionPane.INFORMATION_MESSAGE);
202       resIndex = 0;
203       seqIndex = 0;
204     }
205
206     if (findAll)
207     {
208       String message = resultSize + " matches found.";
209       JOptionPane.showInternalMessageDialog(this, message, null,
210               JOptionPane.INFORMATION_MESSAGE);
211     }
212
213   }
214 }