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.FocusAdapter;
39 import java.awt.event.FocusEvent;
40 import java.awt.event.FocusListener;
41 import java.awt.event.KeyAdapter;
42 import java.awt.event.KeyEvent;
43 import java.awt.event.MouseAdapter;
44 import java.awt.event.MouseEvent;
45 import java.text.DecimalFormat;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.Comparator;
51 import java.util.HashSet;
52 import java.util.List;
54 import javax.swing.ImageIcon;
55 import javax.swing.JButton;
56 import javax.swing.JComboBox;
57 import javax.swing.JFrame;
58 import javax.swing.JInternalFrame;
59 import javax.swing.JLabel;
60 import javax.swing.JPanel;
61 import javax.swing.JScrollPane;
62 import javax.swing.JTabbedPane;
63 import javax.swing.JTable;
64 import javax.swing.JTextField;
65 import javax.swing.Timer;
66 import javax.swing.event.ChangeEvent;
67 import javax.swing.event.ChangeListener;
68 import javax.swing.event.DocumentEvent;
69 import javax.swing.event.DocumentListener;
70 import javax.swing.event.InternalFrameEvent;
71 import javax.swing.table.DefaultTableModel;
72 import javax.swing.table.TableColumn;
75 * This class provides the swing GUI layout for FTS Panel and implements most of
76 * the contracts defined in GFSPanelI
82 @SuppressWarnings("serial")
83 public abstract class GFTSPanel extends JPanel implements GFTSPanelI
85 protected JInternalFrame mainFrame = new JInternalFrame(
88 protected IProgressIndicator progressIndicator;
90 protected JComboBox<FTSDataColumnI> cmb_searchTarget = new JComboBox<FTSDataColumnI>();
92 protected JButton btn_ok = new JButton();
94 protected JButton btn_back = new JButton();
96 protected JButton btn_cancel = new JButton();
98 protected JTextField txt_search = new JTextField(30);
100 protected SequenceFetcher seqFetcher;
102 protected Collection<FTSDataColumnI> wantedFields;
104 private String lastSearchTerm = "";
106 protected JButton btn_next_page = new JButton();
108 protected JButton btn_prev_page = new JButton();
110 protected StringBuilder errorWarning = new StringBuilder();
112 protected ImageIcon warningImage = new ImageIcon(getClass().getResource(
113 "/images/warning.gif"));
115 protected ImageIcon loadingImage = new ImageIcon(getClass().getResource(
116 "/images/loading.gif"));
118 protected ImageIcon balnkPlaceholderImage = new ImageIcon(getClass()
119 .getResource("/images/blank_16x16_placeholder.png"));
121 protected JLabel lbl_warning = new JLabel(warningImage);
123 protected JLabel lbl_loading = new JLabel(loadingImage);
125 protected JLabel lbl_blank = new JLabel(balnkPlaceholderImage);
127 private JTabbedPane tabbedPane = new JTabbedPane();
129 private JPanel pnl_actions = new JPanel();
131 private JPanel pnl_results = new JPanel(new CardLayout());
133 private JPanel pnl_inputs = new JPanel();
135 private BorderLayout mainLayout = new BorderLayout();
137 protected Object[] previousWantedFields;
139 protected int resultSetCount;
141 protected int totalResultSetCount;
143 protected int offSet;
145 protected int pageLimit;
147 protected HashSet<String> paginatorCart = new HashSet<String>();
149 private static final int MIN_WIDTH = 670;
151 private static final int MIN_HEIGHT = 300;
153 protected static final DecimalFormat totalNumberformatter = new DecimalFormat(
156 private JTable tbl_summary = new JTable()
158 private boolean inLayout;
161 public boolean getScrollableTracksViewportWidth()
163 return hasExcessWidth();
168 public void doLayout()
170 if (hasExcessWidth())
172 autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS;
177 autoResizeMode = AUTO_RESIZE_OFF;
180 protected boolean hasExcessWidth()
182 return getPreferredSize().width < getParent().getWidth();
186 public void columnMarginChanged(ChangeEvent e)
192 TableColumn resizingColumn = getTableHeader().getResizingColumn();
193 // Need to do this here, before the parent's
194 // layout manager calls getPreferredSize().
195 if (resizingColumn != null && autoResizeMode == AUTO_RESIZE_OFF
198 resizingColumn.setPreferredWidth(resizingColumn.getWidth());
199 String colHeader = resizingColumn.getHeaderValue().toString();
200 getTempUserPrefs().put(colHeader, resizingColumn.getWidth());
206 public String getToolTipText(MouseEvent evt)
208 String toolTipText = null;
209 java.awt.Point pnt = evt.getPoint();
210 int rowIndex = rowAtPoint(pnt);
211 int colIndex = columnAtPoint(pnt);
215 if (getValueAt(rowIndex, colIndex) == null)
219 toolTipText = getValueAt(rowIndex, colIndex).toString();
221 } catch (Exception e)
225 toolTipText = (toolTipText == null ? null
226 : (toolTipText.length() > 500 ? JvSwingUtils.wrapTooltip(
227 true, toolTipText.subSequence(0, 500) + "...")
228 : JvSwingUtils.wrapTooltip(true, toolTipText)));
234 protected JScrollPane scrl_searchResult = new JScrollPane(tbl_summary);
241 mainFrame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
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
265 Integer width = getTempUserPrefs().get("FTSPanel.width") == null ? 800
266 : getTempUserPrefs().get("FTSPanel.width");
267 Integer height = getTempUserPrefs().get("FTSPanel.height") == null ? 400
268 : getTempUserPrefs().get("FTSPanel.height");
269 lbl_warning.setVisible(false);
270 lbl_warning.setFont(new java.awt.Font("Verdana", 0, 12));
271 lbl_loading.setVisible(false);
272 lbl_loading.setFont(new java.awt.Font("Verdana", 0, 12));
273 lbl_blank.setVisible(true);
274 lbl_blank.setFont(new java.awt.Font("Verdana", 0, 12));
276 tbl_summary.setAutoCreateRowSorter(true);
277 tbl_summary.getTableHeader().setReorderingAllowed(false);
278 tbl_summary.addMouseListener(new MouseAdapter()
281 public void mouseClicked(MouseEvent e)
287 public void mouseReleased(MouseEvent e)
292 tbl_summary.addKeyListener(new KeyAdapter()
295 public void keyPressed(KeyEvent evt)
298 switch (evt.getKeyCode())
300 case KeyEvent.VK_ESCAPE: // escape key
301 btn_back_ActionPerformed();
303 case KeyEvent.VK_ENTER: // enter key
304 if (btn_ok.isEnabled())
310 case KeyEvent.VK_TAB: // tab key
311 if (evt.isShiftDown())
313 tabbedPane.requestFocus();
317 btn_back.requestFocus();
327 btn_back.setFont(new java.awt.Font("Verdana", 0, 12));
328 btn_back.setText(MessageManager.getString("action.back"));
329 btn_back.addActionListener(new java.awt.event.ActionListener()
332 public void actionPerformed(ActionEvent e)
334 btn_back_ActionPerformed();
337 btn_back.addKeyListener(new KeyAdapter()
340 public void keyPressed(KeyEvent evt)
342 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
344 btn_back_ActionPerformed();
349 btn_ok.setEnabled(false);
350 btn_ok.setFont(new java.awt.Font("Verdana", 0, 12));
351 btn_ok.setText(MessageManager.getString("action.ok"));
352 btn_ok.addActionListener(new java.awt.event.ActionListener()
355 public void actionPerformed(ActionEvent e)
360 btn_ok.addKeyListener(new KeyAdapter()
363 public void keyPressed(KeyEvent evt)
365 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
371 btn_next_page.setEnabled(false);
372 btn_next_page.setToolTipText(MessageManager
373 .getString("label.next_page_tooltip"));
374 btn_next_page.setFont(new java.awt.Font("Verdana", 0, 12));
375 btn_next_page.setText(MessageManager.getString("action.next_page"));
376 btn_next_page.addActionListener(new java.awt.event.ActionListener()
379 public void actionPerformed(ActionEvent e)
384 btn_next_page.addKeyListener(new KeyAdapter()
387 public void keyPressed(KeyEvent evt)
389 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
396 btn_prev_page.setEnabled(false);
397 btn_prev_page.setToolTipText(MessageManager
398 .getString("label.prev_page_tooltip"));
399 btn_prev_page.setFont(new java.awt.Font("Verdana", 0, 12));
400 btn_prev_page.setText(MessageManager.getString("action.prev_page"));
401 btn_prev_page.addActionListener(new java.awt.event.ActionListener()
404 public void actionPerformed(ActionEvent e)
409 btn_prev_page.addKeyListener(new KeyAdapter()
412 public void keyPressed(KeyEvent evt)
414 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
421 if (isPaginationEnabled())
423 btn_prev_page.setVisible(true);
424 btn_next_page.setVisible(true);
428 btn_prev_page.setVisible(false);
429 btn_next_page.setVisible(false);
432 btn_cancel.setFont(new java.awt.Font("Verdana", 0, 12));
433 btn_cancel.setText(MessageManager.getString("action.cancel"));
434 btn_cancel.addActionListener(new java.awt.event.ActionListener()
437 public void actionPerformed(ActionEvent e)
439 btn_cancel_ActionPerformed();
442 btn_cancel.addKeyListener(new KeyAdapter()
445 public void keyPressed(KeyEvent evt)
447 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
449 btn_cancel_ActionPerformed();
453 scrl_searchResult.setPreferredSize(new Dimension(width, height));
455 cmb_searchTarget.setFont(new java.awt.Font("Verdana", 0, 12));
456 cmb_searchTarget.addActionListener(new ActionListener()
459 public void actionPerformed(ActionEvent e)
462 if ("all".equalsIgnoreCase(getCmbSearchTarget().getSelectedItem()
465 tooltipText = MessageManager.getString("label.search_all");
467 else if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
468 .getSelectedItem().toString()))
470 tooltipText = MessageManager
471 .getString("label.separate_multiple_accession_ids");
475 tooltipText = MessageManager.formatMessage(
476 "label.separate_multiple_query_values",
477 new Object[] { getCmbSearchTarget().getSelectedItem()
480 txt_search.setToolTipText(JvSwingUtils.wrapTooltip(true,
486 populateCmbSearchTargetOptions();
488 txt_search.setFont(new java.awt.Font("Verdana", 0, 12));
490 txt_search.addKeyListener(new KeyAdapter()
493 public void keyPressed(KeyEvent e)
495 if (e.getKeyCode() == KeyEvent.VK_ENTER)
497 if (txt_search.getText() == null
498 || txt_search.getText().isEmpty())
502 String primaryKeyName = getFTSRestClient().getPrimaryKeyColumn()
504 if (primaryKeyName.equalsIgnoreCase(getCmbSearchTarget()
505 .getSelectedItem().toString()))
507 transferToSequenceFetcher(txt_search.getText());
513 final DeferredTextInputListener listener = new DeferredTextInputListener(
514 1500, new ActionListener()
517 public void actionPerformed(ActionEvent e)
519 if (!getTypedText().equalsIgnoreCase(lastSearchTerm))
522 paginatorCart.clear();
523 lastSearchTerm = getTypedText();
527 txt_search.getDocument().addDocumentListener(listener);
528 txt_search.addFocusListener(new FocusListener()
531 public void focusGained(FocusEvent e)
537 public void focusLost(FocusEvent e)
543 final String searchTabTitle = MessageManager
544 .getString("label.search_result");
545 final String configureCols = MessageManager
546 .getString("label.configure_displayed_columns");
547 ChangeListener changeListener = new ChangeListener()
550 public void stateChanged(ChangeEvent changeEvent)
552 JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
554 int index = sourceTabbedPane.getSelectedIndex();
556 btn_back.setVisible(true);
557 btn_cancel.setVisible(true);
558 btn_ok.setVisible(true);
559 if (sourceTabbedPane.getTitleAt(index).equals(configureCols))
561 btn_back.setVisible(false);
562 btn_cancel.setVisible(false);
563 btn_ok.setVisible(false);
564 btn_back.setEnabled(false);
565 btn_cancel.setEnabled(false);
566 btn_ok.setEnabled(false);
567 btn_next_page.setEnabled(false);
568 btn_prev_page.setEnabled(false);
569 txt_search.setEnabled(false);
570 cmb_searchTarget.setEnabled(false);
571 previousWantedFields = getFTSRestClient()
572 .getAllDefaultDisplayedFTSDataColumns().toArray(
575 if (sourceTabbedPane.getTitleAt(index).equals(searchTabTitle))
577 btn_back.setEnabled(true);
578 btn_cancel.setEnabled(true);
579 refreshPaginatorState();
580 txt_search.setEnabled(true);
581 cmb_searchTarget.setEnabled(true);
582 if (wantedFieldsUpdated())
585 paginatorCart.clear();
594 tabbedPane.addChangeListener(changeListener);
595 tabbedPane.setPreferredSize(new Dimension(width, height));
596 tabbedPane.add(searchTabTitle, scrl_searchResult);
597 tabbedPane.add(configureCols, new FTSDataColumnPreferences(
598 PreferenceSource.SEARCH_SUMMARY, getFTSRestClient()));
600 pnl_actions.add(btn_back);
601 pnl_actions.add(btn_ok);
602 pnl_actions.add(btn_cancel);
604 pnl_results.add(tabbedPane);
605 pnl_inputs.add(cmb_searchTarget);
606 pnl_inputs.add(txt_search);
607 pnl_inputs.add(lbl_loading);
608 pnl_inputs.add(lbl_warning);
609 pnl_inputs.add(lbl_blank);
610 pnl_inputs.add(btn_prev_page);
611 pnl_inputs.add(btn_next_page);
613 this.setLayout(mainLayout);
614 this.add(pnl_inputs, java.awt.BorderLayout.NORTH);
615 this.add(pnl_results, java.awt.BorderLayout.CENTER);
616 this.add(pnl_actions, java.awt.BorderLayout.SOUTH);
617 mainFrame.setVisible(true);
618 mainFrame.setContentPane(this);
619 mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
621 .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
624 public void internalFrameClosing(InternalFrameEvent e)
629 mainFrame.setVisible(true);
630 mainFrame.setContentPane(this);
631 mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
632 Integer x = getTempUserPrefs().get("FTSPanel.x");
633 Integer y = getTempUserPrefs().get("FTSPanel.y");
634 if (x != null && y != null)
636 mainFrame.setLocation(x, y);
638 Desktop.addInternalFrame(mainFrame, getFTSFrameTitle(), width, height);
641 protected void closeAction()
643 // System.out.println(">>>>>>>>>> closing internal frame!!!");
644 // System.out.println("width : " + this.getWidth());
645 // System.out.println("heigh : " + this.getHeight());
646 // System.out.println("x : " + mainFrame.getX());
647 // System.out.println("y : " + mainFrame.getY());
648 getTempUserPrefs().put("FTSPanel.width", this.getWidth());
649 getTempUserPrefs().put("FTSPanel.height", pnl_results.getHeight());
650 getTempUserPrefs().put("FTSPanel.x", mainFrame.getX());
651 getTempUserPrefs().put("FTSPanel.y", mainFrame.getY());
655 public class DeferredTextInputListener implements DocumentListener
657 private final Timer swingTimer;
659 public DeferredTextInputListener(int timeOut, ActionListener listener,
662 swingTimer = new Timer(timeOut, listener);
663 swingTimer.setRepeats(repeats);
677 public void insertUpdate(DocumentEvent e)
679 swingTimer.restart();
683 public void removeUpdate(DocumentEvent e)
685 swingTimer.restart();
689 public void changedUpdate(DocumentEvent e)
691 swingTimer.restart();
696 public boolean wantedFieldsUpdated()
698 if (previousWantedFields == null)
703 return Arrays.equals(getFTSRestClient()
704 .getAllDefaultDisplayedFTSDataColumns().toArray(new Object[0]),
705 previousWantedFields) ? false : true;
709 public void validateSelection()
711 if (tbl_summary.getSelectedRows().length > 0
712 || !paginatorCart.isEmpty())
714 btn_ok.setEnabled(true);
718 btn_ok.setEnabled(false);
722 public JComboBox<FTSDataColumnI> getCmbSearchTarget()
724 return cmb_searchTarget;
727 public JTextField getTxtSearch()
732 public JInternalFrame getMainFrame()
737 protected void delayAndEnableActionButtons()
747 } catch (InterruptedException e)
751 btn_ok.setEnabled(true);
752 btn_back.setEnabled(true);
753 btn_cancel.setEnabled(true);
758 protected void checkForErrors()
760 lbl_warning.setVisible(false);
761 lbl_blank.setVisible(true);
762 if (errorWarning.length() > 0)
764 lbl_loading.setVisible(false);
765 lbl_blank.setVisible(false);
766 lbl_warning.setToolTipText(JvSwingUtils.wrapTooltip(true,
767 errorWarning.toString()));
768 lbl_warning.setVisible(true);
772 protected void btn_back_ActionPerformed()
775 new SequenceFetcher(progressIndicator);
778 protected void disableActionButtons()
780 btn_ok.setEnabled(false);
781 btn_back.setEnabled(false);
782 btn_cancel.setEnabled(false);
785 protected void btn_cancel_ActionPerformed()
791 * Populates search target combo-box options
793 public void populateCmbSearchTargetOptions()
795 List<FTSDataColumnI> searchableTargets = new ArrayList<FTSDataColumnI>();
798 Collection<FTSDataColumnI> foundFTSTargets = getFTSRestClient()
799 .getSearchableDataColumns();
800 searchableTargets.addAll(foundFTSTargets);
801 } catch (Exception e)
806 Collections.sort(searchableTargets, new Comparator<FTSDataColumnI>()
809 public int compare(FTSDataColumnI o1, FTSDataColumnI o2)
811 return o1.getName().compareTo(o2.getName());
815 for (FTSDataColumnI searchTarget : searchableTargets)
817 cmb_searchTarget.addItem(searchTarget);
821 public void transferToSequenceFetcher(String ids)
823 // mainFrame.dispose();
824 seqFetcher.getTextArea().setText(ids);
825 Thread worker = new Thread(seqFetcher);
830 public String getTypedText()
832 return txt_search.getText().trim();
836 public JTable getResultTable()
843 lbl_loading.setVisible(false);
844 errorWarning.setLength(0);
845 lbl_warning.setVisible(false);
846 lbl_blank.setVisible(true);
847 btn_ok.setEnabled(false);
848 mainFrame.setTitle(getFTSFrameTitle());
850 tbl_summary.setModel(new DefaultTableModel());
851 tbl_summary.setVisible(false);
855 public void setPrevPageButtonEnabled(boolean isEnabled)
857 btn_prev_page.setEnabled(isEnabled);
861 public void setNextPageButtonEnabled(boolean isEnabled)
863 btn_next_page.setEnabled(isEnabled);
867 public void setErrorMessage(String message)
869 errorWarning.append(message);
873 public void updateSearchFrameTitle(String title)
875 mainFrame.setTitle(title);
879 public void setSearchInProgress(Boolean isSearchInProgress)
881 lbl_blank.setVisible(!isSearchInProgress);
882 lbl_loading.setVisible(isSearchInProgress);
886 public void prevPageAction()
888 updatePaginatorCart();
889 if (offSet >= pageLimit)
891 offSet = offSet - pageLimit;
896 refreshPaginatorState();
901 public void nextPageAction()
903 updatePaginatorCart();
904 offSet = offSet + pageLimit;
908 public void updatePaginatorCart()
910 int primaryKeyColIndex = 0;
911 JTable resultTable = getResultTable();
912 int totalRows = resultTable.getRowCount();
915 primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
916 wantedFields, false);
917 } catch (Exception e)
922 for (int row = 0; row < totalRows; row++)
924 String id = (String) resultTable.getValueAt(row, primaryKeyColIndex);
925 if (paginatorCart.contains(id))
927 paginatorCart.remove(id);
930 int[] selectedRows = resultTable.getSelectedRows();
931 for (int summaryRow : selectedRows)
933 String idStr = resultTable.getValueAt(summaryRow, primaryKeyColIndex)
935 paginatorCart.add(idStr);
937 // System.out.println("Paginator shopping cart size : "
938 // + paginatorCart.size());
941 public void updateSummaryTableSelections()
943 JTable resultTable = getResultTable();
944 if (paginatorCart.isEmpty())
948 int primaryKeyColIndex = 0;
951 primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
952 wantedFields, false);
953 } catch (Exception e)
957 // System.out.println(">>>>>> got here : 1");
958 int totalRows = resultTable.getRowCount();
959 // resultTable.clearSelection();
960 for (int row = 0; row < totalRows; row++)
962 String id = (String) resultTable.getValueAt(row, primaryKeyColIndex);
963 if (paginatorCart.contains(id))
965 resultTable.addRowSelectionInterval(row, row);
971 public void refreshPaginatorState()
973 // System.out.println("resultSet count : " + resultSetCount);
974 // System.out.println("offSet : " + offSet);
975 // System.out.println("page limit : " + pageLimit);
976 setPrevPageButtonEnabled(false);
977 setNextPageButtonEnabled(false);
978 if (resultSetCount == 0 && pageLimit == 0)
982 if (resultSetCount >= pageLimit)
984 setNextPageButtonEnabled(true);
986 if (offSet >= pageLimit)
988 setPrevPageButtonEnabled(true);
992 public void referesh()
994 mainFrame.setTitle(getFTSFrameTitle());