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