JAL-2835 spike updated with latest
[jalview.git] / src / jalview / gui / FeatureColourChooser.java
index ddfa162..da3819c 100644 (file)
@@ -57,8 +57,16 @@ import javax.swing.event.ChangeListener;
 
 public class FeatureColourChooser extends JalviewDialog
 {
+  private static final String COLON = ":";
+
   private static final int MAX_TOOLTIP_LENGTH = 50;
 
+  private static int NO_COLOUR_OPTION = 0;
+
+  private static int MIN_COLOUR_OPTION = 1;
+
+  private static int MAX_COLOUR_OPTION = 2;
+
   private FeatureRenderer fr;
 
   private FeatureColourI cs;
@@ -157,9 +165,8 @@ public class FeatureColourChooser extends JalviewDialog
     this.fr = frender;
     this.type = theType;
     ap = fr.ap;
-    String title = MessageManager
-            .formatMessage("label.graduated_color_for_params", new String[]
-            { theType });
+    String title = MessageManager.formatMessage("label.variable_color_for",
+            new String[] { theType });
     initDialogFrame(this, true, blocking, title, 470, 300);
 
     slider.addChangeListener(new ChangeListener()
@@ -190,6 +197,7 @@ public class FeatureColourChooser extends JalviewDialog
       }
     });
 
+    // todo move all threshold setup inside a method
     float mm[] = fr.getMinMax().get(theType)[0];
     min = mm[0];
     max = mm[1];
@@ -231,6 +239,8 @@ public class FeatureColourChooser extends JalviewDialog
     }
     minColour.setBackground(oldminColour = cs.getMinColour());
     maxColour.setBackground(oldmaxColour = cs.getMaxColour());
+    noColour = cs.getNoColour();
+
     adjusting = true;
 
     try
@@ -245,15 +255,15 @@ public class FeatureColourChooser extends JalviewDialog
     /*
      * set the initial state of options on screen
      */
-    thresholdIsMin.setSelected(!cs.isAutoScaled());
-
     if (cs.isColourByLabel())
     {
       if (cs.isColourByAttribute())
       {
         byAttributeText.setSelected(true);
         textAttributeCombo.setEnabled(true);
-        textAttributeCombo.setSelectedItem(cs.getAttributeName());
+        String[] attributeName = cs.getAttributeName();
+        textAttributeCombo
+                .setSelectedItem(String.join(COLON, attributeName));
       }
       else
       {
@@ -266,8 +276,9 @@ public class FeatureColourChooser extends JalviewDialog
       if (cs.isColourByAttribute())
       {
         byAttributeValue.setSelected(true);
-        String attributeName = cs.getAttributeName();
-        valueAttributeCombo.setSelectedItem(attributeName);
+        String[] attributeName = cs.getAttributeName();
+        valueAttributeCombo
+                .setSelectedItem(String.join(COLON, attributeName));
         valueAttributeCombo.setEnabled(true);
         updateMinMax();
       }
@@ -278,6 +289,22 @@ public class FeatureColourChooser extends JalviewDialog
       }
     }
 
+    if (noColour == null)
+    {
+      noValueCombo.setSelectedIndex(NO_COLOUR_OPTION);
+    }
+    else if (noColour.equals(oldminColour))
+    {
+      noValueCombo.setSelectedIndex(MIN_COLOUR_OPTION);
+    }
+    else if (noColour.equals(oldmaxColour))
+    {
+      noValueCombo.setSelectedIndex(MAX_COLOUR_OPTION);
+    }
+
+    threshline = new GraphLine((max - min) / 2f, "Threshold", Color.black);
+    threshline.value = cs.getThreshold();
+
     if (cs.hasThreshold())
     {
       // initialise threshold slider and selector
@@ -285,9 +312,6 @@ public class FeatureColourChooser extends JalviewDialog
       slider.setEnabled(true);
       slider.setValue((int) (cs.getThreshold() * scaleFactor));
       thresholdValue.setEnabled(true);
-      threshline = new GraphLine((max - min) / 2f, "Threshold",
-              Color.black);
-      threshline.value = cs.getThreshold();
     }
 
     adjusting = false;
