JAL-1830 refactored so no jalview.gui references from jalview.structure
[jalview.git] / src / jalview / gui / AlignViewport.java
index 997fd52..579b476 100644 (file)
@@ -42,9 +42,11 @@ import jalview.analysis.AlignmentUtils;
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.analysis.NJTree;
 import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
 import jalview.api.ViewStyleI;
 import jalview.bin.Cache;
 import jalview.commands.CommandI;
+import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ColumnSelection;
@@ -70,6 +72,7 @@ import java.awt.Rectangle;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Set;
 import java.util.Vector;
 
 import javax.swing.JInternalFrame;
@@ -426,22 +429,19 @@ public class AlignViewport extends AlignmentViewport implements
    */
   public void replaceMappings(AlignmentI align)
   {
-    StructureSelectionManager ssm = StructureSelectionManager
-            .getStructureSelectionManager(Desktop.instance);
 
     /*
      * Deregister current mappings (if any)
      */
-    if (alignment != null)
-    {
-      ssm.deregisterMappings(alignment.getCodonFrames());
-    }
+    deregisterMappings();
 
     /*
      * Register new mappings (if any)
      */
     if (align != null)
     {
+      StructureSelectionManager ssm = StructureSelectionManager
+              .getStructureSelectionManager(Desktop.instance);
       ssm.registerMappings(align.getCodonFrames());
     }
 
@@ -454,6 +454,27 @@ public class AlignViewport extends AlignmentViewport implements
     }
   }
 
+  protected void deregisterMappings()
+  {
+    AlignmentI al = getAlignment();
+    if (al != null)
+    {
+      Set<AlignedCodonFrame> mappings = al.getCodonFrames();
+      if (mappings != null)
+      {
+        StructureSelectionManager ssm = StructureSelectionManager
+                .getStructureSelectionManager(Desktop.instance);
+        for (AlignedCodonFrame acf : mappings)
+        {
+          if (noReferencesTo(acf))
+          {
+            ssm.deregisterMapping(acf);
+          }
+        }
+      }
+    }
+  }
+
   /**
    * DOCUMENT ME!
    * 
@@ -1059,4 +1080,34 @@ public class AlignViewport extends AlignmentViewport implements
     }
   }
 
+  /**
+   * Answers true if no alignment holds a reference to the given mapping
+   * 
+   * @param acf
+   * @return
+   */
+  protected boolean noReferencesTo(AlignedCodonFrame acf)
+  {
+    AlignFrame[] frames = Desktop.getAlignFrames();
+    if (frames == null)
+    {
+      return true;
+    }
+    for (AlignFrame af : frames)
+    {
+      if (!af.isClosed())
+      {
+        for (AlignmentViewPanel ap : af.getAlignPanels())
+        {
+          AlignmentI al = ap.getAlignment();
+          if (al != null && al.getCodonFrames().contains(acf))
+          {
+            return false;
+          }
+        }
+      }
+    }
+    return true;
+  }
+
 }