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