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 jalview.datamodel.HiddenColumns;
25 import jalview.io.cache.JvCacheableInputBox;
26 import jalview.schemes.AnnotationColourGradient;
27 import jalview.util.MessageManager;
28 import jalview.viewmodel.annotationfilter.AnnotationFilterParameter;
30 import java.awt.BorderLayout;
31 import java.awt.CardLayout;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.ItemEvent;
37 import java.awt.event.ItemListener;
38 import java.awt.event.KeyEvent;
40 import javax.swing.ButtonGroup;
41 import javax.swing.JCheckBox;
42 import javax.swing.JComboBox;
43 import javax.swing.JInternalFrame;
44 import javax.swing.JLayeredPane;
45 import javax.swing.JPanel;
46 import javax.swing.JRadioButton;
47 import javax.swing.border.TitledBorder;
49 import net.miginfocom.swing.MigLayout;
51 @SuppressWarnings("serial")
52 public class AnnotationColumnChooser extends AnnotationRowFilter
53 implements ItemListener
55 private JPanel switchableViewsPanel = new JPanel(new CardLayout());
57 private JPanel annotationComboBoxPanel = new JPanel();
59 private StructureFilterPanel gStructureFilterPanel;
61 private StructureFilterPanel ngStructureFilterPanel;
63 private StructureFilterPanel currentStructureFilterPanel;
65 private SearchPanel currentSearchPanel;
67 private SearchPanel gSearchPanel;
69 private SearchPanel ngSearchPanel;
71 private FurtherActionPanel currentFurtherActionPanel;
73 private FurtherActionPanel gFurtherActionPanel;
75 private FurtherActionPanel ngFurtherActionPanel;
77 public static final int ACTION_OPTION_SELECT = 1;
79 public static int ACTION_OPTION_HIDE = 2;
81 public static String NO_GRAPH_VIEW = "0";
83 public static String GRAPH_VIEW = "1";
85 private int actionOption = ACTION_OPTION_SELECT;
87 private HiddenColumns oldHiddenColumns;
89 protected int MIN_WIDTH = 420;
91 protected int MIN_HEIGHT = 430;
93 public AnnotationColumnChooser(AlignViewport av, final AlignmentPanel ap)
96 frame = new JInternalFrame();
97 frame.setContentPane(this);
98 frame.setLayer(JLayeredPane.PALETTE_LAYER);
99 Desktop.addInternalFrame(frame,
100 MessageManager.getString("label.select_by_annotation"), 520,
102 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
104 addSliderChangeListener();
105 addSliderMouseListeners();
107 if (av.getAlignment().getAlignmentAnnotation() == null)
111 setOldHiddenColumns(av.getAlignment().getHiddenColumns());
114 setAnnotations(new JComboBox<>(getAnnotationItems(false)));
115 populateThresholdComboBox(threshold);
116 AnnotationColumnChooser lastChooser = av
117 .getAnnotationColumnSelectionState();
118 // restore Object state from the previous session if one exists
119 if (lastChooser != null)
121 currentSearchPanel = lastChooser.getCurrentSearchPanel();
122 currentStructureFilterPanel = lastChooser
123 .getCurrentStructureFilterPanel();
124 annotations.setSelectedIndex(
125 lastChooser.getAnnotations().getSelectedIndex());
126 threshold.setSelectedIndex(
127 lastChooser.getThreshold().getSelectedIndex());
128 actionOption = lastChooser.getActionOption();
130 .setSelected(lastChooser.percentThreshold.isSelected());
136 } catch (Exception ex)
147 protected void jbInit()
151 JPanel thresholdPanel = new JPanel();
152 thresholdPanel.setBorder(new TitledBorder(
153 MessageManager.getString("label.threshold_filter")));
154 thresholdPanel.setBackground(Color.white);
155 thresholdPanel.setFont(JvSwingUtils.getLabelFont());
156 thresholdPanel.setLayout(new MigLayout("", "[left][right]", "[][]"));
158 percentThreshold.setBackground(Color.white);
159 percentThreshold.setFont(JvSwingUtils.getLabelFont());
161 JPanel actionPanel = new JPanel();
162 actionPanel.setBackground(Color.white);
163 actionPanel.setFont(JvSwingUtils.getLabelFont());
165 JPanel graphFilterView = new JPanel();
166 graphFilterView.setLayout(new MigLayout("", "[left][right]", "[][]"));
167 graphFilterView.setBackground(Color.white);
169 JPanel noGraphFilterView = new JPanel();
170 noGraphFilterView.setLayout(new MigLayout("", "[left][right]", "[][]"));
171 noGraphFilterView.setBackground(Color.white);
173 annotationComboBoxPanel.setBackground(Color.white);
174 annotationComboBoxPanel.setFont(JvSwingUtils.getLabelFont());
176 gSearchPanel = new SearchPanel(this);
177 ngSearchPanel = new SearchPanel(this);
178 gFurtherActionPanel = new FurtherActionPanel(this);
179 ngFurtherActionPanel = new FurtherActionPanel(this);
180 gStructureFilterPanel = new StructureFilterPanel(this);
181 ngStructureFilterPanel = new StructureFilterPanel(this);
183 thresholdPanel.add(getThreshold());
184 thresholdPanel.add(percentThreshold, "wrap");
185 thresholdPanel.add(slider, "grow");
186 thresholdPanel.add(thresholdValue, "span, wrap");
189 actionPanel.add(cancel);
191 graphFilterView.add(gSearchPanel, "grow, span, wrap");
192 graphFilterView.add(gStructureFilterPanel, "grow, span, wrap");
193 graphFilterView.add(thresholdPanel, "grow, span, wrap");
194 graphFilterView.add(gFurtherActionPanel);
196 noGraphFilterView.add(ngSearchPanel, "grow, span, wrap");
197 noGraphFilterView.add(ngStructureFilterPanel, "grow, span, wrap");
198 noGraphFilterView.add(ngFurtherActionPanel);
200 annotationComboBoxPanel.add(getAnnotations());
201 switchableViewsPanel.add(noGraphFilterView,
202 AnnotationColumnChooser.NO_GRAPH_VIEW);
203 switchableViewsPanel.add(graphFilterView,
204 AnnotationColumnChooser.GRAPH_VIEW);
206 this.setLayout(new BorderLayout());
207 this.add(annotationComboBoxPanel, java.awt.BorderLayout.PAGE_START);
208 this.add(switchableViewsPanel, java.awt.BorderLayout.CENTER);
209 this.add(actionPanel, java.awt.BorderLayout.SOUTH);
211 selectedAnnotationChanged();
212 updateThresholdPanelToolTip();
216 protected void updateThresholdPanelToolTip()
218 thresholdValue.setToolTipText("");
219 slider.setToolTipText("");
221 String defaultTtip = MessageManager
222 .getString("info.change_threshold_mode_to_enable");
224 String thresh = getThreshold().getSelectedItem().toString();
225 if (thresh.equalsIgnoreCase("No Threshold"))
227 thresholdValue.setToolTipText(defaultTtip);
228 slider.setToolTipText(defaultTtip);
233 protected void reset()
235 if (this.getOldHiddenColumns() != null)
237 av.getColumnSelection().clear();
239 if (av.getAnnotationColumnSelectionState() != null)
241 HiddenColumns oldHidden = av.getAnnotationColumnSelectionState()
242 .getOldHiddenColumns();
243 av.getAlignment().setHiddenColumns(oldHidden);
246 ap.paintAlignment(true, true);
251 public void valueChanged(boolean updateAllAnnotation)
253 if (slider.isEnabled())
255 getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
257 propagateSeqAssociatedThreshold(updateAllAnnotation,
258 getCurrentAnnotation());
259 ap.paintAlignment(false, false);
264 public void updateView()
266 // Check if combobox is still adjusting
272 AnnotationFilterParameter filterParams = new AnnotationFilterParameter();
274 setCurrentAnnotation(av.getAlignment()
275 .getAlignmentAnnotation()[annmap[getAnnotations()
276 .getSelectedIndex()]]);
278 int selectedThresholdItem = getSelectedThresholdItem(
279 getThreshold().getSelectedIndex());
281 slider.setEnabled(true);
282 thresholdValue.setEnabled(true);
283 percentThreshold.setEnabled(true);
285 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
287 slider.setEnabled(false);
288 thresholdValue.setEnabled(false);
289 thresholdValue.setText("");
290 percentThreshold.setEnabled(false);
291 // build filter params
293 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
295 if (getCurrentAnnotation().threshold == null)
297 getCurrentAnnotation().setThreshold(new jalview.datamodel.GraphLine(
298 (getCurrentAnnotation().graphMax
299 - getCurrentAnnotation().graphMin) / 2f,
300 "Threshold", Color.black));
304 float range = getCurrentAnnotation().graphMax * 1000
305 - getCurrentAnnotation().graphMin * 1000;
307 slider.setMinimum((int) (getCurrentAnnotation().graphMin * 1000));
308 slider.setMaximum((int) (getCurrentAnnotation().graphMax * 1000));
310 (int) (getCurrentAnnotation().threshold.value * 1000));
312 setThresholdValueText();
314 slider.setMajorTickSpacing((int) (range / 10f));
315 slider.setEnabled(true);
316 thresholdValue.setEnabled(true);
319 // build filter params
320 filterParams.setThresholdType(
321 AnnotationFilterParameter.ThresholdType.NO_THRESHOLD);
322 if (getCurrentAnnotation().isQuantitative())
325 .setThresholdValue(getCurrentAnnotation().threshold.value);
327 if (selectedThresholdItem == AnnotationColourGradient.ABOVE_THRESHOLD)
329 filterParams.setThresholdType(
330 AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD);
332 else if (selectedThresholdItem == AnnotationColourGradient.BELOW_THRESHOLD)
334 filterParams.setThresholdType(
335 AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD);
340 updateThresholdPanelToolTip();
341 if (currentStructureFilterPanel != null)
343 if (currentStructureFilterPanel.alphaHelix.isSelected())
345 filterParams.setFilterAlphaHelix(true);
347 if (currentStructureFilterPanel.betaStrand.isSelected())
349 filterParams.setFilterBetaSheet(true);
351 if (currentStructureFilterPanel.turn.isSelected())
353 filterParams.setFilterTurn(true);
357 if (currentSearchPanel != null)
359 if (!currentSearchPanel.searchBox.getUserInput().isEmpty())
361 filterParams.setRegexString(
362 currentSearchPanel.searchBox.getUserInput());
363 if (currentSearchPanel.displayName.isSelected())
365 filterParams.addRegexSearchField(
366 AnnotationFilterParameter.SearchableAnnotationField.DISPLAY_STRING);
368 if (currentSearchPanel.description.isSelected())
370 filterParams.addRegexSearchField(
371 AnnotationFilterParameter.SearchableAnnotationField.DESCRIPTION);
376 // show hidden columns here, before changing the column selection in
377 // filterAnnotations, because showing hidden columns has the side effect of
378 // adding them to the selection
379 av.showAllHiddenColumns();
380 av.getColumnSelection().filterAnnotations(
381 getCurrentAnnotation().annotations, filterParams);
383 boolean hideCols = getActionOption() == ACTION_OPTION_HIDE;
386 av.hideSelectedColumns();
391 av.setAnnotationColumnSelectionState(this);
392 // only update overview and structures if columns were hidden
393 ap.paintAlignment(hideCols, hideCols);
396 public HiddenColumns getOldHiddenColumns()
398 return oldHiddenColumns;
401 public void setOldHiddenColumns(HiddenColumns currentHiddenColumns)
403 if (currentHiddenColumns != null)
405 this.oldHiddenColumns = new HiddenColumns(currentHiddenColumns);
409 public FurtherActionPanel getCurrentFutherActionPanel()
411 return currentFurtherActionPanel;
414 public void setCurrentFutherActionPanel(
415 FurtherActionPanel currentFutherActionPanel)
417 this.currentFurtherActionPanel = currentFutherActionPanel;
420 public SearchPanel getCurrentSearchPanel()
422 return currentSearchPanel;
425 public void setCurrentSearchPanel(SearchPanel currentSearchPanel)
427 this.currentSearchPanel = currentSearchPanel;
430 public int getActionOption()
435 public void setActionOption(int actionOption)
437 this.actionOption = actionOption;
440 public StructureFilterPanel getCurrentStructureFilterPanel()
442 return currentStructureFilterPanel;
445 public void setCurrentStructureFilterPanel(
446 StructureFilterPanel currentStructureFilterPanel)
448 this.currentStructureFilterPanel = currentStructureFilterPanel;
451 public void select_action(ActionEvent actionEvent)
453 JRadioButton radioButton = (JRadioButton) actionEvent.getSource();
454 if (radioButton.isSelected())
456 setActionOption(ACTION_OPTION_SELECT);
461 public void hide_action(ActionEvent actionEvent)
463 JRadioButton radioButton = (JRadioButton) actionEvent.getSource();
464 if (radioButton.isSelected())
466 setActionOption(ACTION_OPTION_HIDE);
472 public void itemStateChanged(ItemEvent e)
474 selectedAnnotationChanged();
478 public void selectedAnnotationChanged()
480 String currentView = AnnotationColumnChooser.NO_GRAPH_VIEW;
481 if (av.getAlignment().getAlignmentAnnotation()[annmap[getAnnotations()
482 .getSelectedIndex()]].isQuantitative())
484 currentView = AnnotationColumnChooser.GRAPH_VIEW;
487 gSearchPanel.syncState();
488 gFurtherActionPanel.syncState();
489 gStructureFilterPanel.syncState();
491 ngSearchPanel.syncState();
492 ngFurtherActionPanel.syncState();
493 ngStructureFilterPanel.syncState();
495 CardLayout switchableViewsLayout = (CardLayout) switchableViewsPanel
497 switchableViewsLayout.show(switchableViewsPanel, currentView);
501 public class FurtherActionPanel extends JPanel
503 private AnnotationColumnChooser aColChooser;
505 private JRadioButton hideOption = new JRadioButton();
507 private JRadioButton selectOption = new JRadioButton();
509 private ButtonGroup optionsGroup = new ButtonGroup();
511 public FurtherActionPanel(AnnotationColumnChooser aColChooser)
513 this.aColChooser = aColChooser;
514 JvSwingUtils.jvInitComponent(selectOption, "action.select");
515 selectOption.addActionListener(new ActionListener()
518 public void actionPerformed(ActionEvent actionEvent)
520 selectRadioAction(actionEvent);
524 JvSwingUtils.jvInitComponent(hideOption, "action.hide");
525 hideOption.addActionListener(new ActionListener()
528 public void actionPerformed(ActionEvent actionEvent)
530 hideRadioAction(actionEvent);
534 optionsGroup.add(selectOption);
535 optionsGroup.add(hideOption);
536 optionsGroup.setSelected(selectOption.getModel(), true);
538 JvSwingUtils.jvInitComponent(this);
541 this.add(selectOption);
542 this.add(hideOption);
545 public void selectRadioAction(ActionEvent actionEvent)
547 aColChooser.setCurrentFutherActionPanel(this);
548 aColChooser.select_action(actionEvent);
551 public void hideRadioAction(ActionEvent actionEvent)
553 aColChooser.setCurrentFutherActionPanel(this);
554 aColChooser.hide_action(actionEvent);
557 public void syncState()
560 .getActionOption() == AnnotationColumnChooser.ACTION_OPTION_HIDE)
562 this.optionsGroup.setSelected(this.hideOption.getModel(), true);
566 this.optionsGroup.setSelected(this.selectOption.getModel(), true);
571 public class StructureFilterPanel extends JPanel
573 private AnnotationColumnChooser aColChooser;
575 private JCheckBox alphaHelix = new JCheckBox();
577 private JCheckBox betaStrand = new JCheckBox();
579 private JCheckBox turn = new JCheckBox();
581 private JCheckBox all = new JCheckBox();
583 public StructureFilterPanel(AnnotationColumnChooser aColChooser)
585 this.aColChooser = aColChooser;
587 JvSwingUtils.jvInitComponent(alphaHelix, "label.alpha_helix");
588 alphaHelix.addActionListener(new ActionListener()
591 public void actionPerformed(ActionEvent actionEvent)
593 alphaHelix_actionPerformed();
597 JvSwingUtils.jvInitComponent(betaStrand, "label.beta_strand");
598 betaStrand.addActionListener(new ActionListener()
601 public void actionPerformed(ActionEvent actionEvent)
603 betaStrand_actionPerformed();
607 JvSwingUtils.jvInitComponent(turn, "label.turn");
608 turn.addActionListener(new ActionListener()
611 public void actionPerformed(ActionEvent actionEvent)
613 turn_actionPerformed();
617 JvSwingUtils.jvInitComponent(all, "label.select_all");
618 all.addActionListener(new ActionListener()
621 public void actionPerformed(ActionEvent actionEvent)
623 all_actionPerformed();
627 this.setBorder(new TitledBorder(
628 MessageManager.getString("label.structures_filter")));
629 JvSwingUtils.jvInitComponent(this);
632 this.add(alphaHelix);
633 this.add(betaStrand);
637 public void alphaHelix_actionPerformed()
639 updateSelectAllState();
640 aColChooser.setCurrentStructureFilterPanel(this);
641 aColChooser.updateView();
644 public void betaStrand_actionPerformed()
646 updateSelectAllState();
647 aColChooser.setCurrentStructureFilterPanel(this);
648 aColChooser.updateView();
651 public void turn_actionPerformed()
653 updateSelectAllState();
654 aColChooser.setCurrentStructureFilterPanel(this);
655 aColChooser.updateView();
658 public void all_actionPerformed()
660 if (all.isSelected())
662 alphaHelix.setSelected(true);
663 betaStrand.setSelected(true);
664 turn.setSelected(true);
668 alphaHelix.setSelected(false);
669 betaStrand.setSelected(false);
670 turn.setSelected(false);
672 aColChooser.setCurrentStructureFilterPanel(this);
673 aColChooser.updateView();
676 public void updateSelectAllState()
678 if (alphaHelix.isSelected() && betaStrand.isSelected()
679 && turn.isSelected())
681 all.setSelected(true);
685 all.setSelected(false);
689 public void syncState()
691 StructureFilterPanel sfp = aColChooser
692 .getCurrentStructureFilterPanel();
695 alphaHelix.setSelected(sfp.alphaHelix.isSelected());
696 betaStrand.setSelected(sfp.betaStrand.isSelected());
697 turn.setSelected(sfp.turn.isSelected());
698 if (sfp.all.isSelected())
700 all.setSelected(true);
701 alphaHelix.setSelected(true);
702 betaStrand.setSelected(true);
703 turn.setSelected(true);
710 public class SearchPanel extends JPanel
712 private AnnotationColumnChooser aColChooser;
714 private JCheckBox displayName = new JCheckBox();
716 private JCheckBox description = new JCheckBox();
718 private static final String FILTER_BY_ANN_CACHE_KEY = "CACHE.SELECT_FILTER_BY_ANNOT";
720 public JvCacheableInputBox<String> searchBox = new JvCacheableInputBox<>(
721 FILTER_BY_ANN_CACHE_KEY);
723 public SearchPanel(AnnotationColumnChooser aColChooser)
726 this.aColChooser = aColChooser;
727 JvSwingUtils.jvInitComponent(this);
728 this.setBorder(new TitledBorder(
729 MessageManager.getString("label.search_filter")));
731 searchBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXX");
732 searchBox.setToolTipText(
733 MessageManager.getString("info.enter_search_text_here"));
734 searchBox.getEditor().getEditorComponent()
735 .addKeyListener(new java.awt.event.KeyAdapter()
738 public void keyPressed(KeyEvent e)
740 if (e.getKeyCode() == KeyEvent.VK_ENTER)
743 searchStringAction();
748 JvSwingUtils.jvInitComponent(displayName, "label.label");
749 displayName.addActionListener(new ActionListener()
752 public void actionPerformed(ActionEvent actionEvent)
754 displayNameCheckboxAction();
758 JvSwingUtils.jvInitComponent(description, "label.description");
759 description.addActionListener(new ActionListener()
762 public void actionPerformed(ActionEvent actionEvent)
764 discriptionCheckboxAction();
770 this.add(displayName);
771 this.add(description);
774 public void displayNameCheckboxAction()
776 aColChooser.setCurrentSearchPanel(this);
777 aColChooser.updateView();
780 public void discriptionCheckboxAction()
782 aColChooser.setCurrentSearchPanel(this);
783 aColChooser.updateView();
786 public void searchStringAction()
788 aColChooser.setCurrentSearchPanel(this);
789 aColChooser.updateView();
790 updateSearchPanelToolTips();
791 searchBox.updateCache();
794 public void syncState()
796 SearchPanel sp = aColChooser.getCurrentSearchPanel();
799 description.setEnabled(sp.description.isEnabled());
800 description.setSelected(sp.description.isSelected());
802 displayName.setEnabled(sp.displayName.isEnabled());
803 displayName.setSelected(sp.displayName.isSelected());
805 searchBox.setSelectedItem(sp.searchBox.getUserInput());
807 updateSearchPanelToolTips();
810 public void updateSearchPanelToolTips()
812 String defaultTtip = MessageManager
813 .getString("info.enter_search_text_to_enable");
814 String labelTtip = MessageManager.formatMessage(
815 "info.search_in_annotation_label",
816 annotations.getSelectedItem().toString());
817 String descTtip = MessageManager.formatMessage(
818 "info.search_in_annotation_description",
819 annotations.getSelectedItem().toString());
820 displayName.setToolTipText(
821 displayName.isEnabled() ? labelTtip : defaultTtip);
822 description.setToolTipText(
823 description.isEnabled() ? descTtip : defaultTtip);
828 public void ok_actionPerformed()
831 super.ok_actionPerformed();
835 public void cancel_actionPerformed()
838 super.cancel_actionPerformed();
841 private void saveCache()
843 gSearchPanel.searchBox.persistCache();
844 ngSearchPanel.searchBox.persistCache();
845 gSearchPanel.searchBox.updateCache();
846 ngSearchPanel.searchBox.updateCache();