JAL-2808 update conditions list on change of attribute (safely)
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 22 Nov 2017 16:56:35 +0000 (16:56 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 22 Nov 2017 16:56:35 +0000 (16:56 +0000)
src/jalview/gui/FeatureTypeSettings.java

index e280091..372234a 100644 (file)
@@ -1330,7 +1330,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);
 
@@ -1395,7 +1408,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,22 +1418,39 @@ 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);
   }
 
   /**