JAL-3032 avoid use of lambda expression or BasicArrowButton
[jalview.git] / src / jalview / gui / FeatureTypeSettings.java
index c356731..b973708 100644 (file)
@@ -64,11 +64,9 @@ 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
@@ -295,8 +293,8 @@ public class FeatureTypeSettings extends JalviewDialog
         if (fc.isColourByAttribute())
         {
           String[] attributeName = fc.getAttributeName();
-          colourByTextCombo
-                  .setSelectedItem(toAttributeDisplayName(attributeName));
+          colourByTextCombo.setSelectedItem(
+                  FeatureMatcher.toAttributeDisplayName(attributeName));
         }
         else
         {
@@ -325,6 +323,7 @@ public class FeatureTypeSettings extends JalviewDialog
        * Graduated colour, by score or attribute value range
        */
       graduatedColour.setSelected(true);
+      updateColourMinMax(); // ensure min, max are set
       colourByRangeCombo.setEnabled(colourByRangeCombo.getItemCount() > 1);
       minColour.setEnabled(true);
       maxColour.setEnabled(true);
@@ -336,8 +335,8 @@ public class FeatureTypeSettings extends JalviewDialog
       if (fc.isColourByAttribute())
       {
         String[] attributeName = fc.getAttributeName();
-        colourByRangeCombo
-                .setSelectedItem(toAttributeDisplayName(attributeName));
+        colourByRangeCombo.setSelectedItem(
+                FeatureMatcher.toAttributeDisplayName(attributeName));
       }
       else
       {
@@ -478,7 +477,7 @@ public class FeatureTypeSettings extends JalviewDialog
     {
       // colour by attribute range
       minMax = FeatureAttributes.getInstance().getMinMax(featureType,
-              fromAttributeDisplayName(attName));
+              FeatureMatcher.fromAttributeDisplayName(attName));
     }
     return minMax;
   }
@@ -907,7 +906,8 @@ public class FeatureTypeSettings extends JalviewDialog
       String byWhat = (String) colourByTextCombo.getSelectedItem();
       if (!LABEL_18N.equals(byWhat))
       {
-        fc.setAttributeName(fromAttributeDisplayName(byWhat));
+        fc.setAttributeName(
+                FeatureMatcher.fromAttributeDisplayName(byWhat));
       }
       return fc;
     }
@@ -964,7 +964,7 @@ public class FeatureTypeSettings extends JalviewDialog
     String byWhat = (String) colourByRangeCombo.getSelectedItem();
     if (!SCORE_18N.equals(byWhat))
     {
-      fc.setAttributeName(fromAttributeDisplayName(byWhat));
+      fc.setAttributeName(FeatureMatcher.fromAttributeDisplayName(byWhat));
     }
 
     /*
@@ -989,30 +989,6 @@ public class FeatureTypeSettings extends JalviewDialog
     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()
   {
@@ -1159,7 +1135,7 @@ public class FeatureTypeSettings extends JalviewDialog
       {
         continue;
       }
-      displayAtts.add(toAttributeDisplayName(attName));
+      displayAtts.add(FeatureMatcher.toAttributeDisplayName(attName));
       String desc = fa.getDescription(featureType, attName);
       if (desc != null && desc.length() > MAX_TOOLTIP_LENGTH)
       {
@@ -1264,7 +1240,12 @@ public class FeatureTypeSettings extends JalviewDialog
       {
         orFilters.setSelected(true);
       }
-      featureFilters.getMatchers().forEach(matcher -> filters.add(matcher));
+      // avoid use of lambda expression to keep SwingJS happy
+      // featureFilters.getMatchers().forEach(item -> filters.add(item));
+      for (FeatureMatcherI matcher : featureFilters.getMatchers())
+      {
+        filters.add(matcher);
+      }
     }
 
     /*
@@ -1383,7 +1364,8 @@ public class FeatureTypeSettings extends JalviewDialog
     }
     else
     {
-      attCombo.setSelectedItem(toAttributeDisplayName(attName));
+      attCombo.setSelectedItem(
+              FeatureMatcher.toAttributeDisplayName(attName));
     }
     attCombo.addItemListener(new ItemListener()
     {
@@ -1429,16 +1411,13 @@ public class FeatureTypeSettings extends JalviewDialog
      * disable pattern field for condition 'Present / NotPresent'
      */
     Condition selectedCondition = (Condition) condCombo.getSelectedItem();
-    if (!selectedCondition.needsAPattern())
-    {
-      patternField.setEnabled(false);
-    }
+    patternField.setEnabled(selectedCondition.needsAPattern());
 
     /*
      * if a numeric condition is selected, show the value range
      * as a tooltip on the value input field
      */
