Merge branch 'develop' into features/JAL-2446NCList
[jalview.git] / src / jalview / gui / FeatureRenderer.java
index 55c4323..c0e3e73 100644 (file)
@@ -43,6 +43,7 @@ import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
 
 import javax.swing.JColorChooser;
@@ -386,7 +387,10 @@ public class FeatureRenderer extends
 
     FeaturesFile ffile = new FeaturesFile();
 
-    String enteredType = name.getText().trim();
+    final String enteredType = name.getText().trim();
+    final String enteredGroup = group.getText().trim();
+    final String enteredDescription = description.getText().replaceAll("\n", " ");
+
     if (reply == JvOptionPane.OK_OPTION && enteredType.length() > 0)
     {
       /*
@@ -395,7 +399,7 @@ public class FeatureRenderer extends
       if (useLastDefaults)
       {
         lastFeatureAdded = enteredType;
-        lastFeatureGroupAdded = group.getText().trim();
+        lastFeatureGroupAdded = enteredGroup;
         // TODO: determine if the null feature group is valid
         if (lastFeatureGroupAdded.length() < 1)
         {
@@ -421,26 +425,50 @@ public class FeatureRenderer extends
       {
         /*
          * YES_OPTION corresponds to the Amend button
-         * need to refresh Feature Settings if type, group or colour changed
+         * need to refresh Feature Settings if type, group or colour changed;
+         * note we don't force the feature to be visible - the user has been
+         * warned if a hidden feature type or group was entered
          */
-        sf.type = enteredType;
-        sf.featureGroup = group.getText().trim();
-        sf.description = description.getText().replaceAll("\n", " ");
-        boolean refreshSettings = (!featureType.equals(sf.type) || !featureGroup
-                .equals(sf.featureGroup));
+        boolean refreshSettings = (!featureType.equals(enteredType) || !featureGroup
+                .equals(enteredGroup));
         refreshSettings |= (fcol != oldcol);
-
-        setColour(sf.type, fcol);
-
+        setColour(enteredType, fcol);
+        int newBegin = sf.begin;
+        int newEnd = sf.end;
         try
         {
-          sf.begin = ((Integer) start.getValue()).intValue();
-          sf.end = ((Integer) end.getValue()).intValue();
+          newBegin = ((Integer) start.getValue()).intValue();
+          newEnd = ((Integer) end.getValue()).intValue();
         } catch (NumberFormatException ex)
         {
+          // JSpinner doesn't accept invalid format data :-)
+        }
+
+        /*
+         * replace the feature by deleting it and adding a new one
+         * (to ensure integrity of SequenceFeatures data store)
+         */
+        sequences.get(0).deleteFeature(sf);
+        SequenceFeature newSf = new SequenceFeature(enteredType,
+                enteredDescription, newBegin, newEnd, sf.getScore(),
+                enteredGroup);
+        // ensure any additional properties are copied
+        if (sf.otherDetails != null)
+        {
+          newSf.otherDetails = new HashMap<String, Object>(sf.otherDetails);
+        }
+        ffile.parseDescriptionHTML(newSf, false);
+        // add any additional links not parsed from description
+        if (sf.links != null)
+        {
+          for (String link : sf.links)
+          {
+            newSf.addLink(link);
+          }
         }
+        // amend features only gets one sequence to act on
+        sequences.get(0).addSequenceFeature(newSf);
 
-        ffile.parseDescriptionHTML(sf, false);
         if (refreshSettings)
         {
           featuresAdded();
@@ -455,12 +483,11 @@ public class FeatureRenderer extends
         for (int i = 0; i < sequences.size(); i++)
         {
           SequenceFeature sf = features.get(i);
-          sf.type = enteredType;
-          // fix for JAL-1538 - always set feature group here
-          sf.featureGroup = group.getText().trim();
-          sf.description = description.getText().replaceAll("\n", " ");
-          sequences.get(i).addSequenceFeature(sf);
-          ffile.parseDescriptionHTML(sf, false);
+          SequenceFeature sf2 = new SequenceFeature(enteredType,
+                  enteredDescription, sf.getBegin(), sf.getEnd(),
+                  Float.NaN, enteredGroup);
+          sequences.get(i).addSequenceFeature(sf2);
+          ffile.parseDescriptionHTML(sf2, false);
         }
 
         setColour(enteredType, fcol);