JAL-3083 another try: restore original GraphLines with original values
[jalview.git] / src / jalview / gui / AnnotationColourChooser.java
index 153f70c..2b7d678 100644 (file)
@@ -36,7 +36,9 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
-import java.util.Hashtable;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Vector;
 
 import javax.swing.BorderFactory;
@@ -59,7 +61,11 @@ public class AnnotationColourChooser extends AnnotationRowFilter
 
   private JButton defColours;
 
-  private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
+  private Map<SequenceGroup, ColourSchemeI> oldgroupColours;
+
+  private Map<AlignmentAnnotation, GraphLine> oldGraphLines;
+
+  private Map<GraphLine, Float> oldThresholds;
 
   private JCheckBox useOriginalColours = new JCheckBox();
 
@@ -73,21 +79,18 @@ public class AnnotationColourChooser extends AnnotationRowFilter
 
   protected static final int MIN_HEIGHT = 240;
 
+  /**
+   * Constructor
+   * 
+   * @param av
+   * @param ap
+   */
   public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
   {
     super(av, ap);
-    oldcs = av.getGlobalColourScheme();
-    if (av.getAlignment().getGroups() != null)
-    {
-      oldgroupColours = new Hashtable<>();
-      for (SequenceGroup sg : ap.av.getAlignment().getGroups())
-      {
-        if (sg.getColourScheme() != null)
-        {
-          oldgroupColours.put(sg, sg.getColourScheme());
-        }
-      }
-    }
+
+    saveInitialState();
+
     frame = new JInternalFrame();
     frame.setContentPane(this);
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
@@ -158,6 +161,47 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     frame.pack();
   }
 
+  /**
+   * Saves the global and group colour schemes as they are when the dialog is
+   * opened, so they can be restored on Cancel. Note we also save the threshold
+   * value for each annotation, in case this gets modified.
+   */
+  void saveInitialState()
+  {
+    oldcs = av.getGlobalColourScheme();
+    if (av.getAlignment().getGroups() != null)
+    {
+      oldgroupColours = new HashMap<>();
+      for (SequenceGroup sg : ap.av.getAlignment().getGroups())
+      {
+        if (sg.getColourScheme() != null)
+        {
+          oldgroupColours.put(sg, sg.getColourScheme());
+        }
+      }
+    }
+
+    /*
+     * save any existing annotation threshold settings; note we make a copy
+     * of each in case the current threshold value gets amended
+     */
+    oldGraphLines = new HashMap<>();
+    oldThresholds = new HashMap<>();
+    AlignmentAnnotation[] anns = av.getAlignment().getAlignmentAnnotation();
+    if (anns != null)
+    {
+      for (AlignmentAnnotation ann : anns)
+      {
+        GraphLine thresh = ann.getThreshold();
+        oldGraphLines.put(ann, thresh);
+        if (thresh != null)
+        {
+          oldThresholds.put(thresh, thresh.value);
+        }
+      }
+    }
+  }
+
   @Override
   protected void jbInit()
   {
@@ -326,6 +370,16 @@ public class AnnotationColourChooser extends AnnotationRowFilter
         sg.setColourScheme(oldgroupColours.get(sg));
       }
     }
+    for (Entry<AlignmentAnnotation, GraphLine> entry : oldGraphLines
+            .entrySet())
+    {
+      GraphLine graphLine = entry.getValue();
+      entry.getKey().setThreshold(graphLine);
+      if (graphLine != null)
+      {
+        graphLine.value = oldThresholds.get(graphLine).floatValue();
+      }
+    }
   }
 
   @Override
@@ -459,4 +513,11 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     }
   }
 
+  @Override
+  protected void sliderDragReleased()
+  {
+    super.sliderDragReleased();
+    ap.paintAlignment(true, true);
+  }
+
 }