JAL-2480 subtract from total length on deleting feature
[jalview.git] / src / jalview / datamodel / features / FeatureStore.java
index cb0c36e..23edade 100644 (file)
@@ -184,20 +184,38 @@ public class FeatureStore
 
     /*
      * record the total extent of positional features, to make
-     * getAverageFeatureLength possible; we count the length of a 
+     * getTotalFeatureLength possible; we count the length of a 
      * contact feature as 1
      */
-    if (added && !feature.isNonPositional())
+    if (added)
     {
-      int featureLength = feature.isContactFeature() ? 1 : 1
-              + feature.getEnd() - feature.getBegin();
-      totalExtent += featureLength;
+      totalExtent += getFeatureLength(feature);
     }
 
     return added;
   }
 
   /**
+   * Answers the 'length' of the feature, counting 0 for non-positional features
+   * and 1 for contact features
+   * 
+   * @param feature
+   * @return
+   */
+  protected static int getFeatureLength(SequenceFeature feature)
+  {
+    if (feature.isNonPositional())
+    {
+      return 0;
+    }
+    if (feature.isContactFeature())
+    {
+      return 1;
+    }
+    return 1 + feature.getEnd() - feature.getBegin();
+  }
+
+  /**
    * Adds the feature to the list of non-positional features (with lazy
    * instantiation of the list if it is null), and returns true. If the
    * non-positional features already include the new feature (by equality test),
@@ -639,8 +657,18 @@ public class FeatureStore
 
     if (removed)
     {
+      /*
+       * rescan (positional or non-positional) features to rebuild the
+       * set of distinct feature groups present
+       */
       rebuildFeatureGroups(sf.getFeatureGroup(), removedNonPositional);
-      // TODO and recalculate totalExtent (feature may have changed length!)
+
+      /*
+       * subtract deleted feature's length from stored total length
+       * TODO: can start/end have changed since the feature was added?
+       */
+      int extent = getFeatureLength(sf);
+      totalExtent = Math.max(0, totalExtent - extent);
     }
 
     return removed;