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