JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / gui / AlignViewport.java
index 997fd52..8a95015 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
- * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -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;
@@ -106,6 +109,7 @@ public class AlignViewport extends AlignmentViewport implements
   private boolean gatherViewsHere = false;
 
   private AnnotationColumnChooser annotationColumnSelectionState;
+
   /**
    * Creates a new AlignViewport object.
    * 
@@ -396,6 +400,7 @@ public class AlignViewport extends AlignmentViewport implements
             viewStyle.getFontSize()), false);
 
   }
+
   /**
    * DOCUMENT ME!
    * 
@@ -426,22 +431,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 +456,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!
    * 
@@ -544,11 +567,9 @@ public class AlignViewport extends AlignmentViewport implements
     // TODO: JAL-1126
     if (historyList == null || redoList == null)
     {
-      return new long[]
-      { -1, -1 };
+      return new long[] { -1, -1 };
     }
-    return new long[]
-    { historyList.hashCode(), this.redoList.hashCode() };
+    return new long[] { historyList.hashCode(), this.redoList.hashCode() };
   }
 
   /**
@@ -693,8 +714,7 @@ public class AlignViewport extends AlignmentViewport implements
       List<SequenceI> seqs = new ArrayList<SequenceI>();
       for (SequenceI sq : alignment.getSequences())
       {
-        Vector<PDBEntry> pdbs = sq
-                .getDatasetSequence().getAllPDBEntries();
+        Vector<PDBEntry> pdbs = sq.getDatasetSequence().getAllPDBEntries();
         if (pdbs == null)
         {
           continue;
@@ -872,8 +892,8 @@ public class AlignViewport extends AlignmentViewport implements
    */
   protected boolean openLinkedAlignment(AlignmentI al, String title)
   {
-    String[] options = new String[]
-    { MessageManager.getString("action.no"),
+    String[] options = new String[] {
+        MessageManager.getString("action.no"),
         MessageManager.getString("label.split_window"),
         MessageManager.getString("label.new_window"), };
     final String question = JvSwingUtils.wrapTooltip(true,
@@ -916,8 +936,7 @@ public class AlignViewport extends AlignmentViewport implements
             AlignFrame.DEFAULT_HEIGHT);
     newAlignFrame.setTitle(title);
     newAlignFrame.statusBar.setText(MessageManager.formatMessage(
-            "label.successfully_loaded_file", new Object[]
-            { title }));
+            "label.successfully_loaded_file", new Object[] { title }));
 
     // TODO if we want this (e.g. to enable reload of the alignment from file),
     // we will need to add parameters to the stack.
@@ -929,15 +948,13 @@ public class AlignViewport extends AlignmentViewport implements
     if (openInNewWindow)
     {
       Desktop.addInternalFrame(newAlignFrame, title,
-              AlignFrame.DEFAULT_WIDTH,
-              AlignFrame.DEFAULT_HEIGHT);
+              AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
     }
 
     try
     {
       newAlignFrame.setMaximum(jalview.bin.Cache.getDefault(
-              "SHOW_FULLSCREEN",
-              false));
+              "SHOW_FULLSCREEN", false));
     } catch (java.beans.PropertyVetoException ex)
     {
     }
@@ -966,7 +983,8 @@ public class AlignViewport extends AlignmentViewport implements
   {
     /*
      * Make a new frame with a copy of the alignment we are adding to. If this
-     * is protein, the mappings to cDNA will be registered with StructureSelectionManager as a side-effect.
+     * is protein, the mappings to cDNA will be registered with
+     * StructureSelectionManager as a side-effect.
      */
     AlignFrame copyMe = new AlignFrame(complement,
             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
@@ -975,8 +993,7 @@ public class AlignViewport extends AlignmentViewport implements
     AlignmentI al = newAlignFrame.viewport.getAlignment();
     final AlignFrame proteinFrame = al.isNucleotide() ? copyMe
             : newAlignFrame;
-    final AlignFrame cdnaFrame = al.isNucleotide() ? newAlignFrame
-            : copyMe;
+    final AlignFrame cdnaFrame = al.isNucleotide() ? newAlignFrame : copyMe;
     cdnaFrame.setVisible(true);
     proteinFrame.setVisible(true);
     String linkedTitle = MessageManager
@@ -1053,10 +1070,41 @@ public class AlignViewport extends AlignmentViewport implements
     if (!sr.isEmpty())
     {
       // TODO would like next line without cast but needs more refactoring...
-      final AlignmentPanel complementPanel = ((AlignViewport) getCodingComplement()).getAlignPanel();
+      final AlignmentPanel complementPanel = ((AlignViewport) getCodingComplement())
+              .getAlignPanel();
       complementPanel.setFollowingComplementScroll(true);
       complementPanel.scrollToCentre(sr, verticalOffset);
     }
   }
 
+  /**
+   * 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;
+  }
+
 }