JAL-2808 update spike to latest (filter range, Present option)
[jalview.git] / src / jalview / gui / FeatureTypeSettings.java
index e280091..1dd12aa 100644 (file)
@@ -47,6 +47,7 @@ import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
+import java.text.DecimalFormat;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -94,6 +95,9 @@ public class FeatureTypeSettings extends JalviewDialog
 
   private static final int BELOW_THRESHOLD_OPTION = 2;
 
+  private static final DecimalFormat DECFMT_2_2 = new DecimalFormat(
+          "##.##");
+
   /*
    * FeatureRenderer holds colour scheme and filters for feature types
    */
@@ -1238,7 +1242,7 @@ public class FeatureTypeSettings extends JalviewDialog
      * and an empty filter for the user to populate (add)
      */
     KeyedMatcherI noFilter = new KeyedMatcher(Condition.values()[0], "",
-            (String) null);
+            (String[]) null);
     filters.add(noFilter);
 
     /*
@@ -1330,7 +1334,20 @@ public class FeatureTypeSettings extends JalviewDialog
     {
       attCombo.setSelectedItem(toAttributeDisplayName(attName));
     }
-    attCombo.addItemListener(itemListener);
+    attCombo.addItemListener(new ItemListener()
+    {
+      @Override
+      public void itemStateChanged(ItemEvent e)
+      {
+        /*
+         * on change of attribute, refresh the conditions list to
+         * ensure it is appropriate for the attribute datatype
+         */
+        populateConditions((String) attCombo.getSelectedItem(),
+                (Condition) condCombo.getSelectedItem(), condCombo);
+        actionListener.actionPerformed(null);
+      }
+    });
 
     filterRow.add(attCombo);
 
@@ -1339,6 +1356,7 @@ public class FeatureTypeSettings extends JalviewDialog
      */
     populateConditions((String) attCombo.getSelectedItem(), cond,
             condCombo);
+    condCombo.setPreferredSize(new Dimension(150, 20));
     condCombo.addItemListener(itemListener);
     filterRow.add(condCombo);
 
@@ -1358,6 +1376,32 @@ public class FeatureTypeSettings extends JalviewDialog
     filterRow.add(patternField);
 
     /*
+     * disable pattern field for condition 'Present / NotPresent'
+     */
+    Condition selectedCondition = (Condition) condCombo.getSelectedItem();
+    if (selectedCondition == Condition.Present
+            || selectedCondition == Condition.NotPresent)
+    {
+      patternField.setEnabled(false);
+    }
+
+    /*
+     * if a numeric condition is selected, show the value range
+     * as a tooltip on the value input field
+     */
+    if (selectedCondition.isNumeric())
+    {
+      float[] minMax = FeatureAttributes.getInstance()
+              .getMinMax(featureType, attName);
+      if (minMax != null)
+      {
+        String tip = String.format("(%s - %s)",
+                DECFMT_2_2.format(minMax[0]), DECFMT_2_2.format(minMax[1]));
+        patternField.setToolTipText(tip);
+      }
+    }
+
+    /*
      * add remove button if filter is populated (non-empty pattern)
      */
     if (pattern != null && pattern.trim().length() > 0)
@@ -1395,7 +1439,7 @@ public class FeatureTypeSettings extends JalviewDialog
           JComboBox<Condition> condCombo)
   {
     Datatype type = FeatureAttributes.getInstance().getDatatype(featureType,
-            attName);
+            fromAttributeDisplayName(attName));
     if (MessageManager.getString("label.label").equals(attName))
     {
       type = Datatype.Character;
@@ -1405,30 +1449,47 @@ public class FeatureTypeSettings extends JalviewDialog
       type = Datatype.Number;
     }
 
+    /*
+     * remove itemListener before starting
+     */
+    ItemListener listener = condCombo.getItemListeners()[0];
+    condCombo.removeItemListener(listener);
+    boolean condIsValid = false;
+    condCombo.removeAllItems();
     for (Condition c : Condition.values())
     {
       if ((c.isNumeric() && type != Datatype.Character)
               || (!c.isNumeric() && type != Datatype.Number))
       {
         condCombo.addItem(c);
+        if (c == cond)
+        {
+          condIsValid = true;
+        }
       }
     }
 
     /*
      * set the selected condition (does nothing if not in the list)
      */
-    if (cond != null)
+    if (condIsValid)
     {
       condCombo.setSelectedItem(cond);
     }
+    else
+    {
+      condCombo.setSelectedIndex(0);
+    }
+
+    condCombo.addItemListener(listener);
   }
 
   /**
    * 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
+   * If the pattern is expected but 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
@@ -1443,6 +1504,11 @@ public class FeatureTypeSettings extends JalviewDialog
     }
 
     Condition cond = (Condition) condCombo.getSelectedItem();
+    if (cond == Condition.Present || cond == Condition.NotPresent)
+    {
+      return true;
+    }
+
     value.setBackground(Color.white);
     value.setToolTipText("");
     String v1 = value.getText().trim();
@@ -1523,7 +1589,9 @@ public class FeatureTypeSettings extends JalviewDialog
     for (KeyedMatcherI filter : filters)
     {
       String pattern = filter.getMatcher().getPattern();
-      if (pattern.trim().length() > 0)
+      Condition condition = filter.getMatcher().getCondition();
+      if (pattern.trim().length() > 0 || condition == Condition.Present
+              || condition == Condition.NotPresent)
       {
         if (anded)
         {