JAL-2346 restore choice of annotation for colouring faithfully
[jalview.git] / src / jalview / gui / AnnotationRowFilter.java
index c8bd69c..ea531f2 100644 (file)
  */
 package jalview.gui;
 
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Vector;
+
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.GraphLine;
 import jalview.datamodel.SequenceGroup;
 import jalview.schemes.AnnotationColourGradient;
 import jalview.util.MessageManager;
 
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
-import java.util.Vector;
-
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
@@ -49,6 +51,12 @@ public abstract class AnnotationRowFilter extends JPanel
 
   protected int[] annmap;
 
+  /*
+   * map from annotation to its menu item display label
+   * - so we know which item to pre-select on restore
+   */
+  private Map<AlignmentAnnotation, String> annotationLabels;
+
   protected boolean enableSeqAss = false;
 
   private AlignmentAnnotation currentAnnotation;
@@ -141,14 +149,26 @@ public abstract class AnnotationRowFilter extends JPanel
 
   }
 
+  /**
+   * Builds and returns a list of menu items (display text) for choice of
+   * annotation. Also builds maps between annotations, their positions in the
+   * list, and their display labels in the list.
+   * 
+   * @param isSeqAssociated
+   * @return
+   */
   public Vector<String> getAnnotationItems(boolean isSeqAssociated)
   {
+    annotationLabels = new HashMap<AlignmentAnnotation, String>();
+
     Vector<String> list = new Vector<String>();
     int index = 1;
     int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
     for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
     {
-      if (av.getAlignment().getAlignmentAnnotation()[i].sequenceRef == null)
+      AlignmentAnnotation annotation = av.getAlignment()
+              .getAlignmentAnnotation()[i];
+      if (annotation.sequenceRef == null)
       {
         if (isSeqAssociated)
         {
@@ -159,28 +179,27 @@ public abstract class AnnotationRowFilter extends JPanel
       {
         enableSeqAss = true;
       }
-      String label = av.getAlignment().getAlignmentAnnotation()[i].label;
+      String label = annotation.label;
       // add associated sequence ID if available
-      if (!isSeqAssociated
-              && av.getAlignment().getAlignmentAnnotation()[i].sequenceRef != null)
+      if (!isSeqAssociated && annotation.sequenceRef != null)
       {
-        label = label
-                + "_"
-                + av.getAlignment().getAlignmentAnnotation()[i].sequenceRef
-                        .getName();
+        label = label + "_" + annotation.sequenceRef.getName();
       }
       // make label unique
       if (!list.contains(label))
       {
         anmap[list.size()] = i;
         list.add(label);
+        annotationLabels.put(annotation, label);
       }
       else
       {
         if (!isSeqAssociated)
         {
           anmap[list.size()] = i;
-          list.add(label + "_" + (index++));
+          label = label + "_" + (index++);
+          list.add(label);
+          annotationLabels.put(annotation, label);
         }
       }
     }
@@ -272,6 +291,12 @@ public abstract class AnnotationRowFilter extends JPanel
             .getString("label.threshold_feature_below_threshold"));
   }
 
+  /**
+   * Rebuilds the drop-down list of annotations to choose from when the 'per
+   * sequence only' checkbox is checked or unchecked.
+   * 
+   * @param annotations
+   */
   protected void seqAssociated_actionPerformed(JComboBox<String> annotations)
   {
     adjusting = true;
@@ -329,8 +354,8 @@ public abstract class AnnotationRowFilter extends JPanel
     }
   }
 
-  protected boolean colorAlignmContaining(AlignmentAnnotation currentAnn,
-          int selectedThresholdOption)
+  protected boolean colorAlignmentContaining(
+          AlignmentAnnotation currentAnn, int selectedThresholdOption)
   {
 
     AnnotationColourGradient acg = null;
@@ -352,7 +377,7 @@ public abstract class AnnotationRowFilter extends JPanel
       acg.setPredefinedColours(true);
     }
 
-    acg.thresholdIsMinMax = thresholdIsMin.isSelected();
+    acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
 
     av.setGlobalColourScheme(acg);
 
@@ -372,7 +397,6 @@ public abstract class AnnotationRowFilter extends JPanel
                   selectedThresholdOption);
           ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
                   .isSelected());
-
         }
         else
         {
@@ -382,19 +406,17 @@ public abstract class AnnotationRowFilter extends JPanel
           ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
                   .isSelected());
         }
-
       }
     }
     return false;
   }
 
-  public jalview.datamodel.AlignmentAnnotation getCurrentAnnotation()
+  public AlignmentAnnotation getCurrentAnnotation()
   {
     return currentAnnotation;
   }
 
-  public void setCurrentAnnotation(
-          jalview.datamodel.AlignmentAnnotation currentAnnotation)
+  public void setCurrentAnnotation(AlignmentAnnotation currentAnnotation)
   {
     this.currentAnnotation = currentAnnotation;
   }
@@ -404,4 +426,9 @@ public abstract class AnnotationRowFilter extends JPanel
   public abstract void updateView();
 
   public abstract void reset();
+
+  protected String getAnnotationMenuLabel(AlignmentAnnotation ann)
+  {
+    return annotationLabels.get(ann);
+  }
 }