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 protected static final DecimalFormat totalNumberformatter = new DecimalFormat(
152 private JTable tbl_summary = new JTable()
154 private boolean inLayout;
157 public boolean getScrollableTracksViewportWidth()
159 return hasExcessWidth();
164 public void doLayout()
166 if (hasExcessWidth())
168 autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS;
173 autoResizeMode = AUTO_RESIZE_OFF;
176 protected boolean hasExcessWidth()
178 return getPreferredSize().width < getParent().getWidth();
182 public void columnMarginChanged(ChangeEvent e)
188 TableColumn resizingColumn = getTableHeader().getResizingColumn();
189 // Need to do this here, before the parent's
190 // layout manager calls getPreferredSize().
191 if (resizingColumn != null && autoResizeMode == AUTO_RESIZE_OFF
194 resizingColumn.setPreferredWidth(resizingColumn.getWidth());
195 String colHeader = resizingColumn.getHeaderValue().toString();
196 getTempUserPrefs().put(colHeader, resizingColumn.getWidth());
202 public String getToolTipText(MouseEvent evt)
204 String toolTipText = null;
205 java.awt.Point pnt = evt.getPoint();
206 int rowIndex = rowAtPoint(pnt);
207 int colIndex = columnAtPoint(pnt);
211 if (getValueAt(rowIndex, colIndex) == null)
215 toolTipText = getValueAt(rowIndex, colIndex).toString();
217 } catch (Exception e)
221 toolTipText = (toolTipText == null ? null
222 : (toolTipText.length() > 500 ? JvSwingUtils.wrapTooltip(
223 true, toolTipText.subSequence(0, 500) + "...")
224 : JvSwingUtils.wrapTooltip(true, toolTipText)));
230 protected JScrollPane scrl_searchResult = new JScrollPane(tbl_summary);
237 mainFrame.addFocusListener(new FocusAdapter()
240 public void focusGained(FocusEvent e)
242 txt_search.requestFocusInWindow();
245 mainFrame.invalidate();
247 } catch (Exception e)
254 * Initializes the GUI default properties
258 private void jbInit() throws Exception
260 Integer width = getTempUserPrefs().get("FTSPanel.width") == null ? 800
261 : getTempUserPrefs().get("FTSPanel.width");
262 Integer height = getTempUserPrefs().get("FTSPanel.height") == null ? 400
263 : getTempUserPrefs().get("FTSPanel.height");
264 lbl_warning.setVisible(false);
265 lbl_warning.setFont(new java.awt.Font("Verdana", 0, 12));
266 lbl_loading.setVisible(false);
267 lbl_loading.setFont(new java.awt.Font("Verdana", 0, 12));
268 lbl_blank.setVisible(true);
269 lbl_blank.setFont(new java.awt.Font("Verdana", 0, 12));
271 tbl_summary.setAutoCreateRowSorter(true);
272 tbl_summary.getTableHeader().setReorderingAllowed(false);
273 tbl_summary.addMouseListener(new MouseAdapter()
276 public void mouseClicked(MouseEvent e)
282 public void mouseReleased(MouseEvent e)
287 tbl_summary.addKeyListener(new KeyAdapter()
290 public void keyPressed(KeyEvent evt)
293 switch (evt.getKeyCode())
295 case KeyEvent.VK_ESCAPE: // escape key
296 btn_back_ActionPerformed();
298 case KeyEvent.VK_ENTER: // enter key
299 if (btn_ok.isEnabled())
305 case KeyEvent.VK_TAB: // tab key
306 if (evt.isShiftDown())
308 tabbedPane.requestFocus();
312 btn_back.requestFocus();
322 btn_back.setFont(new java.awt.Font("Verdana", 0, 12));
323 btn_back.setText(MessageManager.getString("action.back"));
324 btn_back.addActionListener(new java.awt.event.ActionListener()
327 public void actionPerformed(ActionEvent e)
329 btn_back_ActionPerformed();
332 btn_back.addKeyListener(new KeyAdapter()
335 public void keyPressed(KeyEvent evt)
337 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
339 btn_back_ActionPerformed();
344 btn_ok.setEnabled(false);
345 btn_ok.setFont(new java.awt.Font("Verdana", 0, 12));
346 btn_ok.setText(MessageManager.getString("action.ok"));
347 btn_ok.addActionListener(new java.awt.event.ActionListener()
350 public void actionPerformed(ActionEvent e)
355 btn_ok.addKeyListener(new KeyAdapter()
358 public void keyPressed(KeyEvent evt)
360 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
366 btn_next_page.setEnabled(false);
367 btn_next_page.setToolTipText(MessageManager
368 .getString("label.next_page_tooltip"));
369 btn_next_page.setFont(new java.awt.Font("Verdana", 0, 12));
370 btn_next_page.setText(MessageManager.getString("action.next_page"));
371 btn_next_page.addActionListener(new java.awt.event.ActionListener()
374 public void actionPerformed(ActionEvent e)
379 btn_next_page.addKeyListener(new KeyAdapter()
382 public void keyPressed(KeyEvent evt)
384 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
391 btn_prev_page.setEnabled(false);
392 btn_prev_page.setToolTipText(MessageManager
393 .getString("label.prev_page_tooltip"));
394 btn_prev_page.setFont(new java.awt.Font("Verdana", 0, 12));
395 btn_prev_page.setText(MessageManager.getString("action.prev_page"));
396 btn_prev_page.addActionListener(new java.awt.event.ActionListener()
399 public void actionPerformed(ActionEvent e)
404 btn_prev_page.addKeyListener(new KeyAdapter()
407 public void keyPressed(KeyEvent evt)
409 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
416 if (isPaginationEnabled())
418 btn_prev_page.setVisible(true);
419 btn_next_page.setVisible(true);
423 btn_prev_page.setVisible(false);
424 btn_next_page.setVisible(false);
427 btn_cancel.setFont(new java.awt.Font("Verdana", 0, 12));
428 btn_cancel.setText(MessageManager.getString("action.cancel"));
429 btn_cancel.addActionListener(new java.awt.event.ActionListener()
432 public void actionPerformed(ActionEvent e)
434 btn_cancel_ActionPerformed();
437 btn_cancel.addKeyListener(new KeyAdapter()
440 public void keyPressed(KeyEvent evt)
442 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
444 btn_cancel_ActionPerformed();
448 scrl_searchResult.setPreferredSize(new Dimension(width, height));
450 cmb_searchTarget.setFont(new java.awt.Font("Verdana", 0, 12));
451 cmb_searchTarget.addActionListener(new ActionListener()
454 public void actionPerformed(ActionEvent e)
457 if ("all".equalsIgnoreCase(getCmbSearchTarget().getSelectedItem()
460 tooltipText = MessageManager.getString("label.search_all");
462 else if ("pdb id".equalsIgnoreCase(getCmbSearchTarget()
463 .getSelectedItem().toString()))
465 tooltipText = MessageManager
466 .getString("label.separate_multiple_accession_ids");
470 tooltipText = MessageManager.formatMessage(
471 "label.separate_multiple_query_values",
472 new Object[] { getCmbSearchTarget().getSelectedItem()
475 txt_search.setToolTipText(JvSwingUtils.wrapTooltip(true,
481 populateCmbSearchTargetOptions();
483 txt_search.setFont(new java.awt.Font("Verdana", 0, 12));
485 txt_search.addKeyListener(new KeyAdapter()
488 public void keyPressed(KeyEvent e)
490 if (e.getKeyCode() == KeyEvent.VK_ENTER)
492 if (txt_search.getText() == null
493 || txt_search.getText().isEmpty())
497 String primaryKeyName = getFTSRestClient().getPrimaryKeyColumn()
499 if (primaryKeyName.equalsIgnoreCase(getCmbSearchTarget()
500 .getSelectedItem().toString()))
502 transferToSequenceFetcher(txt_search.getText());
508 final DeferredTextInputListener listener = new DeferredTextInputListener(
509 1500, new ActionListener()
512 public void actionPerformed(ActionEvent e)
514 if (!getTypedText().equalsIgnoreCase(lastSearchTerm))
517 paginatorCart.clear();
518 lastSearchTerm = getTypedText();
522 txt_search.getDocument().addDocumentListener(listener);
523 txt_search.addFocusListener(new FocusListener()
526 public void focusGained(FocusEvent e)
532 public void focusLost(FocusEvent e)
538 final String searchTabTitle = MessageManager
539 .getString("label.search_result");
540 final String configureCols = MessageManager
541 .getString("label.configure_displayed_columns");
542 ChangeListener changeListener = new ChangeListener()
545 public void stateChanged(ChangeEvent changeEvent)
547 JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
549 int index = sourceTabbedPane.getSelectedIndex();
551 btn_back.setVisible(true);
552 btn_cancel.setVisible(true);
553 btn_ok.setVisible(true);
554 if (sourceTabbedPane.getTitleAt(index).equals(configureCols))
556 btn_back.setVisible(false);
557 btn_cancel.setVisible(false);
558 btn_ok.setVisible(false);
559 btn_back.setEnabled(false);
560 btn_cancel.setEnabled(false);
561 btn_ok.setEnabled(false);
562 btn_next_page.setEnabled(false);
563 btn_prev_page.setEnabled(false);
564 txt_search.setEnabled(false);
565 cmb_searchTarget.setEnabled(false);
566 previousWantedFields = getFTSRestClient()
567 .getAllDefaultDisplayedFTSDataColumns().toArray(
570 if (sourceTabbedPane.getTitleAt(index).equals(searchTabTitle))
572 btn_back.setEnabled(true);
573 btn_cancel.setEnabled(true);
574 refreshPaginatorState();
575 txt_search.setEnabled(true);
576 cmb_searchTarget.setEnabled(true);
577 if (wantedFieldsUpdated())
580 paginatorCart.clear();
589 tabbedPane.addChangeListener(changeListener);
590 tabbedPane.setPreferredSize(new Dimension(width, height));
591 tabbedPane.add(searchTabTitle, scrl_searchResult);
592 tabbedPane.add(configureCols, new FTSDataColumnPreferences(
593 PreferenceSource.SEARCH_SUMMARY, getFTSRestClient()));
595 pnl_actions.add(btn_back);
596 pnl_actions.add(btn_ok);
597 pnl_actions.add(btn_cancel);
599 pnl_results.add(tabbedPane);
600 pnl_inputs.add(cmb_searchTarget);
601 pnl_inputs.add(txt_search);
602 pnl_inputs.add(lbl_loading);
603 pnl_inputs.add(lbl_warning);
604 pnl_inputs.add(lbl_blank);
605 pnl_inputs.add(btn_prev_page);
606 pnl_inputs.add(btn_next_page);
608 this.setLayout(mainLayout);
609 this.add(pnl_inputs, java.awt.BorderLayout.NORTH);
610 this.add(pnl_results, java.awt.BorderLayout.CENTER);
611 this.add(pnl_actions, java.awt.BorderLayout.SOUTH);
612 mainFrame.setVisible(true);
613 mainFrame.setContentPane(this);
614 mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
616 .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
619 public void internalFrameClosing(InternalFrameEvent e)
624 mainFrame.setVisible(true);
625 mainFrame.setContentPane(this);
626 mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
627 Integer x = getTempUserPrefs().get("FTSPanel.x");
628 Integer y = getTempUserPrefs().get("FTSPanel.y");
629 if (x != null && y != null)
631 mainFrame.setLocation(x, y);
633 Desktop.addInternalFrame(mainFrame, getFTSFrameTitle(), width, height);
636 protected void closeAction()
638 // System.out.println(">>>>>>>>>> closing internal frame!!!");
639 // System.out.println("width : " + this.getWidth());
640 // System.out.println("heigh : " + this.getHeight());
641 // System.out.println("x : " + mainFrame.getX());
642 // System.out.println("y : " + mainFrame.getY());
643 getTempUserPrefs().put("FTSPanel.width", this.getWidth());
644 getTempUserPrefs().put("FTSPanel.height", pnl_results.getHeight());
645 getTempUserPrefs().put("FTSPanel.x", mainFrame.getX());
646 getTempUserPrefs().put("FTSPanel.y", mainFrame.getY());
650 public class DeferredTextInputListener implements DocumentListener
652 private final Timer swingTimer;
654 public DeferredTextInputListener(int timeOut, ActionListener listener,
657 swingTimer = new Timer(timeOut, listener);
658 swingTimer.setRepeats(repeats);
672 public void insertUpdate(DocumentEvent e)
674 swingTimer.restart();
678 public void removeUpdate(DocumentEvent e)
680 swingTimer.restart();
684 public void changedUpdate(DocumentEvent e)
686 swingTimer.restart();
691 public boolean wantedFieldsUpdated()
693 if (previousWantedFields == null)
698 return Arrays.equals(getFTSRestClient()
699 .getAllDefaultDisplayedFTSDataColumns().toArray(new Object[0]),
700 previousWantedFields) ? false : true;
704 public void validateSelection()
706 if (tbl_summary.getSelectedRows().length > 0
707 || !paginatorCart.isEmpty())
709 btn_ok.setEnabled(true);
713 btn_ok.setEnabled(false);
717 public JComboBox<FTSDataColumnI> getCmbSearchTarget()
719 return cmb_searchTarget;
722 public JTextField getTxtSearch()
727 public JInternalFrame getMainFrame()
732 protected void delayAndEnableActionButtons()
742 } catch (InterruptedException e)
746 btn_ok.setEnabled(true);
747 btn_back.setEnabled(true);
748 btn_cancel.setEnabled(true);
753 protected void checkForErrors()
755 lbl_warning.setVisible(false);
756 lbl_blank.setVisible(true);
757 if (errorWarning.length() > 0)
759 lbl_loading.setVisible(false);
760 lbl_blank.setVisible(false);
761 lbl_warning.setToolTipText(JvSwingUtils.wrapTooltip(true,
762 errorWarning.toString()));
763 lbl_warning.setVisible(true);
767 protected void btn_back_ActionPerformed()
770 new SequenceFetcher(progressIndicator);
773 protected void disableActionButtons()
775 btn_ok.setEnabled(false);
776 btn_back.setEnabled(false);
777 btn_cancel.setEnabled(false);
780 protected void btn_cancel_ActionPerformed()
786 * Populates search target combo-box options
788 public void populateCmbSearchTargetOptions()
790 List<FTSDataColumnI> searchableTargets = new ArrayList<FTSDataColumnI>();
793 Collection<FTSDataColumnI> foundFTSTargets = getFTSRestClient()
794 .getSearchableDataColumns();
795 searchableTargets.addAll(foundFTSTargets);
796 } catch (Exception e)
801 Collections.sort(searchableTargets, new Comparator<FTSDataColumnI>()
804 public int compare(FTSDataColumnI o1, FTSDataColumnI o2)
806 return o1.getName().compareTo(o2.getName());
810 for (FTSDataColumnI searchTarget : searchableTargets)
812 cmb_searchTarget.addItem(searchTarget);
816 public void transferToSequenceFetcher(String ids)
818 // mainFrame.dispose();
819 seqFetcher.getTextArea().setText(ids);
820 Thread worker = new Thread(seqFetcher);
825 public String getTypedText()
827 return txt_search.getText().trim();
831 public JTable getResultTable()
838 lbl_loading.setVisible(false);
839 errorWarning.setLength(0);
840 lbl_warning.setVisible(false);
841 lbl_blank.setVisible(true);
842 btn_ok.setEnabled(false);
843 mainFrame.setTitle(getFTSFrameTitle());
845 tbl_summary.setModel(new DefaultTableModel());
846 tbl_summary.setVisible(false);
850 public void setPrevPageButtonEnabled(boolean isEnabled)
852 btn_prev_page.setEnabled(isEnabled);
856 public void setNextPageButtonEnabled(boolean isEnabled)
858 btn_next_page.setEnabled(isEnabled);
862 public void setErrorMessage(String message)
864 errorWarning.append(message);
868 public void updateSearchFrameTitle(String title)
870 mainFrame.setTitle(title);
874 public void setSearchInProgress(Boolean isSearchInProgress)
876 lbl_blank.setVisible(!isSearchInProgress);
877 lbl_loading.setVisible(isSearchInProgress);
881 public void prevPageAction()
883 updatePaginatorCart();
884 if (offSet >= pageLimit)
886 offSet = offSet - pageLimit;
891 refreshPaginatorState();
896 public void nextPageAction()
898 updatePaginatorCart();
899 offSet = offSet + pageLimit;
903 public void updatePaginatorCart()
905 int primaryKeyColIndex = 0;
906 JTable resultTable = getResultTable();
907 int totalRows = resultTable.getRowCount();
910 primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
911 wantedFields, false);
912 } catch (Exception e)
917 for (int row = 0; row < totalRows; row++)
919 String id = (String) resultTable.getValueAt(row, primaryKeyColIndex);
920 if (paginatorCart.contains(id))
922 paginatorCart.remove(id);
925 int[] selectedRows = resultTable.getSelectedRows();
926 for (int summaryRow : selectedRows)
928 String idStr = resultTable.getValueAt(summaryRow, primaryKeyColIndex)
930 paginatorCart.add(idStr);
932 // System.out.println("Paginator shopping cart size : "
933 // + paginatorCart.size());
936 public void updateSummaryTableSelections()
938 JTable resultTable = getResultTable();
939 if (paginatorCart.isEmpty())
943 int primaryKeyColIndex = 0;
946 primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
947 wantedFields, false);
948 } catch (Exception e)
952 // System.out.println(">>>>>> got here : 1");
953 int totalRows = resultTable.getRowCount();
954 // resultTable.clearSelection();
955 for (int row = 0; row < totalRows; row++)
957 String id = (String) resultTable.getValueAt(row, primaryKeyColIndex);
958 if (paginatorCart.contains(id))
960 resultTable.addRowSelectionInterval(row, row);
966 public void refreshPaginatorState()
968 // System.out.println("resultSet count : " + resultSetCount);
969 // System.out.println("offSet : " + offSet);
970 // System.out.println("page limit : " + pageLimit);
971 setPrevPageButtonEnabled(false);
972 setNextPageButtonEnabled(false);
973 if (resultSetCount == 0 && pageLimit == 0)
977 if (resultSetCount >= pageLimit)
979 setNextPageButtonEnabled(true);
981 if (offSet >= pageLimit)
983 setPrevPageButtonEnabled(true);
987 public void referesh()
989 mainFrame.setTitle(getFTSFrameTitle());