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.
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.FeatureColourI;
26 import jalview.bin.Console;
27 import jalview.datamodel.GraphLine;
28 import jalview.datamodel.features.FeatureAttributes;
29 import jalview.datamodel.features.FeatureAttributes.Datatype;
30 import jalview.datamodel.features.FeatureMatcher;
31 import jalview.datamodel.features.FeatureMatcherI;
32 import jalview.datamodel.features.FeatureMatcherSet;
33 import jalview.datamodel.features.FeatureMatcherSetI;
34 import jalview.gui.JalviewColourChooser.ColourChooserListener;
35 import jalview.schemes.FeatureColour;
36 import jalview.util.ColorUtils;
37 import jalview.util.MessageManager;
38 import jalview.util.matcher.Condition;
40 import java.awt.BorderLayout;
41 import java.awt.Color;
42 import java.awt.Dimension;
43 import java.awt.FlowLayout;
44 import java.awt.GridLayout;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.awt.event.FocusAdapter;
48 import java.awt.event.FocusEvent;
49 import java.awt.event.ItemEvent;
50 import java.awt.event.ItemListener;
51 import java.awt.event.MouseAdapter;
52 import java.awt.event.MouseEvent;
53 import java.math.BigDecimal;
54 import java.math.MathContext;
55 import java.text.DecimalFormat;
56 import java.util.ArrayList;
57 import java.util.List;
59 import javax.swing.BorderFactory;
60 import javax.swing.BoxLayout;
61 import javax.swing.ButtonGroup;
62 import javax.swing.JButton;
63 import javax.swing.JCheckBox;
64 import javax.swing.JComboBox;
65 import javax.swing.JLabel;
66 import javax.swing.JPanel;
67 import javax.swing.JRadioButton;
68 import javax.swing.JTextField;
69 import javax.swing.border.EmptyBorder;
70 import javax.swing.border.LineBorder;
71 import javax.swing.event.ChangeEvent;
72 import javax.swing.event.ChangeListener;
75 * A dialog where the user can configure colour scheme, and any filters, for one
78 * (Was FeatureColourChooser prior to Jalview 1.11, renamed with the addition of
81 public class FeatureTypeSettings extends JalviewDialog
83 private final static MathContext FOUR_SIG_FIG = new MathContext(4);
85 private final static String LABEL_18N = MessageManager
86 .getString("label.label");
88 private final static String SCORE_18N = MessageManager
89 .getString("label.score");
91 private static final int RADIO_WIDTH = 130;
93 private static final String COLON = ":";
95 private static final int MAX_TOOLTIP_LENGTH = 50;
97 private static final int NO_COLOUR_OPTION = 0;
99 private static final int MIN_COLOUR_OPTION = 1;
101 private static final int MAX_COLOUR_OPTION = 2;
103 private static final int ABOVE_THRESHOLD_OPTION = 1;
105 private static final int BELOW_THRESHOLD_OPTION = 2;
107 private static final DecimalFormat DECFMT_2_2 = new DecimalFormat(
111 * FeatureRenderer holds colour scheme and filters for feature types
113 private final FeatureRenderer fr; // todo refactor to allow interface type
117 * the view panel to update when settings change
119 final AlignmentViewPanel ap;
121 final String featureType;
124 * the colour and filters to reset to on Cancel
126 private final FeatureColourI originalColour;
128 private final FeatureMatcherSetI originalFilter;
131 * set flag to true when setting values programmatically,
132 * to avoid invocation of action handlers
134 boolean adjusting = false;
137 * minimum of the value range for graduated colour
138 * (may be for feature score or for a numeric attribute)
143 * maximum of the value range for graduated colour
148 * radio button group, to select what to colour by:
149 * simple colour, by category (text), or graduated
151 JRadioButton simpleColour = new JRadioButton();
153 JRadioButton byCategory = new JRadioButton();
155 JRadioButton graduatedColour = new JRadioButton();
161 JPanel singleColour = new JPanel();
163 JPanel minColour = new JPanel();
165 JPanel maxColour = new JPanel();
167 private JComboBox<Object> threshold = new JComboBox<>();
169 private Slider slider;
171 JTextField thresholdValue = new JTextField(20);
173 private JCheckBox thresholdIsMin = new JCheckBox();
175 private GraphLine threshline;
177 private ActionListener featureSettings = null;
179 private ActionListener changeColourAction;
182 * choice of option for 'colour for no value'
184 private JComboBox<Object> noValueCombo;
187 * choice of what to colour by text (Label or attribute)
189 private JComboBox<Object> colourByTextCombo;
192 * choice of what to colour by range (Score or attribute)
194 private JComboBox<Object> colourByRangeCombo;
196 private JRadioButton andFilters;
198 private JRadioButton orFilters;
201 * filters for the currently selected feature type
203 List<FeatureMatcherI> filters;
205 private JPanel chooseFiltersPanel;
207 public boolean originalDisplay;
214 * @param last - visibility of feature prior to adjustment
216 public FeatureTypeSettings(FeatureRenderer frender, String theType, boolean last)
219 this.featureType = theType;
221 originalFilter = fr.getFeatureFilter(theType);
222 originalColour = fr.getFeatureColours().get(theType);
223 originalDisplay = last;
229 } catch (Exception ex)
231 ex.printStackTrace();
235 updateColoursPanel();
237 updateFiltersPanel();
241 colourChanged(false);
243 String title = MessageManager
244 .formatMessage("label.display_settings_for", new String[]
246 initDialogFrame(this, true, false, title, 580, 500);
251 * Configures the widgets on the Colours panel according to the current
252 * feature colour scheme
254 private void updateColoursPanel()
256 FeatureColourI fc = fr.getFeatureColours().get(featureType);
259 * suppress action handling while updating values programmatically
267 if (fc.isSimpleColour())
269 singleColour.setBackground(fc.getColour());
270 singleColour.setForeground(fc.getColour());
271 simpleColour.setSelected(true);
275 * colour by text (Label or attribute text)
277 if (fc.isColourByLabel())
279 byCategory.setSelected(true);
280 colourByTextCombo.setEnabled(colourByTextCombo.getItemCount() > 1);
281 if (fc.isColourByAttribute())
283 String[] attributeName = fc.getAttributeName();
284 colourByTextCombo.setSelectedItem(
285 FeatureMatcher.toAttributeDisplayName(attributeName));
289 colourByTextCombo.setSelectedItem(LABEL_18N);
294 colourByTextCombo.setEnabled(false);
297 if (!fc.isGraduatedColour())
299 colourByRangeCombo.setEnabled(false);
300 minColour.setEnabled(false);
301 maxColour.setEnabled(false);
302 noValueCombo.setEnabled(false);
303 threshold.setEnabled(false);
304 slider.setEnabled(false);
305 thresholdValue.setEnabled(false);
306 thresholdIsMin.setEnabled(false);
311 * Graduated colour, by score or attribute value range
313 graduatedColour.setSelected(true);
314 updateColourMinMax(); // ensure min, max are set
315 colourByRangeCombo.setEnabled(colourByRangeCombo.getItemCount() > 1);
316 minColour.setEnabled(true);
317 maxColour.setEnabled(true);
318 noValueCombo.setEnabled(true);
319 threshold.setEnabled(true);
320 minColour.setBackground(fc.getMinColour());
321 maxColour.setBackground(fc.getMaxColour());
323 if (fc.isColourByAttribute())
325 String[] attributeName = fc.getAttributeName();
326 colourByRangeCombo.setSelectedItem(
327 FeatureMatcher.toAttributeDisplayName(attributeName));
331 colourByRangeCombo.setSelectedItem(SCORE_18N);
333 Color noColour = fc.getNoColour();
334 if (noColour == null)
336 noValueCombo.setSelectedIndex(NO_COLOUR_OPTION);
338 else if (noColour.equals(fc.getMinColour()))
340 noValueCombo.setSelectedIndex(MIN_COLOUR_OPTION);
342 else if (noColour.equals(fc.getMaxColour()))
344 noValueCombo.setSelectedIndex(MAX_COLOUR_OPTION);
348 * update min-max scaling if there is a range to work with,
349 * else disable the widgets (this shouldn't happen if only
350 * valid options are offered in the combo box)
351 * offset slider to have only non-negative values if necessary (JAL-2983)
353 slider.setSliderModel(min, max, min);
354 slider.setMajorTickSpacing(
355 (int) ((slider.getMaximum() - slider.getMinimum()) / 10f));
357 threshline = new GraphLine((max - min) / 2f, "Threshold",
359 threshline.value = fc.getThreshold();
361 if (fc.hasThreshold())
363 threshold.setSelectedIndex(
364 fc.isAboveThreshold() ? ABOVE_THRESHOLD_OPTION
365 : BELOW_THRESHOLD_OPTION);
366 slider.setEnabled(true);
367 slider.setSliderValue(fc.getThreshold());
368 setThresholdValueText(fc.getThreshold());
369 thresholdValue.setEnabled(true);
370 thresholdIsMin.setEnabled(true);
374 slider.setEnabled(false);
375 thresholdValue.setEnabled(false);
376 thresholdIsMin.setEnabled(false);
378 thresholdIsMin.setSelected(!fc.isAutoScaled());
386 * Configures the initial layout
388 private void initialise()
390 this.setLayout(new BorderLayout());
393 * an ActionListener that applies colour changes
395 changeColourAction = new ActionListener()
398 public void actionPerformed(ActionEvent e)
405 * first panel: colour options
407 JPanel coloursPanel = initialiseColoursPanel();
408 this.add(coloursPanel, BorderLayout.NORTH);
411 * second panel: filter options
413 JPanel filtersPanel = initialiseFiltersPanel();
414 this.add(filtersPanel, BorderLayout.CENTER);
416 JPanel okCancelPanel = initialiseOkCancelPanel();
418 this.add(okCancelPanel, BorderLayout.SOUTH);
422 * Updates the min-max range if Colour By selected item is Score, or an
423 * attribute, with a min-max range
425 protected void updateColourMinMax()
427 if (!graduatedColour.isSelected())
432 String colourBy = (String) colourByRangeCombo.getSelectedItem();
433 float[] minMax = getMinMax(colourBy);
443 * Retrieves the min-max range:
445 * <li>of feature score, if colour or filter is by Score</li>
446 * <li>else of the selected attribute</li>
452 private float[] getMinMax(String attName)
454 float[] minMax = null;
455 if (SCORE_18N.equals(attName))
457 minMax = fr.getMinMax().get(featureType)[0];
461 // colour by attribute range
462 minMax = FeatureAttributes.getInstance().getMinMax(featureType,
463 FeatureMatcher.fromAttributeDisplayName(attName));
469 * Lay out fields for graduated colour (by score or attribute value)
473 private JPanel initialiseGraduatedColourPanel()
475 JPanel graduatedColourPanel = new JPanel();
476 graduatedColourPanel.setLayout(
477 new BoxLayout(graduatedColourPanel, BoxLayout.Y_AXIS));
478 JvSwingUtils.createTitledBorder(graduatedColourPanel,
479 MessageManager.getString("label.graduated_colour"), true);
480 graduatedColourPanel.setBackground(Color.white);
483 * first row: graduated colour radio button, score/attribute drop-down
485 JPanel graduatedChoicePanel = new JPanel(
486 new FlowLayout(FlowLayout.LEFT));
487 graduatedChoicePanel.setBackground(Color.white);
488 graduatedColour = new JRadioButton(
489 MessageManager.getString("label.by_range_of") + COLON);
490 graduatedColour.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
491 graduatedColour.setOpaque(false);
492 graduatedColour.addItemListener(new ItemListener()
495 public void itemStateChanged(ItemEvent e)
497 if (graduatedColour.isSelected())
503 graduatedChoicePanel.add(graduatedColour);
505 List<String[]> attNames = FeatureAttributes.getInstance()
506 .getAttributes(featureType);
507 colourByRangeCombo = populateAttributesDropdown(attNames, true, false);
508 colourByRangeCombo.addItemListener(new ItemListener()
511 public void itemStateChanged(ItemEvent e)
518 * disable graduated colour option if no range found
520 graduatedColour.setEnabled(colourByRangeCombo.getItemCount() > 0);
522 graduatedChoicePanel.add(colourByRangeCombo);
523 graduatedColourPanel.add(graduatedChoicePanel);
526 * second row - min/max/no colours
528 JPanel colourRangePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
529 colourRangePanel.setBackground(Color.white);
530 graduatedColourPanel.add(colourRangePanel);
532 minColour.setFont(JvSwingUtils.getLabelFont());
533 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
534 minColour.setPreferredSize(new Dimension(40, 20));
535 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
536 minColour.addMouseListener(new MouseAdapter()
539 public void mousePressed(MouseEvent e)
541 if (minColour.isEnabled())
543 String ttl = MessageManager
544 .getString("label.select_colour_minimum_value");
545 showColourChooser(minColour, ttl);
550 maxColour.setFont(JvSwingUtils.getLabelFont());
551 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
552 maxColour.setPreferredSize(new Dimension(40, 20));
553 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
554 maxColour.addMouseListener(new MouseAdapter()
557 public void mousePressed(MouseEvent e)
559 if (maxColour.isEnabled())
561 String ttl = MessageManager
562 .getString("label.select_colour_maximum_value");
563 showColourChooser(maxColour, ttl);
567 maxColour.setBorder(new LineBorder(Color.black));
570 * if not set, default max colour to last plain colour,
571 * and make min colour a pale version of max colour
573 Color max = originalColour.getMaxColour();
576 max = originalColour.getColour();
577 minColour.setBackground(ColorUtils.bleachColour(max, 0.9f));
581 maxColour.setBackground(max);
582 minColour.setBackground(originalColour.getMinColour());
585 noValueCombo = new JComboBox<>();
586 noValueCombo.addItem(MessageManager.getString("label.no_colour"));
587 noValueCombo.addItem(MessageManager.getString("label.min_colour"));
588 noValueCombo.addItem(MessageManager.getString("label.max_colour"));
589 noValueCombo.addItemListener(new ItemListener()
592 public void itemStateChanged(ItemEvent e)
598 JLabel minText = new JLabel(
599 MessageManager.getString("label.min_value") + COLON);
600 minText.setFont(JvSwingUtils.getLabelFont());
601 JLabel maxText = new JLabel(
602 MessageManager.getString("label.max_value") + COLON);
603 maxText.setFont(JvSwingUtils.getLabelFont());
604 JLabel noText = new JLabel(
605 MessageManager.getString("label.no_value") + COLON);
606 noText.setFont(JvSwingUtils.getLabelFont());
608 colourRangePanel.add(minText);
609 colourRangePanel.add(minColour);
610 colourRangePanel.add(maxText);
611 colourRangePanel.add(maxColour);
612 colourRangePanel.add(noText);
613 colourRangePanel.add(noValueCombo);
616 * third row - threshold options and value
618 JPanel thresholdPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
619 thresholdPanel.setBackground(Color.white);
620 graduatedColourPanel.add(thresholdPanel);
622 threshold.addActionListener(changeColourAction);
623 threshold.setToolTipText(MessageManager
624 .getString("label.threshold_feature_display_by_score"));
625 threshold.addItem(MessageManager
626 .getString("label.threshold_feature_no_threshold")); // index 0
627 threshold.addItem(MessageManager
628 .getString("label.threshold_feature_above_threshold")); // index 1
629 threshold.addItem(MessageManager
630 .getString("label.threshold_feature_below_threshold")); // index 2
632 thresholdValue.addActionListener(new ActionListener()
635 public void actionPerformed(ActionEvent e)
637 thresholdValue_actionPerformed();
640 thresholdValue.addFocusListener(new FocusAdapter()
643 public void focusLost(FocusEvent e)
645 thresholdValue_actionPerformed();
648 slider = new Slider(0f, 100f, 50f);
649 slider.setPaintLabels(false);
650 slider.setPaintTicks(true);
651 slider.setBackground(Color.white);
652 slider.setEnabled(false);
653 slider.setOpaque(false);
654 slider.setPreferredSize(new Dimension(100, 32));
655 slider.setToolTipText(
656 MessageManager.getString("label.adjust_threshold"));
658 slider.addChangeListener(new ChangeListener()
661 public void stateChanged(ChangeEvent evt)
665 setThresholdValueText(slider.getSliderValue());
666 thresholdValue.setBackground(Color.white); // to reset red for invalid
667 sliderValueChanged();
671 slider.addMouseListener(new MouseAdapter()
674 public void mouseReleased(MouseEvent evt)
677 * only update Overview and/or structure colouring
678 * when threshold slider drag ends (mouse up)
682 refreshDisplay(true);
687 thresholdValue.setEnabled(false);
688 thresholdValue.setColumns(7);
690 thresholdPanel.add(threshold);
691 thresholdPanel.add(slider);
692 thresholdPanel.add(thresholdValue);
694 thresholdIsMin.setBackground(Color.white);
696 .setText(MessageManager.getString("label.threshold_minmax"));
697 thresholdIsMin.setToolTipText(MessageManager
698 .getString("label.toggle_absolute_relative_display_threshold"));
699 thresholdIsMin.addActionListener(changeColourAction);
700 thresholdPanel.add(thresholdIsMin);
702 return graduatedColourPanel;
706 * Lay out OK and Cancel buttons
710 private JPanel initialiseOkCancelPanel()
712 JPanel okCancelPanel = new JPanel();
713 // okCancelPanel.setBackground(Color.white);
714 okCancelPanel.add(ok);
715 okCancelPanel.add(cancel);
716 return okCancelPanel;
720 * Lay out Colour options panel, containing
722 * <li>plain colour, with colour picker</li>
723 * <li>colour by text, with choice of Label or other attribute</li>
724 * <li>colour by range, of score or other attribute, when available</li>
729 private JPanel initialiseColoursPanel()
731 JPanel colourByPanel = new JPanel();
732 colourByPanel.setBackground(Color.white);
733 colourByPanel.setLayout(new BoxLayout(colourByPanel, BoxLayout.Y_AXIS));
734 JvSwingUtils.createTitledBorder(colourByPanel,
735 MessageManager.getString("action.colour"), true);
738 * simple colour radio button and colour picker
740 JPanel simpleColourPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
741 simpleColourPanel.setBackground(Color.white);
742 colourByPanel.add(simpleColourPanel);
744 simpleColour = new JRadioButton(
745 MessageManager.getString("label.simple_colour"));
746 simpleColour.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
747 simpleColour.setOpaque(false);
748 simpleColour.addItemListener(new ItemListener()
751 public void itemStateChanged(ItemEvent e)
753 if (simpleColour.isSelected() && !adjusting)
760 singleColour.setFont(JvSwingUtils.getLabelFont());
761 singleColour.setBorder(BorderFactory.createLineBorder(Color.black));
762 singleColour.setPreferredSize(new Dimension(40, 20));
763 // if (originalColour.isGraduatedColour())
765 // singleColour.setBackground(originalColour.getMaxColour());
766 // singleColour.setForeground(originalColour.getMaxColour());
770 singleColour.setBackground(originalColour.getColour());
771 singleColour.setForeground(originalColour.getColour());
773 singleColour.addMouseListener(new MouseAdapter()
776 public void mousePressed(MouseEvent e)
778 if (simpleColour.isSelected())
780 String ttl = MessageManager
781 .formatMessage("label.select_colour_for", featureType);
782 showColourChooser(singleColour, ttl);
786 simpleColourPanel.add(simpleColour); // radio button
787 simpleColourPanel.add(singleColour); // colour picker button
790 * colour by text (category) radio button and drop-down choice list
792 JPanel byTextPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
793 byTextPanel.setBackground(Color.white);
794 JvSwingUtils.createTitledBorder(byTextPanel,
795 MessageManager.getString("label.colour_by_text"), true);
796 colourByPanel.add(byTextPanel);
797 byCategory = new JRadioButton(
798 MessageManager.getString("label.by_text_of") + COLON);
799 byCategory.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
800 byCategory.setOpaque(false);
801 byCategory.addItemListener(new ItemListener()
804 public void itemStateChanged(ItemEvent e)
806 if (byCategory.isSelected())
812 byTextPanel.add(byCategory);
814 List<String[]> attNames = FeatureAttributes.getInstance()
815 .getAttributes(featureType);
816 colourByTextCombo = populateAttributesDropdown(attNames, false, true);
817 colourByTextCombo.addItemListener(new ItemListener()
820 public void itemStateChanged(ItemEvent e)
825 byTextPanel.add(colourByTextCombo);
828 * graduated colour panel
830 JPanel graduatedColourPanel = initialiseGraduatedColourPanel();
831 colourByPanel.add(graduatedColourPanel);
834 * 3 radio buttons select between simple colour,
835 * by category (text), or graduated
837 ButtonGroup bg = new ButtonGroup();
838 bg.add(simpleColour);
840 bg.add(graduatedColour);
842 return colourByPanel;
846 * Shows a colour chooser dialog, and if a selection is made, updates the
847 * colour of the given panel
850 * the panel whose background colour is being picked
853 void showColourChooser(JPanel colourPanel, String title)
855 ColourChooserListener listener = new ColourChooserListener()
858 public void colourSelected(Color col)
860 colourPanel.setBackground(col);
861 colourPanel.setForeground(col);
862 colourPanel.repaint();
866 JalviewColourChooser.showColourChooser(this, title,
867 colourPanel.getBackground(), listener);
871 * Constructs and sets the selected colour options as the colour for the
872 * feature type, and repaints the alignment, and optionally the Overview
873 * and/or structure viewer if open
875 * @param updateStructsAndOverview
877 void colourChanged(boolean updateStructsAndOverview)
882 * ignore action handlers while setting values programmatically
888 * ensure min-max range is for the latest choice of
889 * 'graduated colour by'
891 updateColourMinMax();
893 FeatureColourI acg = makeColourFromInputs();
896 * save the colour, and repaint stuff
898 fr.setColour(featureType, acg);
899 refreshDisplay(updateStructsAndOverview);
901 updateColoursPanel();
905 * Converts the input values into an instance of FeatureColour
909 private FeatureColourI makeColourFromInputs()
912 * min-max range is to (or from) threshold value if
913 * 'threshold is min/max' is selected
919 thresh = Float.valueOf(thresholdValue.getText());
920 } catch (NumberFormatException e)
922 // invalid inputs are already handled on entry
924 float minValue = min;
925 float maxValue = max;
926 int thresholdOption = threshold.getSelectedIndex();
927 if (thresholdIsMin.isSelected()
928 && thresholdOption == ABOVE_THRESHOLD_OPTION)
932 if (thresholdIsMin.isSelected()
933 && thresholdOption == BELOW_THRESHOLD_OPTION)
937 Color noColour = null;
938 if (noValueCombo.getSelectedIndex() == MIN_COLOUR_OPTION)
940 noColour = minColour.getBackground();
942 else if (noValueCombo.getSelectedIndex() == MAX_COLOUR_OPTION)
944 noColour = maxColour.getBackground();
948 * construct a colour that 'remembers' all the options, including
949 * those not currently selected
951 FeatureColourI fc = new FeatureColour(singleColour.getBackground(),
952 minColour.getBackground(), maxColour.getBackground(), noColour,
956 * easiest case - a single colour
958 if (simpleColour.isSelected())
960 ((FeatureColour) fc).setGraduatedColour(false);
965 * next easiest case - colour by Label, or attribute text
967 if (byCategory.isSelected())
969 fc.setColourByLabel(true);
970 String byWhat = (String) colourByTextCombo.getSelectedItem();
971 if (!LABEL_18N.equals(byWhat))
974 FeatureMatcher.fromAttributeDisplayName(byWhat));
980 * remaining case - graduated colour by score, or attribute value;
981 * set attribute to colour by if selected
983 String byWhat = (String) colourByRangeCombo.getSelectedItem();
984 if (!SCORE_18N.equals(byWhat))
986 fc.setAttributeName(FeatureMatcher.fromAttributeDisplayName(byWhat));
990 * set threshold options and 'autoscaled' which is
991 * false if 'threshold is min/max' is selected
992 * else true (colour range is on actual range of values)
994 fc.setThreshold(thresh);
995 fc.setAutoScaled(!thresholdIsMin.isSelected());
996 fc.setAboveThreshold(thresholdOption == ABOVE_THRESHOLD_OPTION);
997 fc.setBelowThreshold(thresholdOption == BELOW_THRESHOLD_OPTION);
999 if (threshline == null)
1002 * todo not yet implemented: visual indication of feature threshold
1004 threshline = new GraphLine((max - min) / 2f, "Threshold",
1012 protected void raiseClosed()
1014 if (this.featureSettings != null)
1016 featureSettings.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
1021 * Action on OK is just to dismiss the dialog - any changes have already been
1025 public void okPressed()
1030 * Action on Cancel is to restore colour scheme and filters as they were when
1031 * the dialog was opened
1034 public void cancelPressed()
1036 fr.setColour(featureType, originalColour);
1037 fr.setFeatureFilter(featureType, originalFilter);
1038 if (!originalDisplay) {
1039 fr.getFeaturesDisplayed().setHidden(featureType);
1041 refreshDisplay(true);
1045 * Action on text entry of a threshold value
1047 protected void thresholdValue_actionPerformed()
1052 * set 'adjusting' flag while moving the slider, so it
1053 * doesn't then in turn change the value (with rounding)
1056 float f = Float.parseFloat(thresholdValue.getText());
1057 f = Float.max(f, this.min);
1058 f = Float.min(f, this.max);
1059 setThresholdValueText(f);
1060 slider.setSliderValue(f);
1061 threshline.value = f;
1062 thresholdValue.setBackground(Color.white); // ok
1064 colourChanged(true);
1065 } catch (NumberFormatException ex)
1067 thresholdValue.setBackground(Color.red); // not ok
1073 * Sets the text field for threshold value, rounded to four significant
1078 void setThresholdValueText(float f)
1080 BigDecimal formatted = new BigDecimal(f).round(FOUR_SIG_FIG)
1081 .stripTrailingZeros();
1082 thresholdValue.setText(formatted.toPlainString());
1086 * Action on change of threshold slider value. This may be done interactively
1087 * (by moving the slider), or programmatically (to update the slider after
1088 * manual input of a threshold value).
1090 protected void sliderValueChanged()
1092 threshline.value = slider.getSliderValue();
1095 * repaint alignment, but not Overview or structure,
1096 * to avoid overload while dragging the slider
1098 colourChanged(false);
1101 void addActionListener(ActionListener listener)
1103 if (featureSettings != null)
1105 jalview.bin.Console.errPrintln(
1106 "IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
1108 featureSettings = listener;
1112 * A helper method to build the drop-down choice of attributes for a feature.
1113 * If 'withRange' is true, then Score, and any attributes with a min-max
1114 * range, are added. If 'withText' is true, Label and any known attributes are
1115 * added. This allows 'categorical numerical' attributes e.g. codon position
1116 * to be coloured by text.
1118 * Where metadata is available with a description for an attribute, that is
1119 * added as a tooltip.
1121 * Attribute names may be 'simple' e.g. "AC" or 'compound' e.g. {"CSQ",
1122 * "Allele"}. Compound names are rendered for display as (e.g.) CSQ:Allele.
1124 * This method does not add any ActionListener to the JComboBox.
1130 protected JComboBox<Object> populateAttributesDropdown(
1131 List<String[]> attNames, boolean withRange, boolean withText)
1133 List<String> displayAtts = new ArrayList<>();
1134 List<String> tooltips = new ArrayList<>();
1138 displayAtts.add(LABEL_18N);
1139 tooltips.add(MessageManager.getString("label.description"));
1143 float[][] minMax = fr.getMinMax().get(featureType);
1144 if (minMax != null && minMax[0][0] != minMax[0][1])
1146 displayAtts.add(SCORE_18N);
1147 tooltips.add(SCORE_18N);
1151 FeatureAttributes fa = FeatureAttributes.getInstance();
1152 for (String[] attName : attNames)
1154 float[] minMax = fa.getMinMax(featureType, attName);
1155 boolean hasRange = minMax != null && minMax[0] != minMax[1];
1156 if (!withText && !hasRange)
1160 displayAtts.add(FeatureMatcher.toAttributeDisplayName(attName));
1161 String desc = fa.getDescription(featureType, attName);
1162 if (desc != null && desc.length() > MAX_TOOLTIP_LENGTH)
1164 desc = desc.substring(0, MAX_TOOLTIP_LENGTH) + "...";
1166 tooltips.add(desc == null ? "" : desc);
1169 // now convert String List to Object List for buildComboWithTooltips
1170 List<Object> displayAttsObjects = new ArrayList<>(displayAtts);
1171 JComboBox<Object> attCombo = JvSwingUtils
1172 .buildComboWithTooltips(displayAttsObjects, tooltips);
1178 * Populates initial layout of the feature attribute filters panel
1180 private JPanel initialiseFiltersPanel()
1182 filters = new ArrayList<>();
1184 JPanel filtersPanel = new JPanel();
1185 filtersPanel.setLayout(new BoxLayout(filtersPanel, BoxLayout.Y_AXIS));
1186 filtersPanel.setBackground(Color.white);
1187 JvSwingUtils.createTitledBorder(filtersPanel,
1188 MessageManager.getString("label.filters"), true);
1190 JPanel andOrPanel = initialiseAndOrPanel();
1191 filtersPanel.add(andOrPanel);
1194 * panel with filters - populated by refreshFiltersDisplay,
1195 * which also sets the layout manager
1197 chooseFiltersPanel = new JPanel();
1198 chooseFiltersPanel.setBackground(Color.white);
1199 filtersPanel.add(chooseFiltersPanel);
1201 return filtersPanel;
1205 * Lays out the panel with radio buttons to AND or OR filter conditions
1209 private JPanel initialiseAndOrPanel()
1211 JPanel andOrPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
1212 andOrPanel.setBackground(Color.white);
1213 andFilters = new JRadioButton(MessageManager.getString("label.and"));
1214 orFilters = new JRadioButton(MessageManager.getString("label.or"));
1215 andFilters.setOpaque(false);
1216 orFilters.setOpaque(false);
1217 ActionListener actionListener = new ActionListener()
1220 public void actionPerformed(ActionEvent e)
1225 andFilters.addActionListener(actionListener);
1226 orFilters.addActionListener(actionListener);
1227 ButtonGroup andOr = new ButtonGroup();
1228 andOr.add(andFilters);
1229 andOr.add(orFilters);
1230 andFilters.setSelected(true);
1232 new JLabel(MessageManager.getString("label.join_conditions")));
1233 andOrPanel.add(andFilters);
1234 andOrPanel.add(orFilters);
1239 * Refreshes the display to show any filters currently configured for the
1240 * selected feature type (editable, with 'remove' option), plus one extra row
1241 * for adding a condition. This should be called after a filter has been
1242 * removed, added or amended.
1244 private void updateFiltersPanel()
1247 * clear the panel and list of filter conditions
1249 chooseFiltersPanel.removeAll();
1253 * look up attributes known for feature type
1255 List<String[]> attNames = FeatureAttributes.getInstance()
1256 .getAttributes(featureType);
1259 * if this feature type has filters set, load them first
1261 FeatureMatcherSetI featureFilters = fr.getFeatureFilter(featureType);
1262 if (featureFilters != null)
1264 if (!featureFilters.isAnded())
1266 orFilters.setSelected(true);
1268 // avoid use of lambda expression to keep SwingJS happy
1269 // featureFilters.getMatchers().forEach(item -> filters.add(item));
1270 for (FeatureMatcherI matcher : featureFilters.getMatchers())
1272 filters.add(matcher);
1277 * and an empty filter for the user to populate (add)
1279 filters.add(FeatureMatcher.NULL_MATCHER);
1282 * use GridLayout to 'justify' rows to the top of the panel, until
1283 * there are too many to fit in, then fall back on BoxLayout
1285 if (filters.size() <= 5)
1287 chooseFiltersPanel.setLayout(new GridLayout(5, 1));
1291 chooseFiltersPanel.setLayout(
1292 new BoxLayout(chooseFiltersPanel, BoxLayout.Y_AXIS));
1296 * render the conditions in rows, each in its own JPanel
1298 int filterIndex = 0;
1299 for (FeatureMatcherI filter : filters)
1301 JPanel row = addFilter(filter, attNames, filterIndex);
1302 chooseFiltersPanel.add(row);
1311 * A helper method that constructs a row (panel) with one filter condition:
1313 * <li>a drop-down list of Label, Score and attribute names to choose
1315 * <li>a drop-down list of conditions to choose from</li>
1316 * <li>a text field for input of a match pattern</li>
1317 * <li>optionally, a 'remove' button</li>
1319 * The filter values are set as defaults for the input fields. The 'remove'
1320 * button is added unless the pattern is empty (incomplete filter condition).
1322 * Action handlers on these fields provide for
1324 * <li>validate pattern field - should be numeric if condition is numeric</li>
1325 * <li>save filters and refresh display on any (valid) change</li>
1326 * <li>remove filter and refresh on 'Remove'</li>
1327 * <li>update conditions list on change of Label/Score/Attribute</li>
1328 * <li>refresh value field tooltip with min-max range on change of
1334 * @param filterIndex
1337 protected JPanel addFilter(FeatureMatcherI filter,
1338 List<String[]> attNames, int filterIndex)
1340 String[] attName = filter.getAttribute();
1341 Condition cond = filter.getMatcher().getCondition();
1342 String pattern = filter.getMatcher().getPattern();
1344 JPanel filterRow = new JPanel(new FlowLayout(FlowLayout.LEFT));
1345 filterRow.setBackground(Color.white);
1348 * drop-down choice of attribute, with description as a tooltip
1349 * if we can obtain it
1351 final JComboBox<Object> attCombo = populateAttributesDropdown(attNames,
1353 String filterBy = setSelectedAttribute(attCombo, filter);
1355 JComboBox<Condition> condCombo = new JComboBox<>();
1357 JTextField patternField = new JTextField(8);
1358 patternField.setText(pattern);
1361 * action handlers that validate and (if valid) apply changes
1363 ActionListener actionListener = new ActionListener()
1366 public void actionPerformed(ActionEvent e)
1368 if (validateFilter(patternField, condCombo))
1370 if (updateFilter(attCombo, condCombo, patternField, filterIndex))
1377 ItemListener itemListener = new ItemListener()
1380 public void itemStateChanged(ItemEvent e)
1382 actionListener.actionPerformed(null);
1386 if (filter == FeatureMatcher.NULL_MATCHER) // the 'add a condition' row
1388 attCombo.setSelectedIndex(0);
1392 attCombo.setSelectedItem(
1393 FeatureMatcher.toAttributeDisplayName(attName));
1395 attCombo.addItemListener(new ItemListener()
1398 public void itemStateChanged(ItemEvent e)
1401 * on change of attribute, refresh the conditions list to
1402 * ensure it is appropriate for the attribute datatype
1404 populateConditions((String) attCombo.getSelectedItem(),
1405 (Condition) condCombo.getSelectedItem(), condCombo,
1407 actionListener.actionPerformed(null);
1411 filterRow.add(attCombo);
1414 * drop-down choice of test condition
1416 populateConditions(filterBy, cond, condCombo, patternField);
1417 condCombo.setPreferredSize(new Dimension(150, 20));
1418 condCombo.addItemListener(itemListener);
1419 filterRow.add(condCombo);
1422 * pattern to match against
1424 patternField.addActionListener(actionListener);
1425 patternField.addFocusListener(new FocusAdapter()
1428 public void focusLost(FocusEvent e)
1430 actionListener.actionPerformed(null);
1433 filterRow.add(patternField);
1436 * disable pattern field for condition 'Present / NotPresent'
1438 Condition selectedCondition = (Condition) condCombo.getSelectedItem();
1439 patternField.setEnabled(selectedCondition.needsAPattern());
1442 * if a numeric condition is selected, show the value range
1443 * as a tooltip on the value input field
1445 setNumericHints(filterBy, selectedCondition, patternField);
1448 * add remove button if filter is populated (non-empty pattern)
1450 if (!patternField.isEnabled()
1451 || (pattern != null && pattern.trim().length() > 0))
1453 JButton removeCondition = new JButton("\u2717");
1454 // Dingbats cursive x
1455 removeCondition.setBorder(new EmptyBorder(0, 0, 0, 0));
1456 removeCondition.setBackground(Color.WHITE);
1457 removeCondition.setPreferredSize(new Dimension(23, 17));
1458 removeCondition.setToolTipText(
1459 MessageManager.getString("label.delete_condition"));
1460 removeCondition.addActionListener(new ActionListener()
1463 public void actionPerformed(ActionEvent e)
1465 filters.remove(filterIndex);
1469 filterRow.add(removeCondition);
1476 * Sets the selected item in the Label/Score/Attribute drop-down to match the
1482 private String setSelectedAttribute(JComboBox<Object> attCombo,
1483 FeatureMatcherI filter)
1486 if (filter.isByScore())
1490 else if (filter.isByLabel())
1496 item = FeatureMatcher.toAttributeDisplayName(filter.getAttribute());
1498 attCombo.setSelectedItem(item);
1503 * If a numeric comparison condition is selected, retrieves the min-max range
1504 * for the value (score or attribute), and sets it as a tooltip on the value
1505 * field. If the field is currently empty, then pre-populates it with
1507 * <li>the minimum value, if condition is > or >=</li>
1508 * <li>the maximum value, if condition is < or <=</li>
1512 * @param selectedCondition
1513 * @param patternField
1515 private void setNumericHints(String attName, Condition selectedCondition,
1516 JTextField patternField)
1518 patternField.setToolTipText("");
1520 if (selectedCondition.isNumeric())
1522 float[] minMax = getMinMax(attName);
1525 String minFormatted = DECFMT_2_2.format(minMax[0]);
1526 String maxFormatted = DECFMT_2_2.format(minMax[1]);
1527 String tip = String.format("(%s - %s)", minFormatted, maxFormatted);
1528 patternField.setToolTipText(tip);
1529 if (patternField.getText().isEmpty())
1531 if (selectedCondition == Condition.GE
1532 || selectedCondition == Condition.GT)
1534 patternField.setText(minFormatted);
1538 if (selectedCondition == Condition.LE
1539 || selectedCondition == Condition.LT)
1541 patternField.setText(maxFormatted);
1550 * Populates the drop-down list of comparison conditions for the given
1551 * attribute name. The conditions added depend on the datatype of the
1552 * attribute values. The supplied condition is set as the selected item in the
1553 * list, provided it is in the list. If the pattern is now invalid
1554 * (non-numeric pattern for a numeric condition), it is cleared.
1559 * @param patternField
1561 void populateConditions(String attName, Condition cond,
1562 JComboBox<Condition> condCombo, JTextField patternField)
1564 Datatype type = FeatureAttributes.getInstance().getDatatype(featureType,
1565 FeatureMatcher.fromAttributeDisplayName(attName));
1566 if (LABEL_18N.equals(attName))
1568 type = Datatype.Character;
1570 else if (SCORE_18N.equals(attName))
1572 type = Datatype.Number;
1576 * remove itemListener before starting
1578 ItemListener listener = condCombo.getItemListeners()[0];
1579 condCombo.removeItemListener(listener);
1580 boolean condIsValid = false;
1582 condCombo.removeAllItems();
1583 for (Condition c : Condition.values())
1585 if ((c.isNumeric() && type == Datatype.Number)
1586 || (!c.isNumeric() && type != Datatype.Number))
1588 condCombo.addItem(c);
1597 * set the selected condition (does nothing if not in the list)
1601 condCombo.setSelectedItem(cond);
1605 condCombo.setSelectedIndex(0);
1609 * clear pattern if it is now invalid for condition
1611 if (((Condition) condCombo.getSelectedItem()).isNumeric())
1615 String pattern = patternField.getText().trim();
1616 if (pattern.length() > 0)
1618 Float.valueOf(pattern);
1620 } catch (NumberFormatException e)
1622 patternField.setText("");
1627 * restore the listener
1629 condCombo.addItemListener(listener);
1633 * Answers true unless a numeric condition has been selected with a
1634 * non-numeric value. Sets the value field to RED with a tooltip if in error.
1636 * If the pattern is expected but is empty, this method returns false, but
1637 * does not mark the field as invalid. This supports selecting an attribute
1638 * for a new condition before a match pattern has been entered.
1643 protected boolean validateFilter(JTextField value,
1644 JComboBox<Condition> condCombo)
1646 if (value == null || condCombo == null)
1648 return true; // fields not populated
1651 Condition cond = (Condition) condCombo.getSelectedItem();
1652 if (!cond.needsAPattern())
1657 value.setBackground(Color.white);
1658 value.setToolTipText("");
1659 String v1 = value.getText().trim();
1660 if (v1.length() == 0)
1665 if (cond.isNumeric() && v1.length() > 0)
1670 } catch (NumberFormatException e)
1672 value.setBackground(Color.red);
1673 value.setToolTipText(
1674 MessageManager.getString("label.numeric_required"));
1683 * Constructs a filter condition from the given input fields, and replaces the
1684 * condition at filterIndex with the new one. Does nothing if the pattern
1685 * field is blank (unless the match condition is one that doesn't require a
1686 * pattern, e.g. 'Is present'). Answers true if the filter was updated, else
1689 * This method may update the tooltip on the filter value field to show the
1690 * value range, if a numeric condition is selected. This ensures the tooltip
1691 * is updated when a numeric valued attribute is chosen on the last 'add a
1697 * @param filterIndex
1699 protected boolean updateFilter(JComboBox<Object> attCombo,
1700 JComboBox<Condition> condCombo, JTextField valueField,
1706 attName = (String) attCombo.getSelectedItem();
1707 } catch (Exception e)
1709 Console.error("Problem casting Combo box entry to String");
1710 attName = attCombo.getSelectedItem().toString();
1712 Condition cond = (Condition) condCombo.getSelectedItem();
1713 String pattern = valueField.getText().trim();
1715 setNumericHints(attName, cond, valueField);
1717 if (pattern.length() == 0 && cond.needsAPattern())
1719 valueField.setEnabled(true); // ensure pattern field is enabled!
1724 * Construct a matcher that operates on Label, Score,
1725 * or named attribute
1727 FeatureMatcherI km = null;
1728 if (LABEL_18N.equals(attName))
1730 km = FeatureMatcher.byLabel(cond, pattern);
1732 else if (SCORE_18N.equals(attName))
1734 km = FeatureMatcher.byScore(cond, pattern);
1738 km = FeatureMatcher.byAttribute(cond, pattern,
1739 FeatureMatcher.fromAttributeDisplayName(attName));
1742 filters.set(filterIndex, km);
1748 * Action on any change to feature filtering, namely
1750 * <li>change of selected attribute</li>
1751 * <li>change of selected condition</li>
1752 * <li>change of match pattern</li>
1753 * <li>removal of a condition</li>
1755 * The inputs are parsed into a combined filter and this is set for the
1756 * feature type, and the alignment redrawn.
1758 protected void filtersChanged()
1761 * update the filter conditions for the feature type
1763 boolean anded = andFilters.isSelected();
1764 FeatureMatcherSetI combined = new FeatureMatcherSet();
1766 for (FeatureMatcherI filter : filters)
1768 String pattern = filter.getMatcher().getPattern();
1769 Condition condition = filter.getMatcher().getCondition();
1770 if (pattern.trim().length() > 0 || !condition.needsAPattern())
1774 combined.and(filter);
1778 combined.or(filter);
1784 * save the filter conditions in the FeatureRenderer
1785 * (note this might now be an empty filter with no conditions)
1787 fr.setFeatureFilter(featureType, combined.isEmpty() ? null : combined);
1788 refreshDisplay(true);
1790 updateFiltersPanel();
1794 * Repaints alignment, structure and overview (if shown). If there is a
1795 * complementary view which is showing this view's features, then also
1798 * @param updateStructsAndOverview
1800 void refreshDisplay(boolean updateStructsAndOverview)
1802 ap.paintAlignment(true, updateStructsAndOverview);
1803 AlignViewportI complement = ap.getAlignViewport().getCodingComplement();
1804 if (complement != null && complement.isShowComplementFeatures())
1806 AlignFrame af2 = Desktop.getAlignFrameFor(complement);
1807 af2.alignPanel.paintAlignment(true, updateStructsAndOverview);