JAL-1807 update
[jalviewjs.git] / unused / appletgui / Finder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.appletgui;
22
23 import jalview.bin.JalviewLite;
24 import jalview.datamodel.SearchResults;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.MessageManager;
28 import jalview.viewmodel.AlignmentViewport;
29
30 import javax.swing.JButton;
31 import javax.swing.JCheckBox;
32 import java.awt.Font;
33 import javax.swing.JFrame;
34 import java.awt.GridLayout;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37 import java.awt.Rectangle;
38 import javax.swing.JTextField;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.KeyEvent;
42 import java.awt.event.WindowAdapter;
43 import java.awt.event.WindowEvent;
44 import java.util.Vector;
45
46 public class Finder extends JPanel implements ActionListener
47 {
48   AlignmentViewport av;
49
50   AlignmentPanel ap;
51
52   JFrame frame;
53
54   SearchResults searchResults;
55
56   int seqIndex = 0;
57
58   int resIndex = -1;
59
60   public Finder(final AlignmentPanel ap)
61   {
62     try
63     {
64       jbInit();
65
66     } catch (Exception e)
67     {
68       e.printStackTrace();
69     }
70
71     this.av = ap.av;
72     this.ap = ap;
73     frame = new JFrame();
74     frame.add(this);
75     JalviewLite.addFrame(frame,
76             MessageManager.getString("action.find"), 340, 120);
77     frame.repaint();
78     frame.addWindowListener(new WindowAdapter()
79     {
80       public void windowClosing(WindowEvent evt)
81       {
82         ap.highlightSearchResults(null);
83       }
84     });
85     textfield.requestFocus();
86   }
87
88   public void actionPerformed(ActionEvent evt)
89   {
90     if (evt.getSource() == textfield)
91     {
92       doSearch(false);
93     }
94
95     else if (evt.getSource() == findNext)
96     {
97       doSearch(false);
98     }
99
100     else if (evt.getSource() == findAll)
101     {
102       resIndex = -1;
103       seqIndex = 0;
104       doSearch(true);
105     }
106     else if (evt.getSource() == createNewGroup)
107     {
108       createNewGroup_actionPerformed();
109     }
110   }
111
112   public void createNewGroup_actionPerformed()
113   {
114     SequenceI[] seqs = new SequenceI[searchResults.getSize()];
115     SequenceFeature[] features = new SequenceFeature[searchResults
116             .getSize()];
117
118     for (int i = 0; i < searchResults.getSize(); i++)
119     {
120       seqs[i] = searchResults.getResultSequence(i);
121
122       features[i] = new SequenceFeature(textfield.getText().trim(),
123               "Search Results", null, searchResults.getResultStart(i),
124               searchResults.getResultEnd(i), "Search Results");
125     }
126
127     if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
128             features, true, ap))
129     {
130       ap.alignFrame.sequenceFeatures.setState(true);
131       av.setShowSequenceFeatures(true);
132       ap.highlightSearchResults(null);
133     }
134   }
135
136   void doSearch(boolean findAll)
137   {
138     if (ap.av.applet.currentAlignFrame != null)
139     {
140       ap = ap.av.applet.currentAlignFrame.alignPanel;
141       av = ap.av;
142     }
143     createNewGroup.setEnabled(false);
144     jalview.analysis.Finder finder = new jalview.analysis.Finder(
145             av.getAlignment(), av.getSelectionGroup(), seqIndex, resIndex);
146     finder.setCaseSensitive(caseSensitive.isSelected());
147     finder.setIncludeDescription(searchDescription.isSelected());
148     finder.setFindAll(findAll);
149
150     String searchString = textfield.getText();
151
152     finder.find(searchString);
153     seqIndex = finder.getSeqIndex();
154     resIndex = finder.getResIndex();
155     searchResults = finder.getSearchResults();
156     Vector idMatch = finder.getIdMatch();
157     boolean haveResults = false;
158     // set or reset the GUI
159     if ((idMatch.size() > 0))
160     {
161       haveResults = true;
162       ap.idPanel.highlightSearchResults(idMatch);
163     }
164     else
165     {
166       ap.idPanel.highlightSearchResults(null);
167     }
168
169     if (searchResults.getSize() > 0)
170     {
171       haveResults = true;
172       createNewGroup.setEnabled(true);
173
174     }
175     else
176     {
177       searchResults = null;
178     }
179
180     // if allResults is null, this effectively switches displaySearch flag in
181     // seqCanvas
182     ap.highlightSearchResults(searchResults);
183     // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or
184     // 'SelectRegion' selection
185     if (!haveResults)
186     {
187       ap.alignFrame.statusBar.setText(MessageManager
188               .getString("label.finished_searching"));
189       resIndex = -1;
190       seqIndex = 0;
191     }
192     else
193     {
194       if (findAll)
195       {
196         String message = (idMatch.size() > 0) ? "" + idMatch.size()
197                 + " IDs" : "";
198         if (idMatch.size() > 0 && searchResults != null
199                 && searchResults.getSize() > 0)
200         {
201           message += " and ";
202         }
203         if (searchResults != null)
204         {
205           message += searchResults.getSize() + " subsequence matches.";
206         }
207         ap.alignFrame.statusBar.setText(MessageManager.formatMessage(
208                 "label.search_results", new String[]
209                 { searchString, message }));
210
211       }
212       else
213       {
214         // TODO: indicate sequence and matching position in status bar
215         ap.alignFrame.statusBar.setText(MessageManager.formatMessage(
216                 "label.found_match_for", new String[]
217                 { searchString }));
218       }
219     }
220   }
221
222   JLabel jLabel1 = new JLabel();
223
224   protected JTextField textfield = new JTextField();
225
226   protected JButton findAll = new JButton();
227
228   protected JButton findNext = new JButton();
229
230   JPanel actionsPanel = new JPanel();
231
232   GridLayout gridLayout1 = new GridLayout();
233
234   protected JButton createNewGroup = new JButton();
235
236   JCheckBox caseSensitive = new JCheckBox();
237
238   JCheckBox searchDescription = new JCheckBox();
239
240   private void jbInit() throws Exception
241   {
242     jLabel1.setFont(new Font("Verdana", 0, 12));
243     jLabel1.setText(MessageManager.getString("action.find"));
244     jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
245     this.setLayout(null);
246     textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
247     textfield.setText("");
248     textfield.setBounds(new Rectangle(40, 17, 133, 21));
249     textfield.addKeyListener(new java.awt.event.KeyAdapter()
250     {
251       public void keyTyped(KeyEvent e)
252       {
253         textfield_keyTyped(e);
254       }
255     });
256     textfield.addActionListener(this);
257     findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
258     findAll.setLabel(MessageManager.getString("action.find_all"));
259     findAll.addActionListener(this);
260     findNext.setEnabled(false);
261     findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
262     findNext.setLabel(MessageManager.getString("action.find_next"));
263     findNext.addActionListener(this);
264     actionsPanel.setBounds(new Rectangle(195, 5, 141, 64));
265     actionsPanel.setLayout(gridLayout1);
266     gridLayout1.setHgap(0);
267     gridLayout1.setRows(3);
268     gridLayout1.setVgap(2);
269     createNewGroup.setEnabled(false);
270     createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
271     createNewGroup.setLabel(MessageManager.getString("label.new_feature"));
272     createNewGroup.addActionListener(this);
273     caseSensitive.setLabel(MessageManager.getString("label.match_case"));
274     caseSensitive.setBounds(new Rectangle(30, 39, 126, 23));
275
276     searchDescription.setLabel(MessageManager
277             .getString("label.include_description"));
278     searchDescription.setBounds(new Rectangle(30, 59, 170, 23));
279     actionsPanel.add(findNext, null);
280     actionsPanel.add(findAll, null);
281     actionsPanel.add(createNewGroup, null);
282     this.add(caseSensitive);
283     this.add(textfield, null);
284     this.add(jLabel1, null);
285     this.add(actionsPanel, null);
286     this.add(searchDescription);
287   }
288
289   void textfield_keyTyped(KeyEvent e)
290   {
291     findNext.setEnabled(true);
292   }
293
294 }