New seq feature name not added here
[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     SequenceI[] seqs = new SequenceI[searchResults.getSize()];
113     SequenceFeature[] features = new SequenceFeature[searchResults.getSize()];
114
115     for (int i = 0; i < searchResults.getSize(); i++)
116     {
117       seqs[i] = searchResults.getResultSequence(i).getDatasetSequence();
118
119       features[i] = new SequenceFeature(textfield.getText().trim(),
120                                         "Search Results", null,
121                                         searchResults.getResultStart(i),
122                                         searchResults.getResultEnd(i),
123                                         "Search Results");
124     }
125
126     if (ap.seqPanel.seqCanvas.getFeatureRenderer()
127         .createNewFeatures(seqs, features))
128     {
129       ap.alignFrame.showSeqFeatures.setSelected(true);
130       av.setShowSequenceFeatures(true);
131       ap.highlightSearchResults(null);
132     }
133   }
134
135   /**
136    * DOCUMENT ME!
137    *
138    * @param findAll DOCUMENT ME!
139    */
140   void doSearch(boolean findAll)
141   {
142     createNewGroup.setEnabled(false);
143
144     String searchString = textfield.getText().trim();
145
146     if (searchString.length() < 1)
147     {
148       return;
149     }
150     // TODO: extend finder to match descriptions, features and annotation, and other stuff
151     // TODO: add switches to control what is searched - sequences, IDS, descriptions, features
152     jalview.analysis.Finder finder = new jalview.analysis.Finder(av.alignment,
153         av.getSelectionGroup(), seqIndex, resIndex);
154     finder.setCaseSensitive(caseSensitive.isSelected());
155     finder.setFindAll(findAll);
156
157     finder.find(searchString); // returns true if anything was actually found
158
159     seqIndex = finder.getSeqIndex();
160     resIndex = finder.getResIndex();
161
162     searchResults = finder.getSearchResults(); // find(regex, caseSensitive.isSelected(), )
163     Vector idMatch = finder.getIdMatch();
164     // set or reset the GUI
165     if ( (searchResults.getSize() == 0) && (idMatch.size() > 0))
166     {
167       ap.idPanel.highlightSearchResults(idMatch);
168     }
169
170     int resultSize = searchResults.getSize();
171
172     if (searchResults.getSize() > 0)
173     {
174       createNewGroup.setEnabled(true);
175     }
176     else
177     {
178       searchResults = null;
179     }
180
181     // if allResults is null, this effectively switches displaySearch flag in seqCanvas
182     ap.highlightSearchResults(searchResults);
183     // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or 'SelectRegion' selection
184     if (!findAll && resultSize == 0)
185     {
186       JOptionPane.showInternalMessageDialog(this, "Finished searching",
187                                             null,
188                                             JOptionPane.INFORMATION_MESSAGE);
189       resIndex = 0;
190       seqIndex = 0;
191     }
192
193     if (findAll)
194     {
195       String message = resultSize + " matches found.";
196       JOptionPane.showInternalMessageDialog(this, message, null,
197                                             JOptionPane.INFORMATION_MESSAGE);
198     }
199
200   }
201 }