JAL-3083 another try: restore original GraphLines with original values
[jalview.git] / src / jalview / gui / AnnotationColourChooser.java
index d24409f..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,9 +61,13 @@ public class AnnotationColourChooser extends AnnotationRowFilter
 
   private JButton defColours;
 
-  private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
+  private Map<SequenceGroup, ColourSchemeI> oldgroupColours;
 
-  private JCheckBox currentColours = new JCheckBox();
+  private Map<AlignmentAnnotation, GraphLine> oldGraphLines;
+
+  private Map<GraphLine, Float> oldThresholds;
+
+  private JCheckBox useOriginalColours = new JCheckBox();
 
   private JPanel minColour = new JPanel();
 
@@ -69,28 +75,29 @@ public class AnnotationColourChooser extends AnnotationRowFilter
 
   private JCheckBox thresholdIsMin = new JCheckBox();
 
+  protected static final int MIN_WIDTH = 500;
+
+  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<SequenceGroup, ColourSchemeI>();
-      for (SequenceGroup sg : ap.av.getAlignment().getGroups())
-      {
-        if (sg.cs != null)
-        {
-          oldgroupColours.put(sg, sg.cs);
-        }
-      }
-    }
+
+    saveInitialState();
+
     frame = new JInternalFrame();
     frame.setContentPane(this);
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
     Desktop.addInternalFrame(frame,
             MessageManager.getString("label.colour_by_annotation"), 520,
             215);
-
+    frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
     addSliderChangeListener();
     addSliderMouseListeners();
 
@@ -106,8 +113,8 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     if (oldcs instanceof AnnotationColourGradient)
     {
       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
-      currentColours.setSelected(acg.isPredefinedColours()
-              || acg.getBaseColour() != null);
+      useOriginalColours.setSelected(
+              acg.isPredefinedColours() || acg.getBaseColour() != null);
       if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
       {
         minColour.setBackground(acg.getMinColour());
@@ -116,9 +123,9 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       seqAssociated.setSelected(acg.isSeqAssociated());
 
     }
-    Vector<String> annotItems = getAnnotationItems(seqAssociated
-            .isSelected());
-    annotations = new JComboBox<String>(annotItems);
+    Vector<String> annotItems = getAnnotationItems(
+            seqAssociated.isSelected());
+    annotations = new JComboBox<>(annotItems);
 
     populateThresholdComboBox(threshold);
 
@@ -139,9 +146,8 @@ public class AnnotationColourChooser extends AnnotationRowFilter
         getThreshold().setSelectedIndex(2);
         break;
       default:
-        throw new Error(
-                MessageManager
-                        .getString("error.implementation_error_dont_know_about_threshold_setting"));
+        throw new Error(MessageManager.getString(
+                "error.implementation_error_dont_know_about_threshold_setting"));
       }
       thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
       thresholdValue.setText("" + acg.getAnnotationThreshold());
@@ -155,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()
   {
@@ -206,22 +253,22 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       }
     });
 
-    currentColours.setFont(JvSwingUtils.getLabelFont());
-    currentColours.setOpaque(false);
-    currentColours.setText(MessageManager
-            .getString("label.use_original_colours"));
-    currentColours.addActionListener(new ActionListener()
+    useOriginalColours.setFont(JvSwingUtils.getLabelFont());
+    useOriginalColours.setOpaque(false);
+    useOriginalColours.setText(
+            MessageManager.getString("label.use_original_colours"));
+    useOriginalColours.addActionListener(new ActionListener()
     {
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        currentColours_actionPerformed();
+        originalColours_actionPerformed();
       }
     });
     thresholdIsMin.setBackground(Color.white);
     thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
-    thresholdIsMin.setText(MessageManager
-            .getString("label.threshold_minmax"));
+    thresholdIsMin
+            .setText(MessageManager.getString("label.threshold_minmax"));
     thresholdIsMin.addActionListener(new ActionListener()
     {
       @Override
@@ -232,8 +279,8 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     });
     seqAssociated.setBackground(Color.white);
     seqAssociated.setFont(JvSwingUtils.getLabelFont());
-    seqAssociated.setText(MessageManager
-            .getString("label.per_sequence_only"));
+    seqAssociated
+            .setText(MessageManager.getString("label.per_sequence_only"));
     seqAssociated.addActionListener(new ActionListener()
     {
 
@@ -255,7 +302,7 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     jPanel1.add(cancel);
     jPanel2.add(annotations, "grow, wrap");
     jPanel2.add(seqAssociated);
-    jPanel2.add(currentColours);
+    jPanel2.add(useOriginalColours);
     JPanel colpanel = new JPanel(new FlowLayout());
     colpanel.setBackground(Color.white);
     colpanel.add(minColour);
@@ -279,10 +326,10 @@ public class AnnotationColourChooser extends AnnotationRowFilter
 
   private void setDefaultMinMax()
   {
-    minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
-            Color.orange));
-    maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
-            Color.red));
+    minColour.setBackground(
+            Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
+    maxColour.setBackground(
+            Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
   }
 
   public void minColour_actionPerformed()
@@ -320,7 +367,17 @@ public class AnnotationColourChooser extends AnnotationRowFilter
 
       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
       {
-        sg.cs = oldgroupColours.get(sg);
+        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();
       }
     }
   }
