JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / gui / FeatureTypeSettings.java
index bac9d9b..09bb2a3 100644 (file)
  */
 package jalview.gui;
 
+import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureColourI;
-import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.datamodel.GraphLine;
 import jalview.datamodel.features.FeatureAttributes;
 import jalview.datamodel.features.FeatureAttributes.Datatype;
@@ -49,6 +50,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.text.DecimalFormat;
 import java.util.ArrayList;
 import java.util.List;
@@ -62,7 +65,6 @@ import javax.swing.JComboBox;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JRadioButton;
-import javax.swing.JSlider;
 import javax.swing.JTextField;
 import javax.swing.border.EmptyBorder;
 import javax.swing.border.LineBorder;
@@ -78,6 +80,8 @@ import javax.swing.event.ChangeListener;
  */
 public class FeatureTypeSettings extends JalviewDialog
 {
+  private final static MathContext FOUR_SIG_FIG = new MathContext(4);
+
   private final static String LABEL_18N = MessageManager
           .getString("label.label");
 
@@ -141,11 +145,6 @@ public class FeatureTypeSettings extends JalviewDialog
   private float max;
 
   /*
-   * scale factor for conversion between absolute min-max and slider
-   */
-  float scaleFactor;
-
-  /*
    * radio button group, to select what to colour by:
    * simple colour, by category (text), or graduated
    */
@@ -156,7 +155,7 @@ public class FeatureTypeSettings extends JalviewDialog
   JRadioButton graduatedColour = new JRadioButton();
 
   JPanel coloursPanel;
-  
+
   JPanel filtersPanel;
 
   JPanel singleColour = new JPanel();
@@ -167,7 +166,7 @@ public class FeatureTypeSettings extends JalviewDialog
 
   private JComboBox<Object> threshold = new JComboBox<>();
 
-  JSlider slider = new JSlider();
+  private Slider slider;
 
   JTextField thresholdValue = new JTextField(20);
 
@@ -218,9 +217,9 @@ public class FeatureTypeSettings extends JalviewDialog
     ap = fr.ap;
     originalFilter = fr.getFeatureFilter(theType);
     originalColour = fr.getFeatureColours().get(theType);
-    
+
     adjusting = true;
-    
+
     try
     {
       initialise();
@@ -229,15 +228,15 @@ public class FeatureTypeSettings extends JalviewDialog
       ex.printStackTrace();
       return;
     }
-    
+
     updateColoursPanel();
-    
+
     updateFiltersPanel();
-    
+
     adjusting = false;
-    
+
     colourChanged(false);
-    
+
     String title = MessageManager
             .formatMessage("label.display_settings_for", new String[]
             { theType });
@@ -246,8 +245,8 @@ public class FeatureTypeSettings extends JalviewDialog
   }
 
   /**
-   * Configures the widgets on the Colours panel according to the current feature
-   * colour scheme
+   * Configures the widgets on the Colours panel according to the current
+   * feature colour scheme
    */
   private void updateColoursPanel()
   {
@@ -346,12 +345,11 @@ public class FeatureTypeSettings extends JalviewDialog
        * update min-max scaling if there is a range to work with,
        * else disable the widgets (this shouldn't happen if only 
        * valid options are offered in the combo box)
+       * offset slider to have only non-negative values if necessary (JAL-2983)
        */
-      scaleFactor = (max == min) ? 1f : 100f / (max - min);
-      float range = (max - min) * scaleFactor;
-      slider.setMinimum((int) (min * scaleFactor));
-      slider.setMaximum((int) (max * scaleFactor));
-      slider.setMajorTickSpacing((int) (range / 10f));
+      slider.setSliderModel(min, max, min);
+      slider.setMajorTickSpacing(
+              (int) ((slider.getMaximum() - slider.getMinimum()) / 10f));
 
       threshline = new GraphLine((max - min) / 2f, "Threshold",
               Color.black);
@@ -363,8 +361,8 @@ public class FeatureTypeSettings extends JalviewDialog
                 fc.isAboveThreshold() ? ABOVE_THRESHOLD_OPTION
                         : BELOW_THRESHOLD_OPTION);
         slider.setEnabled(true);
