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