JAL-3083 reset threshold on Cancel; Javadoc
[jalview.git] / src / jalview / gui / AnnotationColourChooser.java
index 384635b..b29a1dd 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,9 @@ public class AnnotationColourChooser extends AnnotationRowFilter
 
   private JButton defColours;
 
-  private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
+  private Map<SequenceGroup, ColourSchemeI> oldgroupColours;
+
+  private Map<AlignmentAnnotation, GraphLine> oldThresholds;
 
   private JCheckBox useOriginalColours = new JCheckBox();
 
@@ -73,21 +77,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 +159,43 @@ 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
+     */
+    oldThresholds = new HashMap<>();
+    AlignmentAnnotation[] anns = av.getAlignment().getAlignmentAnnotation();
+    if (anns != null)
+    {
+      for (AlignmentAnnotation ann : anns)
+      {
+        GraphLine thresh = ann.getThreshold();
+        oldThresholds.put(ann,
+                thresh == null ? null : new GraphLine(thresh));
+      }
+    }
+  }
+
   @Override
   protected void jbInit()
   {
@@ -326,6 +364,11 @@ public class AnnotationColourChooser extends AnnotationRowFilter
         sg.setColourScheme(oldgroupColours.get(sg));
       }
     }
+    for (Entry<AlignmentAnnotation, GraphLine> entry : oldThresholds
+            .entrySet())
+    {
+      entry.getKey().setThreshold(entry.getValue());
+    }
   }
 
   @Override