JAL-2808 attribute description as tooltip in dropdown if known
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 3 Nov 2017 11:51:18 +0000 (11:51 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 3 Nov 2017 11:51:18 +0000 (11:51 +0000)
src/jalview/gui/FeatureSettings.java

index d724b8c..0928ec1 100644 (file)
@@ -1562,11 +1562,11 @@ public class FeatureSettings extends JPanel
    * @param attNames
    * @param cond
    * @param pattern
-   * @param i
+   * @param filterIndex
    * @return
    */
   protected JPanel addFilter(String attribute, List<String> attNames,
-          Condition cond, String pattern, int i)
+          Condition cond, String pattern, int filterIndex)
   {
     JPanel filterRow = new JPanel(new FlowLayout(FlowLayout.LEFT));
     filterRow.setBackground(Color.white);
@@ -1574,7 +1574,7 @@ public class FeatureSettings extends JPanel
     /*
      * inputs for attribute, condition, pattern
      */
-    JComboBox<String> attCombo = new JComboBox<>();
+    final JComboBox<String> attCombo = new JComboBox<>();
     JComboBox<Condition> condCombo = new JComboBox<>();
     JTextField patternField = new JTextField(8);
 
@@ -1590,7 +1590,7 @@ public class FeatureSettings extends JPanel
         {
           if (validateFilter(patternField, condCombo))
           {
-            updateFilter(attCombo, condCombo, patternField, i);
+            updateFilter(attCombo, condCombo, patternField, filterIndex);
             filtersChanged();
           }
         }
@@ -1606,31 +1606,21 @@ public class FeatureSettings extends JPanel
     };
 
     /*
-     * drop-down choice of attribute
+     * drop-down choice of attribute, with description as a tooltip 
+     * if we can obtain it
      */
-    if (attNames.isEmpty())
+    String featureType = (String) filteredFeatureChoice.getSelectedItem();
+    populateAttributesDropdown(attCombo, featureType, attNames);
+    if ("".equals(attribute))
     {
-      attCombo.addItem("---");
-      attCombo.setToolTipText(MessageManager
-              .getString("label.no_attributes_known"));
+      attCombo.setSelectedItem(null);
     }
     else
     {
-      attCombo.setToolTipText("");
-      for (String attName : attNames)
-      {
-        attCombo.addItem(attName);
-      }
-      if ("".equals(attribute))
-      {
-        attCombo.setSelectedItem(null);
-      }
-      else
-      {
-        attCombo.setSelectedItem(attribute);
-      }
-      attCombo.addItemListener(itemListener);
+      attCombo.setSelectedItem(attribute);
     }
+    attCombo.addItemListener(itemListener);
+
     filterRow.add(attCombo);
 
     /*
@@ -1676,7 +1666,7 @@ public class FeatureSettings extends JPanel
         @Override
         public void actionPerformed(ActionEvent e)
         {
-          filters.remove(i);
+          filters.remove(filterIndex);
           filtersChanged();
         }
       });
@@ -1687,6 +1677,64 @@ public class FeatureSettings extends JPanel
   }
 
   /**
+   * 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.
+   * 
+   * @param attCombo
+   * @param featureType
+   * @param attNames
+   */
+  protected void populateAttributesDropdown(
+          final JComboBox<String> attCombo, String featureType,
+          List<String> attNames)
+  {
+    final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
+    attCombo.setRenderer(renderer);
+    List<String> tips = new ArrayList<String>();
+    if (attNames.isEmpty())
+    {
+      attCombo.addItem("---");
+      attCombo.setToolTipText(MessageManager
+              .getString("label.no_attributes_known"));
+    }
+    else
+    {
+      attCombo.setToolTipText("");
+      FeatureAttributes fs = FeatureAttributes.getInstance();
+      for (String attName : attNames)
+      {
+        attCombo.addItem(attName);
+        String desc = fs.getDescription(featureType, attName);
+        tips.add(desc == null ? "" : desc);
+      }
+    }
+    renderer.setTooltips(tips);
+    final MouseAdapter mouseListener = new MouseAdapter()
+    {
+      @Override
+      public void mouseEntered(MouseEvent e)
+      {
+        int j = attCombo.getSelectedIndex();
+        if (j > -1)
+        {
+          attCombo.setToolTipText(tips.get(j));
+        }
+      }
+
+      @Override
+      public void mouseExited(MouseEvent e)
+      {
+        attCombo.setToolTipText(null);
+      }
+    };
+    for (Component c : attCombo.getComponents())
+    {
+      c.addMouseListener(mouseListener);
+    }
+  }
+
+  /**
    * Action on any change to feature filtering, namely
    * <ul>
    * <li>change of selected attribute</li>