@@ -369,7 +393,8 @@ public class FeatureColourChooser extends JalviewDialog
     else if (byAttributeValue.isSelected())
     {
       String attName = (String) valueAttributeCombo.getSelectedItem();
-      minMax = FeatureAttributes.getInstance().getMinMax(type, attName);
+      String[] attNames = attName.split(COLON);
+      minMax = FeatureAttributes.getInstance().getMinMax(type, attNames);
     }
     if (minMax != null)
     {
@@ -389,8 +414,8 @@ public class FeatureColourChooser extends JalviewDialog
   {
     JPanel byValuePanel = new JPanel();
     byValuePanel.setLayout(new BoxLayout(byValuePanel, BoxLayout.Y_AXIS));
-    byValuePanel.setBorder(BorderFactory.createTitledBorder(MessageManager
-            .getString("label.colour_by_value")));
+    JvSwingUtils.createItalicTitledBorder(byValuePanel,
+            MessageManager.getString("label.colour_by_value"), true);
     byValuePanel.setBackground(Color.white);
 
     /*
@@ -408,8 +433,8 @@ public class FeatureColourChooser extends JalviewDialog
     byAttributeValue.addActionListener(changeMinMaxAction);
     byWhatPanel.add(byAttributeValue);
 
-    List<String> attNames = FeatureAttributes.getInstance().getAttributes(
-            type);
+    List<String[]> attNames = FeatureAttributes.getInstance()
+            .getAttributes(type);
     valueAttributeCombo = populateAttributesDropdown(type, attNames, true);
 
     /*
@@ -555,21 +580,21 @@ public class FeatureColourChooser extends JalviewDialog
   }
 
   /**
-   * Action on user choice of no / min / max colour when there is no value to
-   * colour by
+   * Action on user choice of no / min / max colour to use when there is no
+   * value to colour by
    */
   protected void setNoValueColour()
   {
     int i = noValueCombo.getSelectedIndex();
-    if (i == 0)
+    if (i == NO_COLOUR_OPTION)
     {
       noColour = null;
     }
-    else if (i == 1)
+    else if (i == MIN_COLOUR_OPTION)
     {
       noColour = minColour.getBackground();
     }
-    else if (i == 2)
+    else if (i == MAX_COLOUR_OPTION)
     {
       noColour = maxColour.getBackground();
     }
@@ -599,8 +624,8 @@ public class FeatureColourChooser extends JalviewDialog
   {
     JPanel byTextPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
     byTextPanel.setBackground(Color.white);
-    byTextPanel.setBorder(BorderFactory.createTitledBorder(MessageManager
-            .getString("label.colour_by_text")));
+    JvSwingUtils.createItalicTitledBorder(byTextPanel,
+            MessageManager.getString("label.colour_by_text"), true);
 
     byDescription.setText(MessageManager.getString("label.label"));
     byDescription.setToolTipText(MessageManager
@@ -612,8 +637,8 @@ public class FeatureColourChooser extends JalviewDialog
     byAttributeText.addActionListener(changeColourAction);
     byTextPanel.add(byAttributeText);
 
-    List<String> attNames = FeatureAttributes.getInstance().getAttributes(
-            type);
+    List<String[]> attNames = FeatureAttributes.getInstance()
+            .getAttributes(type);
     textAttributeCombo = populateAttributesDropdown(type, attNames, false);
     byTextPanel.add(textAttributeCombo);
 
@@ -715,13 +740,18 @@ public class FeatureColourChooser extends JalviewDialog
     {
       attribute = (String) textAttributeCombo.getSelectedItem();
       textAttributeCombo.setEnabled(true);
+      acg.setAttributeName(attribute.split(COLON));
     }
     else if (byAttributeValue.isSelected())
     {
       attribute = (String) valueAttributeCombo.getSelectedItem();
       valueAttributeCombo.setEnabled(true);
+      acg.setAttributeName(attribute.split(COLON));
+    }
+    else
+    {
+      acg.setAttributeName((String) null);
     }
-    acg.setAttributeName(attribute);
 
     if (!hasThreshold)
     {
@@ -798,6 +828,7 @@ public class FeatureColourChooser extends JalviewDialog
       maxColour.setForeground(oldmaxColour);
       minColour.setBackground(oldminColour);
       minColour.setForeground(oldminColour);
+      noColour = oldNoColour;
     }
 
     /*
@@ -912,23 +943,26 @@ public class FeatureColourChooser extends JalviewDialog
   /**
    * A helper method to build the drop-down choice of attributes for a feature.
    * Where metadata is available with a description for an attribute, that is
-   * added as a tooltip. The list may be restricted to attributes for which we
-   * hold a range of numerical values (so suitable candidates for a graduated
-   * colour scheme).
+   * added as a tooltip. The list may optionally be restricted to attributes for
+   * which we hold a range of numerical values (so suitable candidates for a
+   * graduated colour scheme).
+   * <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.
    * 
    * @param featureType
    * @param attNames
    * @param withNumericRange
    */
   protected JComboBox<String> populateAttributesDropdown(
-          String featureType, List<String> attNames,
+          String featureType, List<String[]> attNames,
           boolean withNumericRange)
   {
     List<String> validAtts = new ArrayList<>();
     List<String> tooltips = new ArrayList<>();
 
     FeatureAttributes fa = FeatureAttributes.getInstance();
-    for (String attName : attNames)
+    for (String[] attName : attNames)
     {
       if (withNumericRange)
       {
@@ -938,7 +972,7 @@ public class FeatureColourChooser extends JalviewDialog
           continue;
         }
       }
-      validAtts.add(attName);
+      validAtts.add(String.join(COLON, attName));
       String desc = fa.getDescription(featureType, attName);
       if (desc != null && desc.length() > MAX_TOOLTIP_LENGTH)
       {