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.util.ArrayList;
51 import java.util.List;
53 import javax.swing.BorderFactory;
54 import javax.swing.BoxLayout;
55 import javax.swing.ButtonGroup;
56 import javax.swing.JButton;
57 import javax.swing.JCheckBox;
58 import javax.swing.JColorChooser;
59 import javax.swing.JComboBox;
60 import javax.swing.JLabel;
61 import javax.swing.JPanel;
62 import javax.swing.JRadioButton;
63 import javax.swing.JSlider;
64 import javax.swing.JTabbedPane;
65 import javax.swing.JTextField;
66 import javax.swing.SwingConstants;
67 import javax.swing.border.LineBorder;
68 import javax.swing.event.ChangeEvent;
69 import javax.swing.event.ChangeListener;
70 import javax.swing.plaf.basic.BasicArrowButton;
73 * A dialog where the user can configure colour scheme, and any filters, for one
76 * (Was FeatureColourChooser prior to Jalview 1.11, renamed with the addition of
79 public class FeatureTypeSettings extends JalviewDialog
81 private static final int RADIO_WIDTH = 130;
83 private static final String COLON = ":";
85 private static final int MAX_TOOLTIP_LENGTH = 50;
87 private static final int NO_COLOUR_OPTION = 0;
89 private static final int MIN_COLOUR_OPTION = 1;
91 private static final int MAX_COLOUR_OPTION = 2;
93 private static final int ABOVE_THRESHOLD_OPTION = 1;
95 private static final int BELOW_THRESHOLD_OPTION = 2;
98 * FeatureRenderer holds colour scheme and filters for feature types
100 private final FeatureRenderer fr; // todo refactor to allow interface type here
103 * the view panel to update when settings change
105 private final AlignmentViewPanel ap;
107 private final String featureType;
110 * the colour and filters to reset to on Cancel
112 private final FeatureColourI originalColour;
114 private final KeyedMatcherSetI originalFilter;
117 * set flag to true when setting values programmatically,
118 * to avoid invocation of action handlers
120 private boolean adjusting = false;
126 private float scaleFactor;
129 * radio button group, to select what to colour by:
130 * simple colour, by category (text), or graduated
132 private JRadioButton simpleColour = new JRadioButton();
134 private JRadioButton byCategory = new JRadioButton();
136 private JRadioButton graduatedColour = new JRadioButton();
138 private JPanel singleColour = new JPanel();
140 private JPanel minColour = new JPanel();
142 private JPanel maxColour = new JPanel();
144 private JComboBox<String> threshold = new JComboBox<>();
146 private JSlider slider = new JSlider();
148 private JTextField thresholdValue = new JTextField(20);
150 private JCheckBox thresholdIsMin = new JCheckBox();
152 private GraphLine threshline;
154 private ActionListener featureSettings = null;
156 private ActionListener changeColourAction;
159 * choice of option for 'colour for no value'
161 private JComboBox<String> noValueCombo;
164 * choice of what to colour by text (Label or attribute)
166 private JComboBox<String> colourByTextCombo;
169 * choice of what to colour by range (Score or attribute)
171 private JComboBox<String> colourByRangeCombo;
173 private JRadioButton andFilters;
175 private JRadioButton orFilters;
178 * filters for the currently selected feature type
180 private List<KeyedMatcherI> filters;
182 // set white normally, black to debug layout
183 private Color debugBorderColour = Color.white;
185 private JPanel chooseFiltersPanel;
187 private JTabbedPane tabbedPane;
195 public FeatureTypeSettings(FeatureRenderer frender, String theType)
197 this(frender, false, theType);
201 * Constructor, with option to make a blocking dialog (has to complete in the
202 * AWT event queue thread). Currently this option is always set to false.
208 FeatureTypeSettings(FeatureRenderer frender, boolean blocking,
212 this.featureType = theType;
214 originalFilter = fr.getFeatureFilter(theType);
215 originalColour = fr.getFeatureColours().get(theType);
222 } catch (Exception ex)
224 ex.printStackTrace();
234 colourChanged(false);
236 String title = MessageManager
237 .formatMessage("label.display_settings_for", new String[]
239 initDialogFrame(this, true, blocking, title, 600, 360);
245 * Configures the widgets on the Colours tab according to the current feature
248 private void updateColoursTab()
250 FeatureColourI fc = fr.getFeatureColours().get(featureType);
253 * suppress action handling while updating values programmatically
261 if (fc.isSimpleColour())
263 simpleColour.setSelected(true);
264 singleColour.setBackground(fc.getColour());
265 singleColour.setForeground(fc.getColour());
269 * colour by text (Label or attribute text)
271 if (fc.isColourByLabel())
273 byCategory.setSelected(true);
274 colourByTextCombo.setEnabled(colourByTextCombo.getItemCount() > 1);
275 if (fc.isColourByAttribute())
277 String[] attributeName = fc.getAttributeName();
279 .setSelectedItem(toAttributeDisplayName(attributeName));
284 .setSelectedItem(MessageManager.getString("label.label"));
289 colourByTextCombo.setEnabled(false);
292 if (!fc.isGraduatedColour())
294 colourByRangeCombo.setEnabled(false);
295 minColour.setEnabled(false);
296 maxColour.setEnabled(false);
297 noValueCombo.setEnabled(false);
298 threshold.setEnabled(false);
299 slider.setEnabled(false);
300 thresholdValue.setEnabled(false);
301 thresholdIsMin.setEnabled(false);
306 * Graduated colour, by score or attribute value range
308 graduatedColour.setSelected(true);
309 colourByRangeCombo.setEnabled(colourByRangeCombo.getItemCount() > 1);
310 minColour.setEnabled(true);
311 maxColour.setEnabled(true);
312 noValueCombo.setEnabled(true);
313 threshold.setEnabled(true);
314 minColour.setBackground(fc.getMinColour());
315 maxColour.setBackground(fc.getMaxColour());
317 if (fc.isColourByAttribute())
319 String[] attributeName = fc.getAttributeName();
321 .setSelectedItem(toAttributeDisplayName(attributeName));
326 .setSelectedItem(MessageManager.getString("label.score"));
328 Color noColour = fc.getNoColour();
329 if (noColour == null)
331 noValueCombo.setSelectedIndex(NO_COLOUR_OPTION);
333 else if (noColour.equals(fc.getMinColour()))
335 noValueCombo.setSelectedIndex(MIN_COLOUR_OPTION);
337 else if (noColour.equals(fc.getMaxColour()))
339 noValueCombo.setSelectedIndex(MAX_COLOUR_OPTION);
343 * update min-max scaling if there is a range to work with,
344 * else disable the widgets (this shouldn't happen if only
345 * valid options are offered in the combo box)
347 scaleFactor = (max == min) ? 1f : 100f / (max - min);
348 float range = (max - min) * scaleFactor;
349 slider.setMinimum((int) (min * scaleFactor));
350 slider.setMaximum((int) (max * scaleFactor));
351 slider.setMajorTickSpacing((int) (range / 10f));
353 threshline = new GraphLine((max - min) / 2f, "Threshold",
355 threshline.value = fc.getThreshold();
357 if (fc.hasThreshold())
359 threshold.setSelectedIndex(
360 fc.isAboveThreshold() ? ABOVE_THRESHOLD_OPTION
361 : BELOW_THRESHOLD_OPTION);
362 slider.setEnabled(true);
363 slider.setValue((int) (fc.getThreshold() * scaleFactor));
364 thresholdValue.setText(String.valueOf(getRoundedSliderValue()));
365 thresholdValue.setEnabled(true);
366 thresholdIsMin.setEnabled(true);
370 slider.setEnabled(false);
371 thresholdValue.setEnabled(false);
372 thresholdIsMin.setEnabled(false);
374 thresholdIsMin.setSelected(!fc.isAutoScaled());
382 * Configures the initial layout
384 private void initialise()
386 this.setLayout(new BorderLayout());
387 tabbedPane = new JTabbedPane();
388 this.add(tabbedPane, BorderLayout.CENTER);
391 * an ActionListener that applies colour changes
393 changeColourAction = new ActionListener()
396 public void actionPerformed(ActionEvent e)
403 * first tab: colour options
405 JPanel coloursPanel = initialiseColoursPanel();
406 tabbedPane.addTab(MessageManager.getString("action.colour"),
410 * second tab: filter options
412 JPanel filtersPanel = initialiseFiltersPanel();
413 tabbedPane.addTab(MessageManager.getString("label.filters"),
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 updateMinMax()
427 if (!graduatedColour.isSelected())
432 float[] minMax = null;
433 String colourBy = (String) colourByRangeCombo.getSelectedItem();
434 if (MessageManager.getString("label.score").equals(colourBy))
436 minMax = fr.getMinMax().get(featureType)[0];
440 // colour by attribute range
441 String[] attNames = fromAttributeDisplayName(colourBy);
442 minMax = FeatureAttributes.getInstance().getMinMax(featureType,
454 * Lay out fields for graduated colour (by score or attribute value)
458 private JPanel initialiseGraduatedColourPanel()
460 JPanel graduatedColourPanel = new JPanel();
461 graduatedColourPanel.setLayout(
462 new BoxLayout(graduatedColourPanel, BoxLayout.Y_AXIS));
463 JvSwingUtils.createTitledBorder(graduatedColourPanel,
464 MessageManager.getString("label.graduated_colour"), true);
465 graduatedColourPanel.setBackground(Color.white);
468 * first row: graduated colour radio button, score/attribute drop-down
470 JPanel graduatedChoicePanel = new JPanel(
471 new FlowLayout(FlowLayout.LEFT));
472 graduatedChoicePanel.setBackground(Color.white);
473 graduatedColour = new JRadioButton(
474 MessageManager.getString("label.by_range_of") + COLON);
475 graduatedColour.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
476 graduatedColour.addItemListener(new ItemListener()
479 public void itemStateChanged(ItemEvent e)
481 if (graduatedColour.isSelected())
487 graduatedChoicePanel.add(graduatedColour);
489 List<String[]> attNames = FeatureAttributes.getInstance()
490 .getAttributes(featureType);
491 colourByRangeCombo = populateAttributesDropdown(attNames, true, false);
492 colourByRangeCombo.addItemListener(new ItemListener()
495 public void itemStateChanged(ItemEvent e)
502 * disable graduated colour option if no range found
504 graduatedColour.setEnabled(colourByRangeCombo.getItemCount() > 0);
506 graduatedChoicePanel.add(colourByRangeCombo);
507 graduatedColourPanel.add(graduatedChoicePanel);
510 * second row - min/max/no colours
512 JPanel colourRangePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
513 colourRangePanel.setBackground(Color.white);
514 graduatedColourPanel.add(colourRangePanel);
516 minColour.setFont(JvSwingUtils.getLabelFont());
517 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
518 minColour.setPreferredSize(new Dimension(40, 20));
519 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
520 minColour.addMouseListener(new MouseAdapter()
523 public void mousePressed(MouseEvent e)
525 if (minColour.isEnabled())
527 showColourChooser(minColour, "label.select_colour_minimum_value");
532 maxColour.setFont(JvSwingUtils.getLabelFont());
533 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
534 maxColour.setPreferredSize(new Dimension(40, 20));
535 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
536 maxColour.addMouseListener(new MouseAdapter()
539 public void mousePressed(MouseEvent e)
541 if (maxColour.isEnabled())
543 showColourChooser(maxColour, "label.select_colour_maximum_value");
547 maxColour.setBorder(new LineBorder(Color.black));
550 * default max colour to current colour (if a plain colour),
551 * or to Black if colour by label; make min colour a pale
552 * version of max colour
554 FeatureColourI fc = fr.getFeatureColours().get(featureType);
555 Color bg = fc.isSimpleColour() ? fc.getColour() : Color.BLACK;
556 maxColour.setBackground(bg);
557 minColour.setBackground(ColorUtils.bleachColour(bg, 0.9f));
559 noValueCombo = new JComboBox<>();
560 noValueCombo.addItem(MessageManager.getString("label.no_colour"));
561 noValueCombo.addItem(MessageManager.getString("label.min_colour"));
562 noValueCombo.addItem(MessageManager.getString("label.max_colour"));
563 noValueCombo.addItemListener(new ItemListener()
566 public void itemStateChanged(ItemEvent e)
572 JLabel minText = new JLabel(
573 MessageManager.getString("label.min_value") + COLON);
574 minText.setFont(JvSwingUtils.getLabelFont());
575 JLabel maxText = new JLabel(
576 MessageManager.getString("label.max_value") + COLON);
577 maxText.setFont(JvSwingUtils.getLabelFont());
578 JLabel noText = new JLabel(
579 MessageManager.getString("label.no_value") + COLON);
580 noText.setFont(JvSwingUtils.getLabelFont());
582 colourRangePanel.add(minText);
583 colourRangePanel.add(minColour);
584 colourRangePanel.add(maxText);
585 colourRangePanel.add(maxColour);
586 colourRangePanel.add(noText);
587 colourRangePanel.add(noValueCombo);
590 * third row - threshold options and value
592 JPanel thresholdPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
593 thresholdPanel.setBackground(Color.white);
594 graduatedColourPanel.add(thresholdPanel);
596 threshold.addActionListener(changeColourAction);
597 threshold.setToolTipText(MessageManager
598 .getString("label.threshold_feature_display_by_score"));
599 threshold.addItem(MessageManager
600 .getString("label.threshold_feature_no_threshold")); // index 0
601 threshold.addItem(MessageManager
602 .getString("label.threshold_feature_above_threshold")); // index 1
603 threshold.addItem(MessageManager
604 .getString("label.threshold_feature_below_threshold")); // index 2
606 thresholdValue.addActionListener(new ActionListener()
609 public void actionPerformed(ActionEvent e)
611 thresholdValue_actionPerformed();
614 thresholdValue.addFocusListener(new FocusAdapter()
617 public void focusLost(FocusEvent e)
619 thresholdValue_actionPerformed();
622 slider.setPaintLabels(false);
623 slider.setPaintTicks(true);
624 slider.setBackground(Color.white);
625 slider.setEnabled(false);
626 slider.setOpaque(false);
627 slider.setPreferredSize(new Dimension(100, 32));
628 slider.setToolTipText(
629 MessageManager.getString("label.adjust_threshold"));
631 slider.addChangeListener(new ChangeListener()
634 public void stateChanged(ChangeEvent evt)
639 .setText(String.valueOf(slider.getValue() / scaleFactor));
640 sliderValueChanged();
644 slider.addMouseListener(new MouseAdapter()
647 public void mouseReleased(MouseEvent evt)
650 * only update Overview and/or structure colouring
651 * when threshold slider drag ends (mouse up)
655 ap.paintAlignment(true, true);
660 thresholdValue.setEnabled(false);
661 thresholdValue.setColumns(7);
663 thresholdPanel.add(threshold);
664 thresholdPanel.add(slider);
665 thresholdPanel.add(thresholdValue);
667 thresholdIsMin.setBackground(Color.white);
669 .setText(MessageManager.getString("label.threshold_minmax"));
670 thresholdIsMin.setToolTipText(MessageManager
671 .getString("label.toggle_absolute_relative_display_threshold"));
672 thresholdIsMin.addActionListener(changeColourAction);
673 thresholdPanel.add(thresholdIsMin);
675 return graduatedColourPanel;
679 * Lay out OK and Cancel buttons
683 private JPanel initialiseOkCancelPanel()
685 JPanel okCancelPanel = new JPanel();
686 // okCancelPanel.setBackground(Color.white);
687 okCancelPanel.add(ok);
688 okCancelPanel.add(cancel);
689 return okCancelPanel;
693 * Lay out Colour options panel, containing
695 * <li>plain colour, with colour picker</li>
696 * <li>colour by text, with choice of Label or other attribute</li>
697 * <li>colour by range, of score or other attribute, when available</li>
702 private JPanel initialiseColoursPanel()
704 JPanel colourByPanel = new JPanel();
705 colourByPanel.setLayout(new BoxLayout(colourByPanel, BoxLayout.Y_AXIS));
708 * simple colour radio button and colour picker
710 JPanel simpleColourPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
711 simpleColourPanel.setBackground(Color.white);
712 JvSwingUtils.createTitledBorder(simpleColourPanel,
713 MessageManager.getString("label.simple"), true);
714 colourByPanel.add(simpleColourPanel);
716 simpleColour = new JRadioButton(
717 MessageManager.getString("label.simple_colour"));
718 simpleColour.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
719 simpleColour.addItemListener(new ItemListener()
722 public void itemStateChanged(ItemEvent e)
724 if (simpleColour.isSelected() && !adjusting)
726 showColourChooser(singleColour, "label.select_colour");
732 singleColour.setFont(JvSwingUtils.getLabelFont());
733 singleColour.setBorder(BorderFactory.createLineBorder(Color.black));
734 singleColour.setPreferredSize(new Dimension(40, 20));
735 singleColour.addMouseListener(new MouseAdapter()
738 public void mousePressed(MouseEvent e)
740 if (simpleColour.isSelected())
742 showColourChooser(singleColour, "label.select_colour");
746 simpleColourPanel.add(simpleColour); // radio button
747 simpleColourPanel.add(singleColour); // colour picker button
750 * colour by text (category) radio button and drop-down choice list
752 JPanel byTextPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
753 byTextPanel.setBackground(Color.white);
754 JvSwingUtils.createTitledBorder(byTextPanel,
755 MessageManager.getString("label.colour_by_text"), true);
756 colourByPanel.add(byTextPanel);
757 byCategory = new JRadioButton(
758 MessageManager.getString("label.by_text_of") + COLON);
759 byCategory.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
760 byCategory.addItemListener(new ItemListener()
763 public void itemStateChanged(ItemEvent e)
765 if (byCategory.isSelected())
771 byTextPanel.add(byCategory);
773 List<String[]> attNames = FeatureAttributes.getInstance()
774 .getAttributes(featureType);
775 colourByTextCombo = populateAttributesDropdown(attNames, false, true);
776 colourByTextCombo.addItemListener(new ItemListener()
779 public void itemStateChanged(ItemEvent e)
784 byTextPanel.add(colourByTextCombo);
787 * graduated colour panel
789 JPanel graduatedColourPanel = initialiseGraduatedColourPanel();
790 colourByPanel.add(graduatedColourPanel);
793 * 3 radio buttons select between simple colour,
794 * by category (text), or graduated
796 ButtonGroup bg = new ButtonGroup();
797 bg.add(simpleColour);
799 bg.add(graduatedColour);
801 return colourByPanel;
804 private void showColourChooser(JPanel colourPanel, String key)
806 Color col = JColorChooser.showDialog(this,
807 MessageManager.getString(key), colourPanel.getBackground());
810 colourPanel.setBackground(col);
811 colourPanel.setForeground(col);
813 colourPanel.repaint();
818 * Constructs and sets the selected colour options as the colour for the feature
819 * type, and repaints the alignment, and optionally the Overview and/or
820 * structure viewer if open
822 * @param updateStructsAndOverview
824 void colourChanged(boolean updateStructsAndOverview)
829 * ignore action handlers while setting values programmatically
835 * ensure min-max range is for the latest choice of
836 * 'graduated colour by'
840 FeatureColourI acg = makeColourFromInputs();
843 * save the colour, and repaint stuff
845 fr.setColour(featureType, acg);
846 ap.paintAlignment(updateStructsAndOverview, updateStructsAndOverview);
852 * Converts the input values into an instance of FeatureColour
856 private FeatureColourI makeColourFromInputs()
859 * easiest case - a single colour
861 if (simpleColour.isSelected())
863 return new FeatureColour(singleColour.getBackground());
867 * next easiest case - colour by Label, or attribute text
869 if (byCategory.isSelected())
871 Color c = this.getBackground();
872 FeatureColourI fc = new FeatureColour(c, c, null, 0f, 0f);
873 fc.setColourByLabel(true);
874 String byWhat = (String) colourByTextCombo.getSelectedItem();
875 if (!MessageManager.getString("label.label").equals(byWhat))
877 fc.setAttributeName(fromAttributeDisplayName(byWhat));
883 * remaining case - graduated colour by score, or attribute value
885 Color noColour = null;
886 if (noValueCombo.getSelectedIndex() == MIN_COLOUR_OPTION)
888 noColour = minColour.getBackground();
890 else if (noValueCombo.getSelectedIndex() == MAX_COLOUR_OPTION)
892 noColour = maxColour.getBackground();
898 thresh = Float.valueOf(thresholdValue.getText());
899 } catch (NumberFormatException e)
901 // invalid inputs are already handled on entry
905 * min-max range is to (or from) threshold value if
906 * 'threshold is min/max' is selected
908 float minValue = min;
909 float maxValue = max;
910 final int thresholdOption = threshold.getSelectedIndex();
911 if (thresholdIsMin.isSelected()
912 && thresholdOption == ABOVE_THRESHOLD_OPTION)
916 if (thresholdIsMin.isSelected()
917 && thresholdOption == BELOW_THRESHOLD_OPTION)
923 * make the graduated colour
925 FeatureColourI fc = new FeatureColour(minColour.getBackground(),
926 maxColour.getBackground(), noColour, minValue, maxValue);
929 * set attribute to colour by if selected
931 String byWhat = (String) colourByRangeCombo.getSelectedItem();
932 if (!MessageManager.getString("label.score").equals(byWhat))
934 fc.setAttributeName(fromAttributeDisplayName(byWhat));
938 * set threshold options and 'autoscaled' which is
939 * false if 'threshold is min/max' is selected
940 * else true (colour range is on actual range of values)
942 fc.setThreshold(thresh);
943 fc.setAutoScaled(!thresholdIsMin.isSelected());
944 fc.setAboveThreshold(thresholdOption == ABOVE_THRESHOLD_OPTION);
945 fc.setBelowThreshold(thresholdOption == BELOW_THRESHOLD_OPTION);
947 if (threshline == null)
950 * todo not yet implemented: visual indication of feature threshold
952 threshline = new GraphLine((max - min) / 2f, "Threshold",
960 * A helper method that converts a 'compound' attribute name from its display
961 * form, e.g. CSQ:PolyPhen to array form, e.g. { "CSQ", "PolyPhen" }
966 private String[] fromAttributeDisplayName(String attribute)
968 return attribute == null ? null : attribute.split(COLON);
972 * A helper method that converts a 'compound' attribute name to its display
973 * form, e.g. CSQ:PolyPhen from its array form, e.g. { "CSQ", "PolyPhen" }
978 private String toAttributeDisplayName(String[] attName)
980 return attName == null ? "" : String.join(COLON, attName);
984 protected void raiseClosed()
986 if (this.featureSettings != null)
988 featureSettings.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
993 * Action on OK is just to dismiss the dialog - any changes have already been
997 public void okPressed()
1002 * Action on Cancel is to restore colour scheme and filters as they were when
1003 * the dialog was opened
1006 public void cancelPressed()
1008 fr.setColour(featureType, originalColour);
1009 fr.setFeatureFilter(featureType, originalFilter);
1010 ap.paintAlignment(true, true);
1014 * Action on text entry of a threshold value
1016 protected void thresholdValue_actionPerformed()
1021 float f = Float.parseFloat(thresholdValue.getText());
1022 slider.setValue((int) (f * scaleFactor));
1023 threshline.value = f;
1024 thresholdValue.setBackground(Color.white); // ok
1027 * force repaint of any Overview window or structure
1029 ap.paintAlignment(true, true);
1030 } catch (NumberFormatException ex)
1032 thresholdValue.setBackground(Color.red); // not ok
1040 * Action on change of threshold slider value. This may be done interactively
1041 * (by moving the slider), or programmatically (to update the slider after
1042 * manual input of a threshold value).
1044 protected void sliderValueChanged()
1046 threshline.value = getRoundedSliderValue();
1049 * repaint alignment, but not Overview or structure,
1050 * to avoid overload while dragging the slider
1052 colourChanged(false);
1056 * Converts the slider value to its absolute value by dividing by the
1057 * scaleFactor. Rounding errors are squashed by forcing min/max of slider range
1058 * to the actual min/max of feature score range
1062 private float getRoundedSliderValue()
1064 int value = slider.getValue();
1065 float f = value == slider.getMaximum() ? max
1066 : (value == slider.getMinimum() ? min : value / scaleFactor);
1070 void addActionListener(ActionListener listener)
1072 if (featureSettings != null)
1075 "IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
1077 featureSettings = listener;
1081 * A helper method to build the drop-down choice of attributes for a feature. If
1082 * 'withRange' is true, then Score, and any attributes with a min-max range, are
1083 * added. If 'withText' is true, Label and any known attributes are added. This
1084 * allows 'categorical numerical' attributes e.g. codon position to be coloured
1087 * Where metadata is available with a description for an attribute, that is
1088 * added as a tooltip.
1090 * Attribute names may be 'simple' e.g. "AC" or 'compound' e.g. {"CSQ",
1091 * "Allele"}. Compound names are rendered for display as (e.g.) CSQ:Allele.
1093 * This method does not add any ActionListener to the JComboBox.
1099 protected JComboBox<String> populateAttributesDropdown(
1100 List<String[]> attNames, boolean withRange, boolean withText)
1102 List<String> displayAtts = new ArrayList<>();
1103 List<String> tooltips = new ArrayList<>();
1107 displayAtts.add(MessageManager.getString("label.label"));
1108 tooltips.add(MessageManager.getString("label.description"));
1112 float[][] minMax = fr.getMinMax().get(featureType);
1113 if (minMax != null && minMax[0][0] != minMax[0][1])
1115 displayAtts.add(MessageManager.getString("label.score"));
1116 tooltips.add(MessageManager.getString("label.score"));
1120 FeatureAttributes fa = FeatureAttributes.getInstance();
1121 for (String[] attName : attNames)
1123 float[] minMax = fa.getMinMax(featureType, attName);
1124 boolean hasRange = minMax != null && minMax[0] != minMax[1];
1125 if (!withText && !hasRange)
1129 displayAtts.add(toAttributeDisplayName(attName));
1130 String desc = fa.getDescription(featureType, attName);
1131 if (desc != null && desc.length() > MAX_TOOLTIP_LENGTH)
1133 desc = desc.substring(0, MAX_TOOLTIP_LENGTH) + "...";
1135 tooltips.add(desc == null ? "" : desc);
1138 JComboBox<String> attCombo = JvSwingUtils
1139 .buildComboWithTooltips(displayAtts, tooltips);
1145 * Populates initial layout of the feature attribute filters panel
1147 private JPanel initialiseFiltersPanel()
1149 filters = new ArrayList<>();
1151 JPanel filtersPanel = new JPanel();
1152 filtersPanel.setLayout(new BoxLayout(filtersPanel, BoxLayout.Y_AXIS));
1153 filtersPanel.setBackground(Color.white);
1154 JvSwingUtils.createTitledBorder(filtersPanel,
1155 MessageManager.getString("label.filters"), true);
1157 JPanel andOrPanel = initialiseAndOrPanel();
1158 filtersPanel.add(andOrPanel);
1161 * panel with filters - populated by refreshFiltersDisplay
1163 chooseFiltersPanel = new JPanel();
1164 LayoutManager box = new BoxLayout(chooseFiltersPanel, BoxLayout.Y_AXIS);
1165 chooseFiltersPanel.setLayout(box);
1166 filtersPanel.add(chooseFiltersPanel);
1168 return filtersPanel;
1172 * Lays out the panel with radio buttons to AND or OR filter conditions
1176 private JPanel initialiseAndOrPanel()
1178 JPanel andOrPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
1179 andOrPanel.setBackground(Color.white);
1180 andOrPanel.setBorder(BorderFactory.createLineBorder(debugBorderColour));
1181 andFilters = new JRadioButton(MessageManager.getString("label.and"));
1182 orFilters = new JRadioButton(MessageManager.getString("label.or"));
1183 ActionListener actionListener = new ActionListener()
1186 public void actionPerformed(ActionEvent e)
1191 andFilters.addActionListener(actionListener);
1192 orFilters.addActionListener(actionListener);
1193 ButtonGroup andOr = new ButtonGroup();
1194 andOr.add(andFilters);
1195 andOr.add(orFilters);
1196 andFilters.setSelected(true);
1198 new JLabel(MessageManager.getString("label.join_conditions")));
1199 andOrPanel.add(andFilters);
1200 andOrPanel.add(orFilters);
1205 * Refreshes the display to show any filters currently configured for the
1206 * selected feature type (editable, with 'remove' option), plus one extra row
1207 * for adding a condition. This should be called after a filter has been
1208 * removed, added or amended.
1210 private void updateFiltersTab()
1213 * clear the panel and list of filter conditions
1215 chooseFiltersPanel.removeAll();
1219 * look up attributes known for feature type
1221 List<String[]> attNames = FeatureAttributes.getInstance()
1222 .getAttributes(featureType);
1225 * if this feature type has filters set, load them first
1227 KeyedMatcherSetI featureFilters = fr.getFeatureFilter(featureType);
1228 if (featureFilters != null)
1230 if (!featureFilters.isAnded())
1232 orFilters.setSelected(true);
1234 featureFilters.getMatchers().forEach(matcher -> filters.add(matcher));
1238 * and an empty filter for the user to populate (add)
1240 KeyedMatcherI noFilter = new KeyedMatcher(Condition.values()[0], "",
1242 filters.add(noFilter);
1245 * render the conditions in rows, each in its own JPanel
1247 int filterIndex = 0;
1248 for (KeyedMatcherI filter : filters)
1250 String[] attName = filter.getKey();
1251 Condition condition = filter.getMatcher().getCondition();
1252 String pattern = filter.getMatcher().getPattern();
1253 JPanel row = addFilter(attName, attNames, condition, pattern,
1255 row.setBorder(BorderFactory.createLineBorder(debugBorderColour));
1256 chooseFiltersPanel.add(row);
1265 * A helper method that constructs a row (panel) with one filter condition:
1267 * <li>a drop-down list of attribute names to choose from</li>
1268 * <li>a drop-down list of conditions to choose from</li>
1269 * <li>a text field for input of a match pattern</li>
1270 * <li>optionally, a 'remove' button</li>
1272 * If attribute, condition or pattern are not null, they are set as defaults for
1273 * the input fields. The 'remove' button is added unless the pattern is null or
1274 * empty (incomplete filter condition).
1280 * @param filterIndex
1283 protected JPanel addFilter(String[] attName, List<String[]> attNames,
1284 Condition cond, String pattern, int filterIndex)
1286 JPanel filterRow = new JPanel(new FlowLayout(FlowLayout.LEFT));
1287 filterRow.setBackground(Color.white);
1290 * drop-down choice of attribute, with description as a tooltip
1291 * if we can obtain it
1293 final JComboBox<String> attCombo = populateAttributesDropdown(attNames,
1295 JComboBox<Condition> condCombo = new JComboBox<>();
1296 JTextField patternField = new JTextField(8);
1299 * action handlers that validate and (if valid) apply changes
1301 ActionListener actionListener = new ActionListener()
1304 public void actionPerformed(ActionEvent e)
1306 if (attCombo.getSelectedItem() != null)
1308 if (validateFilter(patternField, condCombo))
1310 updateFilter(attCombo, condCombo, patternField, filterIndex);
1316 ItemListener itemListener = new ItemListener()
1319 public void itemStateChanged(ItemEvent e)
1321 actionListener.actionPerformed(null);
1325 if (attName == null) // the 'add a condition' row
1327 attCombo.setSelectedIndex(0);
1331 attCombo.setSelectedItem(toAttributeDisplayName(attName));
1333 attCombo.addItemListener(itemListener);
1335 filterRow.add(attCombo);
1338 * drop-down choice of test condition
1340 populateConditions((String) attCombo.getSelectedItem(), cond,
1342 condCombo.addItemListener(itemListener);
1343 filterRow.add(condCombo);
1346 * pattern to match against
1348 patternField.setText(pattern);
1349 patternField.addActionListener(actionListener);
1350 patternField.addFocusListener(new FocusAdapter()
1353 public void focusLost(FocusEvent e)
1355 actionListener.actionPerformed(null);
1358 filterRow.add(patternField);
1361 * add remove button if filter is populated (non-empty pattern)
1363 if (pattern != null && pattern.trim().length() > 0)
1365 // todo: gif for button drawing '-' or 'x'
1366 JButton removeCondition = new BasicArrowButton(SwingConstants.WEST);
1368 .setToolTipText(MessageManager.getString("label.delete_row"));
1369 removeCondition.addActionListener(new ActionListener()
1372 public void actionPerformed(ActionEvent e)
1374 filters.remove(filterIndex);
1378 filterRow.add(removeCondition);
1385 * Populates the drop-down list of comparison conditions for the given attribute
1386 * name. The conditions added depend on the datatype of the attribute values.
1387 * The supplied condition is set as the selected item in the list, provided it
1394 private void populateConditions(String attName, Condition cond,
1395 JComboBox<Condition> condCombo)
1397 Datatype type = FeatureAttributes.getInstance().getDatatype(featureType,
1399 if (MessageManager.getString("label.label").equals(attName))
1401 type = Datatype.Character;
1403 else if (MessageManager.getString("label.score").equals(attName))
1405 type = Datatype.Number;
1408 for (Condition c : Condition.values())
1410 if ((c.isNumeric() && type != Datatype.Character)
1411 || (!c.isNumeric() && type != Datatype.Number))
1413 condCombo.addItem(c);
1418 * set the selected condition (does nothing if not in the list)
1422 condCombo.setSelectedItem(cond);
1427 * Answers true unless a numeric condition has been selected with a non-numeric
1428 * value. Sets the value field to RED with a tooltip if in error.
1430 * If the pattern entered is empty, this method returns false, but does not mark
1431 * the field as invalid. This supports selecting an attribute for a new
1432 * condition before a match pattern has been entered.
1437 protected boolean validateFilter(JTextField value,
1438 JComboBox<Condition> condCombo)
1440 if (value == null || condCombo == null)
1442 return true; // fields not populated
1445 Condition cond = (Condition) condCombo.getSelectedItem();
1446 value.setBackground(Color.white);
1447 value.setToolTipText("");
1448 String v1 = value.getText().trim();
1449 if (v1.length() == 0)
1454 if (cond.isNumeric())
1459 } catch (NumberFormatException e)
1461 value.setBackground(Color.red);
1462 value.setToolTipText(
1463 MessageManager.getString("label.numeric_required"));
1472 * Constructs a filter condition from the given input fields, and replaces the
1473 * condition at filterIndex with the new one
1478 * @param filterIndex
1480 protected void updateFilter(JComboBox<String> attCombo,
1481 JComboBox<Condition> condCombo, JTextField valueField,
1484 String attName = (String) attCombo.getSelectedItem();
1485 Condition cond = (Condition) condCombo.getSelectedItem();
1486 String pattern = valueField.getText();
1487 KeyedMatcherI km = new KeyedMatcher(cond, pattern,
1488 fromAttributeDisplayName(attName));
1490 filters.set(filterIndex, km);
1494 * Makes the dialog visible, at the Feature Colour tab or at the Filters tab
1498 public void showTab(boolean coloursTab)
1501 tabbedPane.setSelectedIndex(coloursTab ? 0 : 1);
1505 * Action on any change to feature filtering, namely
1507 * <li>change of selected attribute</li>
1508 * <li>change of selected condition</li>
1509 * <li>change of match pattern</li>
1510 * <li>removal of a condition</li>
1512 * The inputs are parsed into a combined filter and this is set for the feature
1513 * type, and the alignment redrawn.
1515 protected void filtersChanged()
1518 * update the filter conditions for the feature type
1520 boolean anded = andFilters.isSelected();
1521 KeyedMatcherSetI combined = new KeyedMatcherSet();
1523 for (KeyedMatcherI filter : filters)
1525 String pattern = filter.getMatcher().getPattern();
1526 if (pattern.trim().length() > 0)
1530 combined.and(filter);
1534 combined.or(filter);
1540 * save the filter conditions in the FeatureRenderer
1541 * (note this might now be an empty filter with no conditions)
1543 fr.setFeatureFilter(featureType, combined.isEmpty() ? null : combined);
1544 ap.paintAlignment(true, true);