JAL-1821 updated the reference sequence column of structure chooser summary table...
[jalview.git] / src / jalview / jbgui / GStructureChooser.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.datamodel.SequenceI;
25 import jalview.gui.AlignmentPanel;
26 import jalview.gui.Desktop;
27 import jalview.jbgui.PDBDocFieldPreferences.PreferenceSource;
28 import jalview.util.MessageManager;
29
30 import java.awt.BorderLayout;
31 import java.awt.CardLayout;
32 import java.awt.Dimension;
33 import java.awt.FlowLayout;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ItemEvent;
36 import java.awt.event.ItemListener;
37 import java.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39
40 import javax.swing.ImageIcon;
41 import javax.swing.JButton;
42 import javax.swing.JCheckBox;
43 import javax.swing.JComboBox;
44 import javax.swing.JFrame;
45 import javax.swing.JInternalFrame;
46 import javax.swing.JLabel;
47 import javax.swing.JPanel;
48 import javax.swing.JScrollPane;
49 import javax.swing.JTabbedPane;
50 import javax.swing.JTable;
51 import javax.swing.JTextField;
52 import javax.swing.event.ChangeEvent;
53 import javax.swing.event.ChangeListener;
54 import javax.swing.event.DocumentEvent;
55 import javax.swing.event.DocumentListener;
56
57 @SuppressWarnings("serial")
58 /**
59  * GUI layout for structure chooser 
60  * @author tcnofoegbu
61  *
62  */
63 public abstract class GStructureChooser extends JPanel implements
64         ItemListener
65 {
66   protected String frameTitle = MessageManager
67           .getString("label.structure_chooser");
68
69   protected JInternalFrame mainFrame = new JInternalFrame(frameTitle);
70
71   protected JComboBox<FilterOption> cmb_filterOption = new JComboBox<FilterOption>();
72
73   protected AlignmentPanel ap;
74   
75   protected StringBuilder errorWarning = new StringBuilder();
76
77   protected JLabel lbl_result = new JLabel(
78           MessageManager.getString("label.select"));
79
80   protected JButton btn_view = new JButton();
81
82   protected JButton btn_cancel = new JButton();
83
84   protected JButton btn_pdbFromFile = new JButton();
85
86   protected JTextField txt_search = new JTextField(14);
87
88   private JPanel pnl_actions = new JPanel();
89
90   private JPanel pnl_main = new JPanel();
91
92   private JPanel pnl_idInput = new JPanel(new FlowLayout());
93
94   private JPanel pnl_fileChooser = new JPanel(new FlowLayout());
95
96   private JPanel pnl_idInputBL = new JPanel(new BorderLayout());
97
98   private JPanel pnl_fileChooserBL = new JPanel(new BorderLayout());
99
100   private JPanel pnl_locPDB = new JPanel(new BorderLayout());
101
102   protected JPanel pnl_switchableViews = new JPanel(new CardLayout());
103
104   protected CardLayout layout_switchableViews = (CardLayout) (pnl_switchableViews
105           .getLayout());
106
107   private BorderLayout mainLayout = new BorderLayout();
108
109   protected JCheckBox chk_rememberSettings = new JCheckBox(
110           MessageManager.getString("label.dont_ask_me_again"));
111
112   protected JCheckBox chk_invertFilter = new JCheckBox(
113           MessageManager.getString("label.invert"));
114
115   protected ImageIcon loadingImage = new ImageIcon(getClass().getResource(
116           "/images/loading.gif"));
117
118   protected ImageIcon goodImage = new ImageIcon(getClass().getResource(
119           "/images/good.png"));
120
121   protected ImageIcon errorImage = new ImageIcon(getClass().getResource(
122           "/images/error.png"));
123
124   protected ImageIcon warningImage = new ImageIcon(getClass().getResource(
125           "/images/warning.gif"));
126
127   protected JLabel lbl_warning = new JLabel(warningImage);
128
129   protected JLabel lbl_loading = new JLabel(loadingImage);
130
131   protected JLabel lbl_pdbManualFetchStatus = new JLabel(errorImage);
132
133   protected JLabel lbl_fromFileStatus = new JLabel(errorImage);
134
135
136   protected AssciateSeqPanel idInputAssSeqPanel = new AssciateSeqPanel();
137
138   protected AssciateSeqPanel fileChooserAssSeqPanel = new AssciateSeqPanel();
139
140   protected static final String VIEWS_FILTER = "VIEWS_FILTER";
141
142   protected static final String VIEWS_FROM_FILE = "VIEWS_FROM_FILE";
143
144   protected static final String VIEWS_ENTER_ID = "VIEWS_ENTER_ID";
145
146   protected static final String VIEWS_LOCAL_PDB = "VIEWS_LOCAL_PDB";
147
148   protected JTable tbl_summary = new JTable();
149
150   protected JScrollPane scrl_foundStructures = new JScrollPane(
151           tbl_summary);
152
153   protected JTable tbl_local_pdb = new JTable();
154
155   protected JScrollPane scrl_localPDB = new JScrollPane(tbl_local_pdb);
156
157   private JTabbedPane pnl_filter = new JTabbedPane();
158
159   private PDBDocFieldPreferences pdbDocFieldPrefs = new PDBDocFieldPreferences(
160           PreferenceSource.STRUCTURE_CHOOSER);
161
162   public GStructureChooser()
163   {
164     try
165     {
166       jbInit();
167       mainFrame.setVisible(false);
168       mainFrame.invalidate();
169       mainFrame.pack();
170     } catch (Exception e)
171     {
172       e.printStackTrace();
173     }
174   }
175
176   /**
177    * Initializes the GUI default properties
178    * 
179    * @throws Exception
180    */
181   private void jbInit() throws Exception
182   {
183     tbl_summary.setAutoCreateRowSorter(true);
184     tbl_summary.getTableHeader().setReorderingAllowed(false);
185     tbl_local_pdb.setAutoCreateRowSorter(true);
186     tbl_local_pdb.addMouseListener(new MouseAdapter()
187     {
188       public void mouseClicked(MouseEvent e)
189       {
190         updateCurrentView();
191       }
192
193       public void mouseReleased(MouseEvent e)
194       {
195         updateCurrentView();
196       }
197     });
198
199     btn_view.setFont(new java.awt.Font("Verdana", 0, 12));
200     btn_view.setText(MessageManager.getString("action.view"));
201     btn_view.addActionListener(new java.awt.event.ActionListener()
202     {
203       public void actionPerformed(ActionEvent e)
204       {
205         ok_ActionPerformed();
206       }
207     });
208     btn_cancel.setFont(new java.awt.Font("Verdana", 0, 12));
209     btn_cancel.setText(MessageManager.getString("action.cancel"));
210     btn_cancel.addActionListener(new java.awt.event.ActionListener()
211     {
212       public void actionPerformed(ActionEvent e)
213       {
214         mainFrame.dispose();
215       }
216     });
217
218     btn_pdbFromFile.setFont(new java.awt.Font("Verdana", 0, 12));
219     String btn_title = MessageManager.getString("label.select_pdb_file");
220     btn_pdbFromFile.setText(btn_title + "              ");
221     btn_pdbFromFile.addActionListener(new java.awt.event.ActionListener()
222     {
223       public void actionPerformed(ActionEvent e)
224       {
225         pdbFromFile_actionPerformed();
226       }
227     });
228
229     scrl_foundStructures.setPreferredSize(new Dimension(500, 300));
230     scrl_foundStructures
231             .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
232
233     scrl_localPDB.setPreferredSize(new Dimension(500, 300));
234     scrl_localPDB
235             .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
236
237     cmb_filterOption.setFont(new java.awt.Font("Verdana", 0, 12));
238     chk_invertFilter.setFont(new java.awt.Font("Verdana", 0, 12));
239     chk_rememberSettings.setFont(new java.awt.Font("Verdana", 0, 12));
240
241
242     txt_search.setToolTipText(MessageManager
243             .getString("label.enter_pdb_id"));
244     cmb_filterOption.setToolTipText(MessageManager
245             .getString("info.select_filter_option"));
246     txt_search.getDocument().addDocumentListener(new DocumentListener()
247     {
248       @Override
249       public void insertUpdate(DocumentEvent e)
250       {
251         txt_search_ActionPerformed();
252       }
253
254       @Override
255       public void removeUpdate(DocumentEvent e)
256       {
257         txt_search_ActionPerformed();
258       }
259
260       @Override
261       public void changedUpdate(DocumentEvent e)
262       {
263         txt_search_ActionPerformed();
264       }
265     });
266
267     cmb_filterOption.addItemListener(this);
268     chk_invertFilter.addItemListener(this);
269
270     pnl_actions.add(chk_rememberSettings);
271     pnl_actions.add(btn_view);
272     pnl_actions.add(btn_cancel);
273
274     // pnl_filter.add(lbl_result);
275     pnl_main.add(cmb_filterOption);
276     pnl_main.add(lbl_loading);
277     pnl_main.add(chk_invertFilter);
278     lbl_loading.setVisible(false);
279
280
281     pnl_fileChooser.add(btn_pdbFromFile);
282     pnl_fileChooser.add(lbl_fromFileStatus);
283     pnl_fileChooserBL.add(fileChooserAssSeqPanel, BorderLayout.NORTH);
284     pnl_fileChooserBL.add(pnl_fileChooser, BorderLayout.CENTER);
285
286     pnl_idInput.add(txt_search);
287     pnl_idInput.add(lbl_pdbManualFetchStatus);
288     pnl_idInputBL.add(idInputAssSeqPanel, BorderLayout.NORTH);
289     pnl_idInputBL.add(pnl_idInput, BorderLayout.CENTER);
290     
291     final String foundStructureSummary = MessageManager
292             .getString("label.found_structures_summary");
293
294     ChangeListener changeListener = new ChangeListener()
295     {
296       public void stateChanged(ChangeEvent changeEvent)
297       {
298         JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
299                 .getSource();
300         int index = sourceTabbedPane.getSelectedIndex();
301         if (sourceTabbedPane.getTitleAt(index)
302                 .equals(foundStructureSummary))
303         {
304           tabRefresh();
305         }
306       }
307     };
308     pnl_filter.addChangeListener(changeListener);
309     pnl_filter.setPreferredSize(new Dimension(500, 300));
310     pnl_filter.add(foundStructureSummary, scrl_foundStructures);
311     pnl_filter.add(
312             MessageManager.getString("label.configure_displayed_columns"),
313             pdbDocFieldPrefs);
314     
315     pnl_locPDB.add(scrl_localPDB);
316
317     pnl_switchableViews.add(pnl_fileChooserBL, VIEWS_FROM_FILE);
318     pnl_switchableViews.add(pnl_idInputBL, VIEWS_ENTER_ID);
319     pnl_switchableViews.add(pnl_filter, VIEWS_FILTER);
320     pnl_switchableViews.add(pnl_locPDB, VIEWS_LOCAL_PDB);
321     
322     this.setLayout(mainLayout);
323     this.add(pnl_main, java.awt.BorderLayout.NORTH);
324     this.add(pnl_switchableViews, java.awt.BorderLayout.CENTER);
325     this.add(pnl_actions, java.awt.BorderLayout.SOUTH);
326
327     mainFrame.setVisible(true);
328     mainFrame.setContentPane(this);
329     mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
330     Desktop.addInternalFrame(mainFrame, frameTitle, 800, 400);
331   }
332
333   
334   @Override
335   /**
336    * Event listener for the 'filter' combo-box and 'invert' check-box
337    */
338   public void itemStateChanged(ItemEvent e)
339   {
340     stateChanged(e);
341   }
342
343   /**
344    * This inner class provides the data model for the structure filter combo-box
345    * 
346    * @author tcnofoegbu
347    *
348    */
349   public class FilterOption
350   {
351     private String name;
352
353     private String value;
354
355     private String view;
356
357     public FilterOption(String name, String value, String view)
358     {
359       this.name = name;
360       this.value = value;
361       this.view = view;
362     }
363
364     public String getName()
365     {
366       return name;
367     }
368
369     public void setName(String name)
370     {
371       this.name = name;
372     }
373
374     public String getValue()
375     {
376       return value;
377     }
378
379     public void setValue(String value)
380     {
381       this.value = value;
382     }
383
384     public String getView()
385     {
386       return view;
387     }
388
389     public void setView(String view)
390     {
391       this.view = view;
392     }
393
394     public String toString()
395     {
396       return this.name;
397     }
398   }
399
400   /**
401    * This inner class provides the provides the data model for associate
402    * sequence combo-box - cmb_assSeq
403    * 
404    * @author tcnofoegbu
405    *
406    */
407   public class AssociateSeqOptions
408   {
409     private SequenceI sequence;
410     private String name;
411
412     public AssociateSeqOptions(SequenceI seq)
413     {
414       this.sequence = seq;
415       this.name = (seq.getName().length() >= 23) ? seq.getName().substring(
416               0, 23) : seq.getName();
417     }
418
419     public AssociateSeqOptions(String name, SequenceI seq)
420     {
421       this.name = name;
422       this.sequence = seq;
423     }
424
425     public String toString()
426     {
427       return name;
428     }
429
430     public String getName()
431     {
432       return name;
433     }
434
435     public void setName(String name)
436     {
437       this.name = name;
438     }
439
440     public SequenceI getSequence()
441     {
442       return sequence;
443     }
444
445     public void setSequence(SequenceI sequence)
446     {
447       this.sequence = sequence;
448     }
449
450   }
451
452   /**
453    * This inner class holds the Layout and configuration of the panel which
454    * handles association of manually fetched structures to a unique sequence
455    * when more than one sequence selection is made
456    * 
457    * @author tcnofoegbu
458    *
459    */
460   public class AssciateSeqPanel extends JPanel implements ItemListener
461   {
462     private JComboBox<AssociateSeqOptions> cmb_assSeq = new JComboBox<AssociateSeqOptions>();
463
464     private JLabel lbl_associateSeq = new JLabel();
465
466     public AssciateSeqPanel()
467     {
468       this.setLayout(new FlowLayout());
469       this.add(cmb_assSeq);
470       this.add(lbl_associateSeq);
471       cmb_assSeq.setToolTipText(MessageManager
472               .getString("info.associate_wit_sequence"));
473       cmb_assSeq.addItemListener(this);
474     }
475
476     public void loadCmbAssSeq()
477     {
478       populateCmbAssociateSeqOptions(cmb_assSeq, lbl_associateSeq);
479     }
480
481     public JComboBox<AssociateSeqOptions> getCmb_assSeq()
482     {
483       return cmb_assSeq;
484     }
485
486     public void setCmb_assSeq(JComboBox<AssociateSeqOptions> cmb_assSeq)
487     {
488       this.cmb_assSeq = cmb_assSeq;
489     }
490
491     @Override
492     public void itemStateChanged(ItemEvent e)
493     {
494       if (e.getStateChange() == ItemEvent.SELECTED)
495       {
496         cmbAssSeqStateChanged();
497       }
498     }
499   }
500
501   public JComboBox<FilterOption> getCmbFilterOption()
502   {
503     return cmb_filterOption;
504   }
505
506   protected abstract void stateChanged(ItemEvent e);
507
508   protected abstract void updateCurrentView();
509
510   protected abstract void populateFilterComboBox();
511
512   protected abstract void ok_ActionPerformed();
513
514   protected abstract void pdbFromFile_actionPerformed();
515
516   protected abstract void txt_search_ActionPerformed();
517
518   public abstract void populateCmbAssociateSeqOptions(
519           JComboBox<AssociateSeqOptions> cmb_assSeq, JLabel lbl_associateSeq);
520
521   public abstract void cmbAssSeqStateChanged();
522
523   public abstract void tabRefresh();
524 }