-    setPatternTooltip(filterBy, selectedCondition, patternField);
+    setNumericHints(filterBy, selectedCondition, patternField);
 
     /*
      * add remove button if filter is populated (non-empty pattern)
@@ -1446,8 +1425,8 @@ public class FeatureTypeSettings extends JalviewDialog
     if (!patternField.isEnabled()
             || (pattern != null && pattern.trim().length() > 0))
     {
-      // todo: gif for button drawing '-' or 'x'
-      JButton removeCondition = new BasicArrowButton(SwingConstants.WEST);
+      JButton removeCondition = new JButton("\u2717"); // Dingbats cursive x
+      removeCondition.setPreferredSize(new Dimension(23, 17));
       removeCondition
               .setToolTipText(MessageManager.getString("label.delete_row"));
       removeCondition.addActionListener(new ActionListener()
@@ -1486,21 +1465,26 @@ public class FeatureTypeSettings extends JalviewDialog
     }
     else
     {
-      item = toAttributeDisplayName(filter.getAttribute());
+      item = FeatureMatcher.toAttributeDisplayName(filter.getAttribute());
     }
     attCombo.setSelectedItem(item);
     return item;
   }
 
   /**
-   * If a numeric comparison condition is selected, retrieve the min-max range for
-   * the value (score or attribute), and set it as a tooltip on the value file
+   * If a numeric comparison condition is selected, retrieves the min-max range
+   * for the value (score or attribute), and sets it as a tooltip on the value
+   * field. If the field is currently empty, then pre-populates it with
+   * <ul>
+   * <li>the minimum value, if condition is > or >=</li>
+   * <li>the maximum value, if condition is < or <=</li>
+   * </ul>
    * 
    * @param attName
    * @param selectedCondition
    * @param patternField
    */
-  private void setPatternTooltip(String attName,
+  private void setNumericHints(String attName,
           Condition selectedCondition, JTextField patternField)
   {
     patternField.setToolTipText("");
@@ -1510,9 +1494,26 @@ public class FeatureTypeSettings extends JalviewDialog
       float[] minMax = getMinMax(attName);
       if (minMax != null)
       {
-        String tip = String.format("(%s - %s)",
-                DECFMT_2_2.format(minMax[0]), DECFMT_2_2.format(minMax[1]));
+        String minFormatted = DECFMT_2_2.format(minMax[0]);
+        String maxFormatted = DECFMT_2_2.format(minMax[1]);
+        String tip = String.format("(%s - %s)", minFormatted, maxFormatted);
         patternField.setToolTipText(tip);
+        if (patternField.getText().isEmpty())
+        {
+          if (selectedCondition == Condition.GE
+                  || selectedCondition == Condition.GT)
+          {
+            patternField.setText(minFormatted);
+          }
+          else
+          {
+            if (selectedCondition == Condition.LE
+                    || selectedCondition == Condition.LT)
+            {
+              patternField.setText(maxFormatted);
+            }
+          }
+        }
       }
     }
   }
@@ -1533,7 +1534,7 @@ public class FeatureTypeSettings extends JalviewDialog
           JComboBox<Condition> condCombo, JTextField patternField)
   {
     Datatype type = FeatureAttributes.getInstance().getDatatype(featureType,
-            fromAttributeDisplayName(attName));
+            FeatureMatcher.fromAttributeDisplayName(attName));
     if (LABEL_18N.equals(attName))
     {
       type = Datatype.Character;
@@ -1549,10 +1550,11 @@ public class FeatureTypeSettings extends JalviewDialog
     ItemListener listener = condCombo.getItemListeners()[0];
     condCombo.removeItemListener(listener);
     boolean condIsValid = false;
+
     condCombo.removeAllItems();
     for (Condition c : Condition.values())
     {
-      if ((c.isNumeric() && type != Datatype.Character)
+      if ((c.isNumeric() && type == Datatype.Number)
               || (!c.isNumeric() && type != Datatype.Number))
       {
         condCombo.addItem(c);
@@ -1575,8 +1577,6 @@ public class FeatureTypeSettings extends JalviewDialog
       condCombo.setSelectedIndex(0);
     }
 
-    condCombo.addItemListener(listener);
-
     /*
      * clear pattern if it is now invalid for condition
      */
@@ -1594,6 +1594,11 @@ public class FeatureTypeSettings extends JalviewDialog
         patternField.setText("");
       }
     }
+
+    /*
+     * restore the listener
+     */
+    condCombo.addItemListener(listener);
   }
 
   /**
@@ -1670,10 +1675,11 @@ public class FeatureTypeSettings extends JalviewDialog
     Condition cond = (Condition) condCombo.getSelectedItem();
     String pattern = valueField.getText().trim();
 
-    setPatternTooltip(attName, cond, valueField);
+    setNumericHints(attName, cond, valueField);
 
     if (pattern.length() == 0 && cond.needsAPattern())
     {
+      valueField.setEnabled(true); // ensure pattern field is enabled!
       return false;
     }
 
@@ -1693,7 +1699,7 @@ public class FeatureTypeSettings extends JalviewDialog
     else
     {
       km = FeatureMatcher.byAttribute(cond, pattern,
-              fromAttributeDisplayName(attName));
+              FeatureMatcher.fromAttributeDisplayName(attName));
     }
 
     filters.set(filterIndex, km);