JAL-1860 only allow enter key when valid selection are made and the veiew/ok button...
[jalview.git] / src / jalview / jbgui / GPDBSearchPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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
22 package jalview.jbgui;
23
24 import jalview.gui.Desktop;
25 import jalview.gui.JvSwingUtils;
26 import jalview.jbgui.PDBDocFieldPreferences.PreferenceSource;
27 import jalview.util.MessageManager;
28 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
29
30 import java.awt.BorderLayout;
31 import java.awt.Dimension;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyAdapter;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
38 import java.util.Arrays;
39
40 import javax.swing.ImageIcon;
41 import javax.swing.JButton;
42 import javax.swing.JComboBox;
43 import javax.swing.JFrame;
44 import javax.swing.JInternalFrame;
45 import javax.swing.JLabel;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.JTabbedPane;
49 import javax.swing.JTable;
50 import javax.swing.JTextField;
51 import javax.swing.event.ChangeEvent;
52 import javax.swing.event.ChangeListener;
53 import javax.swing.event.DocumentEvent;
54 import javax.swing.event.DocumentListener;
55
56 /**
57  * GUI layout for PDB Fetch Panel
58  * 
59  * @author tcnofoegbu
60  *
61  */
62 @SuppressWarnings("serial")
63 public abstract class GPDBSearchPanel extends JPanel
64 {
65   protected String frameTitle = MessageManager
66           .getString("label.pdb_sequence_getcher");
67
68   protected JInternalFrame mainFrame = new JInternalFrame(frameTitle);
69
70   protected JComboBox<PDBDocField> cmb_searchTarget = new JComboBox<PDBDocField>();
71
72   protected JButton btn_ok = new JButton();
73
74   protected JButton btn_back = new JButton();
75
76   protected JButton btn_cancel = new JButton();
77
78   protected JTextField txt_search = new JTextField(20);
79
80   protected JTable tbl_summary = new JTable()
81   {
82     public String getToolTipText(MouseEvent evt)
83     {
84       String toolTipText = null;
85       java.awt.Point pnt = evt.getPoint();
86       int rowIndex = rowAtPoint(pnt);
87       int colIndex = columnAtPoint(pnt);
88
89       try
90       {
91         toolTipText = getValueAt(rowIndex, colIndex).toString();
92       } catch (Exception e)
93       {
94         e.printStackTrace();
95       }
96       toolTipText = (toolTipText == null ? null
97               : (toolTipText.length() > 500 ? JvSwingUtils.wrapTooltip(
98                       true, toolTipText.subSequence(0, 500) + "...")
99                       : JvSwingUtils.wrapTooltip(true, toolTipText)));
100
101       return toolTipText;
102     }
103   };
104
105   protected StringBuilder errorWarning = new StringBuilder();
106
107   protected JScrollPane scrl_searchResult = new JScrollPane(tbl_summary);
108
109   protected ImageIcon warningImage = new ImageIcon(getClass().getResource(
110           "/images/warning.gif"));
111
112   protected ImageIcon loadingImage = new ImageIcon(getClass().getResource(
113           "/images/loading.gif"));
114
115   protected JLabel lbl_warning = new JLabel(warningImage);
116
117   protected JLabel lbl_loading = new JLabel(loadingImage);
118
119   private JTabbedPane tabbedPane = new JTabbedPane();
120
121   private PDBDocFieldPreferences pdbDocFieldPrefs = new PDBDocFieldPreferences(
122           PreferenceSource.SEARCH_SUMMARY);
123
124   private JPanel pnl_actions = new JPanel();
125
126   private JPanel pnl_results = new JPanel();
127
128   private JPanel pnl_inputs = new JPanel();
129
130   private BorderLayout mainLayout = new BorderLayout();
131
132   protected PDBDocField[] previousWantedFields;
133
134   public GPDBSearchPanel()
135   {
136     try
137     {
138       jbInit();
139       mainFrame.invalidate();
140       mainFrame.pack();
141     } catch (Exception e)
142     {
143       e.printStackTrace();
144     }
145   }
146
147   /**
148    * Initializes the GUI default properties
149    * 
150    * @throws Exception
151    */
152   private void jbInit() throws Exception
153   {
154     lbl_warning.setVisible(false);
155     lbl_warning.setFont(new java.awt.Font("Verdana", 0, 12));
156     lbl_loading.setVisible(false);
157     lbl_loading.setFont(new java.awt.Font("Verdana", 0, 12));
158
159     tbl_summary.setAutoCreateRowSorter(true);
160     tbl_summary.getTableHeader().setReorderingAllowed(false);
161     tbl_summary.addMouseListener(new MouseAdapter()
162     {
163       public void mouseClicked(MouseEvent e)
164       {
165         validateSelection();
166       }
167
168       public void mouseReleased(MouseEvent e)
169       {
170         validateSelection();
171       }
172     });
173     tbl_summary.addKeyListener(new KeyAdapter()
174     {
175       @Override
176       public void keyPressed(KeyEvent evt)
177       {
178         validateSelection();
179         switch (evt.getKeyCode())
180         {
181         case KeyEvent.VK_ESCAPE: // escape key
182           btn_back_ActionPerformed();
183           break;
184         case KeyEvent.VK_ENTER: // enter key
185           if (btn_ok.isEnabled())
186           {
187             btn_ok_ActionPerformed();
188           }
189           break;
190         case KeyEvent.VK_TAB: // tab key
191           btn_back.requestFocus();
192         default:
193           return;
194         }
195       }
196     });
197
198     btn_back.setFont(new java.awt.Font("Verdana", 0, 12));
199     btn_back.setText(MessageManager.getString("action.back"));
200     btn_back.addActionListener(new java.awt.event.ActionListener()
201     {
202       public void actionPerformed(ActionEvent e)
203       {
204         btn_back_ActionPerformed();
205       }
206     });
207     btn_back.addKeyListener(new KeyAdapter()
208     {
209       @Override
210       public void keyPressed(KeyEvent evt)
211       {
212         if (evt.getKeyCode() == KeyEvent.VK_ENTER)
213         {
214           btn_back_ActionPerformed();
215         }
216       }
217     });
218
219     btn_ok.setEnabled(false);
220     btn_ok.setFont(new java.awt.Font("Verdana", 0, 12));
221     btn_ok.setText(MessageManager.getString("action.ok"));
222     btn_ok.addActionListener(new java.awt.event.ActionListener()
223     {
224       public void actionPerformed(ActionEvent e)
225       {
226         btn_ok_ActionPerformed();
227       }
228     });
229     btn_ok.addKeyListener(new KeyAdapter()
230     {
231       @Override
232       public void keyPressed(KeyEvent evt)
233       {
234         if (evt.getKeyCode() == KeyEvent.VK_ENTER)
235         {
236           btn_ok_ActionPerformed();
237         }
238       }
239     });
240
241     btn_cancel.setFont(new java.awt.Font("Verdana", 0, 12));
242     btn_cancel.setText(MessageManager.getString("action.cancel"));
243     btn_cancel.addActionListener(new java.awt.event.ActionListener()
244     {
245       public void actionPerformed(ActionEvent e)
246       {
247         btn_cancel_ActionPerformed();
248       }
249     });
250     btn_cancel.addKeyListener(new KeyAdapter()
251     {
252       @Override
253       public void keyPressed(KeyEvent evt)
254       {
255         if (evt.getKeyCode() == KeyEvent.VK_ENTER)
256         {
257           btn_cancel_ActionPerformed();
258         }
259       }
260     });
261
262     scrl_searchResult.setPreferredSize(new Dimension(500, 300));
263     scrl_searchResult
264             .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
265
266     cmb_searchTarget.setFont(new java.awt.Font("Verdana", 0, 12));
267     cmb_searchTarget.addActionListener(new ActionListener()
268     {
269       @Override
270       public void actionPerformed(ActionEvent e)
271       {
272         String tooltipText;
273         if ("all".equalsIgnoreCase(getCmbSearchTarget().getSelectedItem()
274                 .toString()))
275         {
276           tooltipText = MessageManager.getString("label.search_all");
277         }
278         else if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
279                 .getSelectedItem().toString()))
280         {
281           tooltipText = MessageManager
282                   .getString("label.separate_multiple_accession_ids");
283         }
284         else
285         {
286           tooltipText = MessageManager.formatMessage(
287                   "label.separate_multiple_query_values",
288                   new Object[] { getCmbSearchTarget().getSelectedItem()
289                           .toString() });
290         }
291         txt_search.setToolTipText(JvSwingUtils.wrapTooltip(true,
292                 tooltipText));
293         txt_search_ActionPerformed();
294       }
295     });
296
297     populateCmbSearchTargetOptions();
298
299     txt_search.setFont(new java.awt.Font("Verdana", 0, 12));
300
301     txt_search.addKeyListener(new KeyAdapter()
302     {
303       @Override
304       public void keyPressed(KeyEvent e)
305       {
306         if (e.getKeyCode() == KeyEvent.VK_ENTER)
307         {
308           if (txt_search.getText() == null
309                   || txt_search.getText().isEmpty())
310           {
311             return;
312           }
313           if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
314                   .getSelectedItem().toString()))
315           {
316             transferToSequenceFetcher(txt_search.getText());
317           }
318         }
319       }
320     });
321
322     txt_search.getDocument().addDocumentListener(new DocumentListener()
323     {
324       @Override
325       public void insertUpdate(DocumentEvent e)
326       {
327         txt_search_ActionPerformed();
328       }
329
330       @Override
331       public void removeUpdate(DocumentEvent e)
332       {
333         txt_search_ActionPerformed();
334       }
335
336       @Override
337       public void changedUpdate(DocumentEvent e)
338       {
339         txt_search_ActionPerformed();
340       }
341     });
342
343     final String searchTabTitle = MessageManager
344             .getString("label.search_result");
345     final String configureCols = MessageManager
346             .getString("label.configure_displayed_columns");
347     ChangeListener changeListener = new ChangeListener()
348     {
349       public void stateChanged(ChangeEvent changeEvent)
350       {
351         JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
352                 .getSource();
353         int index = sourceTabbedPane.getSelectedIndex();
354         if (sourceTabbedPane.getTitleAt(index).equals(configureCols))
355         {
356           btn_back.setEnabled(false);
357           btn_cancel.setEnabled(false);
358           btn_ok.setEnabled(false);
359           previousWantedFields = PDBDocFieldPreferences
360                   .getSearchSummaryFields().toArray(new PDBDocField[0]);
361         }
362         if (sourceTabbedPane.getTitleAt(index).equals(searchTabTitle))
363         {
364           btn_back.setEnabled(true);
365           btn_cancel.setEnabled(true);
366           if (wantedFieldsUpdated())
367           {
368             txt_search_ActionPerformed();
369           }
370           else
371           {
372             validateSelection();
373           }
374         }
375       }
376     };
377     tabbedPane.addChangeListener(changeListener);
378     tabbedPane.setPreferredSize(new Dimension(500, 300));
379     tabbedPane.add(searchTabTitle, scrl_searchResult);
380     tabbedPane.add(configureCols, pdbDocFieldPrefs);
381
382     pnl_actions.add(btn_back);
383     pnl_actions.add(btn_ok);
384     pnl_actions.add(btn_cancel);
385
386     pnl_results.add(tabbedPane);
387     pnl_inputs.add(cmb_searchTarget);
388     pnl_inputs.add(txt_search);
389     pnl_inputs.add(lbl_loading);
390     pnl_inputs.add(lbl_warning);
391
392     this.setLayout(mainLayout);
393     this.add(pnl_inputs, java.awt.BorderLayout.NORTH);
394     this.add(pnl_results, java.awt.BorderLayout.CENTER);
395     this.add(pnl_actions, java.awt.BorderLayout.SOUTH);
396     mainFrame.setVisible(true);
397     mainFrame.setContentPane(this);
398     mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
399     Desktop.addInternalFrame(mainFrame, frameTitle, 800, 400);
400   }
401
402   public boolean wantedFieldsUpdated()
403   {
404     if (previousWantedFields == null)
405     {
406       return true;
407     }
408
409     return Arrays.equals(PDBDocFieldPreferences.getSearchSummaryFields()
410             .toArray(new PDBDocField[0]), previousWantedFields) ? false
411             : true;
412
413   }
414
415   public void validateSelection()
416   {
417     if (tbl_summary.getSelectedRows().length > 0)
418     {
419       btn_ok.setEnabled(true);
420     }
421     else
422     {
423       btn_ok.setEnabled(false);
424     }
425   }
426
427   public JComboBox<PDBDocField> getCmbSearchTarget()
428   {
429     return cmb_searchTarget;
430   }
431
432   public JTextField getTxtSearch()
433   {
434     return txt_search;
435   }
436
437   public JInternalFrame getMainFrame()
438   {
439     return mainFrame;
440   }
441
442   public abstract void transferToSequenceFetcher(String ids);
443
444   public abstract void txt_search_ActionPerformed();
445
446   public abstract void btn_ok_ActionPerformed();
447
448   public abstract void btn_back_ActionPerformed();
449
450   public abstract void btn_cancel_ActionPerformed();
451
452   public abstract void populateCmbSearchTargetOptions();
453
454 }