JAL-3853 JAL-1870 toggle to enable/disable posterior probability transparency when...
[jalview.git] / src / jalview / gui / AnnotationRowFilter.java
index 128b99c..50835f2 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);
@@ -78,7 +80,7 @@ public abstract class AnnotationRowFilter extends JPanel
    */
   protected boolean sliderDragging = false;
 
-  protected JComboBox<String> threshold = new JComboBox<String>();
+  protected JComboBox<String> threshold = new JComboBox<>();
 
   protected JComboBox<String> annotations;
 
@@ -88,20 +90,19 @@ 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 +123,33 @@ public abstract class AnnotationRowFilter extends JPanel
       {
         if (!adjusting)
         {
-          thresholdValue.setText((slider.getValue() / 1000f) + "");
+          setThresholdValueText();
           valueChanged(!sliderDragging);
         }
       }
     });
   }
 
+  /**
+   * update the text field from the threshold slider. preserves state of
+   * 'adjusting' so safe to call in init.
+   */
+  protected void setThresholdValueText()
+  {
+    boolean oldadj = adjusting;
+    adjusting = true;
+    if (percentThreshold.isSelected())
+    {
+      thresholdValue.setText("" + (slider.getValue() - slider.getMinimum())
+              * 100f / (slider.getMaximum() - slider.getMinimum()));
+    }
+    else
+    {
+      thresholdValue.setText((slider.getValue() / 1000f) + "");
+    }
+    adjusting = oldadj;
+  }
+
   protected void addSliderMouseListeners()
   {
 
@@ -151,12 +172,7 @@ public abstract class AnnotationRowFilter extends JPanel
       @Override
       public void mouseReleased(MouseEvent evt)
       {
-        if (sliderDragging)
-        {
-          sliderDragging = false;
-          valueChanged(true);
-        }
-        ap.paintAlignment(true);
+        sliderDragReleased();
       }
     });
   }
@@ -171,12 +187,15 @@ public abstract class AnnotationRowFilter extends JPanel
    */
   public Vector<String> getAnnotationItems(boolean isSeqAssociated)
   {
-    annotationLabels = new HashMap<AlignmentAnnotation, String>();
+    annotationLabels = new HashMap<>();
 
-    Vector<String> list = new Vector<String>();
+    Vector<String> list = new Vector<>();
     int index = 1;
-    int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
-    for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
+    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()
               .getAlignmentAnnotation()[i];
@@ -189,7 +208,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 +253,6 @@ public abstract class AnnotationRowFilter extends JPanel
     return selectedThresholdItem;
   }
 
-  public void modelChanged()
-  {
-    seqAssociated.setEnabled(enableSeqAss);
-  }
-
   public void ok_actionPerformed()
   {
     try
@@ -252,7 +266,7 @@ public abstract class AnnotationRowFilter extends JPanel
   public void cancel_actionPerformed()
   {
     reset();
-    ap.paintAlignment(true);
+    ap.paintAlignment(true, true);
     try
     {
       frame.setClosed(true);
@@ -281,13 +295,26 @@ public abstract class AnnotationRowFilter extends JPanel
     try
     {
       float f = Float.parseFloat(thresholdValue.getText());
-      slider.setValue((int) (f * 1000));
+      if (percentThreshold.isSelected())
+      {
+        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 +340,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 +352,6 @@ public abstract class AnnotationRowFilter extends JPanel
       }
       anns.addItem(anitem);
     }
-    adjusting = false;
     if (isvalid)
     {
       anns.setSelectedItem(cursel);
@@ -336,7 +363,11 @@ public abstract class AnnotationRowFilter extends JPanel
         anns.setSelectedIndex(0);
       }
     }
+    adjusting = false;
+
+    updateView();
   }
+  
 
   protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
           AlignmentAnnotation annotation)
@@ -347,9 +378,11 @@ public abstract class AnnotationRowFilter extends JPanel
     }
 
     float thr = annotation.threshold.value;
-    for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
+    for (int i = 0; i < av.getAlignment()
+            .getAlignmentAnnotation().length; i++)
     {
-      AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
+      AlignmentAnnotation aa = av.getAlignment()
+              .getAlignmentAnnotation()[i];
       if (aa.label.equals(annotation.label)
               && (annotation.getCalcId() == null ? aa.getCalcId() == null
                       : annotation.getCalcId().equals(aa.getCalcId())))
@@ -366,16 +399,21 @@ 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;
   }
 
+  /**
+   * update associated view model and trigger any necessary repaints.
+   * 
+   * @param updateAllAnnotation
+   */
   protected abstract void valueChanged(boolean updateAllAnnotation);
 
   protected abstract void updateView();
@@ -419,8 +457,8 @@ public abstract class AnnotationRowFilter extends JPanel
         selectedAnnotationChanged();
       }
     });
-    annotations.setToolTipText(MessageManager
-            .getString("info.select_annotation_row"));
+    annotations.setToolTipText(
+            MessageManager.getString("info.select_annotation_row"));
 
     threshold.addActionListener(new ActionListener()
     {
@@ -442,6 +480,19 @@ 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);
@@ -469,4 +520,13 @@ public abstract class AnnotationRowFilter extends JPanel
   {
     this.annotations = anns;
   }
+
+  protected void sliderDragReleased()
+  {
+    if (sliderDragging)
+    {
+      sliderDragging = false;
+      valueChanged(true);
+    }
+  }
 }