9788fba27fe3f03fd61d744b3736674d6cfa1670
[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.Font;
32 import java.awt.GridLayout;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.KeyAdapter;
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 Font("Verdana",
50           Font.PLAIN, 12);
51
52   private static final String FINDER_CACHE_KEY = "CACHE.FINDER";
53
54   /*
55    * if more checkboxes are wanted, increase this value
56    * and add to centrePanel in jbInit()  
57    */
58   private static final int PANEL_ROWS = 4;
59
60   protected JButton createFeatures;
61
62   protected JvCacheableInputBox<String> searchBox;
63
64   protected JCheckBox caseSensitive;
65
66   protected JCheckBox searchDescription;
67
68   protected JCheckBox ignoreHidden;
69
70   public GFinder()
71   {
72     try
73     {
74       jbInit();
75     } catch (Exception e)
76     {
77       e.printStackTrace();
78     }
79   }
80
81   /**
82    * Constructs the widgets and adds them to the layout
83    */
84   private void jbInit() throws Exception
85   {
86     /*
87      * border layout
88      * West: 4 rows
89      *   first row 'Find'
90      *   remaining rows empty
91      * Center: 4 rows
92      *   first row search box
93      *   second row 'match case' checkbox
94      *   third row 'include description' checkbox
95      *   fourth row 'ignore hidden' checkbox
96      * East: four rows
97      *   first row 'find next' button
98      *   second row 'find all' button
99      *   third row 'new feature' button
100      *   fourth row empty
101      */
102     this.setLayout(new BorderLayout());
103     JPanel eastPanel = new JPanel();
104     eastPanel.setLayout(new GridLayout(PANEL_ROWS, 1));
105     this.add(eastPanel, BorderLayout.EAST);
106     JPanel centrePanel = new JPanel();
107     centrePanel.setLayout(new GridLayout(PANEL_ROWS, 1));
108     this.add(centrePanel, BorderLayout.CENTER);
109     JPanel westPanel = new JPanel();
110     westPanel.setLayout(new GridLayout(PANEL_ROWS, 1));
111     this.add(westPanel, BorderLayout.WEST);
112
113     /*
114      * 'Find' prompt goes top left
115      */
116     JLabel findLabel = new JLabel(
117             " " + MessageManager.getString("label.find") + " ");
118     findLabel.setFont(VERDANA_12);
119     westPanel.add(findLabel);
120
121     /*
122      * search box
123      */
124     searchBox = new JvCacheableInputBox<>(FINDER_CACHE_KEY, 25);
125     searchBox.getComponent().setFont(VERDANA_12);
126     searchBox.addCaretListener(new CaretListener()
127             {
128               @Override
129               public void caretUpdate(CaretEvent e)
130               {
131                 textfield_caretUpdate();
132               }
133             });
134     searchBox.addKeyListener(new KeyAdapter()
135             {
136               @Override
137               public void keyPressed(KeyEvent e)
138               {
139                 textfield_keyPressed(e);
140               }
141             });
142     centrePanel.add(searchBox.getComponent());
143
144     /*
145      * search options checkboxes
146      */
147     caseSensitive = new JCheckBox();
148     caseSensitive.setHorizontalAlignment(SwingConstants.LEFT);
149     caseSensitive.setText(MessageManager.getString("label.match_case"));
150
151     searchDescription = new JCheckBox();
152     searchDescription
153             .setText(MessageManager.getString("label.include_description"));
154
155     ignoreHidden = new JCheckBox();
156     ignoreHidden.setText(MessageManager.getString("label.ignore_hidden"));
157     ignoreHidden.setToolTipText(
158             MessageManager.getString("label.ignore_hidden_tooltip"));
159     
160     centrePanel.add(caseSensitive);
161     centrePanel.add(searchDescription);
162     centrePanel.add(ignoreHidden);
163
164     /*
165      * action buttons
166      */
167     JButton findAll = new JButton(
168             MessageManager.getString("action.find_all"));
169     findAll.setFont(VERDANA_12);
170     findAll.addActionListener(new ActionListener()
171     {
172       @Override
173       public void actionPerformed(ActionEvent e)
174       {
175         findAll_actionPerformed();
176       }
177     });
178     JButton findNext = new JButton(
179             MessageManager.getString("action.find_next"));
180     findNext.setFont(VERDANA_12);
181     findNext.addActionListener(new ActionListener()
182     {
183       @Override
184       public void actionPerformed(ActionEvent e)
185       {
186         findNext_actionPerformed();
187       }
188     });
189     createFeatures = new JButton();
190     createFeatures.setEnabled(false);
191     createFeatures.setFont(VERDANA_12);
192     createFeatures.setText(MessageManager.getString("label.new_feature"));
193     createFeatures.addActionListener(new ActionListener()
194     {
195       @Override
196       public void actionPerformed(ActionEvent e)
197       {
198         createFeatures_actionPerformed();
199       }
200     });
201     eastPanel.add(findNext);
202     eastPanel.add(findAll);
203     eastPanel.add(createFeatures);
204   }
205
206   protected void textfield_keyPressed(KeyEvent e)
207   {
208     if (e.getKeyCode() == KeyEvent.VK_ENTER)
209     {
210       if (!searchBox.isPopupVisible())
211       {
212         e.consume();
213         findNext_actionPerformed();
214       }
215     }
216   }
217
218   protected void findNext_actionPerformed()
219   {
220   }
221
222   protected void findAll_actionPerformed()
223   {
224   }
225
226   public void createFeatures_actionPerformed()
227   {
228   }
229
230   public void textfield_caretUpdate()
231   {
232     // disabled as appears to be running a non-functional
233     if (false && searchBox.getUserInput().indexOf(">") > -1)
234     {
235       SwingUtilities.invokeLater(new Runnable()
236       {
237         @Override
238         public void run()
239         {
240           String str = searchBox.getUserInput();
241           AlignmentI al = null;
242           try
243           {
244             al = new FormatAdapter().readFile(str, DataSourceType.PASTE,
245                     FileFormat.Fasta);
246           } catch (Exception ex)
247           {
248           }
249           if (al != null && al.getHeight() > 0)
250           {
251             str = jalview.analysis.AlignSeq.extractGaps(
252                     jalview.util.Comparison.GapChars,
253                     al.getSequenceAt(0).getSequenceAsString());
254             // todo and what? set str as searchBox text?
255           }
256         }
257       });
258     }
259   }
260
261 }