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.FocusAdapter;
37 import java.awt.event.FocusEvent;
38 import java.awt.event.ItemEvent;
39 import java.awt.event.ItemListener;
40 import java.awt.event.KeyEvent;
42 import javax.swing.ButtonGroup;
43 import javax.swing.JCheckBox;
44 import javax.swing.JComboBox;
45 import javax.swing.JInternalFrame;
46 import javax.swing.JLayeredPane;
47 import javax.swing.JPanel;
48 import javax.swing.JRadioButton;
49 import javax.swing.border.TitledBorder;
51 import net.miginfocom.swing.MigLayout;
53 @SuppressWarnings("serial")
54 public class AnnotationColumnChooser extends AnnotationRowFilter
55 implements ItemListener
57 private JPanel switchableViewsPanel = new JPanel(new CardLayout());
59 private JPanel annotationComboBoxPanel = new JPanel();
61 private StructureFilterPanel gStructureFilterPanel;
63 private StructureFilterPanel ngStructureFilterPanel;
65 private StructureFilterPanel currentStructureFilterPanel;
67 private SearchPanel currentSearchPanel;
69 private SearchPanel gSearchPanel;
71 private SearchPanel ngSearchPanel;
73 private FurtherActionPanel currentFurtherActionPanel;
75 private FurtherActionPanel gFurtherActionPanel;
77 private FurtherActionPanel ngFurtherActionPanel;
79 public static final int ACTION_OPTION_SELECT = 1;
81 public static int ACTION_OPTION_HIDE = 2;
83 public static String NO_GRAPH_VIEW = "0";
85 public static String GRAPH_VIEW = "1";
87 private int actionOption = ACTION_OPTION_SELECT;
89 private HiddenColumns oldHiddenColumns;
92 protected static int MIN_WIDTH = (/** @j2sNative 370||*/420);
95 protected static int MIN_HEIGHT = (/** @j2sNative 370||*/430);
97 public AnnotationColumnChooser(AlignViewport av, final AlignmentPanel ap)
100 frame = new JInternalFrame();
101 frame.setContentPane(this);
102 frame.setLayer(JLayeredPane.PALETTE_LAYER);
103 Desktop.addInternalFrame(frame,
104 MessageManager.getString("label.select_by_annotation"), 0,
106 // BH note: MIGLayout ignores this completely,
107 // possibly creating a frame smaller than specified:
108 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
110 addSliderChangeListener();
111 addSliderMouseListeners();
113 if (av.getAlignment().getAlignmentAnnotation() == null)
117 setOldHiddenColumns(av.getAlignment().getHiddenColumns());
120 setAnnotations(new JComboBox<>(getAnnotationItems(false)));
121 populateThresholdComboBox(threshold);
122 AnnotationColumnChooser lastChooser = av
123 .getAnnotationColumnSelectionState();
124 // restore Object state from the previous session if one exists
125 if (lastChooser != null)
127 currentSearchPanel = lastChooser.getCurrentSearchPanel();
128 currentStructureFilterPanel = lastChooser
129 .getCurrentStructureFilterPanel();
130 annotations.setSelectedIndex(
131 lastChooser.getAnnotations().getSelectedIndex());
132 threshold.setSelectedIndex(
133 lastChooser.getThreshold().getSelectedIndex());
134 actionOption = lastChooser.getActionOption();
136 .setSelected(lastChooser.percentThreshold.isSelected());
142 } catch (Exception ex)
153 protected void jbInit()
157 JPanel thresholdPanel = new JPanel();
158 thresholdPanel.setBorder(new TitledBorder(
159 MessageManager.getString("label.threshold_filter")));
160 thresholdPanel.setBackground(Color.white);
161 thresholdPanel.setFont(JvSwingUtils.getLabelFont());
162 thresholdPanel.setLayout(new MigLayout("", "[left][right]", "[][]") );
164 percentThreshold.setBackground(Color.white);
165 percentThreshold.setFont(JvSwingUtils.getLabelFont());
167 JPanel actionPanel = new JPanel();
168 actionPanel.setBackground(Color.white);
169 actionPanel.setFont(JvSwingUtils.getLabelFont());
171 JPanel graphFilterView = new JPanel();
172 graphFilterView.setLayout(new MigLayout("", "[left][right]", "[][]"));
173 graphFilterView.setBackground(Color.white);
175 JPanel noGraphFilterView = new JPanel();
176 noGraphFilterView.setLayout(new MigLayout("", "[left][right]", "[][]"));
177 noGraphFilterView.setBackground(Color.white);
179 annotationComboBoxPanel.setBackground(Color.white);
180 annotationComboBoxPanel.setFont(JvSwingUtils.getLabelFont());
182 gSearchPanel = new SearchPanel(this);
183 ngSearchPanel = new SearchPanel(this);
184 gFurtherActionPanel = new FurtherActionPanel(this);
185 ngFurtherActionPanel = new FurtherActionPanel(this);
186 gStructureFilterPanel = new StructureFilterPanel(this);
187 ngStructureFilterPanel = new StructureFilterPanel(this);
189 thresholdPanel.add(getThreshold());
190 thresholdPanel.add(percentThreshold, "wrap");
191 thresholdPanel.add(slider, "grow");
192 thresholdPanel.add(thresholdValue, "span, wrap");
195 actionPanel.add(cancel);
197 graphFilterView.add(gSearchPanel, "grow, span, wrap");
198 graphFilterView.add(gStructureFilterPanel, "grow, span, wrap");
199 graphFilterView.add(thresholdPanel, "grow, span, wrap");
200 graphFilterView.add(gFurtherActionPanel);
202 noGraphFilterView.add(ngSearchPanel, "grow, span, wrap");
203 noGraphFilterView.add(ngStructureFilterPanel, "grow, span, wrap");
204 noGraphFilterView.add(ngFurtherActionPanel);
206 annotationComboBoxPanel.add(getAnnotations());
207 switchableViewsPanel.add(noGraphFilterView,
208 AnnotationColumnChooser.NO_GRAPH_VIEW);
209 switchableViewsPanel.add(graphFilterView,
210 AnnotationColumnChooser.GRAPH_VIEW);
211 this.setLayout(new BorderLayout());
212 this.add(annotationComboBoxPanel, java.awt.BorderLayout.PAGE_START);
213 this.add(switchableViewsPanel, java.awt.BorderLayout.CENTER);
214 this.add(actionPanel, java.awt.BorderLayout.SOUTH);
216 selectedAnnotationChanged();
217 updateThresholdPanelToolTip();
221 protected void updateThresholdPanelToolTip()
223 thresholdValue.setToolTipText("");
224 slider.setToolTipText("");
226 String defaultTtip = MessageManager
227 .getString("info.change_threshold_mode_to_enable");
229 String thresh = getThreshold().getSelectedItem().toString();
230 if (thresh.equalsIgnoreCase("No Threshold"))
232 thresholdValue.setToolTipText(defaultTtip);
233 slider.setToolTipText(defaultTtip);
238 protected void reset()
240 if (this.getOldHiddenColumns() != null)
242 av.getColumnSelection().clear();
244 if (av.getAnnotationColumnSelectionState() != null)
246 HiddenColumns oldHidden = av.getAnnotationColumnSelectionState()
247 .getOldHiddenColumns();
248 av.getAlignment().setHiddenColumns(oldHidden);
251 ap.paintAlignment(true, true);
256 public void valueChanged(boolean updateAllAnnotation)
258 if (slider.isEnabled())
260 getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
262 propagateSeqAssociatedThreshold(updateAllAnnotation,
263 getCurrentAnnotation());
264 ap.paintAlignment(false, false);
269 public void updateView()
271 // Check if combobox is still adjusting
277 AnnotationFilterParameter filterParams = new AnnotationFilterParameter();
279 setCurrentAnnotation(av.getAlignment()
280 .getAlignmentAnnotation()[annmap[getAnnotations()
281 .getSelectedIndex()]]);
283 int selectedThresholdItem = getSelectedThresholdItem(
284 getThreshold().getSelectedIndex());
286 slider.setEnabled(true);
287 thresholdValue.setEnabled(true);
288 percentThreshold.setEnabled(true);
290 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
292 slider.setEnabled(false);
293 thresholdValue.setEnabled(false);
294 thresholdValue.setText("");
295 percentThreshold.setEnabled(false);
296 // build filter params
298 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
300 if (getCurrentAnnotation().threshold == null)
302 getCurrentAnnotation().setThreshold(new jalview.datamodel.GraphLine(
303 (getCurrentAnnotation().graphMax
304 - getCurrentAnnotation().graphMin) / 2f,
305 "Threshold", Color.black));
309 float range = getCurrentAnnotation().graphMax * 1000
310 - getCurrentAnnotation().graphMin * 1000;
312 slider.setMinimum((int) (getCurrentAnnotation().graphMin * 1000));
313 slider.setMaximum((int) (getCurrentAnnotation().graphMax * 1000));
315 (int) (getCurrentAnnotation().threshold.value * 1000));
317 setThresholdValueText();
319 slider.setMajorTickSpacing((int) (range / 10f));
320 slider.setEnabled(true);
321 thresholdValue.setEnabled(true);
324 // build filter params
325 filterParams.setThresholdType(
326 AnnotationFilterParameter.ThresholdType.NO_THRESHOLD);
327 if (getCurrentAnnotation().isQuantitative())
330 .setThresholdValue(getCurrentAnnotation().threshold.value);
332 if (selectedThresholdItem == AnnotationColourGradient.ABOVE_THRESHOLD)
334 filterParams.setThresholdType(
335 AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD);
337 else if (selectedThresholdItem == AnnotationColourGradient.BELOW_THRESHOLD)
339 filterParams.setThresholdType(
340 AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD);
345 updateThresholdPanelToolTip();
346 if (currentStructureFilterPanel != null)
348 if (currentStructureFilterPanel.alphaHelix.isSelected())
350 filterParams.setFilterAlphaHelix(true);
352 if (currentStructureFilterPanel.betaStrand.isSelected())
354 filterParams.setFilterBetaSheet(true);
356 if (currentStructureFilterPanel.turn.isSelected())
358 filterParams.setFilterTurn(true);
362 if (currentSearchPanel != null)
364 if (!currentSearchPanel.searchBox.getUserInput().isEmpty())
366 filterParams.setRegexString(
367 currentSearchPanel.searchBox.getUserInput());
368 if (currentSearchPanel.displayName.isSelected())
370 filterParams.addRegexSearchField(
371 AnnotationFilterParameter.SearchableAnnotationField.DISPLAY_STRING);
373 if (currentSearchPanel.description.isSelected())
375 filterParams.addRegexSearchField(
376 AnnotationFilterParameter.SearchableAnnotationField.DESCRIPTION);
381 // show hidden columns here, before changing the column selection in
382 // filterAnnotations, because showing hidden columns has the side effect of
383 // adding them to the selection
384 av.showAllHiddenColumns();
385 av.getColumnSelection().filterAnnotations(
386 getCurrentAnnotation().annotations, filterParams);
388 boolean hideCols = getActionOption() == ACTION_OPTION_HIDE;
391 av.hideSelectedColumns();
396 av.setAnnotationColumnSelectionState(this);
397 // only update overview and structures if columns were hidden
398 ap.paintAlignment(hideCols, hideCols);
401 public HiddenColumns getOldHiddenColumns()
403 return oldHiddenColumns;
406 public void setOldHiddenColumns(HiddenColumns currentHiddenColumns)
408 if (currentHiddenColumns != null)
410 this.oldHiddenColumns = new HiddenColumns(currentHiddenColumns);
414 public FurtherActionPanel getCurrentFutherActionPanel()
416 return currentFurtherActionPanel;
419 public void setCurrentFutherActionPanel(
420 FurtherActionPanel currentFutherActionPanel)
422 this.currentFurtherActionPanel = currentFutherActionPanel;
425 public SearchPanel getCurrentSearchPanel()
427 return currentSearchPanel;
430 public void setCurrentSearchPanel(SearchPanel currentSearchPanel)
432 this.currentSearchPanel = currentSearchPanel;
435 public int getActionOption()
440 public void setActionOption(int actionOption)
442 this.actionOption = actionOption;
445 public StructureFilterPanel getCurrentStructureFilterPanel()
447 return currentStructureFilterPanel;
450 public void setCurrentStructureFilterPanel(
451 StructureFilterPanel currentStructureFilterPanel)
453 this.currentStructureFilterPanel = currentStructureFilterPanel;
456 public void select_action(ActionEvent actionEvent)
458 JRadioButton radioButton = (JRadioButton) actionEvent.getSource();
459 if (radioButton.isSelected())
461 setActionOption(ACTION_OPTION_SELECT);
466 public void hide_action(ActionEvent actionEvent)
468 JRadioButton radioButton = (JRadioButton) actionEvent.getSource();
469 if (radioButton.isSelected())
471 setActionOption(ACTION_OPTION_HIDE);
477 public void itemStateChanged(ItemEvent e)
479 selectedAnnotationChanged();
483 public void selectedAnnotationChanged()
485 String currentView = AnnotationColumnChooser.NO_GRAPH_VIEW;
486 if (av.getAlignment().getAlignmentAnnotation()[annmap[getAnnotations()
487 .getSelectedIndex()]].isQuantitative())
489 currentView = AnnotationColumnChooser.GRAPH_VIEW;
492 gSearchPanel.syncState();
493 gFurtherActionPanel.syncState();
494 gStructureFilterPanel.syncState();
496 ngSearchPanel.syncState();
497 ngFurtherActionPanel.syncState();
498 ngStructureFilterPanel.syncState();
500 CardLayout switchableViewsLayout = (CardLayout) switchableViewsPanel
502 switchableViewsLayout.show(switchableViewsPanel, currentView);
506 public class FurtherActionPanel extends JPanel
508 private AnnotationColumnChooser aColChooser;
510 private JRadioButton hideOption = new JRadioButton();
512 private JRadioButton selectOption = new JRadioButton();
514 private ButtonGroup optionsGroup = new ButtonGroup();
516 public FurtherActionPanel(AnnotationColumnChooser aColChooser)
518 this.aColChooser = aColChooser;
519 JvSwingUtils.jvInitComponent(selectOption, "action.select");
520 selectOption.addActionListener(new ActionListener()
523 public void actionPerformed(ActionEvent actionEvent)
525 selectRadioAction(actionEvent);
529 JvSwingUtils.jvInitComponent(hideOption, "action.hide");
530 hideOption.addActionListener(new ActionListener()
533 public void actionPerformed(ActionEvent actionEvent)
535 hideRadioAction(actionEvent);
539 optionsGroup.add(selectOption);
540 optionsGroup.add(hideOption);
541 optionsGroup.setSelected(selectOption.getModel(), true);
543 JvSwingUtils.jvInitComponent(this);
546 this.add(selectOption);
547 this.add(hideOption);
550 public void selectRadioAction(ActionEvent actionEvent)
552 aColChooser.setCurrentFutherActionPanel(this);
553 aColChooser.select_action(actionEvent);
556 public void hideRadioAction(ActionEvent actionEvent)
558 aColChooser.setCurrentFutherActionPanel(this);
559 aColChooser.hide_action(actionEvent);
562 public void syncState()
565 .getActionOption() == AnnotationColumnChooser.ACTION_OPTION_HIDE)
567 this.optionsGroup.setSelected(this.hideOption.getModel(), true);
571 this.optionsGroup.setSelected(this.selectOption.getModel(), true);
576 public class StructureFilterPanel extends JPanel
578 private AnnotationColumnChooser aColChooser;
580 private JCheckBox alphaHelix = new JCheckBox();
582 private JCheckBox betaStrand = new JCheckBox();
584 private JCheckBox turn = new JCheckBox();
586 private JCheckBox all = new JCheckBox();
588 public StructureFilterPanel(AnnotationColumnChooser aColChooser)
590 this.aColChooser = aColChooser;
592 JvSwingUtils.jvInitComponent(alphaHelix, "label.alpha_helix");
593 alphaHelix.addActionListener(new ActionListener()
596 public void actionPerformed(ActionEvent actionEvent)
598 alphaHelix_actionPerformed();
602 JvSwingUtils.jvInitComponent(betaStrand, "label.beta_strand");
603 betaStrand.addActionListener(new ActionListener()
606 public void actionPerformed(ActionEvent actionEvent)
608 betaStrand_actionPerformed();
612 JvSwingUtils.jvInitComponent(turn, "label.turn");
613 turn.addActionListener(new ActionListener()
616 public void actionPerformed(ActionEvent actionEvent)
618 turn_actionPerformed();
622 JvSwingUtils.jvInitComponent(all, "label.select_all");
623 all.addActionListener(new ActionListener()
626 public void actionPerformed(ActionEvent actionEvent)
628 all_actionPerformed();
632 this.setBorder(new TitledBorder(
633 MessageManager.getString("label.structures_filter")));
634 JvSwingUtils.jvInitComponent(this);
637 this.add(alphaHelix);
638 this.add(betaStrand);
642 public void alphaHelix_actionPerformed()
644 updateSelectAllState();
645 aColChooser.setCurrentStructureFilterPanel(this);
646 aColChooser.updateView();
649 public void betaStrand_actionPerformed()
651 updateSelectAllState();
652 aColChooser.setCurrentStructureFilterPanel(this);
653 aColChooser.updateView();
656 public void turn_actionPerformed()
658 updateSelectAllState();
659 aColChooser.setCurrentStructureFilterPanel(this);
660 aColChooser.updateView();
663 public void all_actionPerformed()
665 if (all.isSelected())
667 alphaHelix.setSelected(true);
668 betaStrand.setSelected(true);
669 turn.setSelected(true);
673 alphaHelix.setSelected(false);
674 betaStrand.setSelected(false);
675 turn.setSelected(false);
677 aColChooser.setCurrentStructureFilterPanel(this);
678 aColChooser.updateView();
681 public void updateSelectAllState()
683 if (alphaHelix.isSelected() && betaStrand.isSelected()
684 && turn.isSelected())
686 all.setSelected(true);
690 all.setSelected(false);
694 public void syncState()
696 StructureFilterPanel sfp = aColChooser
697 .getCurrentStructureFilterPanel();
700 alphaHelix.setSelected(sfp.alphaHelix.isSelected());
701 betaStrand.setSelected(sfp.betaStrand.isSelected());
702 turn.setSelected(sfp.turn.isSelected());
703 if (sfp.all.isSelected())
705 all.setSelected(true);
706 alphaHelix.setSelected(true);
707 betaStrand.setSelected(true);
708 turn.setSelected(true);
715 public class SearchPanel extends JPanel
717 private AnnotationColumnChooser aColChooser;
719 private JCheckBox displayName = new JCheckBox();
721 private JCheckBox description = new JCheckBox();
723 private static final String FILTER_BY_ANN_CACHE_KEY = "CACHE.SELECT_FILTER_BY_ANNOT";
725 public JvCacheableInputBox<String> searchBox = new JvCacheableInputBox<>(
726 FILTER_BY_ANN_CACHE_KEY);
728 public SearchPanel(AnnotationColumnChooser aColChooser)
731 this.aColChooser = aColChooser;
732 JvSwingUtils.jvInitComponent(this);
733 this.setBorder(new TitledBorder(
734 MessageManager.getString("label.search_filter")));
736 searchBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXX");
737 searchBox.getComponent().setToolTipText(
738 MessageManager.getString("info.enter_search_text_here"));
739 searchBox.addKeyListener(new java.awt.event.KeyAdapter()
742 public void keyPressed(KeyEvent e)
744 if (e.getKeyCode() == KeyEvent.VK_ENTER)
747 searchStringAction();
751 searchBox.addFocusListener(new FocusAdapter()
754 public void focusLost(FocusEvent e)
756 searchStringAction();
760 JvSwingUtils.jvInitComponent(displayName, "label.label");
761 displayName.addActionListener(new ActionListener()
764 public void actionPerformed(ActionEvent actionEvent)
766 displayNameCheckboxAction();
770 JvSwingUtils.jvInitComponent(description, "label.description");
771 description.addActionListener(new ActionListener()
774 public void actionPerformed(ActionEvent actionEvent)
776 descriptionCheckboxAction();
781 this.add(searchBox.getComponent());
782 this.add(displayName);
783 this.add(description);
786 public void displayNameCheckboxAction()
788 aColChooser.setCurrentSearchPanel(this);
789 aColChooser.updateView();
792 public void descriptionCheckboxAction()
794 aColChooser.setCurrentSearchPanel(this);
795 aColChooser.updateView();
798 public void searchStringAction()
800 aColChooser.setCurrentSearchPanel(this);
801 aColChooser.updateView();
802 updateSearchPanelToolTips();
803 searchBox.updateCache();
806 public void syncState()
808 SearchPanel sp = aColChooser.getCurrentSearchPanel();
811 description.setEnabled(sp.description.isEnabled());
812 description.setSelected(sp.description.isSelected());
814 displayName.setEnabled(sp.displayName.isEnabled());
815 displayName.setSelected(sp.displayName.isSelected());
817 searchBox.setSelectedItem(sp.searchBox.getUserInput());
819 updateSearchPanelToolTips();
822 public void updateSearchPanelToolTips()
824 String defaultTtip = MessageManager
825 .getString("info.enter_search_text_to_enable");
826 String labelTtip = MessageManager.formatMessage(
827 "info.search_in_annotation_label",
828 annotations.getSelectedItem().toString());
829 String descTtip = MessageManager.formatMessage(
830 "info.search_in_annotation_description",
831 annotations.getSelectedItem().toString());
832 displayName.setToolTipText(
833 displayName.isEnabled() ? labelTtip : defaultTtip);
834 description.setToolTipText(
835 description.isEnabled() ? descTtip : defaultTtip);
840 public void ok_actionPerformed()
843 super.ok_actionPerformed();
847 public void cancel_actionPerformed()
850 super.cancel_actionPerformed();
853 private void saveCache()
855 gSearchPanel.searchBox.persistCache();
856 ngSearchPanel.searchBox.persistCache();
857 gSearchPanel.searchBox.updateCache();
858 ngSearchPanel.searchBox.updateCache();