Merge branch 'features/JAL-2360colourSchemeApplicability' into features/JAL-2371colle...
[jalview.git] / src / jalview / gui / SliderPanel.java
index cbd0d58..e28778c 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.gui;
 
-import java.util.*;
+import jalview.datamodel.SequenceGroup;
+import jalview.jbgui.GSliderPanel;
+import jalview.schemes.CollectionColourSchemeI;
+import jalview.util.MessageManager;
 
-import java.awt.event.*;
-import javax.swing.*;
-import javax.swing.event.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
 
-import jalview.datamodel.*;
-import jalview.jbgui.*;
-import jalview.schemes.*;
-import jalview.util.MessageManager;
+import javax.swing.JInternalFrame;
+import javax.swing.JLayeredPane;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
 
 /**
  * DOCUMENT ME!
@@ -47,7 +50,7 @@ public class SliderPanel extends GSliderPanel
 
   boolean forConservation = true;
 
-  ColourSchemeI cs;
+  CollectionColourSchemeI cs;
 
   /**
    * Creates a new SliderPanel object.
@@ -58,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);
@@ -87,6 +90,7 @@ public class SliderPanel extends GSliderPanel
 
     slider.addChangeListener(new ChangeListener()
     {
+      @Override
       public void stateChanged(ChangeEvent evt)
       {
         valueField.setText(slider.getValue() + "");
@@ -96,6 +100,7 @@ public class SliderPanel extends GSliderPanel
 
     slider.addMouseListener(new MouseAdapter()
     {
+      @Override
       public void mouseReleased(MouseEvent evt)
       {
         ap.paintAlignment(true);
@@ -119,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;
 
@@ -136,7 +141,10 @@ public class SliderPanel extends GSliderPanel
       sp.cs = cs;
     }
 
-    conservationSlider.setTitle(MessageManager.formatMessage("label.conservation_colour_increment", new String[]{source}));
+    conservationSlider
+            .setTitle(MessageManager.formatMessage(
+                    "label.conservation_colour_increment",
+                    new String[] { source }));
 
     if (ap.av.getAlignment().getGroups() != null)
     {
@@ -170,6 +178,7 @@ public class SliderPanel extends GSliderPanel
       conservationSlider
               .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
               {
+                @Override
                 public void internalFrameClosed(
                         javax.swing.event.InternalFrameEvent e)
                 {
@@ -185,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);
@@ -209,10 +219,13 @@ public class SliderPanel extends GSliderPanel
     else
     {
       pid = (SliderPanel) PIDSlider.getContentPane();
-      pid.cs = cs;
+      pid.cs = collectionColourScheme;
     }
 
-    PIDSlider.setTitle(MessageManager.formatMessage("label.percentage_identity_thereshold", new String[]{source}));
+    PIDSlider
+            .setTitle(MessageManager.formatMessage(
+                    "label.percentage_identity_threshold",
+                    new String[] { source }));
 
     if (ap.av.getAlignment().getGroups() != null)
     {
@@ -247,6 +260,7 @@ public class SliderPanel extends GSliderPanel
       PIDSlider
               .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
               {
+                @Override
                 public void internalFrameClosed(
                         javax.swing.event.InternalFrameEvent e)
                 {
@@ -258,45 +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)
+      for (SequenceGroup sg : ap.av.getAlignment().getGroups())
       {
-        toChange.setConservationInc(i);
-      }
-      else
-      {
-        toChange.setThreshold(i, ap.av.getIgnoreGapsConsensus());
-      }
-      if (allGroups != null && allGroups.hasNext())
-      {
-        while ((toChange = allGroups.next().cs) == null
-                && allGroups.hasNext())
-          ;
-      }
-      else
-      {
-        toChange = null;
+        updateColourScheme(percent, sg.getGroupColourScheme());
       }
     }
 
@@ -304,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
@@ -320,6 +337,7 @@ public class SliderPanel extends GSliderPanel
    * @param e
    *          DOCUMENT ME!
    */
+  @Override
   public void valueField_actionPerformed(ActionEvent e)
   {
     try
@@ -353,6 +371,7 @@ public class SliderPanel extends GSliderPanel
     return Integer.parseInt(valueField.getText());
   }
 
+  @Override
   public void slider_mouseReleased(MouseEvent e)
   {
     if (ap.overviewPanel != null)
@@ -361,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);
+  }
+
 }