JAL-1648 code fix - enter key should pick selected item on finder searchbox
[jalview.git] / src / jalview / jbgui / GFinder.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.jbgui;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.io.DataSourceType;
25 import jalview.io.FileFormat;
26 import jalview.io.FormatAdapter;
27 import jalview.io.cache.JvCacheableInputBox;
28 import jalview.util.MessageManager;
29
30 import java.awt.BorderLayout;
31 import java.awt.Dimension;
32 import java.awt.Font;
33 import java.awt.GridLayout;
34 import java.awt.Insets;
35 import java.awt.event.ActionEvent;
36
37 import javax.swing.JButton;
38 import javax.swing.JCheckBox;
39 import javax.swing.JLabel;
40 import javax.swing.JPanel;
41 import javax.swing.SwingConstants;
42 import javax.swing.SwingUtilities;
43 import javax.swing.event.CaretEvent;
44 import javax.swing.event.CaretListener;
45 import javax.swing.text.JTextComponent;
46
47 public class GFinder extends JPanel
48 {
49   JLabel jLabelFind = new JLabel();
50
51   protected JButton findAll = new JButton();
52
53   protected JButton findNext = new JButton();
54
55   JPanel actionsPanel = new JPanel();
56
57   GridLayout gridLayout1 = new GridLayout();
58
59   protected JButton createFeatures = new JButton();
60
61   protected JvCacheableInputBox<String> searchBox = new JvCacheableInputBox<String>(getCacheKey());
62
63   BorderLayout mainBorderLayout = new BorderLayout();
64
65   JPanel jPanel2 = new JPanel();
66
67   JPanel jPanel3 = new JPanel();
68
69   JPanel jPanel4 = new JPanel();
70
71   BorderLayout borderLayout2 = new BorderLayout();
72
73   JPanel jPanel6 = new JPanel();
74
75   protected JCheckBox caseSensitive = new JCheckBox();
76
77   protected JCheckBox searchDescription = new JCheckBox();
78
79   GridLayout optionsGridLayout = new GridLayout();
80
81   private static final String FINDER_CACHE_KEY = "CACHE.FINDER";
82
83   public GFinder()
84   {
85     try
86     {
87       jbInit();
88     } catch (Exception e)
89     {
90       e.printStackTrace();
91     }
92   }
93
94   private void jbInit() throws Exception
95   {
96     jLabelFind.setFont(new java.awt.Font("Verdana", 0, 12));
97     jLabelFind.setText(MessageManager.getString("label.find"));
98     this.setLayout(mainBorderLayout);
99     findAll.setFont(new java.awt.Font("Verdana", 0, 12));
100     findAll.setText(MessageManager.getString("action.find_all"));
101     findAll.addActionListener(new java.awt.event.ActionListener()
102     {
103       @Override
104       public void actionPerformed(ActionEvent e)
105       {
106         findAll_actionPerformed(e);
107       }
108     });
109     findNext.setFont(new java.awt.Font("Verdana", 0, 12));
110     findNext.setText(MessageManager.getString("action.find_next"));
111     findNext.addActionListener(new java.awt.event.ActionListener()
112     {
113       @Override
114       public void actionPerformed(ActionEvent e)
115       {
116         findNext_actionPerformed(e);
117       }
118     });
119     actionsPanel.setLayout(gridLayout1);
120     gridLayout1.setHgap(0);
121     gridLayout1.setRows(3);
122     gridLayout1.setVgap(2);
123     createFeatures.setEnabled(false);
124     createFeatures.setFont(new java.awt.Font("Verdana", 0, 12));
125     createFeatures.setMargin(new Insets(0, 0, 0, 0));
126     createFeatures.setText(MessageManager.getString("label.new_feature"));
127     createFeatures.addActionListener(new java.awt.event.ActionListener()
128     {
129       @Override
130       public void actionPerformed(ActionEvent e)
131       {
132         createFeatures_actionPerformed();
133       }
134     });
135     searchBox.setFont(new java.awt.Font("Verdana", Font.PLAIN, 12));
136     ((JTextComponent) searchBox.getEditor().getEditorComponent())
137             .addCaretListener(new CaretListener()
138     {
139       @Override
140       public void caretUpdate(CaretEvent e)
141       {
142         textfield_caretUpdate(e);
143       }
144     });
145
146     mainBorderLayout.setHgap(5);
147     mainBorderLayout.setVgap(5);
148     jPanel4.setLayout(borderLayout2);
149     jPanel2.setPreferredSize(new Dimension(10, 1));
150     jPanel3.setPreferredSize(new Dimension(10, 1));
151     caseSensitive.setHorizontalAlignment(SwingConstants.LEFT);
152     caseSensitive.setText(MessageManager.getString("label.match_case"));
153
154     searchDescription.setText(MessageManager
155             .getString("label.include_description"));
156
157     actionsPanel.add(findNext, null);
158     actionsPanel.add(findAll, null);
159     actionsPanel.add(createFeatures, null);
160     this.add(jLabelFind, java.awt.BorderLayout.WEST);
161     this.add(actionsPanel, java.awt.BorderLayout.EAST);
162     this.add(jPanel2, java.awt.BorderLayout.SOUTH);
163     this.add(jPanel3, java.awt.BorderLayout.NORTH);
164     this.add(jPanel4, java.awt.BorderLayout.CENTER);
165     jPanel4.add(searchBox, java.awt.BorderLayout.NORTH);
166
167     JPanel optionsPanel = new JPanel();
168
169     optionsGridLayout.setHgap(0);
170     optionsGridLayout.setRows(2);
171     optionsGridLayout.setVgap(2);
172     optionsPanel.setLayout(optionsGridLayout);
173     optionsPanel.add(caseSensitive, null);
174     optionsPanel.add(searchDescription, null);
175
176     jPanel4.add(optionsPanel, java.awt.BorderLayout.WEST);
177   }
178
179   protected void findNext_actionPerformed(ActionEvent e)
180   {
181   }
182
183   protected void findAll_actionPerformed(ActionEvent e)
184   {
185   }
186
187
188   public void createFeatures_actionPerformed()
189   {
190   }
191
192   public void textfield_caretUpdate(CaretEvent e)
193   {
194     if (searchBox.getUserInput().indexOf(">") > -1)
195     {
196       SwingUtilities.invokeLater(new Runnable()
197       {
198         @Override
199         public void run()
200         {
201           String str = searchBox.getUserInput();
202           AlignmentI al = null;
203           try
204           {
205             al = new FormatAdapter().readFile(str, DataSourceType.PASTE,
206                     FileFormat.Fasta);
207           } catch (Exception ex)
208           {
209           }
210           if (al != null && al.getHeight() > 0)
211           {
212             str = jalview.analysis.AlignSeq.extractGaps(
213                     jalview.util.Comparison.GapChars, al.getSequenceAt(0)
214                             .getSequenceAsString());
215
216           }
217         }
218       });
219     }
220   }
221
222
223
224
225
226   /**
227    * Returns unique key used for storing Finder cache items in the cache data
228    * structure
229    * 
230    * @return
231    */
232   public String getCacheKey()
233   {
234     return FINDER_CACHE_KEY;
235   }
236
237
238
239 }