7543b53290830dc96288618efc37baad556c2cc4
[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           btn_ok_ActionPerformed();
186           break;
187         case KeyEvent.VK_TAB: // tab key
188           btn_back.requestFocus();
189         default:
190           return;
191         }
192       }
193     });
194
195     btn_back.setFont(new java.awt.Font("Verdana", 0, 12));
196     btn_back.setText(MessageManager.getString("action.back"));
197     btn_back.addActionListener(new java.awt.event.ActionListener()
198     {
199       public void actionPerformed(ActionEvent e)
200       {
201         btn_back_ActionPerformed();
202       }
203     });
204     btn_back.addKeyListener(new KeyAdapter()
205     {
206       @Override
207       public void keyPressed(KeyEvent evt)
208       {
209         if (evt.getKeyCode() == KeyEvent.VK_ENTER)
210         {
211           btn_back_ActionPerformed();
212         }
213       }
214     });
215
216     btn_ok.setEnabled(false);
217     btn_ok.setFont(new java.awt.Font("Verdana", 0, 12));
218     btn_ok.setText(MessageManager.getString("action.ok"));
219     btn_ok.addActionListener(new java.awt.event.ActionListener()
220     {
221       public void actionPerformed(ActionEvent e)
222       {
223         btn_ok_ActionPerformed();
224       }
225     });
226     btn_ok.addKeyListener(new KeyAdapter()
227     {
228       @Override
229       public void keyPressed(KeyEvent evt)
230       {
231         if (evt.getKeyCode() == KeyEvent.VK_ENTER)
232         {
233           btn_ok_ActionPerformed();
234         }
235       }
236     });
237
238     btn_cancel.setFont(new java.awt.Font("Verdana", 0, 12));
239     btn_cancel.setText(MessageManager.getString("action.cancel"));
240     btn_cancel.addActionListener(new java.awt.event.ActionListener()
241     {
242       public void actionPerformed(ActionEvent e)
243       {
244         btn_cancel_ActionPerformed();
245       }
246     });
247     btn_cancel.addKeyListener(new KeyAdapter()
248     {
249       @Override
250       public void keyPressed(KeyEvent evt)
251       {
252         if (evt.getKeyCode() == KeyEvent.VK_ENTER)
253         {
254           btn_cancel_ActionPerformed();
255         }
256       }
257     });
258
259     scrl_searchResult.setPreferredSize(new Dimension(500, 300));
260     scrl_searchResult
261             .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
262
263     cmb_searchTarget.setFont(new java.awt.Font("Verdana", 0, 12));
264     cmb_searchTarget.addActionListener(new ActionListener()
265     {
266       @Override
267       public void actionPerformed(ActionEvent e)
268       {
269         String tooltipText;
270         if ("all".equalsIgnoreCase(getCmbSearchTarget().getSelectedItem()
271                 .toString()))
272         {
273           tooltipText = MessageManager.getString("label.search_all");
274         }
275         else if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
276                 .getSelectedItem().toString()))
277         {
278           tooltipText = MessageManager
279                   .getString("label.separate_multiple_accession_ids");
280         }
281         else
282         {
283           tooltipText = MessageManager.formatMessage(
284                   "label.separate_multiple_query_values",
285                   new Object[] { getCmbSearchTarget().getSelectedItem()
286                           .toString() });
287         }
288         txt_search.setToolTipText(JvSwingUtils.wrapTooltip(true,
289                 tooltipText));
290         txt_search_ActionPerformed();
291       }
292     });
293
294     populateCmbSearchTargetOptions();
295
296     txt_search.setFont(new java.awt.Font("Verdana", 0, 12));
297
298     txt_search.addKeyListener(new KeyAdapter()
299     {
300       @Override
301       public void keyPressed(KeyEvent e)
302       {
303         if (e.getKeyCode() == KeyEvent.VK_ENTER)
304         {
305           if (txt_search.getText() == null
306                   || txt_search.getText().isEmpty())
307           {
308             return;
309           }
310           if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
311                   .getSelectedItem().toString()))
312           {
313             transferToSequenceFetcher(txt_search.getText());
314           }
315         }
316       }
317     });
318
319     txt_search.getDocument().addDocumentListener(new DocumentListener()
320     {
321       @Override
322       public void insertUpdate(DocumentEvent e)
323       {
324         txt_search_ActionPerformed();
325       }
326
327       @Override
328       public void removeUpdate(DocumentEvent e)
329       {
330         txt_search_ActionPerformed();
331       }
332
333       @Override
334       public void changedUpdate(DocumentEvent e)
335       {
336         txt_search_ActionPerformed();
337       }
338     });
339
340     final String searchTabTitle = MessageManager
341             .getString("label.search_result");
342     final String configureCols = MessageManager
343             .getString("label.configure_displayed_columns");
344     ChangeListener changeListener = new ChangeListener()
345     {
346       public void stateChanged(ChangeEvent changeEvent)
347       {
348         JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
349                 .getSource();
350         int index = sourceTabbedPane.getSelectedIndex();
351         if (sourceTabbedPane.getTitleAt(index).equals(configureCols))
352         {
353           btn_back.setEnabled(false);
354           btn_cancel.setEnabled(false);
355           btn_ok.setEnabled(false);
356           previousWantedFields = PDBDocFieldPreferences
357                   .getSearchSummaryFields().toArray(new PDBDocField[0]);
358         }
359         if (sourceTabbedPane.getTitleAt(index).equals(searchTabTitle))
360         {
361           btn_back.setEnabled(true);
362           btn_cancel.setEnabled(true);
363           if (wantedFieldsUpdated())
364           {
365             txt_search_ActionPerformed();
366           }
367           else
368           {
369             validateSelection();
370           }
371         }
372       }
373     };
374     tabbedPane.addChangeListener(changeListener);
375     tabbedPane.setPreferredSize(new Dimension(500, 300));
376     tabbedPane.add(searchTabTitle, scrl_searchResult);
377     tabbedPane.add(configureCols, pdbDocFieldPrefs);
378
379     pnl_actions.add(btn_back);
380     pnl_actions.add(btn_ok);
381     pnl_actions.add(btn_cancel);
382
383     pnl_results.add(tabbedPane);
384     pnl_inputs.add(cmb_searchTarget);
385     pnl_inputs.add(txt_search);
386     pnl_inputs.add(lbl_loading);
387     pnl_inputs.add(lbl_warning);
388
389     this.setLayout(mainLayout);
390     this.add(pnl_inputs, java.awt.BorderLayout.NORTH);
391     this.add(pnl_results, java.awt.BorderLayout.CENTER);
392     this.add(pnl_actions, java.awt.BorderLayout.SOUTH);
393     mainFrame.setVisible(true);
394     mainFrame.setContentPane(this);
395     mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
396     Desktop.addInternalFrame(mainFrame, frameTitle, 800, 400);
397   }
398
399   public boolean wantedFieldsUpdated()
400   {
401     if (previousWantedFields == null)
402     {
403       return true;
404     }
405
406     return Arrays.equals(PDBDocFieldPreferences.getSearchSummaryFields()
407             .toArray(new PDBDocField[0]), previousWantedFields) ? false
408             : true;
409
410   }
411
412   public void validateSelection()
413   {
414     if (tbl_summary.getSelectedRows().length > 0)
415     {
416       btn_ok.setEnabled(true);
417     }
418     else
419     {
420       btn_ok.setEnabled(false);
421     }
422   }
423
424   public JComboBox<PDBDocField> getCmbSearchTarget()
425   {
426     return cmb_searchTarget;
427   }
428
429   public JTextField getTxtSearch()
430   {
431     return txt_search;
432   }
433
434   public JInternalFrame getMainFrame()
435   {
436     return mainFrame;
437   }
438
439   public abstract void transferToSequenceFetcher(String ids);
440
441   public abstract void txt_search_ActionPerformed();
442
443   public abstract void btn_ok_ActionPerformed();
444
445   public abstract void btn_back_ActionPerformed();
446
447   public abstract void btn_cancel_ActionPerformed();
448
449   public abstract void populateCmbSearchTargetOptions();
450
451 }