added Platform settings separating mouse use of win/mac difference
[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.Dimension;
32 import java.awt.Font;
33 import java.awt.GridLayout;
34 import java.awt.Insets;
35 import java.awt.event.ActionEvent;
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   JLabel jLabelFind = new JLabel();
50
51   protected JButton findAll = new JButton();
52
53   protected JButton findNext = new JButton();
54
55   JPanel actionsPanel = new JPanel();
56
57   GridLayout gridLayout1 = new GridLayout();
58
59   protected JButton createFeatures = new JButton();
60
61   protected JvCacheableInputBox<String> searchBox = new JvCacheableInputBox<String>(
62           getCacheKey());
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   private static final String FINDER_CACHE_KEY = "CACHE.FINDER";
83
84   public GFinder()
85   {
86     try
87     {
88       jbInit();
89     } catch (Exception e)
90     {
91       e.printStackTrace();
92     }
93   }
94
95   private void jbInit() throws Exception
96   {
97     jLabelFind.setFont(new java.awt.Font("Verdana", 0, 12));
98     jLabelFind.setText(MessageManager.getString("label.find"));
99     this.setLayout(mainBorderLayout);
100     findAll.setFont(new java.awt.Font("Verdana", 0, 12));
101     findAll.setText(MessageManager.getString("action.find_all"));
102     findAll.addActionListener(new java.awt.event.ActionListener()
103     {
104       @Override
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       @Override
115       public void actionPerformed(ActionEvent e)
116       {
117         findNext_actionPerformed(e);
118       }
119     });
120     actionsPanel.setLayout(gridLayout1);
121     gridLayout1.setHgap(0);
122     gridLayout1.setRows(3);
123     gridLayout1.setVgap(2);
124     createFeatures.setEnabled(false);
125     createFeatures.setFont(new java.awt.Font("Verdana", 0, 12));
126     createFeatures.setMargin(new Insets(0, 0, 0, 0));
127     createFeatures.setText(MessageManager.getString("label.new_feature"));
128     createFeatures.addActionListener(new java.awt.event.ActionListener()
129     {
130       @Override
131       public void actionPerformed(ActionEvent e)
132       {
133         createFeatures_actionPerformed();
134       }
135     });
136     searchBox.getComponent()
137             .setFont(new java.awt.Font("Verdana", Font.PLAIN, 12));
138     searchBox.addCaretListener(new CaretListener()
139             {
140               @Override
141               public void caretUpdate(CaretEvent e)
142               {
143                 textfield_caretUpdate(e);
144               }
145             });
146     searchBox.addKeyListener(new java.awt.event.KeyAdapter()
147             {
148               @Override
149               public void keyPressed(KeyEvent e)
150               {
151                 textfield_keyPressed(e);
152               }
153             });
154     mainBorderLayout.setHgap(5);
155     mainBorderLayout.setVgap(5);
156     jPanel4.setLayout(borderLayout2);
157     jPanel2.setPreferredSize(new Dimension(10, 1));
158     jPanel3.setPreferredSize(new Dimension(10, 1));
159     caseSensitive.setHorizontalAlignment(SwingConstants.LEFT);
160     caseSensitive.setText(MessageManager.getString("label.match_case"));
161
162     searchDescription
163             .setText(MessageManager.getString("label.include_description"));
164
165     actionsPanel.add(findNext, null);
166     actionsPanel.add(findAll, null);
167     actionsPanel.add(createFeatures, null);
168     this.add(jLabelFind, java.awt.BorderLayout.WEST);
169     this.add(actionsPanel, java.awt.BorderLayout.EAST);
170     // this.add(jPanel2, java.awt.BorderLayout.SOUTH);
171     this.add(jPanel3, java.awt.BorderLayout.NORTH);
172     this.add(jPanel4, java.awt.BorderLayout.CENTER);
173     jPanel4.add(searchBox.getComponent(), java.awt.BorderLayout.NORTH);
174
175     JPanel optionsPanel = new JPanel();
176
177     optionsGridLayout.setHgap(0);
178     optionsGridLayout.setRows(2);
179     optionsGridLayout.setVgap(2);
180     optionsPanel.setLayout(optionsGridLayout);
181     optionsPanel.add(caseSensitive, null);
182     optionsPanel.add(searchDescription, null);
183
184     jPanel4.add(optionsPanel, java.awt.BorderLayout.WEST);
185   }
186
187   protected void textfield_keyPressed(KeyEvent e)
188   {
189     if (e.getKeyCode() == KeyEvent.VK_ENTER)
190     {
191       if (!searchBox.isPopupVisible())
192       {
193         e.consume();
194         findNext_actionPerformed(null);
195       }
196     }
197   }
198
199   protected void findNext_actionPerformed(ActionEvent e)
200   {
201   }
202
203   protected void findAll_actionPerformed(ActionEvent e)
204   {
205   }
206
207   public void createFeatures_actionPerformed()
208   {
209   }
210
211   public void textfield_caretUpdate(CaretEvent e)
212   {
213     if (searchBox.getUserInput().indexOf(">") > -1)
214     {
215       SwingUtilities.invokeLater(new Runnable()
216       {
217         @Override
218         public void run()
219         {
220           String str = searchBox.getUserInput();
221           AlignmentI al = null;
222           try
223           {
224             al = new FormatAdapter().readFile(str, DataSourceType.PASTE,
225                     FileFormat.Fasta);
226           } catch (Exception ex)
227           {
228           }
229           if (al != null && al.getHeight() > 0)
230           {
231             str = jalview.analysis.AlignSeq.extractGaps(
232                     jalview.util.Comparison.GapChars,
233                     al.getSequenceAt(0).getSequenceAsString());
234
235           }
236         }
237       });
238     }
239   }
240
241   /**
242    * Returns unique key used for storing Finder cache items in the cache data
243    * structure
244    * 
245    * @return
246    */
247   public String getCacheKey()
248   {
249     return FINDER_CACHE_KEY;
250   }
251
252 }