2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
22 package jalview.fts.core;
24 import jalview.fts.api.FTSDataColumnI;
25 import jalview.fts.api.GFTSPanelI;
26 import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
27 import jalview.gui.Desktop;
28 import jalview.gui.IProgressIndicator;
29 import jalview.gui.JvSwingUtils;
30 import jalview.gui.SequenceFetcher;
31 import jalview.util.MessageManager;
33 import java.awt.BorderLayout;
34 import java.awt.CardLayout;
35 import java.awt.Dimension;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.FocusEvent;
39 import java.awt.event.FocusListener;
40 import java.awt.event.KeyAdapter;
41 import java.awt.event.KeyEvent;
42 import java.awt.event.MouseAdapter;
43 import java.awt.event.MouseEvent;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.Comparator;
49 import java.util.List;
51 import javax.swing.ImageIcon;
52 import javax.swing.JButton;
53 import javax.swing.JComboBox;
54 import javax.swing.JFrame;
55 import javax.swing.JInternalFrame;
56 import javax.swing.JLabel;
57 import javax.swing.JPanel;
58 import javax.swing.JScrollPane;
59 import javax.swing.JTabbedPane;
60 import javax.swing.JTable;
61 import javax.swing.JTextField;
62 import javax.swing.Timer;
63 import javax.swing.event.ChangeEvent;
64 import javax.swing.event.ChangeListener;
65 import javax.swing.event.DocumentEvent;
66 import javax.swing.event.DocumentListener;
67 import javax.swing.table.DefaultTableModel;
70 * This class provides the swing GUI layout for FTS Panel and implements most of
71 * the contracts defined in GFSPanelI
77 @SuppressWarnings("serial")
78 public abstract class GFTSPanel extends JPanel implements GFTSPanelI
80 protected JInternalFrame mainFrame = new JInternalFrame(
83 protected IProgressIndicator progressIdicator;
85 protected JComboBox<FTSDataColumnI> cmb_searchTarget = new JComboBox<FTSDataColumnI>();
87 protected JButton btn_ok = new JButton();
89 protected JButton btn_back = new JButton();
91 protected JButton btn_cancel = new JButton();
93 protected JTextField txt_search = new JTextField(20);
95 protected SequenceFetcher seqFetcher;
97 protected Collection<FTSDataColumnI> wantedFields;
99 private String lastSearchTerm = "";
101 private JTable tbl_summary = new JTable()
104 public String getToolTipText(MouseEvent evt)
106 String toolTipText = null;
107 java.awt.Point pnt = evt.getPoint();
108 int rowIndex = rowAtPoint(pnt);
109 int colIndex = columnAtPoint(pnt);
113 if (getValueAt(rowIndex, colIndex) == null)
117 toolTipText = getValueAt(rowIndex, colIndex).toString();
119 } catch (Exception e)
123 toolTipText = (toolTipText == null ? null
124 : (toolTipText.length() > 500 ? JvSwingUtils.wrapTooltip(
125 true, toolTipText.subSequence(0, 500) + "...")
126 : JvSwingUtils.wrapTooltip(true, toolTipText)));
132 protected StringBuilder errorWarning = new StringBuilder();
134 protected JScrollPane scrl_searchResult = new JScrollPane(tbl_summary);
136 protected ImageIcon warningImage = new ImageIcon(getClass().getResource(
137 "/images/warning.gif"));
139 protected ImageIcon loadingImage = new ImageIcon(getClass().getResource(
140 "/images/loading.gif"));
142 protected JLabel lbl_warning = new JLabel(warningImage);
144 protected JLabel lbl_loading = new JLabel(loadingImage);
146 private JTabbedPane tabbedPane = new JTabbedPane();
148 private JPanel pnl_actions = new JPanel();
150 private JPanel pnl_results = new JPanel(new CardLayout());
152 private JPanel pnl_inputs = new JPanel();
154 private BorderLayout mainLayout = new BorderLayout();
156 protected Object[] previousWantedFields;
163 mainFrame.invalidate();
165 } catch (Exception e)
172 * Initializes the GUI default properties
176 private void jbInit() throws Exception
178 lbl_warning.setVisible(false);
179 lbl_warning.setFont(new java.awt.Font("Verdana", 0, 12));
180 lbl_loading.setVisible(false);
181 lbl_loading.setFont(new java.awt.Font("Verdana", 0, 12));
183 tbl_summary.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
184 tbl_summary.setAutoCreateRowSorter(true);
185 tbl_summary.getTableHeader().setReorderingAllowed(false);
186 tbl_summary.addMouseListener(new MouseAdapter()
189 public void mouseClicked(MouseEvent e)
195 public void mouseReleased(MouseEvent e)
200 tbl_summary.addKeyListener(new KeyAdapter()
203 public void keyPressed(KeyEvent evt)
206 switch (evt.getKeyCode())
208 case KeyEvent.VK_ESCAPE: // escape key
209 btn_back_ActionPerformed();
211 case KeyEvent.VK_ENTER: // enter key
212 if (btn_ok.isEnabled())
218 case KeyEvent.VK_TAB: // tab key
219 if (evt.isShiftDown())
221 tabbedPane.requestFocus();
225 btn_back.requestFocus();
235 btn_back.setFont(new java.awt.Font("Verdana", 0, 12));
236 btn_back.setText(MessageManager.getString("action.back"));
237 btn_back.addActionListener(new java.awt.event.ActionListener()
240 public void actionPerformed(ActionEvent e)
242 btn_back_ActionPerformed();
245 btn_back.addKeyListener(new KeyAdapter()
248 public void keyPressed(KeyEvent evt)
250 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
252 btn_back_ActionPerformed();
257 btn_ok.setEnabled(false);
258 btn_ok.setFont(new java.awt.Font("Verdana", 0, 12));
259 btn_ok.setText(MessageManager.getString("action.ok"));
260 btn_ok.addActionListener(new java.awt.event.ActionListener()
263 public void actionPerformed(ActionEvent e)
268 btn_ok.addKeyListener(new KeyAdapter()
271 public void keyPressed(KeyEvent evt)
273 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
280 btn_cancel.setFont(new java.awt.Font("Verdana", 0, 12));
281 btn_cancel.setText(MessageManager.getString("action.cancel"));
282 btn_cancel.addActionListener(new java.awt.event.ActionListener()
285 public void actionPerformed(ActionEvent e)
287 btn_cancel_ActionPerformed();
290 btn_cancel.addKeyListener(new KeyAdapter()
293 public void keyPressed(KeyEvent evt)
295 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
297 btn_cancel_ActionPerformed();
302 scrl_searchResult.setPreferredSize(new Dimension(500, 300));
304 .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
306 cmb_searchTarget.setFont(new java.awt.Font("Verdana", 0, 12));
307 cmb_searchTarget.addActionListener(new ActionListener()
310 public void actionPerformed(ActionEvent e)
313 if ("all".equalsIgnoreCase(getCmbSearchTarget().getSelectedItem()
316 tooltipText = MessageManager.getString("label.search_all");
318 else if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
319 .getSelectedItem().toString()))
321 tooltipText = MessageManager
322 .getString("label.separate_multiple_accession_ids");
326 tooltipText = MessageManager.formatMessage(
327 "label.separate_multiple_query_values",
328 new Object[] { getCmbSearchTarget().getSelectedItem()
331 txt_search.setToolTipText(JvSwingUtils.wrapTooltip(true,
337 populateCmbSearchTargetOptions();
339 txt_search.setFont(new java.awt.Font("Verdana", 0, 12));
341 txt_search.addKeyListener(new KeyAdapter()
344 public void keyPressed(KeyEvent e)
346 if (e.getKeyCode() == KeyEvent.VK_ENTER)
348 if (txt_search.getText() == null
349 || txt_search.getText().isEmpty())
353 if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
354 .getSelectedItem().toString()))
356 transferToSequenceFetcher(txt_search.getText());
362 final DeferredTextInputListener listener = new DeferredTextInputListener(
367 public void actionPerformed(ActionEvent e)
369 if (!getTypedText().equalsIgnoreCase(lastSearchTerm))
372 lastSearchTerm = getTypedText();
376 txt_search.getDocument().addDocumentListener(listener);
377 txt_search.addFocusListener(new FocusListener()
380 public void focusGained(FocusEvent e)
386 public void focusLost(FocusEvent e)
392 final String searchTabTitle = MessageManager
393 .getString("label.search_result");
394 final String configureCols = MessageManager
395 .getString("label.configure_displayed_columns");
396 ChangeListener changeListener = new ChangeListener()
399 public void stateChanged(ChangeEvent changeEvent)
401 JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
403 int index = sourceTabbedPane.getSelectedIndex();
405 btn_back.setVisible(true);
406 btn_cancel.setVisible(true);
407 btn_ok.setVisible(true);
408 if (sourceTabbedPane.getTitleAt(index).equals(configureCols))
410 btn_back.setVisible(false);
411 btn_cancel.setVisible(false);
412 btn_ok.setVisible(false);
413 btn_back.setEnabled(false);
414 btn_cancel.setEnabled(false);
415 btn_ok.setEnabled(false);
416 previousWantedFields = getFTSRestClient()
417 .getAllDefaulDisplayedDataColumns()
418 .toArray(new Object[0]);
420 if (sourceTabbedPane.getTitleAt(index).equals(searchTabTitle))
422 btn_back.setEnabled(true);
423 btn_cancel.setEnabled(true);
424 if (wantedFieldsUpdated())
435 tabbedPane.addChangeListener(changeListener);
436 tabbedPane.setPreferredSize(new Dimension(500, 300));
437 tabbedPane.add(searchTabTitle, scrl_searchResult);
438 tabbedPane.add(configureCols, new FTSDataColumnPreferences(
439 PreferenceSource.SEARCH_SUMMARY, getFTSRestClient()));
441 pnl_actions.add(btn_back);
442 pnl_actions.add(btn_ok);
443 pnl_actions.add(btn_cancel);
445 pnl_results.add(tabbedPane);
446 pnl_inputs.add(cmb_searchTarget);
447 pnl_inputs.add(txt_search);
448 pnl_inputs.add(lbl_loading);
449 pnl_inputs.add(lbl_warning);
451 this.setLayout(mainLayout);
452 this.add(pnl_inputs, java.awt.BorderLayout.NORTH);
453 this.add(pnl_results, java.awt.BorderLayout.CENTER);
454 this.add(pnl_actions, java.awt.BorderLayout.SOUTH);
455 mainFrame.setVisible(true);
456 mainFrame.setContentPane(this);
457 mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
458 Desktop.addInternalFrame(mainFrame, getFTSFrameTitle(), 800, 400);
461 public class DeferredTextInputListener implements DocumentListener
463 private final Timer swingTimer;
465 public DeferredTextInputListener(int timeOut, ActionListener listener,
468 swingTimer = new Timer(timeOut, listener);
469 swingTimer.setRepeats(repeats);
483 public void insertUpdate(DocumentEvent e)
485 swingTimer.restart();
489 public void removeUpdate(DocumentEvent e)
491 swingTimer.restart();
495 public void changedUpdate(DocumentEvent e)
497 swingTimer.restart();
502 public boolean wantedFieldsUpdated()
504 if (previousWantedFields == null)
509 return Arrays.equals(getFTSRestClient()
510 .getAllDefaulDisplayedDataColumns()
511 .toArray(new Object[0]), previousWantedFields) ? false
516 public void validateSelection()
518 if (tbl_summary.getSelectedRows().length > 0)
520 btn_ok.setEnabled(true);
524 btn_ok.setEnabled(false);
528 public JComboBox<FTSDataColumnI> getCmbSearchTarget()
530 return cmb_searchTarget;
533 public JTextField getTxtSearch()
538 public JInternalFrame getMainFrame()
543 protected void delayAndEnableActionButtons()
553 } catch (InterruptedException e)
557 btn_ok.setEnabled(true);
558 btn_back.setEnabled(true);
559 btn_cancel.setEnabled(true);
564 protected void checkForErrors()
566 lbl_warning.setVisible(false);
567 if (errorWarning.length() > 0)
569 lbl_loading.setVisible(false);
570 lbl_warning.setToolTipText(JvSwingUtils.wrapTooltip(true,
571 errorWarning.toString()));
572 lbl_warning.setVisible(true);
576 protected void btn_back_ActionPerformed()
579 new SequenceFetcher(progressIdicator);
582 protected void disableActionButtons()
584 btn_ok.setEnabled(false);
585 btn_back.setEnabled(false);
586 btn_cancel.setEnabled(false);
589 protected void btn_cancel_ActionPerformed()
595 * Populates search target combo-box options
597 public void populateCmbSearchTargetOptions()
599 List<FTSDataColumnI> searchableTargets = new ArrayList<FTSDataColumnI>();
602 Collection<FTSDataColumnI> foundFTSTargets = getFTSRestClient()
603 .getSearchableDataColumns();
604 searchableTargets.addAll(foundFTSTargets);
605 } catch (Exception e)
610 Collections.sort(searchableTargets, new Comparator<FTSDataColumnI>()
613 public int compare(FTSDataColumnI o1, FTSDataColumnI o2)
615 return o1.getName().compareTo(o2.getName());
619 for (FTSDataColumnI searchTarget : searchableTargets)
621 cmb_searchTarget.addItem(searchTarget);
626 public void transferToSequenceFetcher(String ids)
628 // mainFrame.dispose();
629 seqFetcher.getTextArea().setText(ids);
630 Thread worker = new Thread(seqFetcher);
635 public String getTypedText()
637 return txt_search.getText().trim();
641 public JTable getResultTable()
648 lbl_loading.setVisible(false);
649 errorWarning.setLength(0);
650 lbl_warning.setVisible(false);
651 btn_ok.setEnabled(false);
652 mainFrame.setTitle(getFTSFrameTitle());
654 tbl_summary.setModel(new DefaultTableModel());
655 tbl_summary.setVisible(false);
659 public void setErrorMessage(String message)
661 errorWarning.append(message);
665 public void updateSearchFrameTitle(String title)
667 mainFrame.setTitle(title);
671 public void setSearchInProgress(Boolean isSearchInProgress)
673 lbl_loading.setVisible(isSearchInProgress);
675 public void referesh()
677 mainFrame.setTitle(getFTSFrameTitle());