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