From 8d455fca935d22c72ac32afc39633eb485da9d38 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Wed, 10 Jan 2018 11:41:58 +0000 Subject: [PATCH] JAL-2848 apply / Cancel colour changes in Amend Features --- src/jalview/gui/FeatureRenderer.java | 63 ++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/src/jalview/gui/FeatureRenderer.java b/src/jalview/gui/FeatureRenderer.java index 9c4b009..b01170b 100644 --- a/src/jalview/gui/FeatureRenderer.java +++ b/src/jalview/gui/FeatureRenderer.java @@ -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 features, boolean create, final AlignmentPanel alignPanel) { + /* + * save original colours, for reset on Cancel + */ + Map 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 colours) + { + for (Entry entry : colours.entrySet()) + { + setColour(entry.getKey(), entry.getValue()); + } + } + + /** * Show a warning message if the entered type is one that is currently hidden * * @param panel -- 1.7.10.2