@@ -330,26 +387,28 @@ public class AnnotationColourChooser extends AnnotationRowFilter
   {
     if (slider.isEnabled())
     {
-      if (currentColours.isSelected()
-              && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
+      if (useOriginalColours.isSelected() && !(av
+              .getGlobalColourScheme() instanceof AnnotationColourGradient))
       {
         updateView();
       }
       getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
       propagateSeqAssociatedThreshold(updateAllAnnotation,
               getCurrentAnnotation());
-      ap.paintAlignment(false);
+      ap.paintAlignment(false, false);
     }
   }
 
-  public void currentColours_actionPerformed()
+  public void originalColours_actionPerformed()
   {
-    if (currentColours.isSelected())
+    boolean selected = useOriginalColours.isSelected();
+    if (selected)
     {
       reset();
     }
-    maxColour.setEnabled(!currentColours.isSelected());
-    minColour.setEnabled(!currentColours.isSelected());
+    maxColour.setEnabled(!selected);
+    minColour.setEnabled(!selected);
+    thresholdIsMin.setEnabled(!selected);
     updateView();
   }
 
@@ -362,15 +421,16 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       return;
     }
 
-    setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[annotations
-            .getSelectedIndex()]]);
+    setCurrentAnnotation(
+            av.getAlignment().getAlignmentAnnotation()[annmap[annotations
+                    .getSelectedIndex()]]);
 
-    int selectedThresholdItem = getSelectedThresholdItem(getThreshold()
-            .getSelectedIndex());
+    int selectedThresholdItem = getSelectedThresholdItem(
+            getThreshold().getSelectedIndex());
 
     slider.setEnabled(true);
     thresholdValue.setEnabled(true);
-    thresholdIsMin.setEnabled(true);
+    thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
 
     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
     {
@@ -382,11 +442,10 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
             && getCurrentAnnotation().threshold == null)
     {
-      getCurrentAnnotation()
-              .setThreshold(
-                      new GraphLine(
-                              (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
-                              "Threshold", Color.black));
+      getCurrentAnnotation().setThreshold(new GraphLine(
+              (getCurrentAnnotation().graphMax
+                      - getCurrentAnnotation().graphMin) / 2f,
+              "Threshold", Color.black));
     }
 
     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
@@ -395,9 +454,12 @@ public class AnnotationColourChooser extends AnnotationRowFilter
       float range = getCurrentAnnotation().graphMax * ONETHOUSAND
               - getCurrentAnnotation().graphMin * ONETHOUSAND;
 
-      slider.setMinimum((int) (getCurrentAnnotation().graphMin * ONETHOUSAND));
-      slider.setMaximum((int) (getCurrentAnnotation().graphMax * ONETHOUSAND));
-      slider.setValue((int) (getCurrentAnnotation().threshold.value * ONETHOUSAND));
+      slider.setMinimum(
+              (int) (getCurrentAnnotation().graphMin * ONETHOUSAND));
+      slider.setMaximum(
+              (int) (getCurrentAnnotation().graphMax * ONETHOUSAND));
+      slider.setValue(
+              (int) (getCurrentAnnotation().threshold.value * ONETHOUSAND));
       thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
       slider.setMajorTickSpacing((int) (range / 10f));
       slider.setEnabled(true);
@@ -407,16 +469,14 @@ public class AnnotationColourChooser extends AnnotationRowFilter
     colorAlignmentContaining(getCurrentAnnotation(), selectedThresholdItem);
 
     ap.alignmentChanged();
-    // ensure all associated views (overviews, structures, etc) are notified of
-    // updated colours.
-    ap.paintAlignment(true);
   }
 
-  protected boolean colorAlignmentContaining(AlignmentAnnotation currentAnn, int selectedThresholdOption)
+  protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
+          int selectedThresholdOption)
   {
-  
+
     AnnotationColourGradient acg = null;
-    if (currentColours.isSelected())
+    if (useOriginalColours.isSelected())
     {
       acg = new AnnotationColourGradient(currentAnn,
               av.getGlobalColourScheme(), selectedThresholdOption);
@@ -428,44 +488,36 @@ public class AnnotationColourChooser extends AnnotationRowFilter
               selectedThresholdOption);
     }
     acg.setSeqAssociated(seqAssociated.isSelected());
-  
+
     if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
     {
       acg.setPredefinedColours(true);
     }
-  
+
     acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
-  
+
     av.setGlobalColourScheme(acg);
-  
+
     if (av.getAlignment().getGroups() != null)
     {
-  
+
       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
       {
         if (sg.cs == null)
         {
           continue;
         }
-  
-        if (currentColours.isSelected())
-        {
-          sg.cs = new AnnotationColourGradient(currentAnn, sg.cs,
-                  selectedThresholdOption);
-          ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
-                  .isSelected());
-        }
-        else
-        {
-          sg.cs = new AnnotationColourGradient(currentAnn,
-                  minColour.getBackground(), maxColour.getBackground(),
-                  selectedThresholdOption);
-          ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
-                  .isSelected());
-        }
+        sg.setColourScheme(
+                acg.getInstance(sg, ap.av.getHiddenRepSequences()));
       }
     }
-    return false;
+  }
+
+  @Override
+  protected void sliderDragReleased()
+  {
+    super.sliderDragReleased();
+    ap.paintAlignment(true, true);
   }
 
 }