Merge branch 'features/JAL-1714_specify-mulitple-id_PDB-query' into Release_2_9_Branch
[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.jbgui.PDBDocFieldPreferences.PreferenceSource;
26 import jalview.util.MessageManager;
27 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
28
29 import java.awt.BorderLayout;
30 import java.awt.Dimension;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.event.MouseAdapter;
34 import java.awt.event.MouseEvent;
35
36 import javax.swing.JButton;
37 import javax.swing.JComboBox;
38 import javax.swing.JFrame;
39 import javax.swing.JInternalFrame;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTabbedPane;
43 import javax.swing.JTable;
44 import javax.swing.JTextField;
45 import javax.swing.event.ChangeEvent;
46 import javax.swing.event.ChangeListener;
47 import javax.swing.event.DocumentEvent;
48 import javax.swing.event.DocumentListener;
49
50 /**
51  * GUI layout for PDB Fetch Panel
52  * 
53  * @author tcnofoegbu
54  *
55  */
56 @SuppressWarnings("serial")
57 public abstract class GPDBSearchPanel extends JPanel
58 {
59   protected String frameTitle = MessageManager
60           .getString("label.pdb_sequence_getcher");
61
62   protected JInternalFrame mainFrame = new JInternalFrame(frameTitle);
63
64   protected JComboBox<PDBDocField> cmb_searchTarget = new JComboBox<PDBDocField>();
65
66   protected JButton btn_ok = new JButton();
67
68   protected JButton btn_back = new JButton();
69   
70   protected JButton btn_cancel = new JButton();
71   
72   protected JTextField txt_search = new JTextField(20);
73   
74   protected JTable tbl_summary = new JTable();
75
76   protected JScrollPane scrl_searchResult = new JScrollPane(
77 tbl_summary);
78
79   private JTabbedPane tabbedPane = new JTabbedPane();
80
81   private PDBDocFieldPreferences pdbDocFieldPrefs = new PDBDocFieldPreferences(
82           PreferenceSource.SEARCH_SUMMARY);
83
84   private JPanel pnl_actions = new JPanel();
85
86   private JPanel pnl_results = new JPanel();
87
88   private JPanel pnl_inputs = new JPanel();
89
90   private BorderLayout mainLayout = new BorderLayout();
91
92   public GPDBSearchPanel()
93   {
94     try
95     {
96       jbInit();
97       mainFrame.invalidate();
98       mainFrame.pack();
99     } catch (Exception e)
100     {
101       e.printStackTrace();
102     }
103   }
104
105   /**
106    * Initializes the GUI default properties
107    * 
108    * @throws Exception
109    */
110   private void jbInit() throws Exception
111   {
112     tbl_summary.setAutoCreateRowSorter(true);
113     tbl_summary.addMouseListener(new MouseAdapter()
114     {
115       public void mouseClicked(MouseEvent e)
116       {
117         if (tbl_summary.getSelectedRows().length > 0)
118         {
119           btn_ok.setEnabled(true);
120         }
121         else
122         {
123           btn_ok.setEnabled(false);
124         }
125       }
126     });
127
128     btn_back.setFont(new java.awt.Font("Verdana", 0, 12));
129     btn_back.setText(MessageManager.getString("action.back"));
130     btn_back.addActionListener(new java.awt.event.ActionListener()
131     {
132       public void actionPerformed(ActionEvent e)
133       {
134         btn_back_ActionPerformed();
135       }
136     });
137
138     btn_ok.setEnabled(false);
139     btn_ok.setFont(new java.awt.Font("Verdana", 0, 12));
140     btn_ok.setText(MessageManager.getString("action.ok"));
141     btn_ok.addActionListener(new java.awt.event.ActionListener()
142     {
143       public void actionPerformed(ActionEvent e)
144       {
145         btn_ok_ActionPerformed();
146       }
147     });
148     btn_cancel.setFont(new java.awt.Font("Verdana", 0, 12));
149     btn_cancel.setText(MessageManager.getString("action.cancel"));
150     btn_cancel.addActionListener(new java.awt.event.ActionListener()
151     {
152       public void actionPerformed(ActionEvent e)
153       {
154         btn_cancel_ActionPerformed();
155       }
156     });
157
158
159     scrl_searchResult.setPreferredSize(new Dimension(500, 300));
160     scrl_searchResult
161             .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
162
163     cmb_searchTarget.setFont(new java.awt.Font("Verdana", 0, 12));
164     cmb_searchTarget.addActionListener(new ActionListener()
165     {
166       @Override
167       public void actionPerformed(ActionEvent e)
168       {
169         txt_search_ActionPerformed();
170       }
171     });
172
173     populateCmbSearchTargetOptions();
174
175     txt_search.setFont(new java.awt.Font("Verdana", 0, 12));
176     txt_search.getDocument().addDocumentListener(new DocumentListener()
177     {
178       @Override
179       public void insertUpdate(DocumentEvent e)
180       {
181         txt_search_ActionPerformed();
182       }
183
184       @Override
185       public void removeUpdate(DocumentEvent e)
186       {
187         txt_search_ActionPerformed();
188       }
189
190       @Override
191       public void changedUpdate(DocumentEvent e)
192       {
193         txt_search_ActionPerformed();
194       }
195     });
196
197     final String searchTabTitle = MessageManager
198             .getString("label.search_result");
199     ChangeListener changeListener = new ChangeListener()
200     {
201       public void stateChanged(ChangeEvent changeEvent)
202       {
203         JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
204                 .getSource();
205         int index = sourceTabbedPane.getSelectedIndex();
206         if (sourceTabbedPane.getTitleAt(index).equals(searchTabTitle))
207         {
208           txt_search_ActionPerformed();
209         }
210       }
211     };
212     tabbedPane.addChangeListener(changeListener);
213     tabbedPane.setPreferredSize(new Dimension(500, 300));
214     tabbedPane.add(searchTabTitle, scrl_searchResult);
215     tabbedPane.add(
216             MessageManager.getString("label.configure_displayed_columns"),
217             pdbDocFieldPrefs);
218
219     pnl_actions.add(btn_back);
220     pnl_actions.add(btn_ok);
221     pnl_actions.add(btn_cancel);
222
223     pnl_results.add(tabbedPane);
224     pnl_inputs.add(cmb_searchTarget);
225     pnl_inputs.add(txt_search);
226
227     this.setLayout(mainLayout);
228     this.add(pnl_inputs, java.awt.BorderLayout.NORTH);
229     this.add(pnl_results, java.awt.BorderLayout.CENTER);
230     this.add(pnl_actions, java.awt.BorderLayout.SOUTH);
231     mainFrame.setVisible(true);
232     mainFrame.setContentPane(this);
233     mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
234     Desktop.addInternalFrame(mainFrame, frameTitle, 800, 400);
235   }
236
237   public JComboBox<PDBDocField> getCmbSearchTarget()
238   {
239     return cmb_searchTarget;
240   }
241
242   public JTextField getTxtSearch()
243   {
244     return txt_search;
245   }
246
247   public JInternalFrame getMainFrame()
248   {
249     return mainFrame;
250   }
251
252   public abstract void txt_search_ActionPerformed();
253
254   public abstract void btn_ok_ActionPerformed();
255
256   public abstract void btn_back_ActionPerformed();
257
258   public abstract void btn_cancel_ActionPerformed();
259
260   public abstract void populateCmbSearchTargetOptions();
261
262 }