JAL-4071 colour rows according to simple/graduated feature colour, beginning of suppo...
[jalview.git] / src / jalview / workers / VisibleFeaturesAnnotationTracks.java
index baa8038..6e54628 100644 (file)
  */
 package jalview.workers;
 
+import java.awt.Color;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import jalview.api.AlignViewportI;
+import jalview.api.FeatureColourI;
 import jalview.api.FeaturesDisplayedI;
 import jalview.datamodel.SequenceFeature;
 import jalview.gui.FeatureRenderer;
@@ -44,7 +47,10 @@ public class VisibleFeaturesAnnotationTracks implements FeatureSetCounterI
     @Override
     public void propertyChange(PropertyChangeEvent evt)
     {
-      if (ourViewport != null) // could check source is
+      if (ourViewport != null) // could
+                               // check
+                               // source
+                               // is
                                // ourFr.getChangeSupport...
       {
         updateFeatureAnnotationTracks();
@@ -91,6 +97,9 @@ public class VisibleFeaturesAnnotationTracks implements FeatureSetCounterI
             .isShowSequenceFeatureCounts()
                     ? ourViewport.getFeaturesDisplayed()
                     : null;
+    // get latest FeatureRenderer, just in case it's different.
+    ourFr = ourFr.getAlignPanel().getFeatureRenderer();
+
     Set<String> visibleFeatures = new HashSet();
     if (featuresDisp != null)
     {
@@ -103,13 +112,35 @@ public class VisibleFeaturesAnnotationTracks implements FeatureSetCounterI
     }
     // otherwise set up tracks accordingly
 
+    int[][] minC = new int[visibleFeatures.size()][3],
+            maxC = new int[visibleFeatures.size()][3];
+    Map<String, FeatureColourI> fcs = ourFr.getDisplayedFeatureCols();
+    int p = 0;
+    for (String s : visibleFeatures)
+    {
+      FeatureColourI color = fcs.get(s);
+      if (color.isSimpleColour())
+      {
+        minC[p] = new int[] { 133, 133, 133 };
+        maxC[p] = new int[] { color.getColour().getRed(),
+            color.getColour().getGreen(), color.getColour().getBlue() };
+      }
+      else
+      {
+        Color min = color.getMinColour(), max = color.getMaxColour();
+        minC[p] = new int[] { min.getRed(), min.getGreen(), min.getBlue() };
+        maxC[p] = new int[] { max.getRed(), max.getGreen(), max.getBlue() };
+      }
+      p++;
+    }
+    minColours = minC;
+    maxColours = maxC;
     /*
      * and register the counter
      */
     if (ourWorker != null)
     {
-      Set<String> toRemove = new HashSet<String>(),
-              toAdd = new HashSet<String>();
+      Set<String> toRemove = new HashSet<String>();
       toRemove.addAll(dispFeatures);
       toRemove.removeAll(visibleFeatures);
       dispFeatures = visibleFeatures;
@@ -169,15 +200,25 @@ public class VisibleFeaturesAnnotationTracks implements FeatureSetCounterI
     return dispFeatures.toArray(new String[0]);
   }
 
+  int[][] minColours = null, maxColours = null;
+
   @Override
-  public int[] getMaxColour()
+  public int[] getMaxColour(int row)
   {
+    if (maxColours != null && row >= 0 && row < maxColours.length)
+    {
+      return maxColours[row];
+    }
     return new int[] { 0, 0, 255 };
   }
 
   @Override
-  public int[] getMinColour()
+  public int[] getMinColour(int row)
   {
-    return new int[] { 0, 255, 255 };
+    if (minColours != null && row >= 0 && row < minColours.length)
+    {
+      return minColours[row];
+    }
+    return new int[] { 133, 133, 133 };
   }
 }
\ No newline at end of file