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.
24 import java.awt.BorderLayout;
25 import java.awt.CardLayout;
26 import java.awt.Color;
27 import java.awt.Dimension;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.FocusAdapter;
31 import java.awt.event.FocusEvent;
32 import java.awt.event.ItemEvent;
33 import java.awt.event.ItemListener;
34 import java.awt.event.KeyEvent;
36 import javax.swing.ButtonGroup;
37 import javax.swing.JCheckBox;
38 import javax.swing.JComboBox;
39 import javax.swing.JInternalFrame;
40 import javax.swing.JLayeredPane;
41 import javax.swing.JPanel;
42 import javax.swing.JRadioButton;
43 import javax.swing.border.TitledBorder;
45 import jalview.bin.Console;
46 import jalview.datamodel.AlignmentAnnotation;
47 import jalview.datamodel.HiddenColumns;
48 import jalview.io.cache.JvCacheableInputBox;
49 import jalview.schemes.AnnotationColourGradient;
50 import jalview.util.MessageManager;
51 import jalview.util.Platform;
52 import jalview.viewmodel.annotationfilter.AnnotationFilterParameter;
53 import net.miginfocom.swing.MigLayout;
55 @SuppressWarnings("serial")
56 public class AnnotationColumnChooser extends AnnotationRowFilter
57 implements ItemListener
59 private JPanel switchableViewsPanel = new JPanel(new CardLayout());
61 private JPanel annotationComboBoxPanel = new JPanel();
63 private StructureFilterPanel gStructureFilterPanel;
65 private StructureFilterPanel ngStructureFilterPanel;
67 private StructureFilterPanel currentStructureFilterPanel;
69 private SearchPanel currentSearchPanel;
71 private SearchPanel gSearchPanel;
73 private SearchPanel ngSearchPanel;
75 private FurtherActionPanel currentFurtherActionPanel;
77 private FurtherActionPanel gFurtherActionPanel;
79 private FurtherActionPanel ngFurtherActionPanel;
81 public static final int ACTION_OPTION_SELECT = 1;
83 public static int ACTION_OPTION_HIDE = 2;
85 public static String NO_GRAPH_VIEW = "0";
87 public static String GRAPH_VIEW = "1";
89 private int actionOption = ACTION_OPTION_SELECT;
91 private HiddenColumns oldHiddenColumns;
93 protected static int MIN_WIDTH = (Platform.isJS() ? 370 : 420);
95 protected static int MIN_HEIGHT = (Platform.isJS() ? 370 : 430);
97 public AnnotationColumnChooser(AlignViewport av, final AlignmentPanel ap)
102 public AnnotationColumnChooser(AlignViewport av, final AlignmentPanel ap,
103 AlignmentAnnotation selectedAnnotation)
106 frame = new JInternalFrame();
107 frame.setFrameIcon(null);
108 frame.setContentPane(this);
109 frame.setLayer(JLayeredPane.PALETTE_LAYER);
110 Desktop.addInternalFrame(frame,
111 MessageManager.getString("label.select_by_annotation"), 0, 0);
112 // BH note: MIGLayout ignores this completely,
113 // possibly creating a frame smaller than specified:
114 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
116 addSliderChangeListener();
117 addSliderMouseListeners();
119 if (av.getAlignment().getAlignmentAnnotation() == null)
123 setOldHiddenColumns(av.getAlignment().getHiddenColumns());
126 setAnnotations(new JComboBox<>(getAnnotationItems(false)));
127 populateThresholdComboBox(threshold);
128 AnnotationColumnChooser lastChooser = av
129 .getAnnotationColumnSelectionState();
130 // restore Object state from the previous session if one exists
131 if (lastChooser != null)
133 currentSearchPanel = lastChooser.getCurrentSearchPanel();
134 currentStructureFilterPanel = lastChooser
135 .getCurrentStructureFilterPanel();
136 annotations.setSelectedIndex(
137 lastChooser.getAnnotations().getSelectedIndex());
138 threshold.setSelectedIndex(
139 lastChooser.getThreshold().getSelectedIndex());
140 actionOption = lastChooser.getActionOption();
142 .setSelected(lastChooser.percentThreshold.isSelected());
144 if (selectedAnnotation != null)
148 setCurrentAnnotation(selectedAnnotation);
149 annotations.setSelectedItem(
150 getAnnotationMenuLabel(selectedAnnotation));
151 } catch (Exception x)
153 Console.error("Couldn't select annotation in column chooser", x);
160 } catch (Exception ex)
171 protected void jbInit()
175 JPanel thresholdPanel = new JPanel();
176 thresholdPanel.setBorder(new TitledBorder(
177 MessageManager.getString("label.threshold_filter")));
178 thresholdPanel.setBackground(Color.white);
179 thresholdPanel.setFont(JvSwingUtils.getLabelFont());
180 thresholdPanel.setLayout(new MigLayout("", "[left][right]", "[][]"));
182 percentThreshold.setBackground(Color.white);
183 percentThreshold.setFont(JvSwingUtils.getLabelFont());
185 JPanel actionPanel = new JPanel();
186 actionPanel.setBackground(Color.white);
187 actionPanel.setFont(JvSwingUtils.getLabelFont());
189 JPanel graphFilterView = new JPanel();
190 graphFilterView.setLayout(new MigLayout("", "[left][right]", "[][]"));
191 graphFilterView.setBackground(Color.white);
193 JPanel noGraphFilterView = new JPanel();
194 noGraphFilterView.setLayout(new MigLayout("", "[left][right]", "[][]"));
195 noGraphFilterView.setBackground(Color.white);
197 annotationComboBoxPanel.setBackground(Color.white);
198 annotationComboBoxPanel.setFont(JvSwingUtils.getLabelFont());
200 gSearchPanel = new SearchPanel(this);
201 ngSearchPanel = new SearchPanel(this);
202 gFurtherActionPanel = new FurtherActionPanel(this);
203 ngFurtherActionPanel = new FurtherActionPanel(this);
204 gStructureFilterPanel = new StructureFilterPanel(this);
205 ngStructureFilterPanel = new StructureFilterPanel(this);
207 thresholdPanel.add(getThreshold());
208 thresholdPanel.add(percentThreshold, "wrap");
209 thresholdPanel.add(slider, "grow");
210 thresholdPanel.add(thresholdValue, "span, wrap");
213 actionPanel.add(cancel);
215 graphFilterView.add(gSearchPanel, "grow, span, wrap");
216 graphFilterView.add(gStructureFilterPanel, "grow, span, wrap");
217 graphFilterView.add(thresholdPanel, "grow, span, wrap");
218 graphFilterView.add(gFurtherActionPanel);
220 noGraphFilterView.add(ngSearchPanel, "grow, span, wrap");
221 noGraphFilterView.add(ngStructureFilterPanel, "grow, span, wrap");
222 noGraphFilterView.add(ngFurtherActionPanel);
224 annotationComboBoxPanel.add(getAnnotations());
225 switchableViewsPanel.add(noGraphFilterView,
226 AnnotationColumnChooser.NO_GRAPH_VIEW);
227 switchableViewsPanel.add(graphFilterView,
228 AnnotationColumnChooser.GRAPH_VIEW);
229 this.setLayout(new BorderLayout());
230 this.add(annotationComboBoxPanel, java.awt.BorderLayout.PAGE_START);
231 this.add(switchableViewsPanel, java.awt.BorderLayout.CENTER);
232 this.add(actionPanel, java.awt.BorderLayout.SOUTH);
234 selectedAnnotationChanged();
235 updateThresholdPanelToolTip();
239 protected void updateThresholdPanelToolTip()
241 thresholdValue.setToolTipText("");
242 slider.setToolTipText("");
244 String defaultTtip = MessageManager
245 .getString("info.change_threshold_mode_to_enable");
247 String thresh = getThreshold().getSelectedItem().toString();
248 if (thresh.equalsIgnoreCase("No Threshold"))
250 thresholdValue.setToolTipText(defaultTtip);
251 slider.setToolTipText(defaultTtip);
256 protected void reset()
258 if (this.getOldHiddenColumns() != null)
260 av.getColumnSelection().clear();
262 if (av.getAnnotationColumnSelectionState() != null)
264 HiddenColumns oldHidden = av.getAnnotationColumnSelectionState()
265 .getOldHiddenColumns();
266 av.getAlignment().setHiddenColumns(oldHidden);
269 ap.paintAlignment(true, true);
274 public void valueChanged(boolean updateAllAnnotation)
276 if (slider.isEnabled())
278 getCurrentAnnotation().threshold.value = getSliderValue();
280 propagateSeqAssociatedThreshold(updateAllAnnotation,
281 getCurrentAnnotation());
282 ap.paintAlignment(false, false);
287 public void updateView()
289 // Check if combobox is still adjusting
295 AnnotationFilterParameter filterParams = new AnnotationFilterParameter();
297 setCurrentAnnotation(av.getAlignment()
298 .getAlignmentAnnotation()[annmap[getAnnotations()
299 .getSelectedIndex()]]);
301 int selectedThresholdItem = getSelectedThresholdItem(
302 getThreshold().getSelectedIndex());
304 slider.setEnabled(true);
305 thresholdValue.setEnabled(true);
306 percentThreshold.setEnabled(true);
308 final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
309 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
311 slider.setEnabled(false);
312 thresholdValue.setEnabled(false);
313 thresholdValue.setText("");
314 percentThreshold.setEnabled(false);
315 // build filter params
317 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
319 if (currentAnnotation.threshold == null)
321 currentAnnotation.setThreshold(new jalview.datamodel.GraphLine(
322 (currentAnnotation.graphMax - currentAnnotation.graphMin)
324 "Threshold", Color.black));
329 setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
330 currentAnnotation.threshold.value);
332 setThresholdValueText();
334 slider.setEnabled(true);
335 thresholdValue.setEnabled(true);
338 // build filter params
339 filterParams.setThresholdType(
340 AnnotationFilterParameter.ThresholdType.NO_THRESHOLD);
341 if (currentAnnotation.isQuantitative())
343 filterParams.setThresholdValue(currentAnnotation.threshold.value);
345 if (selectedThresholdItem == AnnotationColourGradient.ABOVE_THRESHOLD)
347 filterParams.setThresholdType(
348 AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD);
350 else if (selectedThresholdItem == AnnotationColourGradient.BELOW_THRESHOLD)
352 filterParams.setThresholdType(
353 AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD);
358 updateThresholdPanelToolTip();
359 if (currentStructureFilterPanel != null)
361 if (currentStructureFilterPanel.alphaHelix.isSelected())
363 filterParams.setFilterAlphaHelix(true);
365 if (currentStructureFilterPanel.betaStrand.isSelected())
367 filterParams.setFilterBetaSheet(true);
369 if (currentStructureFilterPanel.turn.isSelected())
371 filterParams.setFilterTurn(true);
375 if (currentSearchPanel != null)
377 if (!currentSearchPanel.searchBox.getUserInput().isEmpty())
379 filterParams.setRegexString(
380 currentSearchPanel.searchBox.getUserInput());
381 if (currentSearchPanel.displayName.isSelected())
383 filterParams.addRegexSearchField(
384 AnnotationFilterParameter.SearchableAnnotationField.DISPLAY_STRING);
386 if (currentSearchPanel.description.isSelected())
388 filterParams.addRegexSearchField(
389 AnnotationFilterParameter.SearchableAnnotationField.DESCRIPTION);
394 // show hidden columns here, before changing the column selection in
395 // filterAnnotations, because showing hidden columns has the side effect of
396 // adding them to the selection
397 av.showAllHiddenColumns();
398 av.getColumnSelection().filterAnnotations(currentAnnotation,
401 boolean hideCols = getActionOption() == ACTION_OPTION_HIDE;
404 av.hideSelectedColumns();
409 av.setAnnotationColumnSelectionState(this);
410 // only update overview and structures if columns were hidden
411 ap.paintAlignment(hideCols, hideCols);
414 public HiddenColumns getOldHiddenColumns()
416 return oldHiddenColumns;
419 public void setOldHiddenColumns(HiddenColumns currentHiddenColumns)
421 if (currentHiddenColumns != null)
423 this.oldHiddenColumns = new HiddenColumns(currentHiddenColumns);
427 public FurtherActionPanel getCurrentFutherActionPanel()
429 return currentFurtherActionPanel;
432 public void setCurrentFutherActionPanel(
433 FurtherActionPanel currentFutherActionPanel)
435 this.currentFurtherActionPanel = currentFutherActionPanel;
438 public SearchPanel getCurrentSearchPanel()
440 return currentSearchPanel;
443 public void setCurrentSearchPanel(SearchPanel currentSearchPanel)
445 this.currentSearchPanel = currentSearchPanel;
448 public int getActionOption()
453 public void setActionOption(int actionOption)
455 this.actionOption = actionOption;
458 public StructureFilterPanel getCurrentStructureFilterPanel()
460 return currentStructureFilterPanel;
463 public void setCurrentStructureFilterPanel(
464 StructureFilterPanel currentStructureFilterPanel)
466 this.currentStructureFilterPanel = currentStructureFilterPanel;
469 public void select_action(ActionEvent actionEvent)
471 JRadioButton radioButton = (JRadioButton) actionEvent.getSource();
472 if (radioButton.isSelected())
474 setActionOption(ACTION_OPTION_SELECT);
479 public void hide_action(ActionEvent actionEvent)
481 JRadioButton radioButton = (JRadioButton) actionEvent.getSource();
482 if (radioButton.isSelected())
484 setActionOption(ACTION_OPTION_HIDE);
490 public void itemStateChanged(ItemEvent e)
492 selectedAnnotationChanged();
496 public void selectedAnnotationChanged()
498 String currentView = AnnotationColumnChooser.NO_GRAPH_VIEW;
499 if (av.getAlignment().getAlignmentAnnotation()[annmap[getAnnotations()
500 .getSelectedIndex()]].isQuantitative())
502 currentView = AnnotationColumnChooser.GRAPH_VIEW;
505 gSearchPanel.syncState();
506 gFurtherActionPanel.syncState();
507 gStructureFilterPanel.syncState();
509 ngSearchPanel.syncState();
510 ngFurtherActionPanel.syncState();
511 ngStructureFilterPanel.syncState();
513 CardLayout switchableViewsLayout = (CardLayout) switchableViewsPanel
515 switchableViewsLayout.show(switchableViewsPanel, currentView);
519 public class FurtherActionPanel extends JPanel
521 private AnnotationColumnChooser aColChooser;
523 private JRadioButton hideOption = new JRadioButton();
525 private JRadioButton selectOption = new JRadioButton();
527 private ButtonGroup optionsGroup = new ButtonGroup();
529 public FurtherActionPanel(AnnotationColumnChooser aColChooser)
531 this.aColChooser = aColChooser;
532 JvSwingUtils.jvInitComponent(selectOption, "action.select");
533 selectOption.addActionListener(new ActionListener()
536 public void actionPerformed(ActionEvent actionEvent)
538 selectRadioAction(actionEvent);
542 JvSwingUtils.jvInitComponent(hideOption, "action.hide");
543 hideOption.addActionListener(new ActionListener()
546 public void actionPerformed(ActionEvent actionEvent)
548 hideRadioAction(actionEvent);
552 optionsGroup.add(selectOption);
553 optionsGroup.add(hideOption);
554 optionsGroup.setSelected(selectOption.getModel(), true);
556 JvSwingUtils.jvInitComponent(this);
559 this.add(selectOption);
560 this.add(hideOption);
563 public void selectRadioAction(ActionEvent actionEvent)
565 aColChooser.setCurrentFutherActionPanel(this);
566 aColChooser.select_action(actionEvent);
569 public void hideRadioAction(ActionEvent actionEvent)
571 aColChooser.setCurrentFutherActionPanel(this);
572 aColChooser.hide_action(actionEvent);
575 public void syncState()
578 .getActionOption() == AnnotationColumnChooser.ACTION_OPTION_HIDE)
580 this.optionsGroup.setSelected(this.hideOption.getModel(), true);
584 this.optionsGroup.setSelected(this.selectOption.getModel(), true);
589 public class StructureFilterPanel extends JPanel
591 private AnnotationColumnChooser aColChooser;
593 private JCheckBox alphaHelix = new JCheckBox();
595 private JCheckBox betaStrand = new JCheckBox();
597 private JCheckBox turn = new JCheckBox();
599 private JCheckBox all = new JCheckBox();
601 public StructureFilterPanel(AnnotationColumnChooser aColChooser)
603 this.aColChooser = aColChooser;
605 JvSwingUtils.jvInitComponent(alphaHelix, "label.alpha_helix");
606 alphaHelix.addActionListener(new ActionListener()
609 public void actionPerformed(ActionEvent actionEvent)
611 alphaHelix_actionPerformed();
615 JvSwingUtils.jvInitComponent(betaStrand, "label.beta_strand");
616 betaStrand.addActionListener(new ActionListener()
619 public void actionPerformed(ActionEvent actionEvent)
621 betaStrand_actionPerformed();
625 JvSwingUtils.jvInitComponent(turn, "label.turn");
626 turn.addActionListener(new ActionListener()
629 public void actionPerformed(ActionEvent actionEvent)
631 turn_actionPerformed();
635 JvSwingUtils.jvInitComponent(all, "label.select_all");
636 all.addActionListener(new ActionListener()
639 public void actionPerformed(ActionEvent actionEvent)
641 all_actionPerformed();
645 this.setBorder(new TitledBorder(
646 MessageManager.getString("label.structures_filter")));
647 JvSwingUtils.jvInitComponent(this);
650 this.add(alphaHelix);
651 this.add(betaStrand);
655 public void alphaHelix_actionPerformed()
657 updateSelectAllState();
658 aColChooser.setCurrentStructureFilterPanel(this);
659 aColChooser.updateView();
662 public void betaStrand_actionPerformed()
664 updateSelectAllState();
665 aColChooser.setCurrentStructureFilterPanel(this);
666 aColChooser.updateView();
669 public void turn_actionPerformed()
671 updateSelectAllState();
672 aColChooser.setCurrentStructureFilterPanel(this);
673 aColChooser.updateView();
676 public void all_actionPerformed()
678 if (all.isSelected())
680 alphaHelix.setSelected(true);
681 betaStrand.setSelected(true);
682 turn.setSelected(true);
686 alphaHelix.setSelected(false);
687 betaStrand.setSelected(false);
688 turn.setSelected(false);
690 aColChooser.setCurrentStructureFilterPanel(this);
691 aColChooser.updateView();
694 public void updateSelectAllState()
696 if (alphaHelix.isSelected() && betaStrand.isSelected()
697 && turn.isSelected())
699 all.setSelected(true);
703 all.setSelected(false);
707 public void syncState()
709 StructureFilterPanel sfp = aColChooser
710 .getCurrentStructureFilterPanel();
713 alphaHelix.setSelected(sfp.alphaHelix.isSelected());
714 betaStrand.setSelected(sfp.betaStrand.isSelected());
715 turn.setSelected(sfp.turn.isSelected());
716 if (sfp.all.isSelected())
718 all.setSelected(true);
719 alphaHelix.setSelected(true);
720 betaStrand.setSelected(true);
721 turn.setSelected(true);
728 public class SearchPanel extends JPanel
730 private AnnotationColumnChooser aColChooser;
732 private JCheckBox displayName = new JCheckBox();
734 private JCheckBox description = new JCheckBox();
736 private static final String FILTER_BY_ANN_CACHE_KEY = "CACHE.SELECT_FILTER_BY_ANNOT";
738 public JvCacheableInputBox<String> searchBox = new JvCacheableInputBox<>(
739 FILTER_BY_ANN_CACHE_KEY, 23);
741 public SearchPanel(AnnotationColumnChooser aColChooser)
744 this.aColChooser = aColChooser;
745 JvSwingUtils.jvInitComponent(this);
746 this.setBorder(new TitledBorder(
747 MessageManager.getString("label.search_filter")));
749 searchBox.getComponent().setToolTipText(
750 MessageManager.getString("info.enter_search_text_here"));
751 searchBox.addKeyListener(new java.awt.event.KeyAdapter()
754 public void keyPressed(KeyEvent e)
756 if (e.getKeyCode() == KeyEvent.VK_ENTER)
759 searchStringAction();
763 searchBox.addFocusListener(new FocusAdapter()
766 public void focusLost(FocusEvent e)
768 searchStringAction();
772 JvSwingUtils.jvInitComponent(displayName, "label.label");
773 displayName.addActionListener(new ActionListener()
776 public void actionPerformed(ActionEvent actionEvent)
778 displayNameCheckboxAction();
782 JvSwingUtils.jvInitComponent(description, "label.description");
783 description.addActionListener(new ActionListener()
786 public void actionPerformed(ActionEvent actionEvent)
788 descriptionCheckboxAction();
793 this.add(searchBox.getComponent());
794 this.add(displayName);
795 this.add(description);
798 public void displayNameCheckboxAction()
800 aColChooser.setCurrentSearchPanel(this);
801 aColChooser.updateView();
804 public void descriptionCheckboxAction()
806 aColChooser.setCurrentSearchPanel(this);
807 aColChooser.updateView();
810 public void searchStringAction()
812 aColChooser.setCurrentSearchPanel(this);
813 aColChooser.updateView();
814 updateSearchPanelToolTips();
815 searchBox.updateCache();
818 public void syncState()
820 SearchPanel sp = aColChooser.getCurrentSearchPanel();
823 description.setEnabled(sp.description.isEnabled());
824 description.setSelected(sp.description.isSelected());
826 displayName.setEnabled(sp.displayName.isEnabled());
827 displayName.setSelected(sp.displayName.isSelected());
829 searchBox.setSelectedItem(sp.searchBox.getUserInput());
831 updateSearchPanelToolTips();
834 public void updateSearchPanelToolTips()
836 String defaultTtip = MessageManager
837 .getString("info.enter_search_text_to_enable");
838 String labelTtip = MessageManager.formatMessage(
839 "info.search_in_annotation_label",
840 annotations.getSelectedItem().toString());
841 String descTtip = MessageManager.formatMessage(
842 "info.search_in_annotation_description",
843 annotations.getSelectedItem().toString());
844 displayName.setToolTipText(
845 displayName.isEnabled() ? labelTtip : defaultTtip);
846 description.setToolTipText(
847 description.isEnabled() ? descTtip : defaultTtip);
852 public void ok_actionPerformed()
855 super.ok_actionPerformed();
859 public void cancel_actionPerformed()
862 super.cancel_actionPerformed();
865 private void saveCache()
867 gSearchPanel.searchBox.persistCache();
868 ngSearchPanel.searchBox.persistCache();
869 gSearchPanel.searchBox.updateCache();
870 ngSearchPanel.searchBox.updateCache();
873 public static void displayFor(AlignViewport av, AlignmentPanel ap,
874 AlignmentAnnotation alignmentAnnotation)
876 AnnotationColumnChooser colchooser = new AnnotationColumnChooser(av, ap,
877 alignmentAnnotation);