-        slider.setValue((int) (fc.getThreshold() * scaleFactor));
-        thresholdValue.setText(String.valueOf(fc.getThreshold()));
+        slider.setSliderValue(fc.getThreshold());
+        setThresholdValueText(fc.getThreshold());
         thresholdValue.setEnabled(true);
         thresholdIsMin.setEnabled(true);
       }
@@ -539,7 +537,8 @@ public class FeatureTypeSettings extends JalviewDialog
       {
         if (minColour.isEnabled())
         {
-          String ttl = MessageManager.getString("label.select_colour_minimum_value");
+          String ttl = MessageManager
+                  .getString("label.select_colour_minimum_value");
           showColourChooser(minColour, ttl);
         }
       }
@@ -556,7 +555,8 @@ public class FeatureTypeSettings extends JalviewDialog
       {
         if (maxColour.isEnabled())
         {
-          String ttl = MessageManager.getString("label.select_colour_maximum_value");
+          String ttl = MessageManager
+                  .getString("label.select_colour_maximum_value");
           showColourChooser(maxColour, ttl);
         }
       }
@@ -642,6 +642,7 @@ public class FeatureTypeSettings extends JalviewDialog
         thresholdValue_actionPerformed();
       }
     });
+    slider = new Slider(0f, 100f, 50f);
     slider.setPaintLabels(false);
     slider.setPaintTicks(true);
     slider.setBackground(Color.white);
@@ -658,8 +659,7 @@ public class FeatureTypeSettings extends JalviewDialog
       {
         if (!adjusting)
         {
-          thresholdValue
-                  .setText(String.valueOf(slider.getValue() / scaleFactor));
+          setThresholdValueText(slider.getSliderValue());
           thresholdValue.setBackground(Color.white); // to reset red for invalid
           sliderValueChanged();
         }
@@ -676,7 +676,7 @@ public class FeatureTypeSettings extends JalviewDialog
          */
         if (ap != null)
         {
-          ap.paintAlignment(true, true);
+          refreshDisplay(true);
         }
       }
     });
@@ -764,8 +764,8 @@ public class FeatureTypeSettings extends JalviewDialog
     // }
     // else
     // {
-      singleColour.setBackground(originalColour.getColour());
-      singleColour.setForeground(originalColour.getColour());
+    singleColour.setBackground(originalColour.getColour());
+    singleColour.setForeground(originalColour.getColour());
     // }
     singleColour.addMouseListener(new MouseAdapter()
     {
@@ -774,7 +774,8 @@ public class FeatureTypeSettings extends JalviewDialog
       {
         if (simpleColour.isSelected())
         {
-          String ttl = MessageManager.formatMessage("label.select_colour_for",  featureType);
+          String ttl = MessageManager
+                  .formatMessage("label.select_colour_for", featureType);
           showColourChooser(singleColour, ttl);
         }
       }
@@ -859,8 +860,8 @@ public class FeatureTypeSettings extends JalviewDialog
         colourChanged(true);
       }
     };
-       JalviewColourChooser.showColourChooser(this, title, 
-         colourPanel.getBackground(), listener);
+    JalviewColourChooser.showColourChooser(this, title,
+            colourPanel.getBackground(), listener);
   }
 
   /**
@@ -892,7 +893,7 @@ public class FeatureTypeSettings extends JalviewDialog
      * save the colour, and repaint stuff
      */
     fr.setColour(featureType, acg);
-    ap.paintAlignment(updateStructsAndOverview, updateStructsAndOverview);
+    refreshDisplay(updateStructsAndOverview);
 
     updateColoursPanel();
   }
@@ -1031,7 +1032,7 @@ public class FeatureTypeSettings extends JalviewDialog
   {
     fr.setColour(featureType, originalColour);
     fr.setFeatureFilter(featureType, originalFilter);
-    ap.paintAlignment(true, true);
+    refreshDisplay(true);
   }
 
   /**
@@ -1047,10 +1048,10 @@ public class FeatureTypeSettings extends JalviewDialog
        */
       adjusting = true;
       float f = Float.parseFloat(thresholdValue.getText());
