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;
39 import java.util.ArrayList;
41 import javax.swing.ButtonGroup;
42 import javax.swing.JCheckBox;
43 import javax.swing.JComboBox;
44 import javax.swing.JInternalFrame;
45 import javax.swing.JLayeredPane;
46 import javax.swing.JPanel;
47 import javax.swing.JRadioButton;
48 import javax.swing.border.TitledBorder;
50 import net.miginfocom.swing.MigLayout;
52 @SuppressWarnings("serial")
53 public class AnnotationColumnChooser extends AnnotationRowFilter
54 implements ItemListener
56 private JPanel switchableViewsPanel = new JPanel(new CardLayout());
58 private JPanel annotationComboBoxPanel = new JPanel();
60 private StructureFilterPanel gStructureFilterPanel;
62 private StructureFilterPanel ngStructureFilterPanel;
64 private StructureFilterPanel currentStructureFilterPanel;
66 private SearchPanel currentSearchPanel;
68 private SearchPanel gSearchPanel;
70 private SearchPanel ngSearchPanel;
72 private FurtherActionPanel currentFurtherActionPanel;
74 private FurtherActionPanel gFurtherActionPanel;
76 private FurtherActionPanel ngFurtherActionPanel;
78 public static final int ACTION_OPTION_SELECT = 1;
80 public static int ACTION_OPTION_HIDE = 2;
82 public static String NO_GRAPH_VIEW = "0";
84 public static String GRAPH_VIEW = "1";
86 private int actionOption = ACTION_OPTION_SELECT;
88 private HiddenColumns oldHiddenColumns;
90 protected int MIN_WIDTH = 420;
92 protected int MIN_HEIGHT = 430;
94 public AnnotationColumnChooser(AlignViewport av, final AlignmentPanel ap)
97 frame = new JInternalFrame();
98 frame.setContentPane(this);
99 frame.setLayer(JLayeredPane.PALETTE_LAYER);
100 Desktop.addInternalFrame(frame,
101 MessageManager.getString("label.select_by_annotation"), 520,
103 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
105 addSliderChangeListener();
106 addSliderMouseListeners();
108 if (av.getAlignment().getAlignmentAnnotation() == null)
112 setOldHiddenColumns(av.getAlignment().getHiddenColumns());
115 setAnnotations(new JComboBox<>(getAnnotationItems(false)));
116 populateThresholdComboBox(threshold);
117 AnnotationColumnChooser lastChooser = av
118 .getAnnotationColumnSelectionState();
119 // restore Object state from the previous session if one exists
120 if (lastChooser != null)
122 currentSearchPanel = lastChooser.getCurrentSearchPanel();
123 currentStructureFilterPanel = lastChooser
124 .getCurrentStructureFilterPanel();
125 annotations.setSelectedIndex(
126 lastChooser.getAnnotations().getSelectedIndex());
127 threshold.setSelectedIndex(
128 lastChooser.getThreshold().getSelectedIndex());
129 actionOption = lastChooser.getActionOption();
131 .setSelected(lastChooser.percentThreshold.isSelected());
137 } catch (Exception ex)
148 protected void jbInit()
152 JPanel thresholdPanel = new JPanel();
153 thresholdPanel.setBorder(new TitledBorder(
154 MessageManager.getString("label.threshold_filter")));
155 thresholdPanel.setBackground(Color.white);
156 thresholdPanel.setFont(JvSwingUtils.getLabelFont());
157 thresholdPanel.setLayout(new MigLayout("", "[left][right]", "[][]"));
159 percentThreshold.setBackground(Color.white);
160 percentThreshold.setFont(JvSwingUtils.getLabelFont());
162 JPanel actionPanel = new JPanel();
163 actionPanel.setBackground(Color.white);
164 actionPanel.setFont(JvSwingUtils.getLabelFont());
166 JPanel graphFilterView = new JPanel();
167 graphFilterView.setLayout(new MigLayout("", "[left][right]", "[][]"));
168 graphFilterView.setBackground(Color.white);
170 JPanel noGraphFilterView = new JPanel();
171 noGraphFilterView.setLayout(new MigLayout("", "[left][right]", "[][]"));
172 noGraphFilterView.setBackground(Color.white);
174 annotationComboBoxPanel.setBackground(Color.white);
175 annotationComboBoxPanel.setFont(JvSwingUtils.getLabelFont());
177 gSearchPanel = new SearchPanel(this);
178 ngSearchPanel = new SearchPanel(this);
179 gFurtherActionPanel = new FurtherActionPanel(this);
180 ngFurtherActionPanel = new FurtherActionPanel(this);
181 gStructureFilterPanel = new StructureFilterPanel(this);
182 ngStructureFilterPanel = new StructureFilterPanel(this);
184 thresholdPanel.add(getThreshold());
185 thresholdPanel.add(percentThreshold, "wrap");
186 thresholdPanel.add(slider, "grow");
187 thresholdPanel.add(thresholdValue, "span, wrap");
190 actionPanel.add(cancel);
192 graphFilterView.add(gSearchPanel, "grow, span, wrap");
193 graphFilterView.add(gStructureFilterPanel, "grow, span, wrap");
194 graphFilterView.add(thresholdPanel, "grow, span, wrap");
195 graphFilterView.add(gFurtherActionPanel);
197 noGraphFilterView.add(ngSearchPanel, "grow, span, wrap");
198 noGraphFilterView.add(ngStructureFilterPanel, "grow, span, wrap");
199 noGraphFilterView.add(ngFurtherActionPanel);
201 annotationComboBoxPanel.add(getAnnotations());
202 switchableViewsPanel.add(noGraphFilterView,
203 AnnotationColumnChooser.NO_GRAPH_VIEW);
204 switchableViewsPanel.add(graphFilterView,
205 AnnotationColumnChooser.GRAPH_VIEW);
207 this.setLayout(new BorderLayout());
208 this.add(annotationComboBoxPanel, java.awt.BorderLayout.PAGE_START);
209 this.add(switchableViewsPanel, java.awt.BorderLayout.CENTER);
210 this.add(actionPanel, java.awt.BorderLayout.SOUTH);
212 selectedAnnotationChanged();
213 updateThresholdPanelToolTip();
217 protected void updateThresholdPanelToolTip()
219 thresholdValue.setToolTipText("");
220 slider.setToolTipText("");
222 String defaultTtip = MessageManager
223 .getString("info.change_threshold_mode_to_enable");
225 String thresh = getThreshold().getSelectedItem().toString();
226 if (thresh.equalsIgnoreCase("No Threshold"))
228 thresholdValue.setToolTipText(defaultTtip);
229 slider.setToolTipText(defaultTtip);
234 protected void reset()
236 if (this.getOldHiddenColumns() != null)
238 av.getColumnSelection().clear();
240 if (av.getAnnotationColumnSelectionState() != null)
242 HiddenColumns oldHidden = av.getAnnotationColumnSelectionState()
243 .getOldHiddenColumns();
244 if (oldHidden != null)
246 ArrayList<int[]> regions = oldHidden.getHiddenColumnsCopy();
247 for (int[] positions : regions)
249 av.hideColumns(positions[0], positions[1]);
252 // TODO not clear why we need to hide all the columns (above) if we are
253 // going to copy the hidden columns over wholesale anyway
254 av.getAlignment().setHiddenColumns(oldHidden);
257 ap.paintAlignment(true, true);
262 public void valueChanged(boolean updateAllAnnotation)
264 if (slider.isEnabled())
266 getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
268 propagateSeqAssociatedThreshold(updateAllAnnotation,
269 getCurrentAnnotation());
270 ap.paintAlignment(false, false);
275 public void updateView()
277 // Check if combobox is still adjusting
283 AnnotationFilterParameter filterParams = new AnnotationFilterParameter();
285 setCurrentAnnotation(av.getAlignment()
286 .getAlignmentAnnotation()[annmap[getAnnotations()
287 .getSelectedIndex()]]);
289 int selectedThresholdItem = getSelectedThresholdItem(
290 getThreshold().getSelectedIndex());
292 slider.setEnabled(true);
293 thresholdValue.setEnabled(true);
294 percentThreshold.setEnabled(true);
296 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
298 slider.setEnabled(false);
299 thresholdValue.setEnabled(false);
300 thresholdValue.setText("");
301 percentThreshold.setEnabled(false);
302 // build filter params
304 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
306 if (getCurrentAnnotation().threshold == null)
308 getCurrentAnnotation().setThreshold(new jalview.datamodel.GraphLine(
309 (getCurrentAnnotation().graphMax
310 - getCurrentAnnotation().graphMin) / 2f,
311 "Threshold", Color.black));
315 float range = getCurrentAnnotation().graphMax * 1000
316 - getCurrentAnnotation().graphMin * 1000;
318 slider.setMinimum((int) (getCurrentAnnotation().graphMin * 1000));
319 slider.setMaximum((int) (getCurrentAnnotation().graphMax * 1000));
321 (int) (getCurrentAnnotation().threshold.value * 1000));
323 setThresholdValueText();
325 slider.setMajorTickSpacing((int) (range / 10f));
326 slider.setEnabled(true);
327 thresholdValue.setEnabled(true);
330 // build filter params
331 filterParams.setThresholdType(
332 AnnotationFilterParameter.ThresholdType.NO_THRESHOLD);
333 if (getCurrentAnnotation().isQuantitative())
336 .setThresholdValue(getCurrentAnnotation().threshold.value);
338 if (selectedThresholdItem == AnnotationColourGradient.ABOVE_THRESHOLD)
340 filterParams.setThresholdType(
341 AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD);
343 else if (selectedThresholdItem == AnnotationColourGradient.BELOW_THRESHOLD)
345 filterParams.setThresholdType(
346 AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD);
351 updateThresholdPanelToolTip();
352 if (currentStructureFilterPanel != null)
354 if (currentStructureFilterPanel.alphaHelix.isSelected())
356 filterParams.setFilterAlphaHelix(true);
358 if (currentStructureFilterPanel.betaStrand.isSelected())
360 filterParams.setFilterBetaSheet(true);
362 if (currentStructureFilterPanel.turn.isSelected())
364 filterParams.setFilterTurn(true);
368 if (currentSearchPanel != null)
370 if (!currentSearchPanel.searchBox.getUserInput().isEmpty())
372 filterParams.setRegexString(
373 currentSearchPanel.searchBox.getUserInput());
374 if (currentSearchPanel.displayName.isSelected())
376 filterParams.addRegexSearchField(
377 AnnotationFilterParameter.SearchableAnnotationField.DISPLAY_STRING);
379 if (currentSearchPanel.description.isSelected())
381 filterParams.addRegexSearchField(
382 AnnotationFilterParameter.SearchableAnnotationField.DESCRIPTION);
387 // show hidden columns here, before changing the column selection in
388 // filterAnnotations, because showing hidden columns has the side effect of
389 // adding them to the selection
390 av.showAllHiddenColumns();
391 av.getColumnSelection().filterAnnotations(
392 getCurrentAnnotation().annotations, filterParams);
394 boolean hideCols = getActionOption() == ACTION_OPTION_HIDE;
397 av.hideSelectedColumns();
402 av.setAnnotationColumnSelectionState(this);
403 // only update overview and structures if columns were hidden
404 ap.paintAlignment(hideCols, hideCols);
407 public HiddenColumns getOldHiddenColumns()
409 return oldHiddenColumns;
412 public void setOldHiddenColumns(HiddenColumns currentHiddenColumns)
414 if (currentHiddenColumns != null)
416 this.oldHiddenColumns = new HiddenColumns(currentHiddenColumns);
420 public FurtherActionPanel getCurrentFutherActionPanel()
422 return currentFurtherActionPanel;
425 public void setCurrentFutherActionPanel(
426 FurtherActionPanel currentFutherActionPanel)
428 this.currentFurtherActionPanel = currentFutherActionPanel;
431 public SearchPanel getCurrentSearchPanel()
433 return currentSearchPanel;
436 public void setCurrentSearchPanel(SearchPanel currentSearchPanel)
438 this.currentSearchPanel = currentSearchPanel;
441 public int getActionOption()
446 public void setActionOption(int actionOption)
448 this.actionOption = actionOption;
451 public StructureFilterPanel getCurrentStructureFilterPanel()
453 return currentStructureFilterPanel;
456 public void setCurrentStructureFilterPanel(
457 StructureFilterPanel currentStructureFilterPanel)
459 this.currentStructureFilterPanel = currentStructureFilterPanel;
462 public void select_action(ActionEvent actionEvent)
464 JRadioButton radioButton = (JRadioButton) actionEvent.getSource();
465 if (radioButton.isSelected())
467 setActionOption(ACTION_OPTION_SELECT);
472 public void hide_action(ActionEvent actionEvent)
474 JRadioButton radioButton = (JRadioButton) actionEvent.getSource();
475 if (radioButton.isSelected())
477 setActionOption(ACTION_OPTION_HIDE);
483 public void itemStateChanged(ItemEvent e)
485 selectedAnnotationChanged();
489 public void selectedAnnotationChanged()
491 String currentView = AnnotationColumnChooser.NO_GRAPH_VIEW;
492 if (av.getAlignment().getAlignmentAnnotation()[annmap[getAnnotations()
493 .getSelectedIndex()]].isQuantitative())
495 currentView = AnnotationColumnChooser.GRAPH_VIEW;
498 gSearchPanel.syncState();
499 gFurtherActionPanel.syncState();
500 gStructureFilterPanel.syncState();
502 ngSearchPanel.syncState();
503 ngFurtherActionPanel.syncState();
504 ngStructureFilterPanel.syncState();
506 CardLayout switchableViewsLayout = (CardLayout) switchableViewsPanel
508 switchableViewsLayout.show(switchableViewsPanel, currentView);
512 public class FurtherActionPanel extends JPanel
514 private AnnotationColumnChooser aColChooser;
516 private JRadioButton hideOption = new JRadioButton();
518 private JRadioButton selectOption = new JRadioButton();
520 private ButtonGroup optionsGroup = new ButtonGroup();
522 public FurtherActionPanel(AnnotationColumnChooser aColChooser)
524 this.aColChooser = aColChooser;
525 JvSwingUtils.jvInitComponent(selectOption, "action.select");
526 selectOption.addActionListener(new ActionListener()
529 public void actionPerformed(ActionEvent actionEvent)
531 selectRadioAction(actionEvent);
535 JvSwingUtils.jvInitComponent(hideOption, "action.hide");
536 hideOption.addActionListener(new ActionListener()
539 public void actionPerformed(ActionEvent actionEvent)
541 hideRadioAction(actionEvent);
545 optionsGroup.add(selectOption);
546 optionsGroup.add(hideOption);
547 optionsGroup.setSelected(selectOption.getModel(), true);
549 JvSwingUtils.jvInitComponent(this);
552 this.add(selectOption);
553 this.add(hideOption);
556 public void selectRadioAction(ActionEvent actionEvent)
558 aColChooser.setCurrentFutherActionPanel(this);
559 aColChooser.select_action(actionEvent);
562 public void hideRadioAction(ActionEvent actionEvent)
564 aColChooser.setCurrentFutherActionPanel(this);
565 aColChooser.hide_action(actionEvent);
568 public void syncState()
571 .getActionOption() == AnnotationColumnChooser.ACTION_OPTION_HIDE)
573 this.optionsGroup.setSelected(this.hideOption.getModel(), true);
577 this.optionsGroup.setSelected(this.selectOption.getModel(), true);
582 public class StructureFilterPanel extends JPanel
584 private AnnotationColumnChooser aColChooser;
586 private JCheckBox alphaHelix = new JCheckBox();
588 private JCheckBox betaStrand = new JCheckBox();
590 private JCheckBox turn = new JCheckBox();
592 private JCheckBox all = new JCheckBox();
594 public StructureFilterPanel(AnnotationColumnChooser aColChooser)
596 this.aColChooser = aColChooser;
598 JvSwingUtils.jvInitComponent(alphaHelix, "label.alpha_helix");
599 alphaHelix.addActionListener(new ActionListener()
602 public void actionPerformed(ActionEvent actionEvent)
604 alphaHelix_actionPerformed();
608 JvSwingUtils.jvInitComponent(betaStrand, "label.beta_strand");
609 betaStrand.addActionListener(new ActionListener()
612 public void actionPerformed(ActionEvent actionEvent)
614 betaStrand_actionPerformed();
618 JvSwingUtils.jvInitComponent(turn, "label.turn");
619 turn.addActionListener(new ActionListener()
622 public void actionPerformed(ActionEvent actionEvent)
624 turn_actionPerformed();
628 JvSwingUtils.jvInitComponent(all, "label.select_all");
629 all.addActionListener(new ActionListener()
632 public void actionPerformed(ActionEvent actionEvent)
634 all_actionPerformed();
638 this.setBorder(new TitledBorder(
639 MessageManager.getString("label.structures_filter")));
640 JvSwingUtils.jvInitComponent(this);
643 this.add(alphaHelix);
644 this.add(betaStrand);
648 public void alphaHelix_actionPerformed()
650 updateSelectAllState();
651 aColChooser.setCurrentStructureFilterPanel(this);
652 aColChooser.updateView();
655 public void betaStrand_actionPerformed()
657 updateSelectAllState();
658 aColChooser.setCurrentStructureFilterPanel(this);
659 aColChooser.updateView();
662 public void turn_actionPerformed()
664 updateSelectAllState();
665 aColChooser.setCurrentStructureFilterPanel(this);
666 aColChooser.updateView();
669 public void all_actionPerformed()
671 if (all.isSelected())
673 alphaHelix.setSelected(true);
674 betaStrand.setSelected(true);
675 turn.setSelected(true);
679 alphaHelix.setSelected(false);
680 betaStrand.setSelected(false);
681 turn.setSelected(false);
683 aColChooser.setCurrentStructureFilterPanel(this);
684 aColChooser.updateView();
687 public void updateSelectAllState()
689 if (alphaHelix.isSelected() && betaStrand.isSelected()
690 && turn.isSelected())
692 all.setSelected(true);
696 all.setSelected(false);
700 public void syncState()
702 StructureFilterPanel sfp = aColChooser
703 .getCurrentStructureFilterPanel();
706 alphaHelix.setSelected(sfp.alphaHelix.isSelected());
707 betaStrand.setSelected(sfp.betaStrand.isSelected());
708 turn.setSelected(sfp.turn.isSelected());
709 if (sfp.all.isSelected())
711 all.setSelected(true);
712 alphaHelix.setSelected(true);
713 betaStrand.setSelected(true);
714 turn.setSelected(true);
721 public class SearchPanel extends JPanel
723 private AnnotationColumnChooser aColChooser;
725 private JCheckBox displayName = new JCheckBox();
727 private JCheckBox description = new JCheckBox();
729 private static final String FILTER_BY_ANN_CACHE_KEY = "CACHE.SELECT_FILTER_BY_ANNOT";
731 public JvCacheableInputBox<String> searchBox = new JvCacheableInputBox<>(
732 FILTER_BY_ANN_CACHE_KEY);
734 public SearchPanel(AnnotationColumnChooser aColChooser)
737 this.aColChooser = aColChooser;
738 JvSwingUtils.jvInitComponent(this);
739 this.setBorder(new TitledBorder(
740 MessageManager.getString("label.search_filter")));
742 searchBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXX");
743 searchBox.setToolTipText(
744 MessageManager.getString("info.enter_search_text_here"));
745 searchBox.getEditor().getEditorComponent()
746 .addKeyListener(new java.awt.event.KeyAdapter()
749 public void keyPressed(KeyEvent e)
751 if (e.getKeyCode() == KeyEvent.VK_ENTER)
754 searchStringAction();
759 JvSwingUtils.jvInitComponent(displayName, "label.label");
760 displayName.addActionListener(new ActionListener()
763 public void actionPerformed(ActionEvent actionEvent)
765 displayNameCheckboxAction();
769 JvSwingUtils.jvInitComponent(description, "label.description");
770 description.addActionListener(new ActionListener()
773 public void actionPerformed(ActionEvent actionEvent)
775 discriptionCheckboxAction();
781 this.add(displayName);
782 this.add(description);
785 public void displayNameCheckboxAction()
787 aColChooser.setCurrentSearchPanel(this);
788 aColChooser.updateView();
791 public void discriptionCheckboxAction()
793 aColChooser.setCurrentSearchPanel(this);
794 aColChooser.updateView();
797 public void searchStringAction()
799 aColChooser.setCurrentSearchPanel(this);
800 aColChooser.updateView();
801 updateSearchPanelToolTips();
802 searchBox.updateCache();
805 public void syncState()
807 SearchPanel sp = aColChooser.getCurrentSearchPanel();
810 description.setEnabled(sp.description.isEnabled());
811 description.setSelected(sp.description.isSelected());
813 displayName.setEnabled(sp.displayName.isEnabled());
814 displayName.setSelected(sp.displayName.isSelected());
816 searchBox.setSelectedItem(sp.searchBox.getUserInput());
818 updateSearchPanelToolTips();
821 public void updateSearchPanelToolTips()
823 String defaultTtip = MessageManager
824 .getString("info.enter_search_text_to_enable");
825 String labelTtip = MessageManager.formatMessage(
826 "info.search_in_annotation_label",
827 annotations.getSelectedItem().toString());
828 String descTtip = MessageManager.formatMessage(
829 "info.search_in_annotation_description",
830 annotations.getSelectedItem().toString());
831 displayName.setToolTipText(
832 displayName.isEnabled() ? labelTtip : defaultTtip);
833 description.setToolTipText(
834 description.isEnabled() ? descTtip : defaultTtip);
839 public void ok_actionPerformed()
842 super.ok_actionPerformed();
846 public void cancel_actionPerformed()
849 super.cancel_actionPerformed();
852 private void saveCache()
854 gSearchPanel.searchBox.persistCache();
855 ngSearchPanel.searchBox.persistCache();
856 gSearchPanel.searchBox.updateCache();
857 ngSearchPanel.searchBox.updateCache();