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