-      f = Float.max(f,  this.min);
+      f = Float.max(f, this.min);
       f = Float.min(f, this.max);
-      thresholdValue.setText(String.valueOf(f));
-      slider.setValue((int) (f * scaleFactor));
+      setThresholdValueText(f);
+      slider.setSliderValue(f);
       threshline.value = f;
       thresholdValue.setBackground(Color.white); // ok
       adjusting = false;
@@ -1063,13 +1064,26 @@ public class FeatureTypeSettings extends JalviewDialog
   }
 
   /**
+   * Sets the text field for threshold value, rounded to four significant
+   * figures
+   * 
+   * @param f
+   */
+  void setThresholdValueText(float f)
+  {
+    BigDecimal formatted = new BigDecimal(f).round(FOUR_SIG_FIG)
+            .stripTrailingZeros();
+    thresholdValue.setText(formatted.toPlainString());
+  }
+
+  /**
    * Action on change of threshold slider value. This may be done interactively
    * (by moving the slider), or programmatically (to update the slider after
    * manual input of a threshold value).
    */
   protected void sliderValueChanged()
   {
-    threshline.value = getRoundedSliderValue();
+    threshline.value = slider.getSliderValue();
 
     /*
      * repaint alignment, but not Overview or structure,
@@ -1078,21 +1092,6 @@ public class FeatureTypeSettings extends JalviewDialog
     colourChanged(false);
   }
 
-  /**
-   * Converts the slider value to its absolute value by dividing by the
-   * scaleFactor. Rounding errors are squashed by forcing min/max of slider
-   * range to the actual min/max of feature score range
-   * 
-   * @return
-   */
-  private float getRoundedSliderValue()
-  {
-    int value = slider.getValue();
-    float f = value == slider.getMaximum() ? max
-            : (value == slider.getMinimum() ? min : value / scaleFactor);
-    return f;
-  }
-
   void addActionListener(ActionListener listener)
   {
     if (featureSettings != null)
@@ -1165,7 +1164,7 @@ public class FeatureTypeSettings extends JalviewDialog
     List<Object> displayAttsObjects = new ArrayList<>(displayAtts);
     JComboBox<Object> attCombo = JvSwingUtils
             .buildComboWithTooltips(displayAttsObjects, tooltips);
-    
+
     return attCombo;
   }
 
@@ -1701,7 +1700,7 @@ public class FeatureTypeSettings extends JalviewDialog
       attName = (String) attCombo.getSelectedItem();
     } catch (Exception e)
     {
-      Cache.log.error("Problem casting Combo box entry to String");
+      Console.error("Problem casting Combo box entry to String");
       attName = attCombo.getSelectedItem().toString();
     }
     Condition cond = (Condition) condCombo.getSelectedItem();
@@ -1780,8 +1779,26 @@ public class FeatureTypeSettings extends JalviewDialog
      * (note this might now be an empty filter with no conditions)
      */
     fr.setFeatureFilter(featureType, combined.isEmpty() ? null : combined);
-    ap.paintAlignment(true, true);
+    refreshDisplay(true);
 
     updateFiltersPanel();
   }
+
+  /**
+   * Repaints alignment, structure and overview (if shown). If there is a
+   * complementary view which is showing this view's features, then also
+   * repaints that.
+   * 
+   * @param updateStructsAndOverview
+   */
+  void refreshDisplay(boolean updateStructsAndOverview)
+  {
+    ap.paintAlignment(true, updateStructsAndOverview);
+    AlignViewportI complement = ap.getAlignViewport().getCodingComplement();
+    if (complement != null && complement.isShowComplementFeatures())
+    {
+      AlignFrame af2 = Desktop.getAlignFrameFor(complement);
+      af2.alignPanel.paintAlignment(true, updateStructsAndOverview);
+    }
+  }
 }