JAL-3811 document SHOW_JWS2_SERVICES user preference and provide new menu option...
[jalview.git] / src / jalview / gui / AnnotationRowFilter.java
index 128b99c..6f72e10 100644 (file)
@@ -35,6 +35,8 @@ import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
+import java.math.BigDecimal;
+import java.math.MathContext;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Vector;
@@ -44,7 +46,6 @@ import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
 import javax.swing.JInternalFrame;
 import javax.swing.JPanel;
-import javax.swing.JSlider;
 import javax.swing.JTextField;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
@@ -52,6 +53,10 @@ import javax.swing.event.ChangeListener;
 @SuppressWarnings("serial")
 public abstract class AnnotationRowFilter extends JPanel
 {
+  private static final String TWO_DP = "%.2f";
+
+  private final static MathContext FOUR_SIG_FIG = new MathContext(4);
+
   protected AlignViewport av;
 
   protected AlignmentPanel ap;
@@ -62,7 +67,9 @@ public abstract class AnnotationRowFilter extends JPanel
 
   protected JCheckBox seqAssociated = new JCheckBox();
 
-  protected JSlider slider = new JSlider();
+  protected JCheckBox percentThreshold = new JCheckBox();
+
+  protected Slider slider;
 
   protected JTextField thresholdValue = new JTextField(20);
 
@@ -78,7 +85,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 +95,21 @@ 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;
+    this.slider = new Slider(0f, 100f, 50f);
+
     thresholdValue.addFocusListener(new FocusAdapter()
     {
       @Override
@@ -122,13 +130,79 @@ 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(String.format(TWO_DP, getSliderPercentageValue()));
+    }
+    else
+    {
+      /*
+       * round to 4 significant digits without trailing zeroes
+       */
+      float f = getSliderValue();
+      BigDecimal formatted = new BigDecimal(f).round(FOUR_SIG_FIG)
+              .stripTrailingZeros();
+      thresholdValue.setText(formatted.toPlainString());
+    }
+    adjusting = oldadj;
+  }
+
+  /**
+   * Answers the value of the slider position (descaled to 'true' value)
+   * 
+   * @return
+   */
+  protected float getSliderValue()
+  {
+    return slider.getSliderValue();
+  }
+
+  /**
+   * Sets the slider value (scaled from the true value to the slider range)
+   * 
+   * @param value
+   */
+  protected void setSliderValue(float value)
+  {
+    slider.setSliderValue(value);
+  }
+  /**
+   * Answers the value of the slider position as a percentage between minimum and
+   * maximum of its range
+   * 
+   * @return
+   */
+  protected float getSliderPercentageValue()
+  {
+    return slider.getSliderPercentageValue();
+  }
+
+  /**
+   * Sets the slider position for a given percentage value of its min-max range
+   * 
+   * @param pct
+   */
+  protected void setSliderPercentageValue(float pct)
+  {
+    slider.setSliderPercentageValue(pct);
+  }
+
   protected void addSliderMouseListeners()
   {
 
@@ -151,12 +225,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 +240,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 +261,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 +306,6 @@ public abstract class AnnotationRowFilter extends JPanel
     return selectedThresholdItem;
   }
 
-  public void modelChanged()
-  {
-    seqAssociated.setEnabled(enableSeqAss);
-  }
-
   public void ok_actionPerformed()
   {
     try
@@ -252,7 +319,7 @@ public abstract class AnnotationRowFilter extends JPanel
   public void cancel_actionPerformed()
   {
     reset();
-    ap.paintAlignment(true);
+    ap.paintAlignment(true, true);
     try
     {
       frame.setClosed(true);
@@ -276,18 +343,34 @@ public abstract class AnnotationRowFilter extends JPanel
     updateView();
   }
 
+  /**
+   * Updates the slider position, and the display, for an update in the slider's
+   * text input field
+   */
   protected void thresholdValue_actionPerformed()
   {
     try
     {
       float f = Float.parseFloat(thresholdValue.getText());
-      slider.setValue((int) (f * 1000));
+      if (percentThreshold.isSelected())
+      {
+        setSliderPercentageValue(f);
+      }
+      else
+      {
+        setSliderValue(f);
+      }
       updateView();
     } catch (NumberFormatException ex)
     {
     }
   }
 
+  protected void percentageValue_actionPerformed()
+  {
+    setThresholdValueText();
+  }
+
   protected void thresholdIsMin_actionPerformed()
   {
     updateView();
@@ -313,7 +396,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 +408,6 @@ public abstract class AnnotationRowFilter extends JPanel
       }
       anns.addItem(anitem);
     }
-    adjusting = false;
     if (isvalid)
     {
       anns.setSelectedItem(cursel);
@@ -336,6 +419,9 @@ public abstract class AnnotationRowFilter extends JPanel
         anns.setSelectedIndex(0);
       }
     }
+    adjusting = false;
+
+    updateView();
   }
 
   protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
@@ -347,9 +433,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 +454,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 +512,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 +535,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 +575,32 @@ public abstract class AnnotationRowFilter extends JPanel
   {
     this.annotations = anns;
   }
+
+  protected void sliderDragReleased()
+  {
+    if (sliderDragging)
+    {
+      sliderDragging = false;
+      valueChanged(true);
+    }
+  }
+
+  /**
+   * Sets the min-max range and current value of the slider, with rescaling from
+   * true values to slider range as required
+   * 
+   * @param min
+   * @param max
+   * @param value
+   */
+  protected void setSliderModel(float min, float max, float value)
+  {
+    slider.setSliderModel(min, max, value);
+
+    /*
+     * tick mark every 10th position
+     */
+    slider.setMajorTickSpacing(
+            (slider.getMaximum() - slider.getMinimum()) / 10);
+  }
 }