1e748ce8a3297253c93733a6044ff881501fd15e
[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.analysis.AlignSeq;
24 import jalview.datamodel.AlignmentI;
25 import jalview.io.FormatAdapter;
26 import jalview.util.Comparison;
27 import jalview.util.MessageManager;
28
29 import java.awt.BorderLayout;
30 import java.awt.Dimension;
31 import java.awt.Font;
32 import java.awt.GridLayout;
33 import java.awt.Insets;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.KeyEvent;
36
37 import javax.swing.JButton;
38 import javax.swing.JCheckBox;
39 import javax.swing.JLabel;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTextArea;
43 import javax.swing.SwingConstants;
44 import javax.swing.SwingUtilities;
45 import javax.swing.event.CaretEvent;
46 import javax.swing.event.CaretListener;
47
48 public class GFinder extends JPanel
49 {
50   JLabel jLabelFind = new JLabel();
51
52   protected JButton findAll = new JButton();
53
54   protected JButton findNext = new JButton();
55
56   JPanel actionsPanel = new JPanel();
57
58   GridLayout gridLayout1 = new GridLayout();
59
60   protected JButton createNewGroup = new JButton();
61
62   JScrollPane jScrollPane1 = new JScrollPane();
63
64   protected JTextArea textfield = new JTextArea();
65
66   BorderLayout mainBorderLayout = new BorderLayout();
67
68   JPanel jPanel2 = new JPanel();
69
70   JPanel jPanel3 = new JPanel();
71
72   JPanel jPanel4 = new JPanel();
73
74   BorderLayout borderLayout2 = new BorderLayout();
75
76   JPanel jPanel6 = new JPanel();
77
78   protected JCheckBox caseSensitive = new JCheckBox();
79
80   protected JCheckBox searchDescription = new JCheckBox();
81
82   GridLayout optionsGridLayout = new GridLayout();
83
84   public GFinder()
85   {
86     try
87     {
88       jbInit();
89     } catch (Exception e)
90     {
91       e.printStackTrace();
92     }
93   }
94
95
96   private void jbInit() throws Exception
97   {
98     jLabelFind.setFont(new java.awt.Font("Verdana", 0, 12));
99     jLabelFind.setText(MessageManager.getString("label.find"));
100     this.setLayout(mainBorderLayout);
101     findAll.setFont(new java.awt.Font("Verdana", 0, 12));
102     findAll.setText(MessageManager.getString("action.find_all"));
103     findAll.addActionListener(new java.awt.event.ActionListener()
104     {
105       public void actionPerformed(ActionEvent e)
106       {
107         findAll_actionPerformed(e);
108       }
109     });
110     findNext.setFont(new java.awt.Font("Verdana", 0, 12));
111     findNext.setText(MessageManager.getString("action.find_next"));
112     findNext.addActionListener(new java.awt.event.ActionListener()
113     {
114       public void actionPerformed(ActionEvent e)
115       {
116         findNext_actionPerformed(e);
117       }
118     });
119     actionsPanel.setLayout(gridLayout1);
120     gridLayout1.setHgap(0);
121     gridLayout1.setRows(3);
122     gridLayout1.setVgap(2);
123     createNewGroup.setEnabled(false);
124     createNewGroup.setFont(new java.awt.Font("Verdana", 0, 12));
125     createNewGroup.setMargin(new Insets(0, 0, 0, 0));
126     createNewGroup.setText(MessageManager.getString("label.new_feature"));
127     createNewGroup.addActionListener(new java.awt.event.ActionListener()
128     {
129       public void actionPerformed(ActionEvent e)
130       {
131         createNewGroup_actionPerformed(e);
132       }
133     });
134     textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 12));
135     textfield.setText("");
136     textfield.setLineWrap(true);
137     textfield.addCaretListener(new CaretListener()
138     {
139       public void caretUpdate(CaretEvent e)
140       {
141         textfield_caretUpdate(e);
142       }
143     });
144     textfield.addKeyListener(new java.awt.event.KeyAdapter()
145     {
146       public void keyPressed(KeyEvent e)
147       {
148         textfield_keyPressed(e);
149       }
150     });
151
152     mainBorderLayout.setHgap(5);
153     mainBorderLayout.setVgap(5);
154     jPanel4.setLayout(borderLayout2);
155     jPanel2.setPreferredSize(new Dimension(10, 1));
156     jPanel3.setPreferredSize(new Dimension(10, 1));
157     caseSensitive.setHorizontalAlignment(SwingConstants.LEFT);
158     caseSensitive.setText(MessageManager.getString("label.match_case"));
159
160     searchDescription.setText(MessageManager
161             .getString("label.include_description"));
162
163     actionsPanel.add(findNext, null);
164     actionsPanel.add(findAll, null);
165     actionsPanel.add(createNewGroup, null);
166     this.add(jLabelFind, java.awt.BorderLayout.WEST);
167     this.add(actionsPanel, java.awt.BorderLayout.EAST);
168     this.add(jPanel2, java.awt.BorderLayout.SOUTH);
169     this.add(jPanel3, java.awt.BorderLayout.NORTH);
170     this.add(jPanel4, java.awt.BorderLayout.CENTER);
171     jPanel4.add(jScrollPane1, java.awt.BorderLayout.NORTH);
172     jScrollPane1.getViewport().add(textfield);
173
174     JPanel optionsPanel = new JPanel();
175
176     optionsGridLayout.setHgap(0);
177     optionsGridLayout.setRows(2);
178     optionsGridLayout.setVgap(2);
179     optionsPanel.setLayout(optionsGridLayout);
180     optionsPanel.add(caseSensitive, null);
181     optionsPanel.add(searchDescription, null);
182
183     jPanel4.add(optionsPanel, java.awt.BorderLayout.WEST);
184   }
185
186   protected void findNext_actionPerformed(ActionEvent e)
187   {
188   }
189
190   protected void findAll_actionPerformed(ActionEvent e)
191   {
192   }
193
194   protected void textfield_keyPressed(KeyEvent e)
195   {
196     if (e.getKeyCode() == KeyEvent.VK_ENTER)
197     {
198       e.consume();
199       findNext_actionPerformed(null);
200     }
201   }
202
203   public void createNewGroup_actionPerformed(ActionEvent e)
204   {
205   }
206
207   public void textfield_caretUpdate(CaretEvent e)
208   {
209     if (textfield.getText().indexOf(">") > -1)
210     {
211       SwingUtilities.invokeLater(new Runnable()
212       {
213         public void run()
214         {
215           String str = textfield.getText();
216           AlignmentI al = null;
217           try
218           {
219             al = new FormatAdapter().readFile(str, "Paste", "FASTA");
220           } catch (Exception ex)
221           {
222           }
223           if (al != null && al.getHeight() > 0)
224           {
225             str = AlignSeq.extractGaps(
226                     Comparison.GapChars, al.getSequenceAt(0)
227                             .getSequenceAsString());
228
229             textfield.setText(str);
230           }
231         }
232       });
233     }
234   }
235 }