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