JAL-2432 desktop: filter/select columns by threshold as a percentage
[jalview.git] / src / jalview / gui / AnnotationRowFilter.java
index 128b99c..1035a6c 100644 (file)
@@ -62,6 +62,8 @@ public abstract class AnnotationRowFilter extends JPanel
 
   protected JCheckBox seqAssociated = new JCheckBox();
 
+  protected JCheckBox percentThreshold = new JCheckBox();
+
   protected JSlider slider = new JSlider();
 
   protected JTextField thresholdValue = new JTextField(20);
@@ -88,20 +90,18 @@ public abstract class AnnotationRowFilter extends JPanel
    */
   private Map<AlignmentAnnotation, String> annotationLabels;
 
-  private boolean enableSeqAss = false;
-
   private AlignmentAnnotation currentAnnotation;
 
   /**
    * Constructor
    * 
-   * @param av
-   * @param ap
+   * @param viewport
+   * @param alignPanel
    */
-  public AnnotationRowFilter(AlignViewport av, final AlignmentPanel ap)
+  public AnnotationRowFilter(AlignViewport viewport, final AlignmentPanel alignPanel)
   {
-    this.av = av;
-    this.ap = ap;
+    this.av = viewport;
+    this.ap = alignPanel;
     thresholdValue.addFocusListener(new FocusAdapter()
     {
       @Override
@@ -122,13 +122,27 @@ public abstract class AnnotationRowFilter extends JPanel
       {
         if (!adjusting)
         {
-          thresholdValue.setText((slider.getValue() / 1000f) + "");
+          setThresholdValueText();
           valueChanged(!sliderDragging);
         }
       }
     });
   }
 
+  protected void setThresholdValueText()
+  {
+    adjusting = true;
+    if (percentThreshold.isSelected())
+    {
+      thresholdValue.setText("" + (slider.getValue() - slider.getMinimum())
+              * 100f / (slider.getMaximum() - slider.getMinimum()));
+    }
+    else
+    {
+      thresholdValue.setText((slider.getValue() / 1000f) + "");
+    }
+    adjusting = false;
+  }
   protected void addSliderMouseListeners()
   {
 
@@ -161,14 +175,14 @@ public abstract class AnnotationRowFilter extends JPanel
     });
   }
 
-  /**
-   * Builds and returns a list of menu items (display text) for choice of
-   * annotation. Also builds maps between annotations, their positions in the
-   * list, and their display labels in the list.
-   * 
-   * @param isSeqAssociated
-   * @return
-   */
+/**
+ * Builds and returns a list of menu items (display text) for choice of
+ * annotation. Also builds maps between annotations, their positions in the
+ * list, and their display labels in the list.
+ * 
+ * @param isSeqAssociated
+ * @return
+ */
   public Vector<String> getAnnotationItems(boolean isSeqAssociated)
   {
     annotationLabels = new HashMap<AlignmentAnnotation, String>();
@@ -176,6 +190,7 @@ public abstract class AnnotationRowFilter extends JPanel
     Vector<String> list = new Vector<String>();
     int index = 1;
     int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
+    seqAssociated.setEnabled(false);
     for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
     {
       AlignmentAnnotation annotation = av.getAlignment()
@@ -189,7 +204,7 @@ public abstract class AnnotationRowFilter extends JPanel
       }
       else
       {
-        enableSeqAss = true;
+        seqAssociated.setEnabled(true);
       }
       String label = annotation.label;
       // add associated sequence ID if available
@@ -234,11 +249,6 @@ public abstract class AnnotationRowFilter extends JPanel
     return selectedThresholdItem;
   }
 
-  public void modelChanged()
-  {
-    seqAssociated.setEnabled(enableSeqAss);
-  }
-
   public void ok_actionPerformed()
   {
     try
@@ -281,13 +291,27 @@ public abstract class AnnotationRowFilter extends JPanel
     try
     {
       float f = Float.parseFloat(thresholdValue.getText());
-      slider.setValue((int) (f * 1000));
+      if (percentThreshold.isEnabled())
+      {
+        slider.setValue(slider.getMinimum()
+                + ((int) ((f / 100f) * (slider.getMaximum() - slider
+                        .getMinimum()))));
+      }
+      else
+      {
+        slider.setValue((int) (f * 1000));
+      }
       updateView();
     } catch (NumberFormatException ex)
     {
     }
   }
 
+  protected void percentageValue_actionPerformed()
+  {
+    setThresholdValueText();
+  }
+
   protected void thresholdIsMin_actionPerformed()
   {
     updateView();
@@ -313,7 +337,8 @@ public abstract class AnnotationRowFilter extends JPanel
   {
     adjusting = true;
     String cursel = (String) anns.getSelectedItem();
-    boolean isvalid = false, isseqs = seqAssociated.isSelected();
+    boolean isvalid = false;
+    boolean isseqs = seqAssociated.isSelected();
     anns.removeAllItems();
     for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
     {
@@ -324,7 +349,6 @@ public abstract class AnnotationRowFilter extends JPanel
       }
       anns.addItem(anitem);
     }
-    adjusting = false;
     if (isvalid)
     {
       anns.setSelectedItem(cursel);
@@ -336,6 +360,9 @@ public abstract class AnnotationRowFilter extends JPanel
         anns.setSelectedIndex(0);
       }
     }
+    adjusting = false;
+
+    updateView();
   }
 
   protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
@@ -366,14 +393,14 @@ public abstract class AnnotationRowFilter extends JPanel
     }
   }
 
-  protected AlignmentAnnotation getCurrentAnnotation()
+  public AlignmentAnnotation getCurrentAnnotation()
   {
     return currentAnnotation;
   }
 
-  protected void setCurrentAnnotation(AlignmentAnnotation currentAnnotation)
+  protected void setCurrentAnnotation(AlignmentAnnotation annotation)
   {
-    this.currentAnnotation = currentAnnotation;
+    this.currentAnnotation = annotation;
   }
 
   protected abstract void valueChanged(boolean updateAllAnnotation);
@@ -442,6 +469,18 @@ public abstract class AnnotationRowFilter extends JPanel
       }
     });
 
+    percentThreshold.setText(MessageManager.getString("label.as_percentage"));
+    percentThreshold.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        if (!adjusting)
+        {
+          percentageValue_actionPerformed();
+        }
+      }
+    });
     slider.setPaintLabels(false);
     slider.setPaintTicks(true);
     slider.setBackground(Color.white);