JAL-2371 CollectionColourScheme wraps ColourSchemeI
[jalview.git] / src / jalview / gui / SliderPanel.java
index a381e8b..e28778c 100755 (executable)
@@ -22,13 +22,12 @@ package jalview.gui;
 
 import jalview.datamodel.SequenceGroup;
 import jalview.jbgui.GSliderPanel;
-import jalview.schemes.ColourSchemeI;
+import jalview.schemes.CollectionColourSchemeI;
 import jalview.util.MessageManager;
 
 import java.awt.event.ActionEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
-import java.util.Iterator;
 
 import javax.swing.JInternalFrame;
 import javax.swing.JLayeredPane;
@@ -51,7 +50,7 @@ public class SliderPanel extends GSliderPanel
 
   boolean forConservation = true;
 
-  ColourSchemeI cs;
+  CollectionColourSchemeI cs;
 
   /**
    * Creates a new SliderPanel object.
@@ -62,14 +61,14 @@ public class SliderPanel extends GSliderPanel
    *          DOCUMENT ME!
    * @param forConserve
    *          DOCUMENT ME!
-   * @param cs
+   * @param scheme
    *          DOCUMENT ME!
    */
   public SliderPanel(final AlignmentPanel ap, int value,
-          boolean forConserve, ColourSchemeI cs)
+          boolean forConserve, CollectionColourSchemeI scheme)
   {
     this.ap = ap;
-    this.cs = cs;
+    this.cs = scheme;
     forConservation = forConserve;
     undoButton.setVisible(false);
     applyButton.setVisible(false);
@@ -91,6 +90,7 @@ public class SliderPanel extends GSliderPanel
 
     slider.addChangeListener(new ChangeListener()
     {
+      @Override
       public void stateChanged(ChangeEvent evt)
       {
         valueField.setText(slider.getValue() + "");
@@ -100,6 +100,7 @@ public class SliderPanel extends GSliderPanel
 
     slider.addMouseListener(new MouseAdapter()
     {
+      @Override
       public void mouseReleased(MouseEvent evt)
       {
         ap.paintAlignment(true);
@@ -123,7 +124,7 @@ public class SliderPanel extends GSliderPanel
    * @return DOCUMENT ME!
    */
   public static int setConservationSlider(AlignmentPanel ap,
-          ColourSchemeI cs, String source)
+          CollectionColourSchemeI cs, String source)
   {
     SliderPanel sp = null;
 
@@ -177,6 +178,7 @@ public class SliderPanel extends GSliderPanel
       conservationSlider
               .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
               {
+                @Override
                 public void internalFrameClosed(
                         javax.swing.event.InternalFrameEvent e)
                 {
@@ -192,23 +194,24 @@ public class SliderPanel extends GSliderPanel
    * 
    * @param ap
    *          DOCUMENT ME!
-   * @param cs
+   * @param collectionColourScheme
    *          DOCUMENT ME!
    * @param source
    *          DOCUMENT ME!
    * 
    * @return DOCUMENT ME!
    */
-  public static int setPIDSliderSource(AlignmentPanel ap, ColourSchemeI cs,
+  public static int setPIDSliderSource(AlignmentPanel ap,
+          CollectionColourSchemeI collectionColourScheme,
           String source)
   {
     SliderPanel pid = null;
 
-    int threshold = cs.getThreshold();
+    int threshold = collectionColourScheme.getThreshold();
 
     if (PIDSlider == null)
     {
-      pid = new SliderPanel(ap, threshold, false, cs);
+      pid = new SliderPanel(ap, threshold, false, collectionColourScheme);
       PIDSlider = new JInternalFrame();
       PIDSlider.setContentPane(pid);
       PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
@@ -216,7 +219,7 @@ public class SliderPanel extends GSliderPanel
     else
     {
       pid = (SliderPanel) PIDSlider.getContentPane();
-      pid.cs = cs;
+      pid.cs = collectionColourScheme;
     }
 
     PIDSlider
@@ -257,6 +260,7 @@ public class SliderPanel extends GSliderPanel
       PIDSlider
               .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
               {
+                @Override
                 public void internalFrameClosed(
                         javax.swing.event.InternalFrameEvent e)
                 {
@@ -268,47 +272,25 @@ public class SliderPanel extends GSliderPanel
   }
 
   /**
-   * DOCUMENT ME!
+   * Updates the colour scheme with the current (identity threshold or
+   * conservation) percentage value. Also updates all groups if 'apply to all
+   * groups' is selected.
    * 
-   * @param i
-   *          DOCUMENT ME!
+   * @param percent
    */
-  public void valueChanged(int i)
+  public void valueChanged(int percent)
   {
-    if (cs == null)
+    if (!forConservation)
     {
-      return;
+      ap.av.setThreshold(percent);
     }
-
-    ColourSchemeI toChange = cs;
-    Iterator<SequenceGroup> allGroups = null;
+    updateColourScheme(percent, cs);
 
     if (allGroupsCheck.isSelected())
     {
-      allGroups = ap.av.getAlignment().getGroups().listIterator();
-    }
-
-    while (toChange != null)
-    {
-      if (forConservation)
-      {
-        toChange.setConservationInc(i);
-      }
-      else
-      {
-        toChange.setThreshold(i, ap.av.isIgnoreGapsConsensus());
-      }
-      if (allGroups != null && allGroups.hasNext())
-      {
-        while ((toChange = allGroups.next().cs) == null
-                && allGroups.hasNext())
-        {
-          ;
-        }
-      }
-      else
+      for (SequenceGroup sg : ap.av.getAlignment().getGroups())
       {
-        toChange = null;
+        updateColourScheme(percent, sg.getGroupColourScheme());
       }
     }
 
@@ -316,6 +298,29 @@ public class SliderPanel extends GSliderPanel
   }
 
   /**
+   * Updates the colour scheme (if not null) with the current (identity
+   * threshold or conservation) percentage value
+   * 
+   * @param percent
+   * @param scheme
+   */
+  protected void updateColourScheme(int percent, CollectionColourSchemeI scheme)
+  {
+    if (scheme == null)
+    {
+      return;
+    }
+    if (forConservation)
+    {
+      scheme.setConservationInc(percent);
+    }
+    else
+    {
+      scheme.setThreshold(percent, ap.av.isIgnoreGapsConsensus());
+    }
+  }
+
+  /**
    * DOCUMENT ME!
    * 
    * @param b
@@ -332,6 +337,7 @@ public class SliderPanel extends GSliderPanel
    * @param e
    *          DOCUMENT ME!
    */
+  @Override
   public void valueField_actionPerformed(ActionEvent e)
   {
     try
@@ -365,6 +371,7 @@ public class SliderPanel extends GSliderPanel
     return Integer.parseInt(valueField.getText());
   }
 
+  @Override
   public void slider_mouseReleased(MouseEvent e)
   {
     if (ap.overviewPanel != null)
@@ -373,4 +380,20 @@ public class SliderPanel extends GSliderPanel
     }
   }
 
+  public static int getConservationValue()
+  {
+    return getValue(conservationSlider);
+  }
+
+  static int getValue(JInternalFrame slider)
+  {
+    return slider == null ? 0 : ((SliderPanel) slider.getContentPane())
+            .getValue();
+  }
+
+  public static int getPIDValue()
+  {
+    return getValue(PIDSlider);
+  }
+
 }