+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.gui;
+
+import jalview.api.AlignmentViewPanel;
+import jalview.api.FeatureColourI;
+import jalview.datamodel.GraphLine;
+import jalview.datamodel.features.FeatureAttributes;
+import jalview.datamodel.features.FeatureAttributes.Datatype;
+import jalview.schemes.FeatureColour;
+import jalview.util.ColorUtils;
+import jalview.util.MessageManager;
+import jalview.util.matcher.Condition;
+import jalview.util.matcher.KeyedMatcher;
+import jalview.util.matcher.KeyedMatcherI;
+import jalview.util.matcher.KeyedMatcherSet;
+import jalview.util.matcher.KeyedMatcherSetI;
+
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.awt.LayoutManager;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.BorderFactory;
+import javax.swing.BoxLayout;
+import javax.swing.ButtonGroup;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JColorChooser;
+import javax.swing.JComboBox;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JRadioButton;
+import javax.swing.JSlider;
+import javax.swing.JTabbedPane;
+import javax.swing.JTextField;
+import javax.swing.SwingConstants;
+import javax.swing.border.LineBorder;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import javax.swing.plaf.basic.BasicArrowButton;
+
+/**
+ * A dialog where the user can configure colour scheme, and any filters, for one
+ * feature type
+ * <p>
+ * (Was FeatureColourChooser prior to Jalview 1.11, renamed with the addition of
+ * filter options)
+ */
+public class FeatureTypeSettings extends JalviewDialog
+{
+ private static final int RADIO_WIDTH = 130;
+
+ private static final String COLON = ":";
+
+ private static final int MAX_TOOLTIP_LENGTH = 50;
+
+ private static final int NO_COLOUR_OPTION = 0;
+
+ private static final int MIN_COLOUR_OPTION = 1;
+
+ private static final int MAX_COLOUR_OPTION = 2;
+
+ private static final int ABOVE_THRESHOLD_OPTION = 1;
+
+ private static final int BELOW_THRESHOLD_OPTION = 2;
+
+ /*
+ * FeatureRenderer holds colour scheme and filters for feature types
+ */
+ private final FeatureRenderer fr; // todo refactor to allow interface type here
+
+ /*
+ * the view panel to update when settings change
+ */
+ private final AlignmentViewPanel ap;
+
+ private final String featureType;
+
+ /*
+ * the colour and filters to reset to on Cancel
+ */
+ private final FeatureColourI originalColour;
+
+ private final KeyedMatcherSetI originalFilter;
+
+ /*
+ * set flag to true when setting values programmatically,
+ * to avoid invocation of action handlers
+ */
+ private boolean adjusting = false;
+
+ private float min;
+
+ private float max;
+
+ private float scaleFactor;
+
+ /*
+ * radio button group, to select what to colour by:
+ * simple colour, by category (text), or graduated
+ */
+ private JRadioButton simpleColour = new JRadioButton();
+
+ private JRadioButton byCategory = new JRadioButton();
+
+ private JRadioButton graduatedColour = new JRadioButton();
+
+ private JPanel singleColour = new JPanel();
+
+ private JPanel minColour = new JPanel();
+
+ private JPanel maxColour = new JPanel();
+
+ private JComboBox<String> threshold = new JComboBox<>();
+
+ private JSlider slider = new JSlider();
+
+ private JTextField thresholdValue = new JTextField(20);
+
+ private JCheckBox thresholdIsMin = new JCheckBox();
+
+ private GraphLine threshline;
+
+ private ActionListener featureSettings = null;
+
+ private ActionListener changeColourAction;
+
+ /*
+ * choice of option for 'colour for no value'
+ */
+ private JComboBox<String> noValueCombo;
+
+ /*
+ * choice of what to colour by text (Label or attribute)
+ */
+ private JComboBox<String> colourByTextCombo;
+
+ /*
+ * choice of what to colour by range (Score or attribute)
+ */
+ private JComboBox<String> colourByRangeCombo;
+
+ private JRadioButton andFilters;
+
+ private JRadioButton orFilters;
+
+ /*
+ * filters for the currently selected feature type
+ */
+ private List<KeyedMatcherI> filters;
+
+ // set white normally, black to debug layout
+ private Color debugBorderColour = Color.white;
+
+ private JPanel chooseFiltersPanel;
+
+ private JTabbedPane tabbedPane;
+
+ /**
+ * Constructor
+ *
+ * @param frender
+ * @param theType
+ */
+ public FeatureTypeSettings(FeatureRenderer frender, String theType)
+ {
+ this(frender, false, theType);
+ }
+
+ /**
+ * Constructor, with option to make a blocking dialog (has to complete in the
+ * AWT event queue thread). Currently this option is always set to false.
+ *
+ * @param frender
+ * @param blocking
+ * @param theType
+ */
+ FeatureTypeSettings(FeatureRenderer frender, boolean blocking,
+ String theType)
+ {
+ this.fr = frender;
+ this.featureType = theType;
+ ap = fr.ap;
+ originalFilter = fr.getFeatureFilter(theType);
+ originalColour = fr.getFeatureColours().get(theType);
+
+ adjusting = true;
+
+ try
+ {
+ initialise();
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ return;
+ }
+
+ updateColoursTab();
+
+ updateFiltersTab();
+
+ adjusting = false;
+
+ colourChanged(false);
+
+ String title = MessageManager
+ .formatMessage("label.display_settings_for", new String[]
+ { theType });
+ initDialogFrame(this, true, blocking, title, 600, 360);
+
+ waitForInput();
+ }
+
+ /**
+ * Configures the widgets on the Colours tab according to the current feature
+ * colour scheme
+ */
+ private void updateColoursTab()
+ {
+ FeatureColourI fc = fr.getFeatureColours().get(featureType);
+
+ /*
+ * suppress action handling while updating values programmatically
+ */
+ adjusting = true;
+ try
+ {
+ /*
+ * single colour
+ */
+ if (fc.isSimpleColour())
+ {
+ simpleColour.setSelected(true);
+ singleColour.setBackground(fc.getColour());
+ singleColour.setForeground(fc.getColour());
+ }
+
+ /*
+ * colour by text (Label or attribute text)
+ */
+ if (fc.isColourByLabel())
+ {
+ byCategory.setSelected(true);
+ colourByTextCombo.setEnabled(colourByTextCombo.getItemCount() > 1);
+ if (fc.isColourByAttribute())
+ {
+ String[] attributeName = fc.getAttributeName();
+ colourByTextCombo
+ .setSelectedItem(toAttributeDisplayName(attributeName));
+ }
+ else
+ {
+ colourByTextCombo
+ .setSelectedItem(MessageManager.getString("label.label"));
+ }
+ }
+ else
+ {
+ colourByTextCombo.setEnabled(false);
+ }
+
+ if (!fc.isGraduatedColour())
+ {
+ colourByRangeCombo.setEnabled(false);
+ minColour.setEnabled(false);
+ maxColour.setEnabled(false);
+ noValueCombo.setEnabled(false);
+ threshold.setEnabled(false);
+ slider.setEnabled(false);
+ thresholdValue.setEnabled(false);
+ thresholdIsMin.setEnabled(false);
+ return;
+ }
+
+ /*
+ * Graduated colour, by score or attribute value range
+ */
+ graduatedColour.setSelected(true);
+ colourByRangeCombo.setEnabled(colourByRangeCombo.getItemCount() > 1);
+ minColour.setEnabled(true);
+ maxColour.setEnabled(true);
+ noValueCombo.setEnabled(true);
+ threshold.setEnabled(true);
+ minColour.setBackground(fc.getMinColour());
+ maxColour.setBackground(fc.getMaxColour());
+
+ if (fc.isColourByAttribute())
+ {
+ String[] attributeName = fc.getAttributeName();
+ colourByRangeCombo
+ .setSelectedItem(toAttributeDisplayName(attributeName));
+ }
+ else
+ {
+ colourByRangeCombo
+ .setSelectedItem(MessageManager.getString("label.score"));
+ }
+ Color noColour = fc.getNoColour();
+ if (noColour == null)
+ {
+ noValueCombo.setSelectedIndex(NO_COLOUR_OPTION);
+ }
+ else if (noColour.equals(fc.getMinColour()))
+ {
+ noValueCombo.setSelectedIndex(MIN_COLOUR_OPTION);
+ }
+ else if (noColour.equals(fc.getMaxColour()))
+ {
+ noValueCombo.setSelectedIndex(MAX_COLOUR_OPTION);
+ }
+
+ /*
+ * update min-max scaling if there is a range to work with,
+ * else disable the widgets (this shouldn't happen if only
+ * valid options are offered in the combo box)
+ */
+ scaleFactor = (max == min) ? 1f : 100f / (max - min);
+ float range = (max - min) * scaleFactor;
+ slider.setMinimum((int) (min * scaleFactor));
+ slider.setMaximum((int) (max * scaleFactor));
+ slider.setMajorTickSpacing((int) (range / 10f));
+
+ threshline = new GraphLine((max - min) / 2f, "Threshold",
+ Color.black);
+ threshline.value = fc.getThreshold();
+
+ if (fc.hasThreshold())
+ {
+ threshold.setSelectedIndex(
+ fc.isAboveThreshold() ? ABOVE_THRESHOLD_OPTION
+ : BELOW_THRESHOLD_OPTION);
+ slider.setEnabled(true);
+ slider.setValue((int) (fc.getThreshold() * scaleFactor));
+ thresholdValue.setText(String.valueOf(getRoundedSliderValue()));
+ thresholdValue.setEnabled(true);
+ thresholdIsMin.setEnabled(true);
+ }
+ else
+ {
+ slider.setEnabled(false);
+ thresholdValue.setEnabled(false);
+ thresholdIsMin.setEnabled(false);
+ }
+ thresholdIsMin.setSelected(!fc.isAutoScaled());
+ } finally
+ {
+ adjusting = false;
+ }
+ }
+
+ /**
+ * Configures the initial layout
+ */
+ private void initialise()
+ {
+ this.setLayout(new BorderLayout());
+ tabbedPane = new JTabbedPane();
+ this.add(tabbedPane, BorderLayout.CENTER);
+
+ /*
+ * an ActionListener that applies colour changes
+ */
+ changeColourAction = new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ colourChanged(true);
+ }
+ };
+
+ /*
+ * first tab: colour options
+ */
+ JPanel coloursPanel = initialiseColoursPanel();
+ tabbedPane.addTab(MessageManager.getString("action.colour"),
+ coloursPanel);
+
+ /*
+ * second tab: filter options
+ */
+ JPanel filtersPanel = initialiseFiltersPanel();
+ tabbedPane.addTab(MessageManager.getString("label.filters"),
+ filtersPanel);
+
+ JPanel okCancelPanel = initialiseOkCancelPanel();
+
+ this.add(okCancelPanel, BorderLayout.SOUTH);
+ }
+
+ /**
+ * Updates the min-max range if Colour By selected item is Score, or an
+ * attribute, with a min-max range
+ */
+ protected void updateMinMax()
+ {
+ if (!graduatedColour.isSelected())
+ {
+ return;
+ }
+
+ float[] minMax = null;
+ String colourBy = (String) colourByRangeCombo.getSelectedItem();
+ if (MessageManager.getString("label.score").equals(colourBy))
+ {
+ minMax = fr.getMinMax().get(featureType)[0];
+ }
+ else
+ {
+ // colour by attribute range
+ String[] attNames = fromAttributeDisplayName(colourBy);
+ minMax = FeatureAttributes.getInstance().getMinMax(featureType,
+ attNames);
+ }
+
+ if (minMax != null)
+ {
+ min = minMax[0];
+ max = minMax[1];
+ }
+ }
+
+ /**
+ * Lay out fields for graduated colour (by score or attribute value)
+ *
+ * @return
+ */
+ private JPanel initialiseGraduatedColourPanel()
+ {
+ JPanel graduatedColourPanel = new JPanel();
+ graduatedColourPanel.setLayout(
+ new BoxLayout(graduatedColourPanel, BoxLayout.Y_AXIS));
+ JvSwingUtils.createTitledBorder(graduatedColourPanel,
+ MessageManager.getString("label.graduated_colour"), true);
+ graduatedColourPanel.setBackground(Color.white);
+
+ /*
+ * first row: graduated colour radio button, score/attribute drop-down
+ */
+ JPanel graduatedChoicePanel = new JPanel(
+ new FlowLayout(FlowLayout.LEFT));
+ graduatedChoicePanel.setBackground(Color.white);
+ graduatedColour = new JRadioButton(
+ MessageManager.getString("label.by_range_of") + COLON);
+ graduatedColour.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
+ graduatedColour.addItemListener(new ItemListener()
+ {
+ @Override
+ public void itemStateChanged(ItemEvent e)
+ {
+ if (graduatedColour.isSelected())
+ {
+ colourChanged(true);
+ }
+ }
+ });
+ graduatedChoicePanel.add(graduatedColour);
+
+ List<String[]> attNames = FeatureAttributes.getInstance()
+ .getAttributes(featureType);
+ colourByRangeCombo = populateAttributesDropdown(attNames, true, false);
+ colourByRangeCombo.addItemListener(new ItemListener()
+ {
+ @Override
+ public void itemStateChanged(ItemEvent e)
+ {
+ colourChanged(true);
+ }
+ });
+
+ /*
+ * disable graduated colour option if no range found
+ */
+ graduatedColour.setEnabled(colourByRangeCombo.getItemCount() > 0);
+
+ graduatedChoicePanel.add(colourByRangeCombo);
+ graduatedColourPanel.add(graduatedChoicePanel);
+
+ /*
+ * second row - min/max/no colours
+ */
+ JPanel colourRangePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
+ colourRangePanel.setBackground(Color.white);
+ graduatedColourPanel.add(colourRangePanel);
+
+ minColour.setFont(JvSwingUtils.getLabelFont());
+ minColour.setBorder(BorderFactory.createLineBorder(Color.black));
+ minColour.setPreferredSize(new Dimension(40, 20));
+ minColour.setToolTipText(MessageManager.getString("label.min_colour"));
+ minColour.addMouseListener(new MouseAdapter()
+ {
+ @Override
+ public void mousePressed(MouseEvent e)
+ {
+ if (minColour.isEnabled())
+ {
+ showColourChooser(minColour, "label.select_colour_minimum_value");
+ }
+ }
+ });
+
+ maxColour.setFont(JvSwingUtils.getLabelFont());
+ maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
+ maxColour.setPreferredSize(new Dimension(40, 20));
+ maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
+ maxColour.addMouseListener(new MouseAdapter()
+ {
+ @Override
+ public void mousePressed(MouseEvent e)
+ {
+ if (maxColour.isEnabled())
+ {
+ showColourChooser(maxColour, "label.select_colour_maximum_value");
+ }
+ }
+ });
+ maxColour.setBorder(new LineBorder(Color.black));
+
+ /*
+ * default max colour to current colour (if a plain colour),
+ * or to Black if colour by label; make min colour a pale
+ * version of max colour
+ */
+ FeatureColourI fc = fr.getFeatureColours().get(featureType);
+ Color bg = fc.isSimpleColour() ? fc.getColour() : Color.BLACK;
+ maxColour.setBackground(bg);
+ minColour.setBackground(ColorUtils.bleachColour(bg, 0.9f));
+
+ noValueCombo = new JComboBox<>();
+ noValueCombo.addItem(MessageManager.getString("label.no_colour"));
+ noValueCombo.addItem(MessageManager.getString("label.min_colour"));
+ noValueCombo.addItem(MessageManager.getString("label.max_colour"));
+ noValueCombo.addItemListener(new ItemListener()
+ {
+ @Override
+ public void itemStateChanged(ItemEvent e)
+ {
+ colourChanged(true);
+ }
+ });
+
+ JLabel minText = new JLabel(
+ MessageManager.getString("label.min_value") + COLON);
+ minText.setFont(JvSwingUtils.getLabelFont());
+ JLabel maxText = new JLabel(
+ MessageManager.getString("label.max_value") + COLON);
+ maxText.setFont(JvSwingUtils.getLabelFont());
+ JLabel noText = new JLabel(
+ MessageManager.getString("label.no_value") + COLON);
+ noText.setFont(JvSwingUtils.getLabelFont());
+
+ colourRangePanel.add(minText);
+ colourRangePanel.add(minColour);
+ colourRangePanel.add(maxText);
+ colourRangePanel.add(maxColour);
+ colourRangePanel.add(noText);
+ colourRangePanel.add(noValueCombo);
+
+ /*
+ * third row - threshold options and value
+ */
+ JPanel thresholdPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
+ thresholdPanel.setBackground(Color.white);
+ graduatedColourPanel.add(thresholdPanel);
+
+ threshold.addActionListener(changeColourAction);
+ threshold.setToolTipText(MessageManager
+ .getString("label.threshold_feature_display_by_score"));
+ threshold.addItem(MessageManager
+ .getString("label.threshold_feature_no_threshold")); // index 0
+ threshold.addItem(MessageManager
+ .getString("label.threshold_feature_above_threshold")); // index 1
+ threshold.addItem(MessageManager
+ .getString("label.threshold_feature_below_threshold")); // index 2
+
+ thresholdValue.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ thresholdValue_actionPerformed();
+ }
+ });
+ thresholdValue.addFocusListener(new FocusAdapter()
+ {
+ @Override
+ public void focusLost(FocusEvent e)
+ {
+ thresholdValue_actionPerformed();
+ }
+ });
+ slider.setPaintLabels(false);
+ slider.setPaintTicks(true);
+ slider.setBackground(Color.white);
+ slider.setEnabled(false);
+ slider.setOpaque(false);
+ slider.setPreferredSize(new Dimension(100, 32));
+ slider.setToolTipText(
+ MessageManager.getString("label.adjust_threshold"));
+
+ slider.addChangeListener(new ChangeListener()
+ {
+ @Override
+ public void stateChanged(ChangeEvent evt)
+ {
+ if (!adjusting)
+ {
+ thresholdValue
+ .setText(String.valueOf(slider.getValue() / scaleFactor));
+ sliderValueChanged();
+ }
+ }
+ });
+ slider.addMouseListener(new MouseAdapter()
+ {
+ @Override
+ public void mouseReleased(MouseEvent evt)
+ {
+ /*
+ * only update Overview and/or structure colouring
+ * when threshold slider drag ends (mouse up)
+ */
+ if (ap != null)
+ {
+ ap.paintAlignment(true, true);
+ }
+ }
+ });
+
+ thresholdValue.setEnabled(false);
+ thresholdValue.setColumns(7);
+
+ thresholdPanel.add(threshold);
+ thresholdPanel.add(slider);
+ thresholdPanel.add(thresholdValue);
+
+ thresholdIsMin.setBackground(Color.white);
+ thresholdIsMin
+ .setText(MessageManager.getString("label.threshold_minmax"));
+ thresholdIsMin.setToolTipText(MessageManager
+ .getString("label.toggle_absolute_relative_display_threshold"));
+ thresholdIsMin.addActionListener(changeColourAction);
+ thresholdPanel.add(thresholdIsMin);
+
+ return graduatedColourPanel;
+ }
+
+ /**
+ * Lay out OK and Cancel buttons
+ *
+ * @return
+ */
+ private JPanel initialiseOkCancelPanel()
+ {
+ JPanel okCancelPanel = new JPanel();
+ // okCancelPanel.setBackground(Color.white);
+ okCancelPanel.add(ok);
+ okCancelPanel.add(cancel);
+ return okCancelPanel;
+ }
+
+ /**
+ * Lay out Colour options panel, containing
+ * <ul>
+ * <li>plain colour, with colour picker</li>
+ * <li>colour by text, with choice of Label or other attribute</li>
+ * <li>colour by range, of score or other attribute, when available</li>
+ * </ul>
+ *
+ * @return
+ */
+ private JPanel initialiseColoursPanel()
+ {
+ JPanel colourByPanel = new JPanel();
+ colourByPanel.setLayout(new BoxLayout(colourByPanel, BoxLayout.Y_AXIS));
+
+ /*
+ * simple colour radio button and colour picker
+ */
+ JPanel simpleColourPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
+ simpleColourPanel.setBackground(Color.white);
+ JvSwingUtils.createTitledBorder(simpleColourPanel,
+ MessageManager.getString("label.simple"), true);
+ colourByPanel.add(simpleColourPanel);
+
+ simpleColour = new JRadioButton(
+ MessageManager.getString("label.simple_colour"));
+ simpleColour.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
+ simpleColour.addItemListener(new ItemListener()
+ {
+ @Override
+ public void itemStateChanged(ItemEvent e)
+ {
+ if (simpleColour.isSelected() && !adjusting)
+ {
+ showColourChooser(singleColour, "label.select_colour");
+ }
+ }
+
+ });
+
+ singleColour.setFont(JvSwingUtils.getLabelFont());
+ singleColour.setBorder(BorderFactory.createLineBorder(Color.black));
+ singleColour.setPreferredSize(new Dimension(40, 20));
+ singleColour.addMouseListener(new MouseAdapter()
+ {
+ @Override
+ public void mousePressed(MouseEvent e)
+ {
+ if (simpleColour.isSelected())
+ {
+ showColourChooser(singleColour, "label.select_colour");
+ }
+ }
+ });
+ simpleColourPanel.add(simpleColour); // radio button
+ simpleColourPanel.add(singleColour); // colour picker button
+
+ /*
+ * colour by text (category) radio button and drop-down choice list
+ */
+ JPanel byTextPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
+ byTextPanel.setBackground(Color.white);
+ JvSwingUtils.createTitledBorder(byTextPanel,
+ MessageManager.getString("label.colour_by_text"), true);
+ colourByPanel.add(byTextPanel);
+ byCategory = new JRadioButton(
+ MessageManager.getString("label.by_text_of") + COLON);
+ byCategory.setPreferredSize(new Dimension(RADIO_WIDTH, 20));
+ byCategory.addItemListener(new ItemListener()
+ {
+ @Override
+ public void itemStateChanged(ItemEvent e)
+ {
+ if (byCategory.isSelected())
+ {
+ colourChanged(true);
+ }
+ }
+ });
+ byTextPanel.add(byCategory);
+
+ List<String[]> attNames = FeatureAttributes.getInstance()
+ .getAttributes(featureType);
+ colourByTextCombo = populateAttributesDropdown(attNames, false, true);
+ colourByTextCombo.addItemListener(new ItemListener()
+ {
+ @Override
+ public void itemStateChanged(ItemEvent e)
+ {
+ colourChanged(true);
+ }
+ });
+ byTextPanel.add(colourByTextCombo);
+
+ /*
+ * graduated colour panel
+ */
+ JPanel graduatedColourPanel = initialiseGraduatedColourPanel();
+ colourByPanel.add(graduatedColourPanel);
+
+ /*
+ * 3 radio buttons select between simple colour,
+ * by category (text), or graduated
+ */
+ ButtonGroup bg = new ButtonGroup();
+ bg.add(simpleColour);
+ bg.add(byCategory);
+ bg.add(graduatedColour);
+
+ return colourByPanel;
+ }
+
+ private void showColourChooser(JPanel colourPanel, String key)
+ {
+ Color col = JColorChooser.showDialog(this,
+ MessageManager.getString(key), colourPanel.getBackground());
+ if (col != null)
+ {
+ colourPanel.setBackground(col);
+ colourPanel.setForeground(col);
+ }
+ colourPanel.repaint();
+ colourChanged(true);
+ }
+
+ /**
+ * Constructs and sets the selected colour options as the colour for the feature
+ * type, and repaints the alignment, and optionally the Overview and/or
+ * structure viewer if open
+ *
+ * @param updateStructsAndOverview
+ */
+ void colourChanged(boolean updateStructsAndOverview)
+ {
+ if (adjusting)
+ {
+ /*
+ * ignore action handlers while setting values programmatically
+ */
+ return;
+ }
+
+ /*
+ * ensure min-max range is for the latest choice of
+ * 'graduated colour by'
+ */
+ updateMinMax();
+
+ FeatureColourI acg = makeColourFromInputs();
+
+ /*
+ * save the colour, and repaint stuff
+ */
+ fr.setColour(featureType, acg);
+ ap.paintAlignment(updateStructsAndOverview, updateStructsAndOverview);
+
+ updateColoursTab();
+ }
+
+ /**
+ * Converts the input values into an instance of FeatureColour
+ *
+ * @return
+ */
+ private FeatureColourI makeColourFromInputs()
+ {
+ /*
+ * easiest case - a single colour
+ */
+ if (simpleColour.isSelected())
+ {
+ return new FeatureColour(singleColour.getBackground());
+ }
+
+ /*
+ * next easiest case - colour by Label, or attribute text
+ */
+ if (byCategory.isSelected())
+ {
+ Color c = this.getBackground();
+ FeatureColourI fc = new FeatureColour(c, c, null, 0f, 0f);
+ fc.setColourByLabel(true);
+ String byWhat = (String) colourByTextCombo.getSelectedItem();
+ if (!MessageManager.getString("label.label").equals(byWhat))
+ {
+ fc.setAttributeName(fromAttributeDisplayName(byWhat));
+ }
+ return fc;
+ }
+
+ /*
+ * remaining case - graduated colour by score, or attribute value
+ */
+ Color noColour = null;
+ if (noValueCombo.getSelectedIndex() == MIN_COLOUR_OPTION)
+ {
+ noColour = minColour.getBackground();
+ }
+ else if (noValueCombo.getSelectedIndex() == MAX_COLOUR_OPTION)
+ {
+ noColour = maxColour.getBackground();
+ }
+
+ float thresh = 0f;
+ try
+ {
+ thresh = Float.valueOf(thresholdValue.getText());
+ } catch (NumberFormatException e)
+ {
+ // invalid inputs are already handled on entry
+ }
+
+ /*
+ * min-max range is to (or from) threshold value if
+ * 'threshold is min/max' is selected
+ */
+ float minValue = min;
+ float maxValue = max;
+ final int thresholdOption = threshold.getSelectedIndex();
+ if (thresholdIsMin.isSelected()
+ && thresholdOption == ABOVE_THRESHOLD_OPTION)
+ {
+ minValue = thresh;
+ }
+ if (thresholdIsMin.isSelected()
+ && thresholdOption == BELOW_THRESHOLD_OPTION)
+ {
+ maxValue = thresh;
+ }
+
+ /*
+ * make the graduated colour
+ */
+ FeatureColourI fc = new FeatureColour(minColour.getBackground(),
+ maxColour.getBackground(), noColour, minValue, maxValue);
+
+ /*
+ * set attribute to colour by if selected
+ */
+ String byWhat = (String) colourByRangeCombo.getSelectedItem();
+ if (!MessageManager.getString("label.score").equals(byWhat))
+ {
+ fc.setAttributeName(fromAttributeDisplayName(byWhat));
+ }
+
+ /*
+ * set threshold options and 'autoscaled' which is
+ * false if 'threshold is min/max' is selected
+ * else true (colour range is on actual range of values)
+ */
+ fc.setThreshold(thresh);
+ fc.setAutoScaled(!thresholdIsMin.isSelected());
+ fc.setAboveThreshold(thresholdOption == ABOVE_THRESHOLD_OPTION);
+ fc.setBelowThreshold(thresholdOption == BELOW_THRESHOLD_OPTION);
+
+ if (threshline == null)
+ {
+ /*
+ * todo not yet implemented: visual indication of feature threshold
+ */
+ threshline = new GraphLine((max - min) / 2f, "Threshold",
+ Color.black);
+ }
+
+ return fc;
+ }
+
+ /**
+ * A helper method that converts a 'compound' attribute name from its display
+ * form, e.g. CSQ:PolyPhen to array form, e.g. { "CSQ", "PolyPhen" }
+ *
+ * @param attribute
+ * @return
+ */
+ private String[] fromAttributeDisplayName(String attribute)
+ {
+ return attribute == null ? null : attribute.split(COLON);
+ }
+
+ /**
+ * A helper method that converts a 'compound' attribute name to its display
+ * form, e.g. CSQ:PolyPhen from its array form, e.g. { "CSQ", "PolyPhen" }
+ *
+ * @param attName
+ * @return
+ */
+ private String toAttributeDisplayName(String[] attName)
+ {
+ return attName == null ? "" : String.join(COLON, attName);
+ }
+
+ @Override
+ protected void raiseClosed()
+ {
+ if (this.featureSettings != null)
+ {
+ featureSettings.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
+ }
+ }
+
+ /**
+ * Action on OK is just to dismiss the dialog - any changes have already been
+ * applied
+ */
+ @Override
+ public void okPressed()
+ {
+ }
+
+ /**
+ * Action on Cancel is to restore colour scheme and filters as they were when
+ * the dialog was opened
+ */
+ @Override
+ public void cancelPressed()
+ {
+ fr.setColour(featureType, originalColour);
+ fr.setFeatureFilter(featureType, originalFilter);
+ ap.paintAlignment(true, true);
+ }
+
+ /**
+ * Action on text entry of a threshold value
+ */
+ protected void thresholdValue_actionPerformed()
+ {
+ try
+ {
+ adjusting = true;
+ float f = Float.parseFloat(thresholdValue.getText());
+ slider.setValue((int) (f * scaleFactor));
+ threshline.value = f;
+ thresholdValue.setBackground(Color.white); // ok
+
+ /*
+ * force repaint of any Overview window or structure
+ */
+ ap.paintAlignment(true, true);
+ } catch (NumberFormatException ex)
+ {
+ thresholdValue.setBackground(Color.red); // not ok
+ } finally
+ {
+ adjusting = false;
+ }
+ }
+
+ /**
+ * Action on change of threshold slider value. This may be done interactively
+ * (by moving the slider), or programmatically (to update the slider after
+ * manual input of a threshold value).
+ */
+ protected void sliderValueChanged()
+ {
+ threshline.value = getRoundedSliderValue();
+
+ /*
+ * repaint alignment, but not Overview or structure,
+ * to avoid overload while dragging the slider
+ */
+ colourChanged(false);
+ }
+
+ /**
+ * Converts the slider value to its absolute value by dividing by the
+ * scaleFactor. Rounding errors are squashed by forcing min/max of slider range
+ * to the actual min/max of feature score range
+ *
+ * @return
+ */
+ private float getRoundedSliderValue()
+ {
+ int value = slider.getValue();
+ float f = value == slider.getMaximum() ? max
+ : (value == slider.getMinimum() ? min : value / scaleFactor);
+ return f;
+ }
+
+ void addActionListener(ActionListener listener)
+ {
+ if (featureSettings != null)
+ {
+ System.err.println(
+ "IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
+ }
+ featureSettings = listener;
+ }
+
+ /**
+ * A helper method to build the drop-down choice of attributes for a feature. If
+ * 'withRange' is true, then Score, and any attributes with a min-max range, are
+ * added. If 'withText' is true, Label and any known attributes are added. This
+ * allows 'categorical numerical' attributes e.g. codon position to be coloured
+ * by text.
+ * <p>
+ * Where metadata is available with a description for an attribute, that is
+ * added as a tooltip.
+ * <p>
+ * Attribute names may be 'simple' e.g. "AC" or 'compound' e.g. {"CSQ",
+ * "Allele"}. Compound names are rendered for display as (e.g.) CSQ:Allele.
+ * <p>
+ * This method does not add any ActionListener to the JComboBox.
+ *
+ * @param attNames
+ * @param withRange
+ * @param withText
+ */
+ protected JComboBox<String> populateAttributesDropdown(
+ List<String[]> attNames, boolean withRange, boolean withText)
+ {
+ List<String> displayAtts = new ArrayList<>();
+ List<String> tooltips = new ArrayList<>();
+
+ if (withText)
+ {
+ displayAtts.add(MessageManager.getString("label.label"));
+ tooltips.add(MessageManager.getString("label.description"));
+ }
+ if (withRange)
+ {
+ float[][] minMax = fr.getMinMax().get(featureType);
+ if (minMax != null && minMax[0][0] != minMax[0][1])
+ {
+ displayAtts.add(MessageManager.getString("label.score"));
+ tooltips.add(MessageManager.getString("label.score"));
+ }
+ }
+
+ FeatureAttributes fa = FeatureAttributes.getInstance();
+ for (String[] attName : attNames)
+ {
+ float[] minMax = fa.getMinMax(featureType, attName);
+ boolean hasRange = minMax != null && minMax[0] != minMax[1];
+ if (!withText && !hasRange)
+ {
+ continue;
+ }
+ displayAtts.add(toAttributeDisplayName(attName));
+ String desc = fa.getDescription(featureType, attName);
+ if (desc != null && desc.length() > MAX_TOOLTIP_LENGTH)
+ {
+ desc = desc.substring(0, MAX_TOOLTIP_LENGTH) + "...";
+ }
+ tooltips.add(desc == null ? "" : desc);
+ }
+
+ JComboBox<String> attCombo = JvSwingUtils
+ .buildComboWithTooltips(displayAtts, tooltips);
+
+ return attCombo;
+ }
+
+ /**
+ * Populates initial layout of the feature attribute filters panel
+ */
+ private JPanel initialiseFiltersPanel()
+ {
+ filters = new ArrayList<>();
+
+ JPanel filtersPanel = new JPanel();
+ filtersPanel.setLayout(new BoxLayout(filtersPanel, BoxLayout.Y_AXIS));
+ filtersPanel.setBackground(Color.white);
+ JvSwingUtils.createTitledBorder(filtersPanel,
+ MessageManager.getString("label.filters"), true);
+
+ JPanel andOrPanel = initialiseAndOrPanel();
+ filtersPanel.add(andOrPanel);
+
+ /*
+ * panel with filters - populated by refreshFiltersDisplay
+ */
+ chooseFiltersPanel = new JPanel();
+ LayoutManager box = new BoxLayout(chooseFiltersPanel, BoxLayout.Y_AXIS);
+ chooseFiltersPanel.setLayout(box);
+ filtersPanel.add(chooseFiltersPanel);
+
+ return filtersPanel;
+ }
+
+ /**
+ * Lays out the panel with radio buttons to AND or OR filter conditions
+ *
+ * @return
+ */
+ private JPanel initialiseAndOrPanel()
+ {
+ JPanel andOrPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
+ andOrPanel.setBackground(Color.white);
+ andOrPanel.setBorder(BorderFactory.createLineBorder(debugBorderColour));
+ andFilters = new JRadioButton(MessageManager.getString("label.and"));
+ orFilters = new JRadioButton(MessageManager.getString("label.or"));
+ ActionListener actionListener = new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ filtersChanged();
+ }
+ };
+ andFilters.addActionListener(actionListener);
+ orFilters.addActionListener(actionListener);
+ ButtonGroup andOr = new ButtonGroup();
+ andOr.add(andFilters);
+ andOr.add(orFilters);
+ andFilters.setSelected(true);
+ andOrPanel.add(
+ new JLabel(MessageManager.getString("label.join_conditions")));
+ andOrPanel.add(andFilters);
+ andOrPanel.add(orFilters);
+ return andOrPanel;
+ }
+
+ /**
+ * Refreshes the display to show any filters currently configured for the
+ * selected feature type (editable, with 'remove' option), plus one extra row
+ * for adding a condition. This should be called after a filter has been
+ * removed, added or amended.
+ */
+ private void updateFiltersTab()
+ {
+ /*
+ * clear the panel and list of filter conditions
+ */
+ chooseFiltersPanel.removeAll();
+ filters.clear();
+
+ /*
+ * look up attributes known for feature type
+ */
+ List<String[]> attNames = FeatureAttributes.getInstance()
+ .getAttributes(featureType);
+
+ /*
+ * if this feature type has filters set, load them first
+ */
+ KeyedMatcherSetI featureFilters = fr.getFeatureFilter(featureType);
+ if (featureFilters != null)
+ {
+ if (!featureFilters.isAnded())
+ {
+ orFilters.setSelected(true);
+ }
+ featureFilters.getMatchers().forEach(matcher -> filters.add(matcher));
+ }
+
+ /*
+ * and an empty filter for the user to populate (add)
+ */
+ KeyedMatcherI noFilter = new KeyedMatcher(Condition.values()[0], "",
+ (String) null);
+ filters.add(noFilter);
+
+ /*
+ * render the conditions in rows, each in its own JPanel
+ */
+ int filterIndex = 0;
+ for (KeyedMatcherI filter : filters)
+ {
+ String[] attName = filter.getKey();
+ Condition condition = filter.getMatcher().getCondition();
+ String pattern = filter.getMatcher().getPattern();
+ JPanel row = addFilter(attName, attNames, condition, pattern,
+ filterIndex);
+ row.setBorder(BorderFactory.createLineBorder(debugBorderColour));
+ chooseFiltersPanel.add(row);
+ filterIndex++;
+ }
+
+ this.validate();
+ this.repaint();
+ }
+
+ /**
+ * A helper method that constructs a row (panel) with one filter condition:
+ * <ul>
+ * <li>a drop-down list of attribute names to choose from</li>
+ * <li>a drop-down list of conditions to choose from</li>
+ * <li>a text field for input of a match pattern</li>
+ * <li>optionally, a 'remove' button</li>
+ * </ul>
+ * If attribute, condition or pattern are not null, they are set as defaults for
+ * the input fields. The 'remove' button is added unless the pattern is null or
+ * empty (incomplete filter condition).
+ *
+ * @param attName
+ * @param attNames
+ * @param cond
+ * @param pattern
+ * @param filterIndex
+ * @return
+ */
+ protected JPanel addFilter(String[] attName, List<String[]> attNames,
+ Condition cond, String pattern, int filterIndex)
+ {
+ JPanel filterRow = new JPanel(new FlowLayout(FlowLayout.LEFT));
+ filterRow.setBackground(Color.white);
+
+ /*
+ * drop-down choice of attribute, with description as a tooltip
+ * if we can obtain it
+ */
+ final JComboBox<String> attCombo = populateAttributesDropdown(attNames,
+ true, true);
+ JComboBox<Condition> condCombo = new JComboBox<>();
+ JTextField patternField = new JTextField(8);
+
+ /*
+ * action handlers that validate and (if valid) apply changes
+ */
+ ActionListener actionListener = new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ if (attCombo.getSelectedItem() != null)
+ {
+ if (validateFilter(patternField, condCombo))
+ {
+ updateFilter(attCombo, condCombo, patternField, filterIndex);
+ filtersChanged();
+ }
+ }
+ }
+ };
+ ItemListener itemListener = new ItemListener()
+ {
+ @Override
+ public void itemStateChanged(ItemEvent e)
+ {
+ actionListener.actionPerformed(null);
+ }
+ };
+
+ if (attName == null) // the 'add a condition' row
+ {
+ attCombo.setSelectedIndex(0);
+ }
+ else
+ {
+ attCombo.setSelectedItem(toAttributeDisplayName(attName));
+ }
+ attCombo.addItemListener(itemListener);
+
+ filterRow.add(attCombo);
+
+ /*
+ * drop-down choice of test condition
+ */
+ populateConditions((String) attCombo.getSelectedItem(), cond,
+ condCombo);
+ condCombo.addItemListener(itemListener);
+ filterRow.add(condCombo);
+
+ /*
+ * pattern to match against
+ */
+ patternField.setText(pattern);
+ patternField.addActionListener(actionListener);
+ patternField.addFocusListener(new FocusAdapter()
+ {
+ @Override
+ public void focusLost(FocusEvent e)
+ {
+ actionListener.actionPerformed(null);
+ }
+ });
+ filterRow.add(patternField);
+
+ /*
+ * add remove button if filter is populated (non-empty pattern)
+ */
+ if (pattern != null && pattern.trim().length() > 0)
+ {
+ // todo: gif for button drawing '-' or 'x'
+ JButton removeCondition = new BasicArrowButton(SwingConstants.WEST);
+ removeCondition
+ .setToolTipText(MessageManager.getString("label.delete_row"));
+ removeCondition.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ filters.remove(filterIndex);
+ filtersChanged();
+ }
+ });
+ filterRow.add(removeCondition);
+ }
+
+ return filterRow;
+ }
+
+ /**
+ * Populates the drop-down list of comparison conditions for the given attribute
+ * name. The conditions added depend on the datatype of the attribute values.
+ * The supplied condition is set as the selected item in the list, provided it
+ * is in the list.
+ *
+ * @param attName
+ * @param cond
+ * @param condCombo
+ */
+ private void populateConditions(String attName, Condition cond,
+ JComboBox<Condition> condCombo)
+ {
+ Datatype type = FeatureAttributes.getInstance().getDatatype(featureType,
+ attName);
+ if (MessageManager.getString("label.label").equals(attName))
+ {
+ type = Datatype.Character;
+ }
+ else if (MessageManager.getString("label.score").equals(attName))
+ {
+ type = Datatype.Number;
+ }
+
+ for (Condition c : Condition.values())
+ {
+ if ((c.isNumeric() && type != Datatype.Character)
+ || (!c.isNumeric() && type != Datatype.Number))
+ {
+ condCombo.addItem(c);
+ }
+ }
+
+ /*
+ * set the selected condition (does nothing if not in the list)
+ */
+ if (cond != null)
+ {
+ condCombo.setSelectedItem(cond);
+ }
+ }
+
+ /**
+ * Answers true unless a numeric condition has been selected with a non-numeric
+ * value. Sets the value field to RED with a tooltip if in error.
+ * <p>
+ * If the pattern entered is empty, this method returns false, but does not mark
+ * the field as invalid. This supports selecting an attribute for a new
+ * condition before a match pattern has been entered.
+ *
+ * @param value
+ * @param condCombo
+ */
+ protected boolean validateFilter(JTextField value,
+ JComboBox<Condition> condCombo)
+ {
+ if (value == null || condCombo == null)
+ {
+ return true; // fields not populated
+ }
+
+ Condition cond = (Condition) condCombo.getSelectedItem();
+ value.setBackground(Color.white);
+ value.setToolTipText("");
+ String v1 = value.getText().trim();
+ if (v1.length() == 0)
+ {
+ return false;
+ }
+
+ if (cond.isNumeric())
+ {
+ try
+ {
+ Float.valueOf(v1);
+ } catch (NumberFormatException e)
+ {
+ value.setBackground(Color.red);
+ value.setToolTipText(
+ MessageManager.getString("label.numeric_required"));
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Constructs a filter condition from the given input fields, and replaces the
+ * condition at filterIndex with the new one
+ *
+ * @param attCombo
+ * @param condCombo
+ * @param valueField
+ * @param filterIndex
+ */
+ protected void updateFilter(JComboBox<String> attCombo,
+ JComboBox<Condition> condCombo, JTextField valueField,
+ int filterIndex)
+ {
+ String attName = (String) attCombo.getSelectedItem();
+ Condition cond = (Condition) condCombo.getSelectedItem();
+ String pattern = valueField.getText();
+ KeyedMatcherI km = new KeyedMatcher(cond, pattern,
+ fromAttributeDisplayName(attName));
+
+ filters.set(filterIndex, km);
+ }
+
+ /**
+ * Makes the dialog visible, at the Feature Colour tab or at the Filters tab
+ *
+ * @param coloursTab
+ */
+ public void showTab(boolean coloursTab)
+ {
+ setVisible(true);
+ tabbedPane.setSelectedIndex(coloursTab ? 0 : 1);
+ }
+
+ /**
+ * Action on any change to feature filtering, namely
+ * <ul>
+ * <li>change of selected attribute</li>
+ * <li>change of selected condition</li>
+ * <li>change of match pattern</li>
+ * <li>removal of a condition</li>
+ * </ul>
+ * The inputs are parsed into a combined filter and this is set for the feature
+ * type, and the alignment redrawn.
+ */
+ protected void filtersChanged()
+ {
+ /*
+ * update the filter conditions for the feature type
+ */
+ boolean anded = andFilters.isSelected();
+ KeyedMatcherSetI combined = new KeyedMatcherSet();
+
+ for (KeyedMatcherI filter : filters)
+ {
+ String pattern = filter.getMatcher().getPattern();
+ if (pattern.trim().length() > 0)
+ {
+ if (anded)
+ {
+ combined.and(filter);
+ }
+ else
+ {
+ combined.or(filter);
+ }
+ }
+ }
+
+ /*
+ * save the filter conditions in the FeatureRenderer
+ * (note this might now be an empty filter with no conditions)
+ */
+ fr.setFeatureFilter(featureType, combined.isEmpty() ? null : combined);
+ ap.paintAlignment(true, true);
+
+ updateFiltersTab();
+ }
+}