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