JAL-2738 copy to spikes/mungo
[jalview.git] / src / jalview / appletgui / SliderPanel.java
index 2aa010f..565ebe8 100644 (file)
@@ -20,8 +20,9 @@
  */
 package jalview.appletgui;
 
+import jalview.analysis.Conservation;
 import jalview.datamodel.SequenceGroup;
-import jalview.schemes.CollectionColourSchemeI;
+import jalview.renderer.ResidueShaderI;
 import jalview.util.MessageManager;
 
 import java.awt.BorderLayout;
@@ -44,45 +45,46 @@ import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
-import java.util.Iterator;
+import java.util.List;
 
-public class SliderPanel extends Panel implements ActionListener,
-        AdjustmentListener, MouseListener
+public class SliderPanel extends Panel
+        implements ActionListener, AdjustmentListener, MouseListener
 {
+  private static final String BACKGROUND = "Background";
+
   AlignmentPanel ap;
 
   boolean forConservation = true;
 
-  CollectionColourSchemeI cs;
+  ResidueShaderI cs;
 
   static Frame conservationSlider;
 
   static Frame PIDSlider;
 
   public static int setConservationSlider(AlignmentPanel ap,
-          CollectionColourSchemeI collectionColourSchemeI, String source)
+          ResidueShaderI ccs, String source)
   {
     SliderPanel sp = null;
 
     if (conservationSlider == null)
     {
-      sp = new SliderPanel(ap,
-              collectionColourSchemeI.getConservationInc(), true,
-              collectionColourSchemeI);
+      sp = new SliderPanel(ap, ccs.getConservationInc(), true, ccs);
       conservationSlider = new Frame();
       conservationSlider.add(sp);
     }
     else
     {
       sp = (SliderPanel) conservationSlider.getComponent(0);
-      sp.cs = collectionColourSchemeI;
+      sp.cs = ccs;
+      sp.valueField.setText(String.valueOf(ccs.getConservationInc()));
     }
 
-    conservationSlider
-            .setTitle(MessageManager.formatMessage(
-                    "label.conservation_colour_increment",
-                    new String[] { source }));
-    if (ap.av.getAlignment().getGroups() != null)
+    conservationSlider.setTitle(MessageManager.formatMessage(
+            "label.conservation_colour_increment", new String[]
+            { source == null ? BACKGROUND : source }));
+    List<SequenceGroup> groups = ap.av.getAlignment().getGroups();
+    if (groups != null && !groups.isEmpty())
     {
       sp.setAllGroupsCheckEnabled(true);
     }
@@ -122,7 +124,7 @@ public class SliderPanel extends Panel implements ActionListener,
   }
 
   public static int setPIDSliderSource(AlignmentPanel ap,
-          CollectionColourSchemeI ccs, String source)
+          ResidueShaderI ccs, String source)
   {
     SliderPanel pid = null;
     if (PIDSlider == null)
@@ -135,11 +137,11 @@ public class SliderPanel extends Panel implements ActionListener,
     {
       pid = (SliderPanel) PIDSlider.getComponent(0);
       pid.cs = ccs;
+      pid.valueField.setText(String.valueOf(ccs.getThreshold()));
     }
-    PIDSlider
-            .setTitle(MessageManager.formatMessage(
-                    "label.percentage_identity_threshold",
-                    new String[] { source }));
+    PIDSlider.setTitle(MessageManager.formatMessage(
+            "label.percentage_identity_threshold", new String[]
+            { source == null ? BACKGROUND : source }));
 
     if (ap.av.getAlignment().getGroups() != null)
     {
@@ -166,8 +168,8 @@ public class SliderPanel extends Panel implements ActionListener,
 
     if (!PIDSlider.isVisible())
     {
-      jalview.bin.JalviewLite.addFrame(PIDSlider, PIDSlider.getTitle(),
-              420, 100);
+      jalview.bin.JalviewLite.addFrame(PIDSlider, PIDSlider.getTitle(), 420,
+              100);
       PIDSlider.addWindowListener(new WindowAdapter()
       {
         @Override
@@ -203,8 +205,9 @@ public class SliderPanel extends Panel implements ActionListener,
       conservationSlider = null;
     }
   }
+
   public SliderPanel(AlignmentPanel ap, int value, boolean forConserve,
-          CollectionColourSchemeI collectionColourSchemeI)
+          ResidueShaderI shader)
   {
     try
     {
@@ -214,7 +217,7 @@ public class SliderPanel extends Panel implements ActionListener,
       e.printStackTrace();
     }
     this.ap = ap;
-    this.cs = collectionColourSchemeI;
+    this.cs = shader;
     forConservation = forConserve;
     undoButton.setVisible(false);
     applyButton.setVisible(false);
@@ -248,45 +251,53 @@ public class SliderPanel extends Panel implements ActionListener,
     {
       return;
     }
-
-    CollectionColourSchemeI toChange = cs;
-    Iterator<SequenceGroup> allGroups = null;
-
-    if (allGroupsCheck.getState())
+    if (forConservation)
     {
-      allGroups = ap.av.getAlignment().getGroups().listIterator();
+      cs.setConservationApplied(true);
+      cs.setConservationInc(i);
+    }
+    else
+    {
+      cs.setThreshold(i, ap.av.isIgnoreGapsConsensus());
     }
 
-    while (toChange != null)
+    if (allGroupsCheck.getState())
     {
-      if (forConservation)
+      for (SequenceGroup group : ap.av.getAlignment().getGroups())
       {
-        toChange.setConservationInc(i);
-      }
-      else
-      {
-        toChange.setThreshold(i, ap.av.isIgnoreGapsConsensus());
-      }
-      if (allGroups != null && allGroups.hasNext())
-      {
-        while ((toChange = allGroups.next().cs) == null
-                && allGroups.hasNext())
+        ResidueShaderI groupColourScheme = group.getGroupColourScheme();
+        if (forConservation)
         {
-          ;
+          if (!groupColourScheme.conservationApplied())
+          {
+            /*
+             * first time the colour scheme has had Conservation shading applied
+             * - compute conservation
+             */
+            Conservation c = new Conservation("Group",
+                    group.getSequences(null), group.getStartRes(),
+                    group.getEndRes());
+            c.calculate();
+            c.verdict(false, ap.av.getConsPercGaps());
+            group.cs.setConservation(c);
+
+          }
+          groupColourScheme.setConservationApplied(true);
+          groupColourScheme.setConservationInc(i);
+        }
+        else
+        {
+          groupColourScheme.setThreshold(i, ap.av.isIgnoreGapsConsensus());
         }
-      }
-      else
-      {
-        toChange = null;
       }
     }
 
     ap.seqPanel.seqCanvas.repaint();
-
   }
 
   public void setAllGroupsCheckEnabled(boolean b)
   {
+    allGroupsCheck.setState(ap.av.getColourAppliesToAllGroups());
     allGroupsCheck.setEnabled(b);
   }
 
@@ -384,7 +395,7 @@ public class SliderPanel extends Panel implements ActionListener,
         valueChanged(slider.getValue());
       }
     });
-    
+
     label.setFont(new java.awt.Font("Verdana", 0, 11));
     label.setText(MessageManager.getString("label.set_this_label_text"));
     jPanel1.setLayout(borderLayout1);
@@ -398,10 +409,10 @@ public class SliderPanel extends Panel implements ActionListener,
     undoButton.addActionListener(this);
     allGroupsCheck.setEnabled(false);
     allGroupsCheck.setFont(new java.awt.Font("Verdana", 0, 11));
-    allGroupsCheck.setLabel(MessageManager
-            .getString("action.apply_threshold_all_groups"));
-    allGroupsCheck.setName(MessageManager
-            .getString("action.apply_all_groups"));
+    allGroupsCheck.setLabel(
+            MessageManager.getString("action.apply_threshold_all_groups"));
+    allGroupsCheck
+            .setName(MessageManager.getString("action.apply_all_groups"));
     this.setBackground(Color.white);
     this.setForeground(Color.black);
     jPanel2.add(label, null);