e5d760cc72f68a80fa95d5fd76ebea5a1d79a123
[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.AppCache;
28 import jalview.io.cache.CacheBoxI;
29 import jalview.io.cache.Cacheable;
30 import jalview.util.MessageManager;
31
32 import java.awt.BorderLayout;
33 import java.awt.Dimension;
34 import java.awt.Font;
35 import java.awt.GridLayout;
36 import java.awt.Insets;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.KeyEvent;
39
40 import javax.swing.JButton;
41 import javax.swing.JCheckBox;
42 import javax.swing.JComboBox;
43 import javax.swing.JLabel;
44 import javax.swing.JPanel;
45 import javax.swing.SwingConstants;
46 import javax.swing.SwingUtilities;
47 import javax.swing.event.CaretEvent;
48 import javax.swing.event.CaretListener;
49 import javax.swing.text.JTextComponent;
50
51 public class GFinder extends JPanel implements Cacheable
52 {
53   JLabel jLabelFind = new JLabel();
54
55   protected JButton findAll = new JButton();
56
57   protected JButton findNext = new JButton();
58
59   JPanel actionsPanel = new JPanel();
60
61   GridLayout gridLayout1 = new GridLayout();
62
63   protected JButton createNewGroup = new JButton();
64
65   protected JComboBox<String> searchBox = new JComboBox<String>();
66
67   BorderLayout mainBorderLayout = new BorderLayout();
68
69   JPanel jPanel2 = new JPanel();
70
71   JPanel jPanel3 = new JPanel();
72
73   JPanel jPanel4 = new JPanel();
74
75   BorderLayout borderLayout2 = new BorderLayout();
76
77   JPanel jPanel6 = new JPanel();
78
79   protected JCheckBox caseSensitive = new JCheckBox();
80
81   protected JCheckBox searchDescription = new JCheckBox();
82
83   GridLayout optionsGridLayout = new GridLayout();
84
85   private static final String FINDER_CACHE_KEY = "FINDER_CACHE_KEY";
86
87   public GFinder()
88   {
89     try
90     {
91       jbInit();
92     } catch (Exception e)
93     {
94       e.printStackTrace();
95     }
96   }
97
98   private void jbInit() throws Exception
99   {
100     jLabelFind.setFont(new java.awt.Font("Verdana", 0, 12));
101     jLabelFind.setText(MessageManager.getString("label.find"));
102     this.setLayout(mainBorderLayout);
103     findAll.setFont(new java.awt.Font("Verdana", 0, 12));
104     findAll.setText(MessageManager.getString("action.find_all"));
105     findAll.addActionListener(new java.awt.event.ActionListener()
106     {
107       @Override
108       public void actionPerformed(ActionEvent e)
109       {
110         findAll_actionPerformed(e);
111       }
112     });
113     findNext.setFont(new java.awt.Font("Verdana", 0, 12));
114     findNext.setText(MessageManager.getString("action.find_next"));
115     findNext.addActionListener(new java.awt.event.ActionListener()
116     {
117       @Override
118       public void actionPerformed(ActionEvent e)
119       {
120         findNext_actionPerformed(e);
121       }
122     });
123     actionsPanel.setLayout(gridLayout1);
124     gridLayout1.setHgap(0);
125     gridLayout1.setRows(3);
126     gridLayout1.setVgap(2);
127     createNewGroup.setEnabled(false);
128     createNewGroup.setFont(new java.awt.Font("Verdana", 0, 12));
129     createNewGroup.setMargin(new Insets(0, 0, 0, 0));
130     createNewGroup.setText(MessageManager.getString("label.new_feature"));
131     createNewGroup.addActionListener(new java.awt.event.ActionListener()
132     {
133       @Override
134       public void actionPerformed(ActionEvent e)
135       {
136         createNewGroup_actionPerformed(e);
137       }
138     });
139     searchBox.setFont(new java.awt.Font("Verdana", Font.PLAIN, 12));
140     searchBox.setEditable(true);
141     searchBox
142 .setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
143     ((JTextComponent) searchBox.getEditor().getEditorComponent())
144             .addCaretListener(new CaretListener()
145     {
146       @Override
147       public void caretUpdate(CaretEvent e)
148       {
149         textfield_caretUpdate(e);
150       }
151     });
152     searchBox.getEditor().getEditorComponent()
153             .addKeyListener(new java.awt.event.KeyAdapter()
154     {
155       @Override
156       public void keyPressed(KeyEvent e)
157       {
158         textfield_keyPressed(e);
159       }
160     });
161
162     mainBorderLayout.setHgap(5);
163     mainBorderLayout.setVgap(5);
164     jPanel4.setLayout(borderLayout2);
165     jPanel2.setPreferredSize(new Dimension(10, 1));
166     jPanel3.setPreferredSize(new Dimension(10, 1));
167     caseSensitive.setHorizontalAlignment(SwingConstants.LEFT);
168     caseSensitive.setText(MessageManager.getString("label.match_case"));
169
170     searchDescription.setText(MessageManager
171             .getString("label.include_description"));
172
173     actionsPanel.add(findNext, null);
174     actionsPanel.add(findAll, null);
175     actionsPanel.add(createNewGroup, null);
176     this.add(jLabelFind, java.awt.BorderLayout.WEST);
177     this.add(actionsPanel, java.awt.BorderLayout.EAST);
178     this.add(jPanel2, java.awt.BorderLayout.SOUTH);
179     this.add(jPanel3, java.awt.BorderLayout.NORTH);
180     this.add(jPanel4, java.awt.BorderLayout.CENTER);
181     jPanel4.add(searchBox, java.awt.BorderLayout.NORTH);
182
183     JPanel optionsPanel = new JPanel();
184
185     optionsGridLayout.setHgap(0);
186     optionsGridLayout.setRows(2);
187     optionsGridLayout.setVgap(2);
188     optionsPanel.setLayout(optionsGridLayout);
189     optionsPanel.add(caseSensitive, null);
190     optionsPanel.add(searchDescription, null);
191
192     jPanel4.add(optionsPanel, java.awt.BorderLayout.WEST);
193     fireCache();
194   }
195
196   protected void findNext_actionPerformed(ActionEvent e)
197   {
198   }
199
200   protected void findAll_actionPerformed(ActionEvent e)
201   {
202   }
203
204   protected void textfield_keyPressed(KeyEvent e)
205   {
206     if (e.getKeyCode() == KeyEvent.VK_ENTER)
207     {
208       e.consume();
209       findNext_actionPerformed(null);
210       fireCache();
211     }
212   }
213
214   public void createNewGroup_actionPerformed(ActionEvent e)
215   {
216   }
217
218   public void textfield_caretUpdate(CaretEvent e)
219   {
220     if (searchBox.getEditor().getItem().toString().indexOf(">") > -1)
221     {
222       SwingUtilities.invokeLater(new Runnable()
223       {
224         @Override
225         public void run()
226         {
227           String str = searchBox.getEditor().getItem().toString();
228           AlignmentI al = null;
229           try
230           {
231             al = new FormatAdapter().readFile(str, DataSourceType.PASTE,
232                     FileFormat.Fasta);
233           } catch (Exception ex)
234           {
235           }
236           if (al != null && al.getHeight() > 0)
237           {
238             str = jalview.analysis.AlignSeq.extractGaps(
239                     jalview.util.Comparison.GapChars, al.getSequenceAt(0)
240                             .getSequenceAsString());
241
242             searchBox.setSelectedItem(str);
243           }
244         }
245       });
246     }
247   }
248
249
250   protected void fireCache()
251   {
252     AppCache.getInstance().updateCache(this);
253   }
254
255   @Override
256   public void init()
257   {
258     // TODO Auto-generated method stub
259   }
260
261   @Override
262   public CacheBoxI<String> getCacheComboBox()
263   {
264     return new CacheBoxI<String>()
265     {
266
267       @Override
268       public void setSelectedItem(Object anObject)
269       {
270         searchBox.setSelectedItem(anObject);
271       }
272
273       @Override
274       public void requestFocus()
275       {
276         searchBox.requestFocusInWindow();
277       }
278
279       @Override
280       public void looseFocus()
281       {
282         jPanel6.requestFocusInWindow();
283       }
284
285       @Override
286       public void addItem(String item)
287       {
288         searchBox.addItem(item);
289       }
290
291       @Override
292       public void removeAllItems()
293       {
294         searchBox.removeAllItems();
295       }
296
297       @Override
298       public int getItemCount()
299       {
300         return searchBox.getItemCount();
301       }
302
303       @Override
304       public String getUserInput()
305       {
306         return searchBox.getEditor().getItem() == null ? "" : searchBox
307                 .getEditor().getItem().toString().trim();
308       }
309     };
310   }
311
312   @Override
313   public String getCacheKey()
314   {
315     return FINDER_CACHE_KEY;
316   }
317
318 }