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