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.AlignmentViewPanel;
24 import jalview.api.FeatureColourI;
25 import jalview.datamodel.GraphLine;
26 import jalview.datamodel.features.FeatureAttributes;
27 import jalview.datamodel.features.FeatureAttributes.Datatype;
28 import jalview.schemes.FeatureColour;
29 import jalview.util.ColorUtils;
30 import jalview.util.MessageManager;
31 import jalview.util.matcher.Condition;
32 import jalview.util.matcher.KeyedMatcher;
33 import jalview.util.matcher.KeyedMatcherI;
34 import jalview.util.matcher.KeyedMatcherSet;
35 import jalview.util.matcher.KeyedMatcherSetI;
37 import java.awt.BorderLayout;
38 import java.awt.Color;
39 import java.awt.Dimension;
40 import java.awt.FlowLayout;
41 import java.awt.LayoutManager;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.awt.event.FocusAdapter;
45 import java.awt.event.FocusEvent;
46 import java.awt.event.ItemEvent;
47 import java.awt.event.ItemListener;
48 import java.awt.event.MouseAdapter;
49 import java.awt.event.MouseEvent;
50 import java.text.DecimalFormat;
51 import java.util.ArrayList;
52 import java.util.List;
54 import javax.swing.BorderFactory;
55 import javax.swing.BoxLayout;
56 import javax.swing.ButtonGroup;
57 import javax.swing.JButton;
58 import javax.swing.JCheckBox;
59 import javax.swing.JColorChooser;
60 import javax.swing.JComboBox;
61 import javax.swing.JLabel;
62 import javax.swing.JPanel;
63 import javax.swing.JRadioButton;
64 import javax.swing.JSlider;
65 import javax.swing.JTabbedPane;
66 import javax.swing.JTextField;
67 import javax.swing.SwingConstants;
68 import javax.swing.border.LineBorder;
69 import javax.swing.event.ChangeEvent;
70 import javax.swing.event.ChangeListener;
71 import javax.swing.plaf.basic.BasicArrowButton;
74 * A dialog where the user can configure colour scheme, and any filters, for one
77 * (Was FeatureColourChooser prior to Jalview 1.11, renamed with the addition of
80 public class FeatureTypeSettings extends JalviewDialog
82 private static final int RADIO_WIDTH = 130;
84 private static final String COLON = ":";
86 private static final int MAX_TOOLTIP_LENGTH = 50;
88 private static final int NO_COLOUR_OPTION = 0;
90 private static final int MIN_COLOUR_OPTION = 1;
92 private static final int MAX_COLOUR_OPTION = 2;
94 private static final int ABOVE_THRESHOLD_OPTION = 1;
96 private static final int BELOW_THRESHOLD_OPTION = 2;
98 private static final DecimalFormat DECFMT_2_2 = new DecimalFormat(
102 * FeatureRenderer holds colour scheme and filters for feature types
104 private final FeatureRenderer fr; // todo refactor to allow interface type here
107 * the view panel to update when settings change
109 private final AlignmentViewPanel ap;
111 private final String featureType;
114 * the colour and filters to reset to on Cancel
116 private final FeatureColourI originalColour;
118 private final KeyedMatcherSetI originalFilter;
121 * set flag to true when setting values programmatically,
122 * to avoid invocation of action handlers
124 private boolean adjusting = false;
130 private float scaleFactor;
133 * radio button group, to select what to colour by:
134 * simple colour, by category (text), or graduated
136 private JRadioButton simpleColour = new JRadioButton();
138 private JRadioButton byCategory = new JRadioButton();
140 private JRadioButton graduatedColour = new JRadioButton();
142 private JPanel singleColour = new JPanel();
144 private JPanel minColour = new JPanel();
146 private JPanel maxColour = new JPanel();
148 private JComboBox<String> threshold = new JComboBox<>();
150 private JSlider slider = new JSlider();
152 private JTextField thresholdValue = new JTextField(20);
154 private JCheckBox thresholdIsMin = new JCheckBox();
156 private GraphLine threshline;
158 private ActionListener featureSettings = null;
160 private ActionListener changeColourAction;
163 * choice of option for 'colour for no value'
165 private JComboBox<String> noValueCombo;
168 * choice of what to colour by text (Label or attribute)
170 private JComboBox<String> colourByTextCombo;
173 * choice of what to colour by range (Score or attribute)
175 private JComboBox<String> colourByRangeCombo;
177 private JRadioButton andFilters;
179 private JRadioButton orFilters;
182 * filters for the currently selected feature type
184 private List<KeyedMatcherI> filters;
186 // set white normally, black to debug layout
187 private Color debugBorderColour = Color.white;
189 private JPanel chooseFiltersPanel;
191 private JTabbedPane tabbedPane;
199 public FeatureTypeSettings(FeatureRenderer frender, String theType)
201 this(frender, false, theType);
205 * Constructor, with option to make a blocking dialog (has to complete in the
206 * AWT event queue thread). Currently this option is always set to false.
212 FeatureTypeSettings(FeatureRenderer frender, boolean blocking,
216 this.featureType = theType;
218 originalFilter = fr.getFeatureFilter(theType);
219 originalColour = fr.getFeatureColours().get(theType);
226 } catch (Exception ex)
228 ex.printStackTrace();
238 colourChanged(false);
240 String title = MessageManager
241 .formatMessage("label.display_settings_for", new String[]
243 initDialogFrame(this, true, blocking, title, 600, 360);
249 * Configures the widgets on the Colours tab according to the current feature
252 private void updateColoursTab()
254 FeatureColourI fc = fr.getFeatureColours().get(featureType);
257 * suppress action handling while updating values programmatically
265 if (fc.isSimpleColour())
267 simpleColour.setSelected(true);
268 singleColour.setBackground(fc.getColour());
269 singleColour.setForeground(fc.getColour());
273 * colour by text (Label or attribute text)
275 if (fc.isColourByLabel())
277 byCategory.setSelected(true);
278 colourByTextCombo.setEnabled(colourByTextCombo.getItemCount() > 1);
279 if (fc.isColourByAttribute())
281 String[] attributeName = fc.getAttributeName();
283 .setSelectedItem(toAttributeDisplayName(attributeName));
288 .setSelectedItem(MessageManager.getString("label.label"));
293 colourByTextCombo.setEnabled(false);
296 if (!fc.isGraduatedColour())
298 colourByRangeCombo.setEnabled(false);
299 minColour.setEnabled(false);
300 maxColour.setEnabled(false);
301 noValueCombo.setEnabled(false);
302 threshold.setEnabled(false);
303 slider.setEnabled(false);
304 thresholdValue.setEnabled(false);
305 thresholdIsMin.setEnabled(false);
310 * Graduated colour, by score or attribute value range
312 graduatedColour.setSelected(true);
313 colourByRangeCombo.setEnabled(colourByRangeCombo.getItemCount() > 1);
314 minColour.setEnabled(true);
315 maxColour.setEnabled(true);
316 noValueCombo.setEnabled(true);
317 threshold.setEnabled(true);
318 minColour.setBackground(fc.getMinColour());
319 maxColour.setBackground(fc.getMaxColour());
321 if (fc.isColourByAttribute())
323 String[] attributeName = fc.getAttributeName();
325 .setSelectedItem(toAttributeDisplayName(attributeName));
330 .setSelectedItem(MessageManager.getString("label.score"));
332 Color noColour = fc.getNoColour();
333 if (noColour == null)
335 noValueCombo.setSelectedIndex(NO_COLOUR_OPTION);
337 else if (noColour.equals(fc.getMinColour()))
339 noValueCombo.setSelectedIndex(MIN_COLOUR_OPTION);
341 else if (noColour.equals(fc.getMaxColour()))
343 noValueCombo.setSelectedIndex(MAX_COLOUR_OPTION);
347 * update min-max scaling if there is a range to work with,
348 * else disable the widgets (this shouldn't happen if only
349 * valid options are offered in the combo box)
351 scaleFactor = (max == min) ? 1f : 100f / (max - min);
352 float range = (max - min) * scaleFactor;
353 slider.setMinimum((int) (min * scaleFactor));
354 slider.setMaximum((int) (max * scaleFactor));
355 slider.setMajorTickSpacing((int) (range / 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.setValue((int) (fc.getThreshold() * scaleFactor));
368 thresholdValue.setText(String.valueOf(getRoundedSliderValue()));
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());
391 tabbedPane = new JTabbedPane();
392 this.add(tabbedPane, BorderLayout.CENTER);
395 * an ActionListener that applies colour changes
397 changeColourAction = new ActionListener()
400 public void actionPerformed(ActionEvent e)
407 * first tab: colour options
409 JPanel coloursPanel = initialiseColoursPanel();
410 tabbedPane.addTab(MessageManager.getString("action.colour"),
414 * second tab: filter options
416 JPanel filtersPanel = initialiseFiltersPanel();
417 tabbedPane.addTab(MessageManager.getString("label.filters"),
420 JPanel okCancelPanel = initialiseOkCancelPanel();
422 this.add(okCancelPanel, BorderLayout.SOUTH);
426 * Updates the min-max range if Colour By selected item is Score, or an
427 * attribute, with a min-max range
429 protected void updateMinMax()
431 if (!graduatedColour.isSelected())
436 float[] minMax = null;
437 String colourBy = (String) colourByRangeCombo.getSelectedItem();
438 if (MessageManager.getString("label.score").equals(colourBy))
440 minMax = fr.getMinMax().get(featureType)[0];
444 // colour by attribute range
445 String[] attNames = fromAttributeDisplayName(colourBy);
446 minMax = FeatureAttributes.getInstance().getMinMax(featureType,
458 * Lay out fields for graduated colour (by score or attribute value)
462 private JPanel initialiseGraduatedColourPanel()
464 JPanel graduatedColourPanel = new JPanel();
465 graduatedColourPanel.setLayout(
466 new BoxLayout(graduatedColourPanel, BoxLayout.Y_AXIS));
467 JvSwingUtils.createTitledBorder(graduatedColourPanel,
468 MessageManager.getString("label.graduated_colour"), true);
469 graduatedColourPanel.setBackground(Color.white);
472 * first row: graduated colour radio button, score/attribute drop-down
474 JPanel graduatedChoicePanel = new JPanel(
475 new FlowLayout(FlowLayout.LEFT));
476 graduatedChoicePanel.setBackground(Color.white);
477 graduatedColour = new JRadioButton(
478 MessageManager.getString("label.by_range_of") + COLON);
479 graduatedColour.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
480 graduatedColour.addItemListener(new ItemListener()
483 public void itemStateChanged(ItemEvent e)
485 if (graduatedColour.isSelected())
491 graduatedChoicePanel.add(graduatedColour);
493 List<String[]> attNames = FeatureAttributes.getInstance()
494 .getAttributes(featureType);
495 colourByRangeCombo = populateAttributesDropdown(attNames, true, false);
496 colourByRangeCombo.addItemListener(new ItemListener()
499 public void itemStateChanged(ItemEvent e)
506 * disable graduated colour option if no range found
508 graduatedColour.setEnabled(colourByRangeCombo.getItemCount() > 0);
510 graduatedChoicePanel.add(colourByRangeCombo);
511 graduatedColourPanel.add(graduatedChoicePanel);
514 * second row - min/max/no colours
516 JPanel colourRangePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
517 colourRangePanel.setBackground(Color.white);
518 graduatedColourPanel.add(colourRangePanel);
520 minColour.setFont(JvSwingUtils.getLabelFont());
521 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
522 minColour.setPreferredSize(new Dimension(40, 20));
523 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
524 minColour.addMouseListener(new MouseAdapter()
527 public void mousePressed(MouseEvent e)
529 if (minColour.isEnabled())
531 showColourChooser(minColour, "label.select_colour_minimum_value");
536 maxColour.setFont(JvSwingUtils.getLabelFont());
537 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
538 maxColour.setPreferredSize(new Dimension(40, 20));
539 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
540 maxColour.addMouseListener(new MouseAdapter()
543 public void mousePressed(MouseEvent e)
545 if (maxColour.isEnabled())
547 showColourChooser(maxColour, "label.select_colour_maximum_value");
551 maxColour.setBorder(new LineBorder(Color.black));
554 * default max colour to current colour (if a plain colour),
555 * or to Black if colour by label; make min colour a pale
556 * version of max colour
558 FeatureColourI fc = fr.getFeatureColours().get(featureType);
559 Color bg = fc.isSimpleColour() ? fc.getColour() : Color.BLACK;
560 maxColour.setBackground(bg);
561 minColour.setBackground(ColorUtils.bleachColour(bg, 0.9f));
563 noValueCombo = new JComboBox<>();
564 noValueCombo.addItem(MessageManager.getString("label.no_colour"));
565 noValueCombo.addItem(MessageManager.getString("label.min_colour"));
566 noValueCombo.addItem(MessageManager.getString("label.max_colour"));
567 noValueCombo.addItemListener(new ItemListener()
570 public void itemStateChanged(ItemEvent e)
576 JLabel minText = new JLabel(
577 MessageManager.getString("label.min_value") + COLON);
578 minText.setFont(JvSwingUtils.getLabelFont());
579 JLabel maxText = new JLabel(
580 MessageManager.getString("label.max_value") + COLON);
581 maxText.setFont(JvSwingUtils.getLabelFont());
582 JLabel noText = new JLabel(
583 MessageManager.getString("label.no_value") + COLON);
584 noText.setFont(JvSwingUtils.getLabelFont());
586 colourRangePanel.add(minText);
587 colourRangePanel.add(minColour);
588 colourRangePanel.add(maxText);
589 colourRangePanel.add(maxColour);
590 colourRangePanel.add(noText);
591 colourRangePanel.add(noValueCombo);
594 * third row - threshold options and value
596 JPanel thresholdPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
597 thresholdPanel.setBackground(Color.white);
598 graduatedColourPanel.add(thresholdPanel);
600 threshold.addActionListener(changeColourAction);
601 threshold.setToolTipText(MessageManager
602 .getString("label.threshold_feature_display_by_score"));
603 threshold.addItem(MessageManager
604 .getString("label.threshold_feature_no_threshold")); // index 0
605 threshold.addItem(MessageManager
606 .getString("label.threshold_feature_above_threshold")); // index 1
607 threshold.addItem(MessageManager
608 .getString("label.threshold_feature_below_threshold")); // index 2
610 thresholdValue.addActionListener(new ActionListener()
613 public void actionPerformed(ActionEvent e)
615 thresholdValue_actionPerformed();
618 thresholdValue.addFocusListener(new FocusAdapter()
621 public void focusLost(FocusEvent e)
623 thresholdValue_actionPerformed();
626 slider.setPaintLabels(false);
627 slider.setPaintTicks(true);
628 slider.setBackground(Color.white);
629 slider.setEnabled(false);
630 slider.setOpaque(false);
631 slider.setPreferredSize(new Dimension(100, 32));
632 slider.setToolTipText(
633 MessageManager.getString("label.adjust_threshold"));
635 slider.addChangeListener(new ChangeListener()
638 public void stateChanged(ChangeEvent evt)
643 .setText(String.valueOf(slider.getValue() / scaleFactor));
644 sliderValueChanged();
648 slider.addMouseListener(new MouseAdapter()
651 public void mouseReleased(MouseEvent evt)
654 * only update Overview and/or structure colouring
655 * when threshold slider drag ends (mouse up)
659 ap.paintAlignment(true, true);
664 thresholdValue.setEnabled(false);
665 thresholdValue.setColumns(7);
667 thresholdPanel.add(threshold);
668 thresholdPanel.add(slider);
669 thresholdPanel.add(thresholdValue);
671 thresholdIsMin.setBackground(Color.white);
673 .setText(MessageManager.getString("label.threshold_minmax"));
674 thresholdIsMin.setToolTipText(MessageManager
675 .getString("label.toggle_absolute_relative_display_threshold"));
676 thresholdIsMin.addActionListener(changeColourAction);
677 thresholdPanel.add(thresholdIsMin);
679 return graduatedColourPanel;
683 * Lay out OK and Cancel buttons
687 private JPanel initialiseOkCancelPanel()
689 JPanel okCancelPanel = new JPanel();
690 // okCancelPanel.setBackground(Color.white);
691 okCancelPanel.add(ok);
692 okCancelPanel.add(cancel);
693 return okCancelPanel;
697 * Lay out Colour options panel, containing
699 * <li>plain colour, with colour picker</li>
700 * <li>colour by text, with choice of Label or other attribute</li>
701 * <li>colour by range, of score or other attribute, when available</li>
706 private JPanel initialiseColoursPanel()
708 JPanel colourByPanel = new JPanel();
709 colourByPanel.setLayout(new BoxLayout(colourByPanel, BoxLayout.Y_AXIS));
712 * simple colour radio button and colour picker
714 JPanel simpleColourPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
715 simpleColourPanel.setBackground(Color.white);
716 JvSwingUtils.createTitledBorder(simpleColourPanel,
717 MessageManager.getString("label.simple"), true);
718 colourByPanel.add(simpleColourPanel);
720 simpleColour = new JRadioButton(
721 MessageManager.getString("label.simple_colour"));
722 simpleColour.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
723 simpleColour.addItemListener(new ItemListener()
726 public void itemStateChanged(ItemEvent e)
728 if (simpleColour.isSelected() && !adjusting)
730 showColourChooser(singleColour, "label.select_colour");
736 singleColour.setFont(JvSwingUtils.getLabelFont());
737 singleColour.setBorder(BorderFactory.createLineBorder(Color.black));
738 singleColour.setPreferredSize(new Dimension(40, 20));
739 singleColour.addMouseListener(new MouseAdapter()
742 public void mousePressed(MouseEvent e)
744 if (simpleColour.isSelected())
746 showColourChooser(singleColour, "label.select_colour");
750 simpleColourPanel.add(simpleColour); // radio button
751 simpleColourPanel.add(singleColour); // colour picker button
754 * colour by text (category) radio button and drop-down choice list
756 JPanel byTextPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
757 byTextPanel.setBackground(Color.white);
758 JvSwingUtils.createTitledBorder(byTextPanel,
759 MessageManager.getString("label.colour_by_text"), true);
760 colourByPanel.add(byTextPanel);
761 byCategory = new JRadioButton(
762 MessageManager.getString("label.by_text_of") + COLON);
763 byCategory.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
764 byCategory.addItemListener(new ItemListener()
767 public void itemStateChanged(ItemEvent e)
769 if (byCategory.isSelected())
775 byTextPanel.add(byCategory);
777 List<String[]> attNames = FeatureAttributes.getInstance()
778 .getAttributes(featureType);
779 colourByTextCombo = populateAttributesDropdown(attNames, false, true);
780 colourByTextCombo.addItemListener(new ItemListener()
783 public void itemStateChanged(ItemEvent e)
788 byTextPanel.add(colourByTextCombo);
791 * graduated colour panel
793 JPanel graduatedColourPanel = initialiseGraduatedColourPanel();
794 colourByPanel.add(graduatedColourPanel);
797 * 3 radio buttons select between simple colour,
798 * by category (text), or graduated
800 ButtonGroup bg = new ButtonGroup();
801 bg.add(simpleColour);
803 bg.add(graduatedColour);
805 return colourByPanel;
808 private void showColourChooser(JPanel colourPanel, String key)
810 Color col = JColorChooser.showDialog(this,
811 MessageManager.getString(key), colourPanel.getBackground());
814 colourPanel.setBackground(col);
815 colourPanel.setForeground(col);
817 colourPanel.repaint();
822 * Constructs and sets the selected colour options as the colour for the feature
823 * type, and repaints the alignment, and optionally the Overview and/or
824 * structure viewer if open
826 * @param updateStructsAndOverview
828 void colourChanged(boolean updateStructsAndOverview)
833 * ignore action handlers while setting values programmatically
839 * ensure min-max range is for the latest choice of
840 * 'graduated colour by'
844 FeatureColourI acg = makeColourFromInputs();
847 * save the colour, and repaint stuff
849 fr.setColour(featureType, acg);
850 ap.paintAlignment(updateStructsAndOverview, updateStructsAndOverview);
856 * Converts the input values into an instance of FeatureColour
860 private FeatureColourI makeColourFromInputs()
863 * easiest case - a single colour
865 if (simpleColour.isSelected())
867 return new FeatureColour(singleColour.getBackground());
871 * next easiest case - colour by Label, or attribute text
873 if (byCategory.isSelected())
875 Color c = this.getBackground();
876 FeatureColourI fc = new FeatureColour(c, c, null, 0f, 0f);
877 fc.setColourByLabel(true);
878 String byWhat = (String) colourByTextCombo.getSelectedItem();
879 if (!MessageManager.getString("label.label").equals(byWhat))
881 fc.setAttributeName(fromAttributeDisplayName(byWhat));
887 * remaining case - graduated colour by score, or attribute value
889 Color noColour = null;
890 if (noValueCombo.getSelectedIndex() == MIN_COLOUR_OPTION)
892 noColour = minColour.getBackground();
894 else if (noValueCombo.getSelectedIndex() == MAX_COLOUR_OPTION)
896 noColour = maxColour.getBackground();
902 thresh = Float.valueOf(thresholdValue.getText());
903 } catch (NumberFormatException e)
905 // invalid inputs are already handled on entry
909 * min-max range is to (or from) threshold value if
910 * 'threshold is min/max' is selected
912 float minValue = min;
913 float maxValue = max;
914 final int thresholdOption = threshold.getSelectedIndex();
915 if (thresholdIsMin.isSelected()
916 && thresholdOption == ABOVE_THRESHOLD_OPTION)
920 if (thresholdIsMin.isSelected()
921 && thresholdOption == BELOW_THRESHOLD_OPTION)
927 * make the graduated colour
929 FeatureColourI fc = new FeatureColour(minColour.getBackground(),
930 maxColour.getBackground(), noColour, minValue, maxValue);
933 * set attribute to colour by if selected
935 String byWhat = (String) colourByRangeCombo.getSelectedItem();
936 if (!MessageManager.getString("label.score").equals(byWhat))
938 fc.setAttributeName(fromAttributeDisplayName(byWhat));
942 * set threshold options and 'autoscaled' which is
943 * false if 'threshold is min/max' is selected
944 * else true (colour range is on actual range of values)
946 fc.setThreshold(thresh);
947 fc.setAutoScaled(!thresholdIsMin.isSelected());
948 fc.setAboveThreshold(thresholdOption == ABOVE_THRESHOLD_OPTION);
949 fc.setBelowThreshold(thresholdOption == BELOW_THRESHOLD_OPTION);
951 if (threshline == null)
954 * todo not yet implemented: visual indication of feature threshold
956 threshline = new GraphLine((max - min) / 2f, "Threshold",
964 * A helper method that converts a 'compound' attribute name from its display
965 * form, e.g. CSQ:PolyPhen to array form, e.g. { "CSQ", "PolyPhen" }
970 private String[] fromAttributeDisplayName(String attribute)
972 return attribute == null ? null : attribute.split(COLON);
976 * A helper method that converts a 'compound' attribute name to its display
977 * form, e.g. CSQ:PolyPhen from its array form, e.g. { "CSQ", "PolyPhen" }
982 private String toAttributeDisplayName(String[] attName)
984 return attName == null ? "" : String.join(COLON, attName);
988 protected void raiseClosed()
990 if (this.featureSettings != null)
992 featureSettings.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
997 * Action on OK is just to dismiss the dialog - any changes have already been
1001 public void okPressed()
1006 * Action on Cancel is to restore colour scheme and filters as they were when
1007 * the dialog was opened
1010 public void cancelPressed()
1012 fr.setColour(featureType, originalColour);
1013 fr.setFeatureFilter(featureType, originalFilter);
1014 ap.paintAlignment(true, true);
1018 * Action on text entry of a threshold value
1020 protected void thresholdValue_actionPerformed()
1025 float f = Float.parseFloat(thresholdValue.getText());
1026 slider.setValue((int) (f * scaleFactor));
1027 threshline.value = f;
1028 thresholdValue.setBackground(Color.white); // ok
1031 * force repaint of any Overview window or structure
1033 ap.paintAlignment(true, true);
1034 } catch (NumberFormatException ex)
1036 thresholdValue.setBackground(Color.red); // not ok
1044 * Action on change of threshold slider value. This may be done interactively
1045 * (by moving the slider), or programmatically (to update the slider after
1046 * manual input of a threshold value).
1048 protected void sliderValueChanged()
1050 threshline.value = getRoundedSliderValue();
1053 * repaint alignment, but not Overview or structure,
1054 * to avoid overload while dragging the slider
1056 colourChanged(false);
1060 * Converts the slider value to its absolute value by dividing by the
1061 * scaleFactor. Rounding errors are squashed by forcing min/max of slider range
1062 * to the actual min/max of feature score range
1066 private float getRoundedSliderValue()
1068 int value = slider.getValue();
1069 float f = value == slider.getMaximum() ? max
1070 : (value == slider.getMinimum() ? min : value / scaleFactor);
1074 void addActionListener(ActionListener listener)
1076 if (featureSettings != null)
1079 "IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
1081 featureSettings = listener;
1085 * A helper method to build the drop-down choice of attributes for a feature. If
1086 * 'withRange' is true, then Score, and any attributes with a min-max range, are
1087 * added. If 'withText' is true, Label and any known attributes are added. This
1088 * allows 'categorical numerical' attributes e.g. codon position to be coloured
1091 * Where metadata is available with a description for an attribute, that is
1092 * added as a tooltip.
1094 * Attribute names may be 'simple' e.g. "AC" or 'compound' e.g. {"CSQ",
1095 * "Allele"}. Compound names are rendered for display as (e.g.) CSQ:Allele.
1097 * This method does not add any ActionListener to the JComboBox.
1103 protected JComboBox<String> populateAttributesDropdown(
1104 List<String[]> attNames, boolean withRange, boolean withText)
1106 List<String> displayAtts = new ArrayList<>();
1107 List<String> tooltips = new ArrayList<>();
1111 displayAtts.add(MessageManager.getString("label.label"));
1112 tooltips.add(MessageManager.getString("label.description"));
1116 float[][] minMax = fr.getMinMax().get(featureType);
1117 if (minMax != null && minMax[0][0] != minMax[0][1])
1119 displayAtts.add(MessageManager.getString("label.score"));
1120 tooltips.add(MessageManager.getString("label.score"));
1124 FeatureAttributes fa = FeatureAttributes.getInstance();
1125 for (String[] attName : attNames)
1127 float[] minMax = fa.getMinMax(featureType, attName);
1128 boolean hasRange = minMax != null && minMax[0] != minMax[1];
1129 if (!withText && !hasRange)
1133 displayAtts.add(toAttributeDisplayName(attName));
1134 String desc = fa.getDescription(featureType, attName);
1135 if (desc != null && desc.length() > MAX_TOOLTIP_LENGTH)
1137 desc = desc.substring(0, MAX_TOOLTIP_LENGTH) + "...";
1139 tooltips.add(desc == null ? "" : desc);
1142 JComboBox<String> attCombo = JvSwingUtils
1143 .buildComboWithTooltips(displayAtts, tooltips);
1149 * Populates initial layout of the feature attribute filters panel
1151 private JPanel initialiseFiltersPanel()
1153 filters = new ArrayList<>();
1155 JPanel filtersPanel = new JPanel();
1156 filtersPanel.setLayout(new BoxLayout(filtersPanel, BoxLayout.Y_AXIS));
1157 filtersPanel.setBackground(Color.white);
1158 JvSwingUtils.createTitledBorder(filtersPanel,
1159 MessageManager.getString("label.filters"), true);
1161 JPanel andOrPanel = initialiseAndOrPanel();
1162 filtersPanel.add(andOrPanel);
1165 * panel with filters - populated by refreshFiltersDisplay
1167 chooseFiltersPanel = new JPanel();
1168 LayoutManager box = new BoxLayout(chooseFiltersPanel, BoxLayout.Y_AXIS);
1169 chooseFiltersPanel.setLayout(box);
1170 filtersPanel.add(chooseFiltersPanel);
1172 return filtersPanel;
1176 * Lays out the panel with radio buttons to AND or OR filter conditions
1180 private JPanel initialiseAndOrPanel()
1182 JPanel andOrPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
1183 andOrPanel.setBackground(Color.white);
1184 andOrPanel.setBorder(BorderFactory.createLineBorder(debugBorderColour));
1185 andFilters = new JRadioButton(MessageManager.getString("label.and"));
1186 orFilters = new JRadioButton(MessageManager.getString("label.or"));
1187 ActionListener actionListener = new ActionListener()
1190 public void actionPerformed(ActionEvent e)
1195 andFilters.addActionListener(actionListener);
1196 orFilters.addActionListener(actionListener);
1197 ButtonGroup andOr = new ButtonGroup();
1198 andOr.add(andFilters);
1199 andOr.add(orFilters);
1200 andFilters.setSelected(true);
1202 new JLabel(MessageManager.getString("label.join_conditions")));
1203 andOrPanel.add(andFilters);
1204 andOrPanel.add(orFilters);
1209 * Refreshes the display to show any filters currently configured for the
1210 * selected feature type (editable, with 'remove' option), plus one extra row
1211 * for adding a condition. This should be called after a filter has been
1212 * removed, added or amended.
1214 private void updateFiltersTab()
1217 * clear the panel and list of filter conditions
1219 chooseFiltersPanel.removeAll();
1223 * look up attributes known for feature type
1225 List<String[]> attNames = FeatureAttributes.getInstance()
1226 .getAttributes(featureType);
1229 * if this feature type has filters set, load them first
1231 KeyedMatcherSetI featureFilters = fr.getFeatureFilter(featureType);
1232 if (featureFilters != null)
1234 if (!featureFilters.isAnded())
1236 orFilters.setSelected(true);
1238 featureFilters.getMatchers().forEach(matcher -> filters.add(matcher));
1242 * and an empty filter for the user to populate (add)
1244 KeyedMatcherI noFilter = new KeyedMatcher(Condition.values()[0], "",
1246 filters.add(noFilter);
1249 * render the conditions in rows, each in its own JPanel
1251 int filterIndex = 0;
1252 for (KeyedMatcherI filter : filters)
1254 String[] attName = filter.getKey();
1255 Condition condition = filter.getMatcher().getCondition();
1256 String pattern = filter.getMatcher().getPattern();
1257 JPanel row = addFilter(attName, attNames, condition, pattern,
1259 row.setBorder(BorderFactory.createLineBorder(debugBorderColour));
1260 chooseFiltersPanel.add(row);
1269 * A helper method that constructs a row (panel) with one filter condition:
1271 * <li>a drop-down list of attribute names to choose from</li>
1272 * <li>a drop-down list of conditions to choose from</li>
1273 * <li>a text field for input of a match pattern</li>
1274 * <li>optionally, a 'remove' button</li>
1276 * If attribute, condition or pattern are not null, they are set as defaults for
1277 * the input fields. The 'remove' button is added unless the pattern is null or
1278 * empty (incomplete filter condition).
1284 * @param filterIndex
1287 protected JPanel addFilter(String[] attName, List<String[]> attNames,
1288 Condition cond, String pattern, int filterIndex)
1290 JPanel filterRow = new JPanel(new FlowLayout(FlowLayout.LEFT));
1291 filterRow.setBackground(Color.white);
1294 * drop-down choice of attribute, with description as a tooltip
1295 * if we can obtain it
1297 final JComboBox<String> attCombo = populateAttributesDropdown(attNames,
1299 JComboBox<Condition> condCombo = new JComboBox<>();
1300 JTextField patternField = new JTextField(8);
1303 * action handlers that validate and (if valid) apply changes
1305 ActionListener actionListener = new ActionListener()
1308 public void actionPerformed(ActionEvent e)
1310 if (attCombo.getSelectedItem() != null)
1312 if (validateFilter(patternField, condCombo))
1314 updateFilter(attCombo, condCombo, patternField, filterIndex);
1320 ItemListener itemListener = new ItemListener()
1323 public void itemStateChanged(ItemEvent e)
1325 actionListener.actionPerformed(null);
1329 if (attName == null) // the 'add a condition' row
1331 attCombo.setSelectedIndex(0);
1335 attCombo.setSelectedItem(toAttributeDisplayName(attName));
1337 attCombo.addItemListener(new ItemListener()
1340 public void itemStateChanged(ItemEvent e)
1343 * on change of attribute, refresh the conditions list to
1344 * ensure it is appropriate for the attribute datatype
1346 populateConditions((String) attCombo.getSelectedItem(),
1347 (Condition) condCombo.getSelectedItem(), condCombo);
1348 actionListener.actionPerformed(null);
1352 filterRow.add(attCombo);
1355 * drop-down choice of test condition
1357 populateConditions((String) attCombo.getSelectedItem(), cond,
1359 condCombo.setPreferredSize(new Dimension(150, 20));
1360 condCombo.addItemListener(itemListener);
1361 filterRow.add(condCombo);
1364 * pattern to match against
1366 patternField.setText(pattern);
1367 patternField.addActionListener(actionListener);
1368 patternField.addFocusListener(new FocusAdapter()
1371 public void focusLost(FocusEvent e)
1373 actionListener.actionPerformed(null);
1376 filterRow.add(patternField);
1379 * disable pattern field for condition 'Present / NotPresent'
1381 Condition selectedCondition = (Condition) condCombo.getSelectedItem();
1382 if (selectedCondition == Condition.Present
1383 || selectedCondition == Condition.NotPresent)
1385 patternField.setEnabled(false);
1389 * if a numeric condition is selected, show the value range
1390 * as a tooltip on the value input field
1392 if (selectedCondition.isNumeric())
1394 float[] minMax = FeatureAttributes.getInstance()
1395 .getMinMax(featureType, attName);
1398 String tip = String.format("(%s - %s)",
1399 DECFMT_2_2.format(minMax[0]), DECFMT_2_2.format(minMax[1]));
1400 patternField.setToolTipText(tip);
1405 * add remove button if filter is populated (non-empty pattern)
1407 if (pattern != null && pattern.trim().length() > 0)
1409 // todo: gif for button drawing '-' or 'x'
1410 JButton removeCondition = new BasicArrowButton(SwingConstants.WEST);
1412 .setToolTipText(MessageManager.getString("label.delete_row"));
1413 removeCondition.addActionListener(new ActionListener()
1416 public void actionPerformed(ActionEvent e)
1418 filters.remove(filterIndex);
1422 filterRow.add(removeCondition);
1429 * Populates the drop-down list of comparison conditions for the given attribute
1430 * name. The conditions added depend on the datatype of the attribute values.
1431 * The supplied condition is set as the selected item in the list, provided it
1438 private void populateConditions(String attName, Condition cond,
1439 JComboBox<Condition> condCombo)
1441 Datatype type = FeatureAttributes.getInstance().getDatatype(featureType,
1442 fromAttributeDisplayName(attName));
1443 if (MessageManager.getString("label.label").equals(attName))
1445 type = Datatype.Character;
1447 else if (MessageManager.getString("label.score").equals(attName))
1449 type = Datatype.Number;
1453 * remove itemListener before starting
1455 ItemListener listener = condCombo.getItemListeners()[0];
1456 condCombo.removeItemListener(listener);
1457 boolean condIsValid = false;
1458 condCombo.removeAllItems();
1459 for (Condition c : Condition.values())
1461 if ((c.isNumeric() && type != Datatype.Character)
1462 || (!c.isNumeric() && type != Datatype.Number))
1464 condCombo.addItem(c);
1473 * set the selected condition (does nothing if not in the list)
1477 condCombo.setSelectedItem(cond);
1481 condCombo.setSelectedIndex(0);
1484 condCombo.addItemListener(listener);
1488 * Answers true unless a numeric condition has been selected with a non-numeric
1489 * value. Sets the value field to RED with a tooltip if in error.
1491 * If the pattern is expected but is empty, this method returns false, but does
1492 * not mark the field as invalid. This supports selecting an attribute for a new
1493 * condition before a match pattern has been entered.
1498 protected boolean validateFilter(JTextField value,
1499 JComboBox<Condition> condCombo)
1501 if (value == null || condCombo == null)
1503 return true; // fields not populated
1506 Condition cond = (Condition) condCombo.getSelectedItem();
1507 if (cond == Condition.Present || cond == Condition.NotPresent)
1512 value.setBackground(Color.white);
1513 value.setToolTipText("");
1514 String v1 = value.getText().trim();
1515 if (v1.length() == 0)
1520 if (cond.isNumeric())
1525 } catch (NumberFormatException e)
1527 value.setBackground(Color.red);
1528 value.setToolTipText(
1529 MessageManager.getString("label.numeric_required"));
1538 * Constructs a filter condition from the given input fields, and replaces the
1539 * condition at filterIndex with the new one
1544 * @param filterIndex
1546 protected void updateFilter(JComboBox<String> attCombo,
1547 JComboBox<Condition> condCombo, JTextField valueField,
1550 String attName = (String) attCombo.getSelectedItem();
1551 Condition cond = (Condition) condCombo.getSelectedItem();
1552 String pattern = valueField.getText();
1553 KeyedMatcherI km = new KeyedMatcher(cond, pattern,
1554 fromAttributeDisplayName(attName));
1556 filters.set(filterIndex, km);
1560 * Makes the dialog visible, at the Feature Colour tab or at the Filters tab
1564 public void showTab(boolean coloursTab)
1567 tabbedPane.setSelectedIndex(coloursTab ? 0 : 1);
1571 * Action on any change to feature filtering, namely
1573 * <li>change of selected attribute</li>
1574 * <li>change of selected condition</li>
1575 * <li>change of match pattern</li>
1576 * <li>removal of a condition</li>
1578 * The inputs are parsed into a combined filter and this is set for the feature
1579 * type, and the alignment redrawn.
1581 protected void filtersChanged()
1584 * update the filter conditions for the feature type
1586 boolean anded = andFilters.isSelected();
1587 KeyedMatcherSetI combined = new KeyedMatcherSet();
1589 for (KeyedMatcherI filter : filters)
1591 String pattern = filter.getMatcher().getPattern();
1592 Condition condition = filter.getMatcher().getCondition();
1593 if (pattern.trim().length() > 0 || condition == Condition.Present
1594 || condition == Condition.NotPresent)
1598 combined.and(filter);
1602 combined.or(filter);
1608 * save the filter conditions in the FeatureRenderer
1609 * (note this might now be an empty filter with no conditions)
1611 fr.setFeatureFilter(featureType, combined.isEmpty() ? null : combined);
1612 ap.paintAlignment(true, true);