merge from develop
[jalview.git] / src / jalview / structure / StructureSelectionManager.java
index ac14b52..4359877 100644 (file)
  */
 package jalview.structure;
 
+import jalview.analysis.AlignSeq;
+import jalview.api.StructureSelectionManagerProvider;
+import jalview.commands.CommandI;
+import jalview.commands.EditCommand;
+import jalview.commands.OrderCommand;
+import jalview.datamodel.AlignedCodonFrame;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Annotation;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SearchResults;
+import jalview.datamodel.SequenceI;
+import jalview.io.AppletFormatAdapter;
+import jalview.util.MappingUtils;
+import jalview.util.MessageManager;
+
 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -37,22 +53,6 @@ import MCview.Atom;
 import MCview.PDBChain;
 import MCview.PDBfile;
 
-import jalview.analysis.AlignSeq;
-import jalview.api.StructureSelectionManagerProvider;
-import jalview.commands.CommandI;
-import jalview.commands.EditCommand;
-import jalview.commands.OrderCommand;
-import jalview.datamodel.AlignedCodonFrame;
-import jalview.datamodel.AlignmentAnnotation;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.Annotation;
-import jalview.datamodel.PDBEntry;
-import jalview.datamodel.SearchResults;
-import jalview.datamodel.SequenceI;
-import jalview.io.AppletFormatAdapter;
-import jalview.util.MappingUtils;
-import jalview.util.MessageManager;
-
 public class StructureSelectionManager
 {
   public final static String NEWLINE = System.lineSeparator();
@@ -70,13 +70,7 @@ public class StructureSelectionManager
   /*
    * Set of any registered mappings between (dataset) sequences.
    */
-  Set<AlignedCodonFrame> seqmappings = new LinkedHashSet<AlignedCodonFrame>();
-
-  /*
-   * Reference counters for the above mappings. Remove mappings when ref count
-   * goes to zero.
-   */
-  Map<AlignedCodonFrame, Integer> seqMappingRefCounts = new HashMap<AlignedCodonFrame, Integer>();
+  public Set<AlignedCodonFrame> seqmappings = new LinkedHashSet<AlignedCodonFrame>();
 
   private List<CommandListener> commandListeners = new ArrayList<CommandListener>();
 
@@ -518,8 +512,12 @@ public class StructureSelectionManager
         if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
         {
           resNum = tmp.resNumber;
-          mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
-          mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
+          if (tmp.alignmentMapping >= -1)
+          {
+            // TODO (JAL-1836) address root cause: negative residue no in PDB file
+            mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
+            mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
+          }
         }
 
         index++;
@@ -721,13 +719,16 @@ public class StructureSelectionManager
               {
                 results = MappingUtils.buildSearchResults(seq, index,
                         seqmappings);
-              }
+               }
               if (handlingVamsasMo)
               {
                 results.addResult(seq, index, index);
 
               }
-              seqListener.highlightSequence(results);
+              if (!results.isEmpty())
+              {
+                seqListener.highlightSequence(results);
+              }
             }
           }
         }
@@ -908,80 +909,89 @@ public class StructureSelectionManager
   }
 
   /**
-   * Decrement the reference counter for each of the given mappings, and remove
-   * it entirely if its reference counter reduces to zero.
+   * Remove the given mapping
    * 
-   * @param set
+   * @param acf
    */
-  public void removeMappings(Set<AlignedCodonFrame> set)
+  public void deregisterMapping(AlignedCodonFrame acf)
   {
-    if (set != null)
+    if (acf != null)
     {
-      for (AlignedCodonFrame acf : set)
-      {
-        removeMapping(acf);
+      boolean removed = seqmappings.remove(acf);
+      if (removed && seqmappings.isEmpty())
+      { // debug
+        System.out.println("All mappings removed");
       }
     }
   }
 
   /**
-   * Decrement the reference counter for the given mapping, and remove it
-   * entirely if its reference counter reduces to zero.
+   * Add each of the given codonFrames to the stored set, if not aready present.
    * 
-   * @param acf
+   * @param set
    */
-  public void removeMapping(AlignedCodonFrame acf)
+  public void registerMappings(Set<AlignedCodonFrame> set)
   {
-    if (acf != null && seqmappings.contains(acf))
+    if (set != null)
     {
-      int count = seqMappingRefCounts.get(acf);
-      count--;
-      if (count > 0)
-      {
-        seqMappingRefCounts.put(acf, count);
-      }
-      else
+      for (AlignedCodonFrame acf : set)
       {
-        seqmappings.remove(acf);
-        seqMappingRefCounts.remove(acf);
+        registerMapping(acf);
       }
     }
   }
 
   /**
-   * Add each of the given codonFrames to the stored set. If not aready present,
-   * increments its reference count instead.
-   * 
-   * @param set
+   * Add the given mapping to the stored set, unless already stored.
    */
-  public void addMappings(Set<AlignedCodonFrame> set)
+  public void registerMapping(AlignedCodonFrame acf)
   {
-    if (set != null)
+    if (acf != null)
     {
-      for (AlignedCodonFrame acf : set)
+      if (!seqmappings.contains(acf))
       {
-        addMapping(acf);
+        seqmappings.add(acf);
       }
     }
   }
 
   /**
-   * Add the given mapping to the stored set, or if already stored, increment
-   * its reference counter.
+   * Resets this object to its initial state by removing all registered
+   * listeners, codon mappings, PDB file mappings
    */
-  public void addMapping(AlignedCodonFrame acf)
+  public void resetAll()
   {
-    if (acf != null)
+    if (mappings != null)
     {
-      if (seqmappings.contains(acf))
-      {
-        seqMappingRefCounts.put(acf, seqMappingRefCounts.get(acf) + 1);
-      }
-      else
-      {
-        seqmappings.add(acf);
-        seqMappingRefCounts.put(acf, 1);
-      }
+      mappings.clear();
+    }
+    if (seqmappings != null)
+    {
+      seqmappings.clear();
+    }
+    if (sel_listeners != null)
+    {
+      sel_listeners.clear();
+    }
+    if (listeners != null)
+    {
+      listeners.clear();
+    }
+    if (commandListeners != null)
+    {
+      commandListeners.clear();
+    }
+    if (view_listeners != null)
+    {
+      view_listeners.clear();
+    }
+    if (pdbFileNameId != null)
+    {
+      pdbFileNameId.clear();
+    }
+    if (pdbIdFileName != null)
+    {
+      pdbIdFileName.clear();
     }
   }