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;
68 import javax.swing.table.TableColumn;
71 * This class provides the swing GUI layout for FTS Panel and implements most of
72 * the contracts defined in GFSPanelI
78 @SuppressWarnings("serial")
79 public abstract class GFTSPanel extends JPanel implements GFTSPanelI
81 protected JInternalFrame mainFrame = new JInternalFrame(
84 protected IProgressIndicator progressIdicator;
86 protected JComboBox<FTSDataColumnI> cmb_searchTarget = new JComboBox<FTSDataColumnI>();
88 protected JButton btn_ok = new JButton();
90 protected JButton btn_back = new JButton();
92 protected JButton btn_cancel = new JButton();
94 protected JTextField txt_search = new JTextField(30);
96 protected SequenceFetcher seqFetcher;
98 protected Collection<FTSDataColumnI> wantedFields;
100 private String lastSearchTerm = "";
102 protected JButton btn_next_page = new JButton();
104 protected JButton btn_prev_page = new JButton();
106 private JTable tbl_summary = new JTable()
108 private boolean inLayout;
111 public boolean getScrollableTracksViewportWidth()
113 return hasExcessWidth();
118 public void doLayout()
120 if (hasExcessWidth())
122 autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS;
127 autoResizeMode = AUTO_RESIZE_OFF;
130 protected boolean hasExcessWidth()
132 return getPreferredSize().width < getParent().getWidth();
136 public void columnMarginChanged(ChangeEvent e)
142 TableColumn resizingColumn = getTableHeader().getResizingColumn();
143 // Need to do this here, before the parent's
144 // layout manager calls getPreferredSize().
145 if (resizingColumn != null && autoResizeMode == AUTO_RESIZE_OFF
148 resizingColumn.setPreferredWidth(resizingColumn.getWidth());
154 public String getToolTipText(MouseEvent evt)
156 String toolTipText = null;
157 java.awt.Point pnt = evt.getPoint();
158 int rowIndex = rowAtPoint(pnt);
159 int colIndex = columnAtPoint(pnt);
163 if (getValueAt(rowIndex, colIndex) == null)
167 toolTipText = getValueAt(rowIndex, colIndex).toString();
169 } catch (Exception e)
173 toolTipText = (toolTipText == null ? null
174 : (toolTipText.length() > 500 ? JvSwingUtils.wrapTooltip(
175 true, toolTipText.subSequence(0, 500) + "...")
176 : JvSwingUtils.wrapTooltip(true, toolTipText)));
182 protected StringBuilder errorWarning = new StringBuilder();
184 protected JScrollPane scrl_searchResult = new JScrollPane(tbl_summary);
186 protected ImageIcon warningImage = new ImageIcon(getClass().getResource(
187 "/images/warning.gif"));
189 protected ImageIcon loadingImage = new ImageIcon(getClass().getResource(
190 "/images/loading.gif"));
192 protected ImageIcon balnkPlaceholderImage = new ImageIcon(getClass()
193 .getResource("/images/blank_16x16_placeholder.png"));
195 protected JLabel lbl_warning = new JLabel(warningImage);
197 protected JLabel lbl_loading = new JLabel(loadingImage);
199 protected JLabel lbl_blank = new JLabel(balnkPlaceholderImage);
201 private JTabbedPane tabbedPane = new JTabbedPane();
203 private JPanel pnl_actions = new JPanel();
205 private JPanel pnl_results = new JPanel(new CardLayout());
207 private JPanel pnl_inputs = new JPanel();
209 private BorderLayout mainLayout = new BorderLayout();
211 protected Object[] previousWantedFields;
218 mainFrame.invalidate();
220 } catch (Exception e)
227 * Initializes the GUI default properties
231 private void jbInit() throws Exception
233 lbl_warning.setVisible(false);
234 lbl_warning.setFont(new java.awt.Font("Verdana", 0, 12));
235 lbl_loading.setVisible(false);
236 lbl_loading.setFont(new java.awt.Font("Verdana", 0, 12));
237 lbl_blank.setVisible(true);
238 lbl_blank.setFont(new java.awt.Font("Verdana", 0, 12));
240 tbl_summary.setAutoCreateRowSorter(true);
241 tbl_summary.getTableHeader().setReorderingAllowed(false);
242 tbl_summary.addMouseListener(new MouseAdapter()
245 public void mouseClicked(MouseEvent e)
251 public void mouseReleased(MouseEvent e)
256 tbl_summary.addKeyListener(new KeyAdapter()
259 public void keyPressed(KeyEvent evt)
262 switch (evt.getKeyCode())
264 case KeyEvent.VK_ESCAPE: // escape key
265 btn_back_ActionPerformed();
267 case KeyEvent.VK_ENTER: // enter key
268 if (btn_ok.isEnabled())
274 case KeyEvent.VK_TAB: // tab key
275 if (evt.isShiftDown())
277 tabbedPane.requestFocus();
281 btn_back.requestFocus();
291 btn_back.setFont(new java.awt.Font("Verdana", 0, 12));
292 btn_back.setText(MessageManager.getString("action.back"));
293 btn_back.addActionListener(new java.awt.event.ActionListener()
296 public void actionPerformed(ActionEvent e)
298 btn_back_ActionPerformed();
301 btn_back.addKeyListener(new KeyAdapter()
304 public void keyPressed(KeyEvent evt)
306 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
308 btn_back_ActionPerformed();
313 btn_ok.setEnabled(false);
314 btn_ok.setFont(new java.awt.Font("Verdana", 0, 12));
315 btn_ok.setText(MessageManager.getString("action.ok"));
316 btn_ok.addActionListener(new java.awt.event.ActionListener()
319 public void actionPerformed(ActionEvent e)
324 btn_ok.addKeyListener(new KeyAdapter()
327 public void keyPressed(KeyEvent evt)
329 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
336 btn_next_page.setEnabled(false);
337 btn_next_page.setToolTipText(MessageManager
338 .getString("label.next_page_tooltop"));
339 btn_next_page.setFont(new java.awt.Font("Verdana", 0, 12));
340 btn_next_page.setText(MessageManager.getString("action.next_page"));
341 btn_next_page.addActionListener(new java.awt.event.ActionListener()
344 public void actionPerformed(ActionEvent e)
349 btn_next_page.addKeyListener(new KeyAdapter()
352 public void keyPressed(KeyEvent evt)
354 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
361 btn_prev_page.setEnabled(false);
362 btn_prev_page.setToolTipText(MessageManager
363 .getString("label.prev_page_tooltop"));
364 btn_prev_page.setFont(new java.awt.Font("Verdana", 0, 12));
365 btn_prev_page.setText(MessageManager.getString("action.prev_page"));
366 btn_prev_page.addActionListener(new java.awt.event.ActionListener()
369 public void actionPerformed(ActionEvent e)
374 btn_prev_page.addKeyListener(new KeyAdapter()
377 public void keyPressed(KeyEvent evt)
379 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
386 if (isPaginationEnabled())
388 btn_prev_page.setVisible(true);
389 btn_next_page.setVisible(true);
393 btn_prev_page.setVisible(false);
394 btn_next_page.setVisible(false);
397 btn_cancel.setFont(new java.awt.Font("Verdana", 0, 12));
398 btn_cancel.setText(MessageManager.getString("action.cancel"));
399 btn_cancel.addActionListener(new java.awt.event.ActionListener()
402 public void actionPerformed(ActionEvent e)
404 btn_cancel_ActionPerformed();
407 btn_cancel.addKeyListener(new KeyAdapter()
410 public void keyPressed(KeyEvent evt)
412 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
414 btn_cancel_ActionPerformed();
418 scrl_searchResult.setPreferredSize(new Dimension(800, 400));
420 cmb_searchTarget.setFont(new java.awt.Font("Verdana", 0, 12));
421 cmb_searchTarget.addActionListener(new ActionListener()
424 public void actionPerformed(ActionEvent e)
427 if ("all".equalsIgnoreCase(getCmbSearchTarget().getSelectedItem()
430 tooltipText = MessageManager.getString("label.search_all");
432 else if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
433 .getSelectedItem().toString()))
435 tooltipText = MessageManager
436 .getString("label.separate_multiple_accession_ids");
440 tooltipText = MessageManager.formatMessage(
441 "label.separate_multiple_query_values",
442 new Object[] { getCmbSearchTarget().getSelectedItem()
445 txt_search.setToolTipText(JvSwingUtils.wrapTooltip(true,
451 populateCmbSearchTargetOptions();
453 txt_search.setFont(new java.awt.Font("Verdana", 0, 12));
455 txt_search.addKeyListener(new KeyAdapter()
458 public void keyPressed(KeyEvent e)
460 if (e.getKeyCode() == KeyEvent.VK_ENTER)
462 if (txt_search.getText() == null
463 || txt_search.getText().isEmpty())
467 String primaryKeyName = getFTSRestClient().getPrimaryKeyColumn()
469 if (primaryKeyName.equalsIgnoreCase(getCmbSearchTarget()
470 .getSelectedItem().toString()))
472 transferToSequenceFetcher(txt_search.getText());
478 final DeferredTextInputListener listener = new DeferredTextInputListener(
483 public void actionPerformed(ActionEvent e)
485 if (!getTypedText().equalsIgnoreCase(lastSearchTerm))
488 lastSearchTerm = getTypedText();
492 txt_search.getDocument().addDocumentListener(listener);
493 txt_search.addFocusListener(new FocusListener()
496 public void focusGained(FocusEvent e)
502 public void focusLost(FocusEvent e)
508 final String searchTabTitle = MessageManager
509 .getString("label.search_result");
510 final String configureCols = MessageManager
511 .getString("label.configure_displayed_columns");
512 ChangeListener changeListener = new ChangeListener()
515 public void stateChanged(ChangeEvent changeEvent)
517 JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
519 int index = sourceTabbedPane.getSelectedIndex();
521 btn_back.setVisible(true);
522 btn_cancel.setVisible(true);
523 btn_ok.setVisible(true);
524 if (sourceTabbedPane.getTitleAt(index).equals(configureCols))
526 btn_back.setVisible(false);
527 btn_cancel.setVisible(false);
528 btn_ok.setVisible(false);
529 btn_back.setEnabled(false);
530 btn_cancel.setEnabled(false);
531 btn_ok.setEnabled(false);
532 previousWantedFields = getFTSRestClient()
533 .getAllDefaulDisplayedDataColumns()
534 .toArray(new Object[0]);
536 if (sourceTabbedPane.getTitleAt(index).equals(searchTabTitle))
538 btn_back.setEnabled(true);
539 btn_cancel.setEnabled(true);
540 if (wantedFieldsUpdated())
551 tabbedPane.addChangeListener(changeListener);
552 tabbedPane.setPreferredSize(new Dimension(800, 400));
553 tabbedPane.add(searchTabTitle, scrl_searchResult);
554 tabbedPane.add(configureCols, new FTSDataColumnPreferences(
555 PreferenceSource.SEARCH_SUMMARY, getFTSRestClient()));
557 pnl_actions.add(btn_back);
558 pnl_actions.add(btn_ok);
559 pnl_actions.add(btn_cancel);
561 pnl_results.add(tabbedPane);
562 pnl_inputs.add(cmb_searchTarget);
563 pnl_inputs.add(txt_search);
564 pnl_inputs.add(lbl_loading);
565 pnl_inputs.add(lbl_warning);
566 pnl_inputs.add(lbl_blank);
567 pnl_inputs.add(btn_prev_page);
568 pnl_inputs.add(btn_next_page);
570 this.setLayout(mainLayout);
571 this.add(pnl_inputs, java.awt.BorderLayout.NORTH);
572 this.add(pnl_results, java.awt.BorderLayout.CENTER);
573 this.add(pnl_actions, java.awt.BorderLayout.SOUTH);
574 mainFrame.setVisible(true);
575 mainFrame.setContentPane(this);
576 mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
577 Desktop.addInternalFrame(mainFrame, getFTSFrameTitle(), 900, 500);
580 public class DeferredTextInputListener implements DocumentListener
582 private final Timer swingTimer;
584 public DeferredTextInputListener(int timeOut, ActionListener listener,
587 swingTimer = new Timer(timeOut, listener);
588 swingTimer.setRepeats(repeats);
602 public void insertUpdate(DocumentEvent e)
604 swingTimer.restart();
608 public void removeUpdate(DocumentEvent e)
610 swingTimer.restart();
614 public void changedUpdate(DocumentEvent e)
616 swingTimer.restart();
621 public boolean wantedFieldsUpdated()
623 if (previousWantedFields == null)
628 return Arrays.equals(getFTSRestClient()
629 .getAllDefaulDisplayedDataColumns()
630 .toArray(new Object[0]), previousWantedFields) ? false
635 public void validateSelection()
637 if (tbl_summary.getSelectedRows().length > 0)
639 btn_ok.setEnabled(true);
643 btn_ok.setEnabled(false);
647 public JComboBox<FTSDataColumnI> getCmbSearchTarget()
649 return cmb_searchTarget;
652 public JTextField getTxtSearch()
657 public JInternalFrame getMainFrame()
662 protected void delayAndEnableActionButtons()
672 } catch (InterruptedException e)
676 btn_ok.setEnabled(true);
677 btn_back.setEnabled(true);
678 btn_cancel.setEnabled(true);
683 protected void checkForErrors()
685 lbl_warning.setVisible(false);
686 lbl_blank.setVisible(true);
687 if (errorWarning.length() > 0)
689 lbl_loading.setVisible(false);
690 lbl_blank.setVisible(false);
691 lbl_warning.setToolTipText(JvSwingUtils.wrapTooltip(true,
692 errorWarning.toString()));
693 lbl_warning.setVisible(true);
697 protected void btn_back_ActionPerformed()
700 new SequenceFetcher(progressIdicator);
703 protected void disableActionButtons()
705 btn_ok.setEnabled(false);
706 btn_back.setEnabled(false);
707 btn_cancel.setEnabled(false);
710 protected void btn_cancel_ActionPerformed()
716 * Populates search target combo-box options
718 public void populateCmbSearchTargetOptions()
720 List<FTSDataColumnI> searchableTargets = new ArrayList<FTSDataColumnI>();
723 Collection<FTSDataColumnI> foundFTSTargets = getFTSRestClient()
724 .getSearchableDataColumns();
725 searchableTargets.addAll(foundFTSTargets);
726 } catch (Exception e)
731 Collections.sort(searchableTargets, new Comparator<FTSDataColumnI>()
734 public int compare(FTSDataColumnI o1, FTSDataColumnI o2)
736 return o1.getName().compareTo(o2.getName());
740 for (FTSDataColumnI searchTarget : searchableTargets)
742 cmb_searchTarget.addItem(searchTarget);
747 public void transferToSequenceFetcher(String ids)
749 // mainFrame.dispose();
750 seqFetcher.getTextArea().setText(ids);
751 Thread worker = new Thread(seqFetcher);
756 public String getTypedText()
758 return txt_search.getText().trim();
762 public JTable getResultTable()
769 lbl_loading.setVisible(false);
770 errorWarning.setLength(0);
771 lbl_warning.setVisible(false);
772 lbl_blank.setVisible(true);
773 btn_ok.setEnabled(false);
774 mainFrame.setTitle(getFTSFrameTitle());
776 tbl_summary.setModel(new DefaultTableModel());
777 tbl_summary.setVisible(false);
781 public void setPrevPageButtonEnabled(boolean isEnabled)
783 btn_prev_page.setEnabled(isEnabled);
787 public void setNextPageButtonEnabled(boolean isEnabled)
789 btn_next_page.setEnabled(isEnabled);
793 public void setErrorMessage(String message)
795 errorWarning.append(message);
799 public void updateSearchFrameTitle(String title)
801 mainFrame.setTitle(title);
805 public void setSearchInProgress(Boolean isSearchInProgress)
807 lbl_blank.setVisible(!isSearchInProgress);
808 lbl_loading.setVisible(isSearchInProgress);
810 public void referesh()
812 mainFrame.setTitle(getFTSFrameTitle());