JAL-1648 Added improvement to persist cache items to properties file beyound a Jalvie...
[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 = "CACHE.FINDER";
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   }
194
195   protected void findNext_actionPerformed(ActionEvent e)
196   {
197   }
198
199   protected void findAll_actionPerformed(ActionEvent e)
200   {
201   }
202
203   protected void textfield_keyPressed(KeyEvent e)
204   {
205     if (e.getKeyCode() == KeyEvent.VK_ENTER)
206     {
207       e.consume();
208       findNext_actionPerformed(null);
209     }
210   }
211
212   public void createNewGroup_actionPerformed(ActionEvent e)
213   {
214   }
215
216   public void textfield_caretUpdate(CaretEvent e)
217   {
218     if (searchBox.getEditor().getItem().toString().indexOf(">") > -1)
219     {
220       SwingUtilities.invokeLater(new Runnable()
221       {
222         @Override
223         public void run()
224         {
225           String str = searchBox.getEditor().getItem().toString();
226           AlignmentI al = null;
227           try
228           {
229             al = new FormatAdapter().readFile(str, DataSourceType.PASTE,
230                     FileFormat.Fasta);
231           } catch (Exception ex)
232           {
233           }
234           if (al != null && al.getHeight() > 0)
235           {
236             str = jalview.analysis.AlignSeq.extractGaps(
237                     jalview.util.Comparison.GapChars, al.getSequenceAt(0)
238                             .getSequenceAsString());
239
240             searchBox.setSelectedItem(str);
241           }
242         }
243       });
244     }
245   }
246
247
248
249
250   @Override
251   public CacheBoxI<String> getCacheComboBox()
252   {
253     return new CacheBoxI<String>()
254     {
255
256       @Override
257       public void setSelectedItem(Object anObject)
258       {
259         searchBox.setSelectedItem(anObject);
260       }
261
262       @Override
263       public void requestFocus()
264       {
265         searchBox.requestFocusInWindow();
266       }
267
268       @Override
269       public void looseFocus()
270       {
271         jPanel6.requestFocusInWindow();
272       }
273
274       @Override
275       public void addItem(String item)
276       {
277         searchBox.addItem(item);
278       }
279
280       @Override
281       public void removeAllItems()
282       {
283         searchBox.removeAllItems();
284       }
285
286       @Override
287       public int getItemCount()
288       {
289         return searchBox.getItemCount();
290       }
291
292       @Override
293       public String getUserInput()
294       {
295         return searchBox.getEditor().getItem() == null ? "" : searchBox
296                 .getEditor().getItem().toString().trim();
297       }
298     };
299   }
300
301   @Override
302   public String getCacheKey()
303   {
304     return FINDER_CACHE_KEY;
305   }
306
307   @Override
308   public void initCache()
309   {
310     AppCache.getInstance().initCache(this);
311   }
312
313   @Override
314   public void updateCache()
315   {
316     AppCache.getInstance().updateCache(this);
317   }
318
319 }