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.io.cache.AppCache;
32 import jalview.io.cache.Cacheable;
33 import jalview.util.MessageManager;
35 import java.awt.BorderLayout;
36 import java.awt.CardLayout;
37 import java.awt.Dimension;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.FocusAdapter;
41 import java.awt.event.FocusEvent;
42 import java.awt.event.FocusListener;
43 import java.awt.event.ItemEvent;
44 import java.awt.event.ItemListener;
45 import java.awt.event.KeyAdapter;
46 import java.awt.event.KeyEvent;
47 import java.awt.event.MouseAdapter;
48 import java.awt.event.MouseEvent;
49 import java.text.DecimalFormat;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.Collection;
53 import java.util.Collections;
54 import java.util.Comparator;
55 import java.util.HashSet;
56 import java.util.List;
58 import javax.swing.ImageIcon;
59 import javax.swing.JButton;
60 import javax.swing.JComboBox;
61 import javax.swing.JComponent;
62 import javax.swing.JFrame;
63 import javax.swing.JInternalFrame;
64 import javax.swing.JLabel;
65 import javax.swing.JPanel;
66 import javax.swing.JScrollPane;
67 import javax.swing.JTabbedPane;
68 import javax.swing.JTable;
69 import javax.swing.Timer;
70 import javax.swing.event.ChangeEvent;
71 import javax.swing.event.ChangeListener;
72 import javax.swing.event.DocumentEvent;
73 import javax.swing.event.DocumentListener;
74 import javax.swing.event.InternalFrameEvent;
75 import javax.swing.table.DefaultTableModel;
76 import javax.swing.table.TableColumn;
77 import javax.swing.text.JTextComponent;
80 * This class provides the swing GUI layout for FTS Panel and implements most of
81 * the contracts defined in GFSPanelI
87 @SuppressWarnings("serial")
88 public abstract class GFTSPanel extends JPanel implements GFTSPanelI, Cacheable
90 protected JInternalFrame mainFrame = new JInternalFrame(
93 protected IProgressIndicator progressIndicator;
95 protected JComboBox<FTSDataColumnI> cmb_searchTarget = new JComboBox<FTSDataColumnI>();
97 protected JButton btn_ok = new JButton();
99 protected JButton btn_back = new JButton();
101 protected JButton btn_cancel = new JButton();
103 protected JComboBox<String> txt_search;
105 protected SequenceFetcher seqFetcher;
107 protected Collection<FTSDataColumnI> wantedFields;
109 private String lastSearchTerm = "";
111 protected JButton btn_next_page = new JButton();
113 protected JButton btn_prev_page = new JButton();
115 protected StringBuilder errorWarning = new StringBuilder();
117 protected ImageIcon warningImage = new ImageIcon(getClass().getResource(
118 "/images/warning.gif"));
120 protected ImageIcon loadingImage = new ImageIcon(getClass().getResource(
121 "/images/loading.gif"));
123 protected ImageIcon balnkPlaceholderImage = new ImageIcon(getClass()
124 .getResource("/images/blank_16x16_placeholder.png"));
126 protected JLabel lbl_warning = new JLabel(warningImage);
128 protected JLabel lbl_loading = new JLabel(loadingImage);
130 protected JLabel lbl_blank = new JLabel(balnkPlaceholderImage);
132 private JTabbedPane tabbedPane = new JTabbedPane();
134 private JPanel pnl_actions = new JPanel();
136 private JPanel pnl_results = new JPanel(new CardLayout());
138 private JPanel pnl_inputs = new JPanel();
140 private BorderLayout mainLayout = new BorderLayout();
142 protected Object[] previousWantedFields;
144 protected int resultSetCount;
146 protected int totalResultSetCount;
148 protected int offSet;
150 protected int pageLimit;
152 protected HashSet<String> paginatorCart = new HashSet<String>();
154 protected static final DecimalFormat totalNumberformatter = new DecimalFormat(
157 private JTable tbl_summary = new JTable()
159 private boolean inLayout;
162 public boolean getScrollableTracksViewportWidth()
164 return hasExcessWidth();
169 public void doLayout()
171 if (hasExcessWidth())
173 autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS;
178 autoResizeMode = AUTO_RESIZE_OFF;
181 protected boolean hasExcessWidth()
183 return getPreferredSize().width < getParent().getWidth();
187 public void columnMarginChanged(ChangeEvent e)
193 TableColumn resizingColumn = getTableHeader().getResizingColumn();
194 // Need to do this here, before the parent's
195 // layout manager calls getPreferredSize().
196 if (resizingColumn != null && autoResizeMode == AUTO_RESIZE_OFF
199 resizingColumn.setPreferredWidth(resizingColumn.getWidth());
200 String colHeader = resizingColumn.getHeaderValue().toString();
201 getTempUserPrefs().put(colHeader, resizingColumn.getWidth());
207 public String getToolTipText(MouseEvent evt)
209 String toolTipText = null;
210 java.awt.Point pnt = evt.getPoint();
211 int rowIndex = rowAtPoint(pnt);
212 int colIndex = columnAtPoint(pnt);
216 if (getValueAt(rowIndex, colIndex) == null)
220 toolTipText = getValueAt(rowIndex, colIndex).toString();
222 } catch (Exception e)
226 toolTipText = (toolTipText == null ? null
227 : (toolTipText.length() > 500 ? JvSwingUtils.wrapTooltip(
228 true, toolTipText.subSequence(0, 500) + "...")
229 : JvSwingUtils.wrapTooltip(true, toolTipText)));
235 protected JScrollPane scrl_searchResult = new JScrollPane(tbl_summary);
242 mainFrame.addFocusListener(new FocusAdapter()
245 public void focusGained(FocusEvent e)
247 txt_search.requestFocusInWindow();
250 mainFrame.invalidate();
252 } catch (Exception e)
259 * Initializes the GUI default properties
263 private void jbInit() throws Exception
266 txt_search = new JComboBox<String>();
267 txt_search.setEditable(true);
269 .setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
270 populateCmbSearchTargetOptions();
271 Integer width = getTempUserPrefs().get("FTSPanel.width") == null ? 800
272 : getTempUserPrefs().get("FTSPanel.width");
273 Integer height = getTempUserPrefs().get("FTSPanel.height") == null ? 400
274 : getTempUserPrefs().get("FTSPanel.height");
275 lbl_warning.setVisible(false);
276 lbl_warning.setFont(new java.awt.Font("Verdana", 0, 12));
277 lbl_loading.setVisible(false);
278 lbl_loading.setFont(new java.awt.Font("Verdana", 0, 12));
279 lbl_blank.setVisible(true);
280 lbl_blank.setFont(new java.awt.Font("Verdana", 0, 12));
282 tbl_summary.setAutoCreateRowSorter(true);
283 tbl_summary.getTableHeader().setReorderingAllowed(false);
284 tbl_summary.addMouseListener(new MouseAdapter()
287 public void mouseClicked(MouseEvent e)
293 public void mouseReleased(MouseEvent e)
298 tbl_summary.addKeyListener(new KeyAdapter()
301 public void keyPressed(KeyEvent evt)
304 switch (evt.getKeyCode())
306 case KeyEvent.VK_ESCAPE: // escape key
307 btn_back_ActionPerformed();
309 case KeyEvent.VK_ENTER: // enter key
310 if (btn_ok.isEnabled())
316 case KeyEvent.VK_TAB: // tab key
317 if (evt.isShiftDown())
319 tabbedPane.requestFocus();
323 btn_back.requestFocus();
333 btn_back.setFont(new java.awt.Font("Verdana", 0, 12));
334 btn_back.setText(MessageManager.getString("action.back"));
335 btn_back.addActionListener(new java.awt.event.ActionListener()
338 public void actionPerformed(ActionEvent e)
340 btn_back_ActionPerformed();
343 btn_back.addKeyListener(new KeyAdapter()
346 public void keyPressed(KeyEvent evt)
348 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
350 btn_back_ActionPerformed();
355 btn_ok.setEnabled(false);
356 btn_ok.setFont(new java.awt.Font("Verdana", 0, 12));
357 btn_ok.setText(MessageManager.getString("action.ok"));
358 btn_ok.addActionListener(new java.awt.event.ActionListener()
361 public void actionPerformed(ActionEvent e)
366 btn_ok.addKeyListener(new KeyAdapter()
369 public void keyPressed(KeyEvent evt)
371 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
377 btn_next_page.setEnabled(false);
378 btn_next_page.setToolTipText(MessageManager
379 .getString("label.next_page_tooltip"));
380 btn_next_page.setFont(new java.awt.Font("Verdana", 0, 12));
381 btn_next_page.setText(MessageManager.getString("action.next_page"));
382 btn_next_page.addActionListener(new java.awt.event.ActionListener()
385 public void actionPerformed(ActionEvent e)
390 btn_next_page.addKeyListener(new KeyAdapter()
393 public void keyPressed(KeyEvent evt)
395 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
402 btn_prev_page.setEnabled(false);
403 btn_prev_page.setToolTipText(MessageManager
404 .getString("label.prev_page_tooltip"));
405 btn_prev_page.setFont(new java.awt.Font("Verdana", 0, 12));
406 btn_prev_page.setText(MessageManager.getString("action.prev_page"));
407 btn_prev_page.addActionListener(new java.awt.event.ActionListener()
410 public void actionPerformed(ActionEvent e)
415 btn_prev_page.addKeyListener(new KeyAdapter()
418 public void keyPressed(KeyEvent evt)
420 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
427 if (isPaginationEnabled())
429 btn_prev_page.setVisible(true);
430 btn_next_page.setVisible(true);
434 btn_prev_page.setVisible(false);
435 btn_next_page.setVisible(false);
438 btn_cancel.setFont(new java.awt.Font("Verdana", 0, 12));
439 btn_cancel.setText(MessageManager.getString("action.cancel"));
440 btn_cancel.addActionListener(new java.awt.event.ActionListener()
443 public void actionPerformed(ActionEvent e)
445 btn_cancel_ActionPerformed();
448 btn_cancel.addKeyListener(new KeyAdapter()
451 public void keyPressed(KeyEvent evt)
453 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
455 btn_cancel_ActionPerformed();
459 scrl_searchResult.setPreferredSize(new Dimension(width, height));
461 cmb_searchTarget.setFont(new java.awt.Font("Verdana", 0, 12));
462 cmb_searchTarget.addItemListener(new ItemListener()
465 public void itemStateChanged(ItemEvent e)
467 if (e.getStateChange() == ItemEvent.SELECTED)
470 if ("all".equalsIgnoreCase(getCmbSearchTarget().getSelectedItem()
473 tooltipText = MessageManager.getString("label.search_all");
475 else if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
476 .getSelectedItem().toString()))
478 tooltipText = MessageManager
479 .getString("label.separate_multiple_accession_ids");
483 tooltipText = MessageManager.formatMessage(
484 "label.separate_multiple_query_values",
485 new Object[] { getCmbSearchTarget().getSelectedItem()
488 txt_search.setToolTipText(JvSwingUtils.wrapTooltip(true,
497 txt_search.setFont(new java.awt.Font("Verdana", 0, 12));
499 txt_search.getEditor().getEditorComponent()
500 .addKeyListener(new KeyAdapter()
503 public void keyPressed(KeyEvent e)
505 if (e.getKeyCode() == KeyEvent.VK_ENTER)
507 if (getTypedText() == null || getTypedText().isEmpty())
511 String primaryKeyName = getFTSRestClient().getPrimaryKeyColumn()
513 if (primaryKeyName.equalsIgnoreCase(getCmbSearchTarget()
514 .getSelectedItem().toString()))
516 transferToSequenceFetcher(getTypedText());
522 final DeferredTextInputListener listener = new DeferredTextInputListener(
523 1500, new ActionListener()
526 public void actionPerformed(ActionEvent e)
528 if (!getTypedText().equalsIgnoreCase(lastSearchTerm))
531 paginatorCart.clear();
532 lastSearchTerm = getTypedText();
536 ((JTextComponent) txt_search.getEditor().getEditorComponent())
537 .getDocument().addDocumentListener(listener);
539 txt_search.addFocusListener(new FocusListener()
542 public void focusGained(FocusEvent e)
548 public void focusLost(FocusEvent e)
554 final String searchTabTitle = MessageManager
555 .getString("label.search_result");
556 final String configureCols = MessageManager
557 .getString("label.configure_displayed_columns");
558 ChangeListener changeListener = new ChangeListener()
561 public void stateChanged(ChangeEvent changeEvent)
563 JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
565 int index = sourceTabbedPane.getSelectedIndex();
567 btn_back.setVisible(true);
568 btn_cancel.setVisible(true);
569 btn_ok.setVisible(true);
570 if (sourceTabbedPane.getTitleAt(index).equals(configureCols))
572 btn_back.setVisible(false);
573 btn_cancel.setVisible(false);
574 btn_ok.setVisible(false);
575 btn_back.setEnabled(false);
576 btn_cancel.setEnabled(false);
577 btn_ok.setEnabled(false);
578 btn_next_page.setEnabled(false);
579 btn_prev_page.setEnabled(false);
580 txt_search.setEnabled(false);
581 cmb_searchTarget.setEnabled(false);
582 previousWantedFields = getFTSRestClient()
583 .getAllDefaultDisplayedFTSDataColumns().toArray(
586 if (sourceTabbedPane.getTitleAt(index).equals(searchTabTitle))
588 btn_back.setEnabled(true);
589 btn_cancel.setEnabled(true);
590 refreshPaginatorState();
591 txt_search.setEnabled(true);
592 cmb_searchTarget.setEnabled(true);
593 if (wantedFieldsUpdated())
596 paginatorCart.clear();
605 tabbedPane.addChangeListener(changeListener);
606 tabbedPane.setPreferredSize(new Dimension(width, height));
607 tabbedPane.add(searchTabTitle, scrl_searchResult);
608 tabbedPane.add(configureCols, new FTSDataColumnPreferences(
609 PreferenceSource.SEARCH_SUMMARY, getFTSRestClient()));
611 pnl_actions.add(btn_back);
612 pnl_actions.add(btn_ok);
613 pnl_actions.add(btn_cancel);
615 pnl_results.add(tabbedPane);
616 pnl_inputs.add(cmb_searchTarget);
617 pnl_inputs.add(txt_search);
618 pnl_inputs.add(lbl_loading);
619 pnl_inputs.add(lbl_warning);
620 pnl_inputs.add(lbl_blank);
621 pnl_inputs.add(btn_prev_page);
622 pnl_inputs.add(btn_next_page);
624 this.setLayout(mainLayout);
625 this.add(pnl_inputs, java.awt.BorderLayout.NORTH);
626 this.add(pnl_results, java.awt.BorderLayout.CENTER);
627 this.add(pnl_actions, java.awt.BorderLayout.SOUTH);
628 mainFrame.setVisible(true);
629 mainFrame.setContentPane(this);
630 mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
632 .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
635 public void internalFrameClosing(InternalFrameEvent e)
640 mainFrame.setVisible(true);
641 mainFrame.setContentPane(this);
642 mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
643 Integer x = getTempUserPrefs().get("FTSPanel.x");
644 Integer y = getTempUserPrefs().get("FTSPanel.y");
645 if (x != null && y != null)
647 mainFrame.setLocation(x, y);
649 Desktop.addInternalFrame(mainFrame, getFTSFrameTitle(), width, height);
652 protected void closeAction()
654 getTempUserPrefs().put("FTSPanel.width", this.getWidth());
655 getTempUserPrefs().put("FTSPanel.height", pnl_results.getHeight());
656 getTempUserPrefs().put("FTSPanel.x", mainFrame.getX());
657 getTempUserPrefs().put("FTSPanel.y", mainFrame.getY());
661 public class DeferredTextInputListener implements DocumentListener
663 private final Timer swingTimer;
665 public DeferredTextInputListener(int timeOut, ActionListener listener,
668 swingTimer = new Timer(timeOut, listener);
669 swingTimer.setRepeats(repeats);
683 public void insertUpdate(DocumentEvent e)
685 swingTimer.restart();
689 public void removeUpdate(DocumentEvent e)
691 swingTimer.restart();
695 public void changedUpdate(DocumentEvent e)
697 swingTimer.restart();
702 public boolean wantedFieldsUpdated()
704 if (previousWantedFields == null)
709 return Arrays.equals(getFTSRestClient()
710 .getAllDefaultDisplayedFTSDataColumns().toArray(new Object[0]),
711 previousWantedFields) ? false : true;
715 public void validateSelection()
717 if (tbl_summary.getSelectedRows().length > 0
718 || !paginatorCart.isEmpty())
720 btn_ok.setEnabled(true);
724 btn_ok.setEnabled(false);
728 public JComboBox<FTSDataColumnI> getCmbSearchTarget()
730 return cmb_searchTarget;
733 public JComboBox<String> getTxtSearch()
738 public JInternalFrame getMainFrame()
743 protected void delayAndEnableActionButtons()
753 } catch (InterruptedException e)
757 btn_ok.setEnabled(true);
758 btn_back.setEnabled(true);
759 btn_cancel.setEnabled(true);
764 protected void checkForErrors()
766 lbl_warning.setVisible(false);
767 lbl_blank.setVisible(true);
768 if (errorWarning.length() > 0)
770 lbl_loading.setVisible(false);
771 lbl_blank.setVisible(false);
772 lbl_warning.setToolTipText(JvSwingUtils.wrapTooltip(true,
773 errorWarning.toString()));
774 lbl_warning.setVisible(true);
778 protected void btn_back_ActionPerformed()
781 new SequenceFetcher(progressIndicator);
784 protected void disableActionButtons()
786 btn_ok.setEnabled(false);
787 btn_back.setEnabled(false);
788 btn_cancel.setEnabled(false);
791 protected void btn_cancel_ActionPerformed()
797 * Populates search target combo-box options
799 public void populateCmbSearchTargetOptions()
801 List<FTSDataColumnI> searchableTargets = new ArrayList<FTSDataColumnI>();
804 Collection<FTSDataColumnI> foundFTSTargets = getFTSRestClient()
805 .getSearchableDataColumns();
806 searchableTargets.addAll(foundFTSTargets);
807 } catch (Exception e)
812 Collections.sort(searchableTargets, new Comparator<FTSDataColumnI>()
815 public int compare(FTSDataColumnI o1, FTSDataColumnI o2)
817 return o1.getName().compareTo(o2.getName());
821 for (FTSDataColumnI searchTarget : searchableTargets)
823 cmb_searchTarget.addItem(searchTarget);
827 public void transferToSequenceFetcher(String ids)
829 seqFetcher.getTextArea().setText(ids);
830 Thread worker = new Thread(seqFetcher);
835 public String getTypedText()
837 return txt_search.getEditor().getItem() == null ? "" : txt_search
838 .getEditor().getItem().toString().trim();
842 public JTable getResultTable()
849 lbl_loading.setVisible(false);
850 errorWarning.setLength(0);
851 lbl_warning.setVisible(false);
852 lbl_blank.setVisible(true);
853 btn_ok.setEnabled(false);
854 mainFrame.setTitle(getFTSFrameTitle());
856 tbl_summary.setModel(new DefaultTableModel());
857 tbl_summary.setVisible(false);
861 public void setPrevPageButtonEnabled(boolean isEnabled)
863 btn_prev_page.setEnabled(isEnabled);
867 public void setNextPageButtonEnabled(boolean isEnabled)
869 btn_next_page.setEnabled(isEnabled);
873 public void setErrorMessage(String message)
875 errorWarning.append(message);
879 public void updateSearchFrameTitle(String title)
881 mainFrame.setTitle(title);
885 public void setSearchInProgress(Boolean isSearchInProgress)
887 lbl_blank.setVisible(!isSearchInProgress);
888 lbl_loading.setVisible(isSearchInProgress);
892 public void prevPageAction()
894 updatePaginatorCart();
895 if (offSet >= pageLimit)
897 offSet = offSet - pageLimit;
902 refreshPaginatorState();
907 public void nextPageAction()
909 updatePaginatorCart();
910 offSet = offSet + pageLimit;
914 public void updatePaginatorCart()
916 int primaryKeyColIndex = 0;
917 JTable resultTable = getResultTable();
918 int totalRows = resultTable.getRowCount();
921 primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
922 wantedFields, false);
923 } catch (Exception e)
928 for (int row = 0; row < totalRows; row++)
930 String id = (String) resultTable.getValueAt(row, primaryKeyColIndex);
931 if (paginatorCart.contains(id))
933 paginatorCart.remove(id);
936 int[] selectedRows = resultTable.getSelectedRows();
937 for (int summaryRow : selectedRows)
939 String idStr = resultTable.getValueAt(summaryRow, primaryKeyColIndex)
941 paginatorCart.add(idStr);
945 public void updateSummaryTableSelections()
947 JTable resultTable = getResultTable();
948 if (paginatorCart.isEmpty())
952 int primaryKeyColIndex = 0;
955 primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
956 wantedFields, false);
957 } catch (Exception e)
961 int totalRows = resultTable.getRowCount();
962 for (int row = 0; row < totalRows; row++)
964 String id = (String) resultTable.getValueAt(row, primaryKeyColIndex);
965 if (paginatorCart.contains(id))
967 resultTable.addRowSelectionInterval(row, row);
973 public void refreshPaginatorState()
975 setPrevPageButtonEnabled(false);
976 setNextPageButtonEnabled(false);
977 if (resultSetCount == 0 && pageLimit == 0)
981 if (resultSetCount >= pageLimit)
983 setNextPageButtonEnabled(true);
985 if (offSet >= pageLimit)
987 setPrevPageButtonEnabled(true);
991 public void referesh()
993 mainFrame.setTitle(getFTSFrameTitle());
997 protected void fireCache()
999 AppCache.getInstance().updateCache(this);
1009 public JComponent getNextFocusableElement()
1015 public String getCacheKey()
1017 return getCacheKey();
1021 public JComboBox<String> getCacheComboBox()