JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / gui / AlignViewport.java
index 16e3de7..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.
  * 
  */
 package jalview.gui;
 
-import java.awt.Container;
-import java.awt.Dimension;
-import java.awt.Font;
-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;
-import javax.swing.JOptionPane;
-
 import jalview.analysis.AlignmentUtils;
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.analysis.NJTree;
 import jalview.api.AlignViewportI;
-import jalview.api.FeatureRenderer;
+import jalview.api.AlignmentViewPanel;
 import jalview.api.ViewStyleI;
 import jalview.bin.Cache;
 import jalview.commands.CommandI;
@@ -78,6 +65,19 @@ import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.ws.params.AutoCalcSetting;
 
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.Font;
+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;
+import javax.swing.JOptionPane;
+
 /**
  * DOCUMENT ME!
  * 
@@ -97,9 +97,6 @@ public class AlignViewport extends AlignmentViewport implements
 
   private Rectangle explodedGeometry;
 
-  private FeatureRenderer featureRenderer;
-
-  private boolean includeHiddenRegion = true;
   String viewName;
 
   /*
@@ -112,6 +109,7 @@ public class AlignViewport extends AlignmentViewport implements
   private boolean gatherViewsHere = false;
 
   private AnnotationColumnChooser annotationColumnSelectionState;
+
   /**
    * Creates a new AlignViewport object.
    * 
@@ -402,6 +400,7 @@ public class AlignViewport extends AlignmentViewport implements
             viewStyle.getFontSize()), false);
 
   }
+
   /**
    * DOCUMENT ME!
    * 
@@ -420,16 +419,61 @@ public class AlignViewport extends AlignmentViewport implements
    */
   public void setAlignment(AlignmentI align)
   {
-    if (alignment != null && alignment.getCodonFrames() != null)
+    replaceMappings(align);
+    this.alignment = align;
+  }
+
+  /**
+   * Replace any codon mappings for this viewport with those for the given
+   * viewport
+   * 
+   * @param align
+   */
+  public void replaceMappings(AlignmentI align)
+  {
+
+    /*
+     * Deregister current mappings (if any)
+     */
+    deregisterMappings();
+
+    /*
+     * Register new mappings (if any)
+     */
+    if (align != null)
     {
-      StructureSelectionManager.getStructureSelectionManager(
-              Desktop.instance).removeMappings(alignment.getCodonFrames());
+      StructureSelectionManager ssm = StructureSelectionManager
+              .getStructureSelectionManager(Desktop.instance);
+      ssm.registerMappings(align.getCodonFrames());
     }
-    this.alignment = align;
-    if (alignment != null && alignment.getCodonFrames() != null)
+
+    /*
+     * replace mappings on our alignment
+     */
+    if (alignment != null && align != null)
     {
-      StructureSelectionManager.getStructureSelectionManager(
-              Desktop.instance).addMappings(alignment.getCodonFrames());
+      alignment.setCodonFrames(align.getCodonFrames());
+    }
+  }
+
+  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);
+          }
+        }
+      }
     }
   }
 
@@ -523,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() };
   }
 
   /**
@@ -672,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().getPDBId();
+        Vector<PDBEntry> pdbs = sq.getDatasetSequence().getAllPDBEntries();
         if (pdbs == null)
         {
           continue;
@@ -805,6 +846,17 @@ public class AlignViewport extends AlignmentViewport implements
      */
     if (Cache.getDefault(Preferences.ENABLE_SPLIT_FRAME, true))
     {
+      if (al.getDataset() == null)
+      {
+        // need to create ds seqs
+        for (SequenceI sq : al.getSequences())
+        {
+          if (sq.getDatasetSequence() == null)
+          {
+            sq.createDatasetSequence();
+          }
+        }
+      }
       if (AlignmentUtils.isMappable(al, getAlignment()))
       {
         if (openLinkedAlignment(al, title))
@@ -840,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,
@@ -877,16 +929,14 @@ public class AlignViewport extends AlignmentViewport implements
     AlignmentUtils.mapProteinToCdna(protein, cdna);
 
     /*
-     * Create the AlignFrame for the added alignment. Note this will include the
-     * cDNA consensus annotation if it is protein (because the alignment holds
-     * mappings to nucleotide)
+     * Create the AlignFrame for the added alignment. If it is protein, mappings
+     * are registered with StructureSelectionManager as a side-effect.
      */
     AlignFrame newAlignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
             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.
@@ -898,33 +948,23 @@ 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)
     {
     }
 
     if (openSplitPane)
     {
-      protein = openSplitFrame(newAlignFrame, thisAlignment,
-              protein.getCodonFrames());
+      al.alignAs(thisAlignment);
+      protein = openSplitFrame(newAlignFrame, thisAlignment);
     }
 
-    /*
-     * Register the mappings (held on the protein alignment) with the
-     * StructureSelectionManager (for mouseover linking).
-     */
-    final StructureSelectionManager ssm = StructureSelectionManager
-            .getStructureSelectionManager(Desktop.instance);
-    ssm.addMappings(protein.getCodonFrames());
-
     return true;
   }
 
@@ -936,16 +976,15 @@ public class AlignViewport extends AlignmentViewport implements
    *          containing a new alignment to be shown
    * @param complement
    *          cdna/protein complement alignment to show in the other split half
-   * @param mappings
    * @return the protein alignment in the split frame
    */
   protected AlignmentI openSplitFrame(AlignFrame newAlignFrame,
-          AlignmentI complement, Set<AlignedCodonFrame> mappings)
+          AlignmentI complement)
   {
     /*
      * Make a new frame with a copy of the alignment we are adding to. If this
-     * is protein, the new frame will have a cDNA consensus annotation row
-     * added.
+     * 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);
@@ -954,11 +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;
-    AlignmentI protein = proteinFrame.viewport.getAlignment();
-    protein.setCodonFrames(mappings);
-
+    final AlignFrame cdnaFrame = al.isNucleotide() ? newAlignFrame : copyMe;
     cdnaFrame.setVisible(true);
     proteinFrame.setVisible(true);
     String linkedTitle = MessageManager
@@ -970,7 +1005,7 @@ public class AlignViewport extends AlignmentViewport implements
     JInternalFrame splitFrame = new SplitFrame(cdnaFrame, proteinFrame);
     Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
 
-    return protein;
+    return proteinFrame.viewport.getAlignment();
   }
 
   public AnnotationColumnChooser getAnnotationColumnSelectionState()
@@ -1031,35 +1066,45 @@ public class AlignViewport extends AlignmentViewport implements
      * is found, the result will be empty.
      */
     SearchResults sr = new SearchResults();
-    int seqOffset = findComplementScrollTarget(sr);
+    int verticalOffset = findComplementScrollTarget(sr);
     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, seqOffset);
+      complementPanel.scrollToCentre(sr, verticalOffset);
     }
   }
 
-  @Override
-  public FeatureRenderer getFeatureRenderer()
-  {
-    return featureRenderer;
-  }
-
-  @Override
-  public void setFeatureRenderer(FeatureRenderer featureRenderer)
-  {
-    this.featureRenderer = featureRenderer;
-  }
-
-  public boolean isIncludeHiddenRegion()
+  /**
+   * Answers true if no alignment holds a reference to the given mapping
+   * 
+   * @param acf
+   * @return
+   */
+  protected boolean noReferencesTo(AlignedCodonFrame acf)
   {
-    return includeHiddenRegion;
+    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;
   }
 
-  public void setIncludeHiddenRegion(boolean includeHiddenRegion)
-  {
-    this.includeHiddenRegion = includeHiddenRegion;
-  }
 }