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