JAL-2848 apply / Cancel colour changes in Amend Features bug/JAL-2848resetColoursAmendFeatures
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 10 Jan 2018 11:41:58 +0000 (11:41 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 10 Jan 2018 11:41:58 +0000 (11:41 +0000)
src/jalview/gui/FeatureRenderer.java

index 9c4b009..b01170b 100644 (file)
@@ -44,7 +44,10 @@ import java.awt.event.MouseEvent;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import javax.swing.JColorChooser;
 import javax.swing.JComboBox;
@@ -79,6 +82,14 @@ public class FeatureRenderer
 
   AlignmentPanel ap;
 
+  /*
+   * the colour scheme selected by the user for the current feature type
+   * when amending features
+   */
+  FeatureColourI fcol;
+
+  int featureIndex = 0;
+
   /**
    * Creates a new FeatureRenderer object
    * 
@@ -96,10 +107,6 @@ public class FeatureRenderer
     }
   }
 
-  FeatureColourI oldcol, fcol;
-
-  int featureIndex = 0;
-
   /**
    * Presents a dialog allowing the user to add new features, or amend or delete
    * existing features. Currently this can be on
@@ -127,6 +134,19 @@ public class FeatureRenderer
           final List<SequenceFeature> features, boolean create,
           final AlignmentPanel alignPanel)
   {
+    /*
+     * save original colours, for reset on Cancel
+     */
+    Map<String, FeatureColourI> oldColours = new HashMap<>();
+    for (SequenceFeature sf : features)
+    {
+      String type = sf.getType();
+      if (type != null)
+      {
+        oldColours.put(type, getFeatureStyle(type));
+      }
+    }
+
     featureIndex = 0;
 
     final JPanel mainPanel = new JPanel(new BorderLayout());
@@ -192,6 +212,8 @@ public class FeatureRenderer
       @Override
       public void mousePressed(MouseEvent evt)
       {
+        final String ft = features.get(featureIndex).getType();
+        final String type = ft == null ? lastFeatureAdded : ft;
         if (fcol.isSimpleColour())
         {
           Color col = JColorChooser.showDialog(Desktop.desktop,
@@ -200,6 +222,8 @@ public class FeatureRenderer
           if (col != null)
           {
             fcol = new FeatureColour(col);
+            setColour(type, fcol);
+            alignPanel.paintAlignment(true, true);
             updateColourButton(mainPanel, colour, fcol);
           }
         }
@@ -207,8 +231,6 @@ public class FeatureRenderer
         {
           if (fcc == null)
           {
-            final String ft = features.get(featureIndex).getType();
-            final String type = ft == null ? lastFeatureAdded : ft;
             fcc = new FeatureColourChooser(me, type);
             fcc.setRequestFocusEnabled(true);
             fcc.requestFocus();
@@ -287,7 +309,7 @@ public class FeatureRenderer
             col = new FeatureColour(
                     ColorUtils.createColourFromName(name.getText()));
           }
-          oldcol = fcol = col;
+          fcol = col;
           updateColourButton(mainPanel, colour, col);
         }
       });
@@ -362,8 +384,9 @@ public class FeatureRenderer
     start.setValue(new Integer(firstFeature.getBegin()));
     end.setValue(new Integer(firstFeature.getEnd()));
     description.setText(firstFeature.getDescription());
-    updateColourButton(mainPanel, colour,
-            (oldcol = fcol = getFeatureStyle(featureType)));
+    fcol = getFeatureStyle(featureType);
+    oldColours.put(featureType, fcol);
+    updateColourButton(mainPanel, colour, fcol);
     Object[] options;
     if (!create)
     {
@@ -437,7 +460,7 @@ public class FeatureRenderer
          */
         boolean refreshSettings = (!featureType.equals(enteredType) || !featureGroup
                 .equals(enteredGroup));
-        refreshSettings |= (fcol != oldcol);
+        refreshSettings |= (fcol != oldColours.get(featureType));
         setColour(enteredType, fcol);
         int newBegin = sf.begin;
         int newEnd = sf.end;
@@ -467,6 +490,11 @@ public class FeatureRenderer
           featuresAdded();
         }
       }
+      else
+      {
+        // Cancel: reset any colour changes applied
+        resetColours(oldColours);
+      }
     }
     else
     // NEW FEATURES ADDED
@@ -493,6 +521,8 @@ public class FeatureRenderer
       }
       else
       {
+        // Cancel: reset any colour changes applied
+        resetColours(oldColours);
         return false;
       }
     }
@@ -503,6 +533,19 @@ public class FeatureRenderer
   }
 
   /**
+   * Reset features to their original colours (on Cancel)
+   * 
+   * @param colours
+   */
+  private void resetColours(Map<String, FeatureColourI> colours)
+  {
+    for (Entry<String, FeatureColourI> entry : colours.entrySet())
+    {
+      setColour(entry.getKey(), entry.getValue());
+    }
+  }
+
+  /**
    * Show a warning message if the entered type is one that is currently hidden
    * 
    * @param panel