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