selection sending and receiving, autoscrolling to highlighted position, new viewport...
authorjprocter <Jim Procter>
Wed, 12 Nov 2008 17:04:04 +0000 (17:04 +0000)
committerjprocter <Jim Procter>
Wed, 12 Nov 2008 17:04:04 +0000 (17:04 +0000)
src/jalview/gui/AlignFrame.java
src/jalview/gui/AlignViewport.java
src/jalview/gui/Jalview2XML.java
src/jalview/gui/VamsasApplication.java

index be92acf..f5a1494 100755 (executable)
@@ -47,6 +47,7 @@ import jalview.ws.*;
 public class AlignFrame extends GAlignFrame implements DropTargetListener,
         IProgressIndicator
 {
+
   /** DOCUMENT ME!! */
   public static final int DEFAULT_WIDTH = 700;
 
@@ -70,16 +71,39 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   String fileName = null;
 
   /**
-   * Creates a new AlignFrame object.
+   * Creates a new AlignFrame object with specific width and height.
    * 
    * @param al
-   *                DOCUMENT ME!
+   * @param width
+   * @param height
    */
   public AlignFrame(AlignmentI al, int width, int height)
   {
     this(al, null, width, height);
   }
-
+  /**
+   * Creates a new AlignFrame object with specific width, height and sequenceSetId
+   * @param al
+   * @param width
+   * @param height
+   * @param sequenceSetId
+   */
+  public AlignFrame(AlignmentI al, int width, int height, String sequenceSetId)
+  {
+    this(al, null, width, height, sequenceSetId);
+  }
+  /**
+   * Creates a new AlignFrame object with specific width, height and sequenceSetId
+   * @param al
+   * @param width
+   * @param height
+   * @param sequenceSetId
+   * @param viewId
+   */
+  public AlignFrame(AlignmentI al, int width, int height, String sequenceSetId, String viewId)
+  {
+    this(al, null, width, height, sequenceSetId, viewId);
+  }
   /**
    * new alignment window with hidden columns
    * 
@@ -87,12 +111,42 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    *                AlignmentI
    * @param hiddenColumns
    *                ColumnSelection or null
+   * @param width Width of alignment frame
+   * @param height height of frame.
    */
   public AlignFrame(AlignmentI al, ColumnSelection hiddenColumns,
           int width, int height)
   {
-    this.setSize(width, height);
-    viewport = new AlignViewport(al, hiddenColumns);
+    this(al, hiddenColumns, width, height, null);
+  }
+  /**
+   * Create alignment frame for al with hiddenColumns, a specific width and height, and specific sequenceId
+   * @param al
+   * @param hiddenColumns
+   * @param width
+   * @param height
+   * @param sequenceSetId (may be null)
+   */
+  public AlignFrame(AlignmentI al, ColumnSelection hiddenColumns,
+          int width, int height, String sequenceSetId)
+  {
+    this(al, hiddenColumns, width, height, sequenceSetId, null);
+  }
+
+  /**
+   * Create alignment frame for al with hiddenColumns, a specific width and height, and specific sequenceId
+   * @param al
+   * @param hiddenColumns
+   * @param width
+   * @param height
+   * @param sequenceSetId (may be null)
+   * @param viewId (may be null)
+   */
+  public AlignFrame(AlignmentI al, ColumnSelection hiddenColumns, int width, int height,
+          String sequenceSetId, String viewId)
+  {
+    setSize(width, height);
+    viewport = new AlignViewport(al, hiddenColumns, sequenceSetId, viewId);
 
     alignPanel = new AlignmentPanel(this, viewport);
 
@@ -120,7 +174,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     addAlignmentPanel(ap, false);
     init();
   }
-
+  /**
+   * initalise the alignframe from the underlying viewport data and the configurations
+   */
   void init()
   {
     if (viewport.conservation == null)
@@ -701,8 +757,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   public void save_actionPerformed(ActionEvent e)
   {
     if (fileName == null
-            || (currentFileFormat == null || jalview.io.AppletFormatAdapter
-                    .isValidFormat(currentFileFormat, true))
+            || (currentFileFormat == null 
+                    || !jalview.io.FormatAdapter.isValidIOFormat(currentFileFormat, true))
             || fileName.startsWith("http"))
     {
       saveAs_actionPerformed(null);
@@ -980,10 +1036,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * DOCUMENT ME!
+   * Close the current view or all views in the alignment frame. 
+   * If the frame only contains one view then the alignment will be removed from memory.
    * 
-   * @param e
-   *                DOCUMENT ME!
+   * @param closeAllTabs 
    */
   public void closeMenuItem_actionPerformed(boolean closeAllTabs)
   {
@@ -1001,9 +1057,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
           for (int i = 0; i < alignPanels.size(); i++)
           {
             AlignmentPanel ap = (AlignmentPanel) alignPanels.elementAt(i);
-            jalview.structure.StructureSelectionManager
-                    .getStructureSelectionManager()
-                    .removeStructureViewerListener(ap.seqPanel, null);
+            jalview.structure.StructureSelectionManager ssm = 
+              jalview.structure.StructureSelectionManager
+                    .getStructureSelectionManager();
+            ssm.removeStructureViewerListener(ap.seqPanel, null);
+            ssm.removeSelectionListener(ap.seqPanel);
             PaintRefresher.RemoveComponent(ap.seqPanel.seqCanvas);
             PaintRefresher.RemoveComponent(ap.idPanel.idCanvas);
             PaintRefresher.RemoveComponent(ap);
@@ -1012,25 +1070,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
         }
         else
         {
-          int index = tabbedPane.getSelectedIndex();
-
-          alignPanels.removeElement(alignPanel);
-          PaintRefresher.RemoveComponent(alignPanel.seqPanel.seqCanvas);
-          PaintRefresher.RemoveComponent(alignPanel.idPanel.idCanvas);
-          PaintRefresher.RemoveComponent(alignPanel);
-          viewport.alignment = null;
-          alignPanel = null;
+          closeView(alignPanel);
           viewport = null;
-
-          tabbedPane.removeTabAt(index);
-          tabbedPane.validate();
-
-          if (index == tabbedPane.getTabCount())
-          {
-            index--;
-          }
-
-          this.tabSelectionChanged(index);
         }
       }
 
@@ -1044,6 +1085,34 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
   }
 
+  public void closeView(AlignmentPanel alignPanel2)
+  {
+    int index = tabbedPane.getSelectedIndex();
+    int closedindex = tabbedPane.indexOfComponent(alignPanel2);
+    alignPanels.removeElement(alignPanel2);
+    PaintRefresher.RemoveComponent(alignPanel2.seqPanel.seqCanvas);
+    PaintRefresher.RemoveComponent(alignPanel2.idPanel.idCanvas);
+    PaintRefresher.RemoveComponent(alignPanel2);
+    alignPanel2.av.alignment = null;
+    
+    if (viewport == alignPanel2.av)
+    {
+      viewport = null;
+    }
+    alignPanel2 = null;
+    
+    tabbedPane.removeTabAt(closedindex);
+    tabbedPane.validate();
+
+    if (index > closedindex || index == tabbedPane.getTabCount())
+    {
+      // modify currently selected tab index if necessary.
+      index--;
+    }
+
+    this.tabSelectionChanged(index);    
+  }
+
   /**
    * DOCUMENT ME!
    */
@@ -1765,11 +1834,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
             viewport.alignment));
 
     viewport.setSelectionGroup(null);
+    viewport.sendSelection();
     viewport.alignment.deleteGroup(sg);
 
     viewport.firePropertyChange("alignment", null, viewport.getAlignment()
             .getSequences());
-
     if (viewport.getAlignment().getHeight() < 1)
     {
       try
@@ -1813,6 +1882,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
     sg.setEndRes(viewport.alignment.getWidth() - 1);
     viewport.setSelectionGroup(sg);
+    viewport.sendSelection();
     alignPanel.paintAlignment(true);
     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
   }
@@ -1862,7 +1932,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
 
     alignPanel.paintAlignment(true);
-
+    viewport.sendSelection();
     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
   }
 
@@ -2109,7 +2179,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
     newap.av.viewName = newViewName;
 
-    addAlignmentPanel(newap, false);
+    addAlignmentPanel(newap, true);
 
     if (alignPanels.size() == 2)
     {
@@ -2165,6 +2235,16 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     viewport.centreColumnLabels = centreColumnLabelsMenuItem.getState();
     alignPanel.paintAlignment(true);
   }
+  /* (non-Javadoc)
+   * @see jalview.jbgui.GAlignFrame#followHighlight_actionPerformed()
+   */
+  protected void followHighlight_actionPerformed()
+  {
+    if (viewport.followHighlight = this.followHighlightMenuItem.getState())
+    {
+      alignPanel.scrollToPosition(alignPanel.seqPanel.seqCanvas.searchResults, false);
+    }
+  }
 
   /**
    * DOCUMENT ME!
@@ -3715,12 +3795,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
             }
             Alignment al = new Alignment(sprods);
             AlignedCodonFrame[] cf = prods.getCodonFrames();
+            al.setDataset(ds);
             for (int s = 0; cf != null && s < cf.length; s++)
             {
               al.addCodonFrame(cf[s]);
               cf[s] = null;
             }
-            al.setDataset(ds);
             AlignFrame naf = new AlignFrame(al, DEFAULT_WIDTH,
                     DEFAULT_HEIGHT);
             String newtitle = "" + ((fdna) ? "Proteins " : "Nucleotides ")
@@ -4077,6 +4157,33 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   {
     viewport.setShowNpFeats(showNpFeatsMenuitem.isSelected());
   }
+
+  /**
+   * find the viewport amongst the tabs in this alignment frame and close that tab
+   * @param av
+   */
+  public boolean closeView(AlignViewport av)
+  {
+    if (viewport == av)
+    {
+      this.closeMenuItem_actionPerformed(false);
+      return true;
+    }
+    Component[] comp = tabbedPane.getComponents();
+    for (int i=0;comp!=null && i<comp.length;i++)
+    {
+      if (comp[i] instanceof AlignmentPanel)
+      {
+        if (((AlignmentPanel) comp[i]).av == av)
+        {
+          // close the view.
+          closeView((AlignmentPanel) comp[i]);
+          return true;
+        }
+      }
+    }
+    return false;
+  }
 }
 
 class PrintThread extends Thread
index 9f1d3c6..ac3ff77 100755 (executable)
@@ -47,6 +47,7 @@ import jalview.bin.*;
 import jalview.datamodel.*;
 
 import jalview.schemes.*;
+import jalview.structure.SelectionSource;
 import jalview.structure.StructureSelectionManager;
 
 /**
@@ -55,7 +56,7 @@ import jalview.structure.StructureSelectionManager;
  * @author $author$
  * @version $Revision$
  */
-public class AlignViewport
+public class AlignViewport implements SelectionSource
 {
   int startRes;
 
@@ -184,14 +185,32 @@ public class AlignViewport
   /**
    * Creates a new AlignViewport object.
    * 
-   * @param al
-   *                DOCUMENT ME!
+   * @param al alignment to view
    */
   public AlignViewport(AlignmentI al)
   {
     setAlignment(al);
     init();
   }
+  /**
+   * Create a new AlignViewport object with a specific sequence set ID
+   * @param al
+   * @param seqsetid (may be null - but potential for ambiguous constructor exception)
+   */
+  public AlignViewport(AlignmentI al, String seqsetid)
+  {
+    this(al,seqsetid,null);
+  }
+  public AlignViewport(AlignmentI al, String seqsetid, String viewid)
+  {
+    sequenceSetID = seqsetid;
+    viewId = viewid;
+    // TODO remove these once 2.4.VAMSAS release finished
+    if (Cache.log!=null && Cache.log.isDebugEnabled() && seqsetid!=null) { Cache.log.debug("Setting viewport's sequence set id : "+sequenceSetID); }
+    if (Cache.log!=null && Cache.log.isDebugEnabled() && viewId!=null) { Cache.log.debug("Setting viewport's view id : "+viewId); }
+    setAlignment(al);
+    init();
+  }
 
   /**
    * Create a new AlignViewport with hidden regions
@@ -214,6 +233,41 @@ public class AlignViewport
     }
     init();
   }
+  /**
+   * New viewport with hidden columns and an existing sequence set id
+   * @param al
+   * @param hiddenColumns
+   * @param seqsetid (may be null)
+   */
+  public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns, String seqsetid)
+  {
+    this(al,hiddenColumns,seqsetid,null);
+  }
+  /**
+   * New viewport with hidden columns and an existing sequence set id and viewid
+   * @param al
+   * @param hiddenColumns
+   * @param seqsetid (may be null)
+   * @param viewid (may be null)
+   */
+  public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns, String seqsetid, String viewid)
+  {
+    sequenceSetID = seqsetid;
+    viewId = viewid;
+    // TODO remove these once 2.4.VAMSAS release finished
+    if (Cache.log!=null && Cache.log.isDebugEnabled() && seqsetid!=null) { Cache.log.debug("Setting viewport's sequence set id : "+sequenceSetID); }
+    if (Cache.log!=null && Cache.log.isDebugEnabled() && viewId!=null) { Cache.log.debug("Setting viewport's view id : "+viewId); }
+    setAlignment(al);
+    if (hiddenColumns != null)
+    {
+      this.colSel = hiddenColumns;
+      if (hiddenColumns.getHiddenColumns() != null)
+      {
+        hasHiddenColumns = true;
+      }
+    }
+    init();
+  }
 
   void init()
   {
@@ -679,7 +733,7 @@ public class AlignViewport
    */
   public void setSelectionGroup(SequenceGroup sg)
   {
-    selectionGroup = sg;
+    selectionGroup = sg;    
   }
 
   /**
@@ -1414,7 +1468,8 @@ public class AlignViewport
         seqs[index++] = sg.getSequenceAt(i);
       }
     }
-
+    sg.setSeqrep(repSequence);
+    sg.setHidereps(true);
     hideSequence(seqs);
 
   }
@@ -1463,6 +1518,7 @@ public class AlignViewport
         selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
       }
       firePropertyChange("alignment", null, alignment.getSequences());
+      sendSelection();
     }
 
     if (alignment.getHiddenSequences().getSize() < 1)
@@ -1502,6 +1558,7 @@ public class AlignViewport
         selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
       }
       firePropertyChange("alignment", null, alignment.getSequences());
+      sendSelection();
       hasHiddenRows = false;
       hiddenRepSequences = null;
     }
@@ -1800,7 +1857,22 @@ public class AlignViewport
 
     return sequenceSetID;
   }
+  /**
+   * unique viewId for synchronizing state with stored Jalview Project 
+   * 
+   */
+  private String viewId=null;
 
+  
+  public String getViewId()
+  {
+    if (viewId==null)
+    {
+      viewId = this.getSequenceSetId()+"."+this.hashCode()+"";
+    }
+    return viewId;
+  }
+  
   public void alignmentChanged(AlignmentPanel ap)
   {
     if (padGaps)
@@ -2040,4 +2112,70 @@ public class AlignViewport
   {
     shownpfeats=show;
   }
+  /**
+   * 
+   * @return true if view has hidden rows
+   */
+  public boolean hasHiddenRows()
+  {
+    return hasHiddenRows;
+  }
+  /**
+   * 
+   * @return true if view has hidden columns
+   */
+  public boolean hasHiddenColumns()
+  {
+    return hasHiddenColumns;
+  }
+  /**
+   * when set, view will scroll to show the highlighted position
+   */
+  public boolean followHighlight=true;
+  /**
+   * @return true if view should scroll to show the highlighted region of a sequence
+   * @return
+   */
+  public boolean getFollowHighlight() {
+    return followHighlight;
+  }
+  public boolean followSelection=true;
+  /**
+   * @return true if view selection should always follow the selections broadcast by other selection sources
+   */
+  public boolean getFollowSelection() {
+    return followSelection;
+  }
+  private long sgrouphash=-1,colselhash=-1;
+  /**
+   * checks current SelectionGroup against record of last hash value, and updates record.
+   * @return true if SelectionGroup changed since last call
+   */
+  boolean isSelectionGroupChanged() {
+    int hc=(selectionGroup==null) ? -1 : selectionGroup.hashCode();
+    if (hc!=sgrouphash)
+    {
+      sgrouphash = hc;
+      return true;
+    }
+    return false;
+  }
+  /**
+   * checks current colsel against record of last hash value, and updates record.
+   * @return true if colsel changed since last call
+   */
+  boolean isColSelChanged() {
+    int hc=(colSel==null) ? -1 : colSel.hashCode();
+    if (hc!=colselhash)
+    {
+      colselhash = hc;
+      return true;
+    }
+    return false;
+  }
+  public void sendSelection()
+  {
+    jalview.structure.StructureSelectionManager.getStructureSelectionManager().sendSelection(new SequenceGroup(getSelectionGroup()), new ColumnSelection(getColumnSelection()), this);
+  }
+
 }
index f54383e..4664a6a 100755 (executable)
@@ -18,6 +18,7 @@
  */
 package jalview.gui;
 
+import java.awt.Rectangle;
 import java.io.*;
 import java.net.*;
 import java.util.*;
@@ -28,12 +29,14 @@ import javax.swing.*;
 import org.exolab.castor.xml.*;
 
 import uk.ac.vamsas.objects.utils.MapList;
+import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceI;
 import jalview.schemabinding.version2.*;
 import jalview.schemes.*;
 import jalview.structure.StructureSelectionManager;
+import jalview.util.jarInputStreamProvider;
 
 /**
  * Write out the current jalview desktop state as a Jalview XML stream.
@@ -67,6 +70,8 @@ public class Jalview2XML
     {
       // create sequential key
       String key = "sq" + (seqsToIds.size() + 1);
+      key = makeHashCode(sq, key); // check we don't have an external reference
+      // for it already.
       seqsToIds.put(sq, key);
       return key;
     }
@@ -74,8 +79,26 @@ public class Jalview2XML
 
   void clearSeqRefs()
   {
-    seqRefIds.clear();
-    seqsToIds.clear();
+    if (_cleartables)
+    {
+      if (seqRefIds != null)
+      {
+        seqRefIds.clear();
+      }
+      if (seqsToIds != null)
+      {
+        seqsToIds.clear();
+      }
+      // seqRefIds = null;
+      // seqsToIds = null;
+    }
+    else
+    {
+      // do nothing
+      warn("clearSeqRefs called when _cleartables was not set. Doing nothing.");
+      // seqRefIds = new Hashtable();
+      // seqsToIds = new IdentityHashMap();
+    }
   }
 
   void initSeqRefs()
@@ -90,8 +113,17 @@ public class Jalview2XML
     }
   }
 
-  java.util.IdentityHashMap seqsToIds = null; // SequenceI->key resolution
+  /**
+   * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
+   * of sequence objects are created.
+   */
+  java.util.IdentityHashMap seqsToIds = null;
 
+  /**
+   * jalview XML Sequence ID to jalview sequence object reference (both dataset
+   * and alignment sequences. Populated as XML reps of sequence objects are
+   * created.)
+   */
   java.util.Hashtable seqRefIds = null; // key->SequenceI resolution
 
   Vector frefedSequence = null;
@@ -158,9 +190,9 @@ public class Jalview2XML
                         .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for "
                                 + ref[1].getClass() + " type objects.");
               }
-              frefedSequence.remove(r);
-              rSize--;
             }
+            frefedSequence.remove(r);
+            rSize--;
           }
           else
           {
@@ -174,6 +206,7 @@ public class Jalview2XML
         }
         else
         {
+          // empty reference
           frefedSequence.remove(r);
           rSize--;
         }
@@ -199,6 +232,37 @@ public class Jalview2XML
   // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
   public void SaveState(File statefile)
   {
+    try
+    {
+      FileOutputStream fos = new FileOutputStream(statefile);
+      JarOutputStream jout = new JarOutputStream(fos);
+      SaveState(jout);
+
+    } catch (Exception e)
+    {
+      // TODO: inform user of the problem - they need to know if their data was
+      // not saved !
+      if (errorMessage == null)
+      {
+        errorMessage = "Couldn't write Jalview Archive to output file '"
+                + statefile + "' - See console error log for details";
+      }
+      else
+      {
+        errorMessage += "(output file was '" + statefile + "')";
+      }
+      e.printStackTrace();
+    }
+    reportErrors();
+  }
+
+  /**
+   * Writes a jalview project archive to the given Jar output stream.
+   * 
+   * @param jout
+   */
+  public void SaveState(JarOutputStream jout)
+  {
     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
 
     if (frames == null)
@@ -208,8 +272,6 @@ public class Jalview2XML
 
     try
     {
-      FileOutputStream fos = new FileOutputStream(statefile);
-      JarOutputStream jout = new JarOutputStream(fos);
 
       // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
       // //////////////////////////////////////////////////
@@ -224,6 +286,13 @@ public class Jalview2XML
         if (frames[i] instanceof AlignFrame)
         {
           AlignFrame af = (AlignFrame) frames[i];
+          // skip ?
+          if (skipList != null
+                  && skipList.containsKey(af.getViewport()
+                          .getSequenceSetId()))
+          {
+            continue;
+          }
 
           String shortName = af.getTitle();
 
@@ -281,6 +350,10 @@ public class Jalview2XML
     {
       // TODO: inform user of the problem - they need to know if their data was
       // not saved !
+      if (errorMessage == null)
+      {
+        errorMessage = "Couldn't write Jalview Archive - see error output for details";
+      }
       ex.printStackTrace();
     }
   }
@@ -317,6 +390,7 @@ public class Jalview2XML
       return true;
     } catch (Exception ex)
     {
+      errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
       ex.printStackTrace();
       return false;
     }
@@ -366,7 +440,7 @@ public class Jalview2XML
     if (jal.getDataset() != null)
     {
       // dataset id is the dataset's hashcode
-      vamsasSet.setDatasetId(jal.getDataset().hashCode() + "");
+      vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
     }
     if (jal.getProperties() != null)
     {
@@ -505,7 +579,9 @@ public class Jalview2XML
             if (frames[f] instanceof AppJmol)
             {
               jmol = (AppJmol) frames[f];
-              if (!jmol.pdbentry.getId().equals(entry.getId()))
+              if (!jmol.pdbentry.getId().equals(entry.getId()) 
+                      && !(entry.getId().length()>4 
+                              && entry.getId().startsWith(jmol.pdbentry.getId())))
                 continue;
 
               StructureState state = new StructureState();
@@ -514,7 +590,7 @@ public class Jalview2XML
               state.setYpos(jmol.getY());
               state.setWidth(jmol.getWidth());
               state.setHeight(jmol.getHeight());
-
+              state.setViewId(jmol.getViewId());
               String statestring = jmol.viewer.getStateInfo();
               if (state != null)
               {
@@ -603,9 +679,13 @@ public class Jalview2XML
         for (int p = 0; p < jac[i].aaWidth; p++)
         {
           Alcodon cmap = new Alcodon();
-          cmap.setPos1(jac[i].codons[p][0]);
-          cmap.setPos2(jac[i].codons[p][1]);
-          cmap.setPos3(jac[i].codons[p][2]);
+          if (jac[i].codons[p]!=null)
+          {
+            // Null codons indicate a gapped column in the translated peptide alignment. 
+            cmap.setPos1(jac[i].codons[p][0]);
+            cmap.setPos2(jac[i].codons[p][1]);
+            cmap.setPos3(jac[i].codons[p][2]);
+          }
           alc.addAlcodon(cmap);
         }
         if (jac[i].getProtMappings() != null
@@ -616,7 +696,7 @@ public class Jalview2XML
           for (int m = 0; m < pmaps.length; m++)
           {
             AlcodMap alcmap = new AlcodMap();
-            alcmap.setDnasq("" + dnas[m].hashCode());
+            alcmap.setDnasq(seqHash(dnas[m])); 
             alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
                     false));
             alc.addAlcodMap(alcmap);
@@ -662,7 +742,7 @@ public class Jalview2XML
               tree.setWidth(tp.getWidth());
               tree.setXpos(tp.getX());
               tree.setYpos(tp.getY());
-
+              tree.setId(makeHashCode(tp, null));
               jms.addTree(tree);
             }
           }
@@ -702,6 +782,8 @@ public class Jalview2XML
 
         if (aa[i].sequenceRef != null)
         {
+          // TODO later annotation sequenceRef should be the XML ID of the
+          // sequence rather than its display name
           an.setSequenceRef(aa[i].sequenceRef.getName());
         }
 
@@ -786,7 +868,9 @@ public class Jalview2XML
                 .getGroups().elementAt(i);
         groups[i].setStart(sg.getStartRes());
         groups[i].setEnd(sg.getEndRes());
-        groups[i].setName(sg.getName());
+        groups[i].setName(sg.getName()); // TODO later sequence group should
+        // specify IDs of sequences, not just
+        // names
         if (sg.cs != null)
         {
           if (sg.cs.conservationApplied())
@@ -846,7 +930,9 @@ public class Jalview2XML
     // /////////SAVE VIEWPORT
     Viewport view = new Viewport();
     view.setTitle(ap.alignFrame.getTitle());
-    view.setSequenceSetId(av.getSequenceSetId());
+    view.setSequenceSetId(makeHashCode(av.getSequenceSetId(), av
+            .getSequenceSetId()));
+    view.setId(av.getViewId());
     view.setViewName(av.viewName);
     view.setGatheredViews(av.gatherViewsHere);
 
@@ -1058,6 +1144,68 @@ public class Jalview2XML
     return object;
   }
 
+  /**
+   * External mapping between jalview objects and objects yielding a valid and
+   * unique object ID string. This is null for normal Jalview project IO, but
+   * non-null when a jalview project is being read or written as part of a
+   * vamsas session.
+   */
+  IdentityHashMap jv2vobj = null;
+
+  /**
+   * Construct a unique ID for jvobj using either existing bindings or if none
+   * exist, the result of the hashcode call for the object.
+   * 
+   * @param jvobj
+   *                jalview data object
+   * @return unique ID for referring to jvobj
+   */
+  private String makeHashCode(Object jvobj, String altCode)
+  {
+    if (jv2vobj != null)
+    {
+      Object id = jv2vobj.get(jvobj);
+      if (id != null)
+      {
+        return id.toString();
+      }
+      // check string ID mappings
+      if (jvids2vobj != null && jvobj instanceof String)
+      {
+        id = jvids2vobj.get(jvobj);
+      }
+      if (id != null)
+      {
+        return id.toString();
+      }
+      // give up and warn that something has gone wrong
+      warn("Cannot find ID for object in external mapping : " + jvobj);
+    }
+    return altCode;
+  }
+
+  /**
+   * return local jalview object mapped to ID, if it exists
+   * 
+   * @param idcode
+   *                (may be null)
+   * @return null or object bound to idcode
+   */
+  private Object retrieveExistingObj(String idcode)
+  {
+    if (idcode != null && vobj2jv != null)
+    {
+      return vobj2jv.get(idcode);
+    }
+    return null;
+  }
+
+  /**
+   * binding from ID strings from external mapping table to jalview data model
+   * objects.
+   */
+  private Hashtable vobj2jv;
+
   private Sequence createVamsasSequence(String id, SequenceI jds)
   {
     return createVamsasSequence(true, id, jds, null);
@@ -1083,7 +1231,7 @@ public class Jalview2XML
     else
     {
       vamsasSeq.setDsseqid(id); // so we can tell which sequences really are
-                                // dataset sequences only
+      // dataset sequences only
       dbrefs = jds.getDBRef();
     }
     if (dbrefs != null)
@@ -1258,49 +1406,122 @@ public class Jalview2XML
   }
 
   /**
-   * DOCUMENT ME!
+   * contains last error message (if any) encountered by XML loader.
+   */
+  String errorMessage = null;
+
+  /**
+   * flag to control whether the Jalview2XML_V1 parser should be deferred to if
+   * exceptions are raised during project XML parsing
+   */
+  public boolean attemptversion1parse = true;
+
+  /**
+   * Load a jalview project archive from a jar file
    * 
-   * @param file
-   *                DOCUMENT ME!
+   * @param file -
+   *                HTTP URL or filename
    */
   public AlignFrame LoadJalviewAlign(final String file)
   {
-    uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
 
     jalview.gui.AlignFrame af = null;
 
-    seqRefIds = new Hashtable();
-    viewportsAdded = new Hashtable();
-    frefedSequence = new Vector();
-    Hashtable gatherToThisFrame = new Hashtable();
-
-    String errorMessage = null;
-
     try
     {
       // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
-      URL url = null;
+      // Workaround is to make sure caller implements the JarInputStreamProvider
+      // interface
+      // so we can re-open the jar input stream for each entry.
 
-      if (file.startsWith("http://"))
-      {
-        url = new URL(file);
-      }
+      jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
+      af = LoadJalviewAlign(jprovider);
+    } catch (MalformedURLException e)
+    {
+      errorMessage = "Invalid URL format for '" + file + "'";
+      reportErrors();
+    }
+    return af;
+  }
 
-      JarInputStream jin = null;
-      JarEntry jarentry = null;
-      int entryCount = 1;
+  private jarInputStreamProvider createjarInputStreamProvider(
+          final String file) throws MalformedURLException
+  {
+    URL url = null;
+    errorMessage = null;
+    uniqueSetSuffix = null;
+    seqRefIds = null;
+    viewportsAdded = null;
+    frefedSequence = null;
 
-      do
+    if (file.startsWith("http://"))
+    {
+      url = new URL(file);
+    }
+    final URL _url = url;
+    return new jarInputStreamProvider()
+    {
+
+      public JarInputStream getJarInputStream() throws IOException
       {
-        if (url != null)
+        if (_url != null)
         {
-          jin = new JarInputStream(url.openStream());
+          return new JarInputStream(_url.openStream());
         }
         else
         {
-          jin = new JarInputStream(new FileInputStream(file));
+          return new JarInputStream(new FileInputStream(file));
         }
+      }
+
+      public String getFilename()
+      {
+        return file;
+      }
+    };
+  }
+
+  /**
+   * Recover jalview session from a jalview project archive. Caller may
+   * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
+   * themselves. Any null fields will be initialised with default values,
+   * non-null fields are left alone.
+   * 
+   * @param jprovider
+   * @return
+   */
+  public AlignFrame LoadJalviewAlign(final jarInputStreamProvider jprovider)
+  {
+    errorMessage = null;
+    if (uniqueSetSuffix == null)
+    {
+      uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
+    }
+    if (seqRefIds == null)
+    {
+      seqRefIds = new Hashtable();
+    }
+    if (viewportsAdded == null)
+    {
+      viewportsAdded = new Hashtable();
+    }
+    if (frefedSequence == null)
+    {
+      frefedSequence = new Vector();
+    }
+
+    jalview.gui.AlignFrame af = null;
+    Hashtable gatherToThisFrame = new Hashtable();
+    final String file = jprovider.getFilename();
+    try
+    {
+      JarInputStream jin = null;
+      JarEntry jarentry = null;
+      int entryCount = 1;
 
+      do
+      {
+        jin = jprovider.getJarInputStream();
         for (int i = 0; i < entryCount; i++)
         {
           jarentry = jin.getNextJarEntry();
@@ -1314,11 +1535,13 @@ public class Jalview2XML
           Unmarshaller unmar = new Unmarshaller(object);
           unmar.setValidation(false);
           object = (JalviewModel) unmar.unmarshal(in);
-
-          af = LoadFromObject(object, file, true);
-          if (af.viewport.gatherViewsHere)
+          if (true) // !skipViewport(object))
           {
-            gatherToThisFrame.put(af.viewport.getSequenceSetId(), af);
+            af = LoadFromObject(object, file, true, jprovider);
+            if (af.viewport.gatherViewsHere)
+            {
+              gatherToThisFrame.put(af.viewport.getSequenceSetId(), af);
+            }
           }
           entryCount++;
         }
@@ -1345,18 +1568,19 @@ public class Jalview2XML
     {
       System.err.println("Parsing as Jalview Version 2 file failed.");
       ex.printStackTrace(System.err);
-
-      // Is Version 1 Jar file?
-      try
+      if (attemptversion1parse)
       {
-        af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(file);
-      } catch (Exception ex2)
-      {
-        System.err.println("Exception whilst loading as jalviewXMLV1:");
-        ex2.printStackTrace();
-        af = null;
+        // Is Version 1 Jar file?
+        try
+        {
+          af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(jprovider);
+        } catch (Exception ex2)
+        {
+          System.err.println("Exception whilst loading as jalviewXMLV1:");
+          ex2.printStackTrace();
+          af = null;
+        }
       }
-
       if (Desktop.instance != null)
       {
         Desktop.instance.stopLoading();
@@ -1370,6 +1594,10 @@ public class Jalview2XML
 
       System.err.println("Exception whilst loading jalview XML file : "
               + ex + "\n");
+    } catch (OutOfMemoryError e)
+    {
+      new jalview.gui.OOMWarning("loading jalview XML file", e,
+              Desktop.instance);
     }
 
     if (Desktop.instance != null)
@@ -1382,7 +1610,25 @@ public class Jalview2XML
     {
       Desktop.instance.gatherViews((AlignFrame) en.nextElement());
     }
+    if (errorMessage != null)
+    {
+      reportErrors();
+    }
+    return af;
+  }
+
+  /**
+   * check errorMessage for a valid error message and raise an error box in the
+   * GUI or write the current errorMessage to stderr and then clear the error
+   * state.
+   */
+  protected void reportErrors()
+  {
+    reportErrors(false);
+  }
 
+  protected void reportErrors(final boolean saving)
+  {
     if (errorMessage != null)
     {
       final String finalErrorMessage = errorMessage;
@@ -1393,8 +1639,9 @@ public class Jalview2XML
           public void run()
           {
             JOptionPane.showInternalMessageDialog(Desktop.desktop,
-                    finalErrorMessage, "Error loading Jalview file",
-                    JOptionPane.WARNING_MESSAGE);
+                    finalErrorMessage, "Error "
+                            + (saving ? "saving" : "loading")
+                            + " Jalview file", JOptionPane.WARNING_MESSAGE);
           }
         });
       }
@@ -1403,13 +1650,19 @@ public class Jalview2XML
         System.err.println("Problem loading Jalview file: " + errorMessage);
       }
     }
-
-    return af;
+    errorMessage = null;
   }
 
   Hashtable alreadyLoadedPDB;
 
-  String loadPDBFile(String file, String pdbId)
+  /**
+   * when set, local views will be updated from view stored in JalviewXML
+   * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
+   * sync if this is set to true.
+   */
+  private boolean updateLocalViews = false;
+
+  String loadPDBFile(jarInputStreamProvider jprovider, String pdbId)
   {
     if (alreadyLoadedPDB == null)
       alreadyLoadedPDB = new Hashtable();
@@ -1419,45 +1672,46 @@ public class Jalview2XML
 
     try
     {
-      JarInputStream jin = null;
-
-      if (file.startsWith("http://"))
-      {
-        jin = new JarInputStream(new URL(file).openStream());
-      }
-      else
-      {
-        jin = new JarInputStream(new FileInputStream(file));
-      }
+      JarInputStream jin = jprovider.getJarInputStream();
+      /*
+       * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
+       * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
+       * FileInputStream(jprovider)); }
+       */
 
       JarEntry entry = null;
       do
       {
         entry = jin.getNextJarEntry();
-      } while (!entry.getName().equals(pdbId));
+      } while (entry != null && !entry.getName().equals(pdbId));
+      if (entry != null)
+      {
+        BufferedReader in = new BufferedReader(new InputStreamReader(jin));
+        File outFile = File.createTempFile("jalview_pdb", ".txt");
+        outFile.deleteOnExit();
+        PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
+        String data;
 
-      BufferedReader in = new BufferedReader(new InputStreamReader(jin));
-      File outFile = File.createTempFile("jalview_pdb", ".txt");
-      outFile.deleteOnExit();
-      PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
-      String data;
+        while ((data = in.readLine()) != null)
+        {
+          out.println(data);
+        }
+        try
+        {
+          out.flush();
+        } catch (Exception foo)
+        {
+        }
+        ;
+        out.close();
 
-      while ((data = in.readLine()) != null)
-      {
-        out.println(data);
+        alreadyLoadedPDB.put(pdbId, outFile.getAbsolutePath());
+        return outFile.getAbsolutePath();
       }
-      try
-      {
-        out.flush();
-      } catch (Exception foo)
+      else
       {
+        warn("Couldn't find PDB file entry in Jalview Jar for " + pdbId);
       }
-      ;
-      out.close();
-
-      alreadyLoadedPDB.put(pdbId, outFile.getAbsolutePath());
-      return outFile.getAbsolutePath();
-
     } catch (Exception ex)
     {
       ex.printStackTrace();
@@ -1466,8 +1720,21 @@ public class Jalview2XML
     return null;
   }
 
+  /**
+   * Load alignment frame from jalview XML DOM object
+   * 
+   * @param object
+   *                DOM
+   * @param file
+   *                filename source string
+   * @param loadTreesAndStructures
+   *                when false only create Viewport
+   * @param jprovider
+   *                data source provider
+   * @return alignment frame created from view stored in DOM
+   */
   AlignFrame LoadFromObject(JalviewModel object, String file,
-          boolean loadTreesAndStructures)
+          boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
   {
     SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
     Sequence[] vamsasSeq = vamsasSet.getSequence();
@@ -1475,7 +1742,6 @@ public class Jalview2XML
     JalviewModelSequence jms = object.getJalviewModelSequence();
 
     Viewport view = jms.getViewport(0);
-
     // ////////////////////////////////
     // LOAD SEQUENCES
 
@@ -1490,7 +1756,7 @@ public class Jalview2XML
     int vi = 0; // counter in vamsasSeq array
     for (int i = 0; i < JSEQ.length; i++)
     {
-      String seqId = JSEQ[i].getId() + "";
+      String seqId = JSEQ[i].getId();
 
       if (seqRefIds.get(seqId) != null)
       {
@@ -1505,7 +1771,7 @@ public class Jalview2XML
         jseq.setStart(JSEQ[i].getStart());
         jseq.setEnd(JSEQ[i].getEnd());
         jseq.setVamsasId(uniqueSetSuffix + seqId);
-        seqRefIds.put(vamsasSeq[vi].getId() + "", jseq);
+        seqRefIds.put(vamsasSeq[vi].getId(), jseq);
         tmpseqs.add(jseq);
         vi++;
       }
@@ -1559,6 +1825,8 @@ public class Jalview2XML
     Hashtable pdbloaded = new Hashtable();
     if (!multipleView)
     {
+      // load sequence features, database references and any associated PDB
+      // structures for the alignment
       for (int i = 0; i < vamsasSeq.length; i++)
       {
         if (JSEQ[i].getFeaturesCount() > 0)
@@ -1605,7 +1873,7 @@ public class Jalview2XML
             {
               if (!pdbloaded.containsKey(ids[p].getFile()))
               {
-                entry.setFile(loadPDBFile(file, ids[p].getId()));
+                entry.setFile(loadPDBFile(jprovider, ids[p].getId()));
               }
               else
               {
@@ -1617,12 +1885,15 @@ public class Jalview2XML
           }
         }
       }
-    }
+    } // end !multipleview
 
     // ///////////////////////////////
     // LOAD SEQUENCE MAPPINGS
+
     if (vamsasSet.getAlcodonFrameCount() > 0)
     {
+      // TODO Potentially this should only be done once for all views of an
+      // alignment
       AlcodonFrame[] alc = vamsasSet.getAlcodonFrame();
       for (int i = 0; i < alc.length; i++)
       {
@@ -1633,10 +1904,16 @@ public class Jalview2XML
           Alcodon[] alcods = alc[i].getAlcodon();
           for (int p = 0; p < cf.codons.length; p++)
           {
-            cf.codons[p] = new int[3];
-            cf.codons[p][0] = (int) alcods[p].getPos1();
-            cf.codons[p][1] = (int) alcods[p].getPos2();
-            cf.codons[p][2] = (int) alcods[p].getPos3();
+            if (alcods[p].hasPos1() && alcods[p].hasPos2() && alcods[p].hasPos3())
+            {
+              // translated codons require three valid positions
+              cf.codons[p] = new int[3];
+              cf.codons[p][0] = (int) alcods[p].getPos1();
+              cf.codons[p][1] = (int) alcods[p].getPos2();
+              cf.codons[p][2] = (int) alcods[p].getPos3();
+            } else {
+              cf.codons[p] = null;
+            }
           }
         }
         if (alc[i].getAlcodMapCount() > 0)
@@ -1680,6 +1957,7 @@ public class Jalview2XML
 
       for (int i = 0; i < an.length; i++)
       {
+        // set visibility for automatic annotation for this view
         if (an[i].getLabel().equals("Quality"))
         {
           hideQuality = false;
@@ -1695,12 +1973,14 @@ public class Jalview2XML
           hideConsensus = false;
           continue;
         }
-
+        // set visiblity for other annotation in this view
         if (an[i].getId() != null
                 && annotationIds.containsKey(an[i].getId()))
         {
           jalview.datamodel.AlignmentAnnotation jda = (jalview.datamodel.AlignmentAnnotation) annotationIds
                   .get(an[i].getId());
+          // in principle Visible should always be true for annotation displayed
+          // in multiple views
           if (an[i].hasVisible())
             jda.visible = an[i].getVisible();
 
@@ -1708,7 +1988,7 @@ public class Jalview2XML
 
           continue;
         }
-
+        // Construct new annotation from model.
         AnnotationElement[] ae = an[i].getAnnotationElement();
         jalview.datamodel.Annotation[] anot = null;
 
@@ -1764,13 +2044,13 @@ public class Jalview2XML
           jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
                   an[i].getDescription(), anot);
         }
-
+        // register new annotation
         if (an[i].getId() != null)
         {
           annotationIds.put(an[i].getId(), jaa);
           jaa.annotationId = an[i].getId();
         }
-
+        // recover sequence association
         if (an[i].getSequenceRef() != null)
         {
           if (al.findName(an[i].getSequenceRef()) != null)
@@ -1794,6 +2074,7 @@ public class Jalview2XML
 
     // ///////////////////////
     // LOAD GROUPS
+    // Create alignment markup and styles for this view
     if (jms.getJGroupCount() > 0)
     {
       JGroup[] groups = jms.getJGroup();
@@ -1868,70 +2149,331 @@ public class Jalview2XML
     // ///////////////////////////////
     // LOAD VIEWPORT
 
-    AlignFrame af = new AlignFrame(al, view.getWidth(), view.getHeight());
-
-    af.setFileName(file, "Jalview");
-
-    for (int i = 0; i < JSEQ.length; i++)
-    {
-      af.viewport.setSequenceColour(af.viewport.alignment.getSequenceAt(i),
-              new java.awt.Color(JSEQ[i].getColour()));
-    }
-
     // If we just load in the same jar file again, the sequenceSetId
     // will be the same, and we end up with multiple references
     // to the same sequenceSet. We must modify this id on load
     // so that each load of the file gives a unique id
     String uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
+    String viewId = (view.getId() == null ? null : view.getId()
+            + uniqueSetSuffix);
+    AlignFrame af = null;
+    AlignViewport av = null;
+    // now check to see if we really need to create a new viewport.
+    if (multipleView && viewportsAdded.size() == 0)
+    {
+      // We recovered an alignment for which a viewport already exists.
+      // TODO: fix up any settings necessary for overlaying stored state onto
+      // state recovered from another document. (may not be necessary).
+      // we may need a binding from a viewport in memory to one recovered from
+      // XML.
+      // and then recover its containing af to allow the settings to be applied.
+      // TODO: fix for vamsas demo
+      System.err
+              .println("About to recover a viewport for existing alignment: Sequence set ID is "
+                      + uniqueSeqSetId);
+      Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
+      if (seqsetobj != null)
+      {
+        if (seqsetobj instanceof String)
+        {
+          uniqueSeqSetId = (String) seqsetobj;
+          System.err
+                  .println("Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
+                          + uniqueSeqSetId);
+        }
+        else
+        {
+          System.err
+                  .println("Warning : Collision between sequence set ID string and existing jalview object mapping.");
+        }
 
-    af.viewport.gatherViewsHere = view.getGatheredViews();
-
-    if (view.getSequenceSetId() != null)
-    {
-      jalview.gui.AlignViewport av = (jalview.gui.AlignViewport) viewportsAdded
-              .get(uniqueSeqSetId);
-
-      af.viewport.sequenceSetID = uniqueSeqSetId;
-      if (av != null)
-      {
-
-        af.viewport.historyList = av.historyList;
-        af.viewport.redoList = av.redoList;
-      }
-      else
-      {
-        viewportsAdded.put(uniqueSeqSetId, af.viewport);
       }
-
-      PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
     }
-    if (hiddenSeqs != null)
+    AlignmentPanel ap = null;
+    boolean isnewview = true;
+    if (viewId != null)
     {
-      for (int s = 0; s < JSEQ.length; s++)
+      // Check to see if this alignment already has a view id == viewId
+      jalview.gui.AlignmentPanel views[] = Desktop
+              .getAlignmentPanels(uniqueSeqSetId);
+      if (views != null && views.length > 0)
       {
-        jalview.datamodel.SequenceGroup hidden = new jalview.datamodel.SequenceGroup();
-
-        for (int r = 0; r < JSEQ[s].getHiddenSequencesCount(); r++)
+        for (int v = 0; v < views.length; v++)
         {
-          hidden.addSequence(al
-                  .getSequenceAt(JSEQ[s].getHiddenSequences(r)), false);
+          if (views[v].av.getViewId().equalsIgnoreCase(viewId))
+          {
+            // recover the existing alignpanel, alignframe, viewport
+            af = views[v].alignFrame;
+            av = views[v].av;
+            ap = views[v];
+            // TODO: could even skip resetting view settings if we don't want to
+            // change the local settings from other jalview processes
+            isnewview = false;
+          }
         }
-        af.viewport.hideRepSequences(al.getSequenceAt(s), hidden);
       }
+    }
 
-      jalview.datamodel.SequenceI[] hseqs = new jalview.datamodel.SequenceI[hiddenSeqs
-              .size()];
-
-      for (int s = 0; s < hiddenSeqs.size(); s++)
+    if (isnewview)
+    {
+      af = loadViewport(file, JSEQ, hiddenSeqs, al, hideConsensus,
+              hideQuality, hideConservation, jms, view, uniqueSeqSetId,
+              viewId);
+      av = af.viewport;
+      ap = af.alignPanel;
+    }
+    // LOAD TREES
+    // /////////////////////////////////////
+    if (loadTreesAndStructures && jms.getTreeCount() > 0)
+    {
+      try
       {
-        hseqs[s] = (jalview.datamodel.SequenceI) hiddenSeqs.elementAt(s);
-      }
+        for (int t = 0; t < jms.getTreeCount(); t++)
+        {
 
-      af.viewport.hideSequence(hseqs);
+          Tree tree = jms.getTree(t);
 
-    }
+          TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
+          if (tp == null)
+          {
+            tp = af.ShowNewickTree(new jalview.io.NewickFile(tree
+                    .getNewick()), tree.getTitle(), tree.getWidth(), tree
+                    .getHeight(), tree.getXpos(), tree.getYpos());
+            if (tree.getId() != null)
+            {
 
-    if ((hideConsensus || hideQuality || hideConservation)
+            }
+          }
+          else
+          {
+            // update local tree attributes ?
+            tp.setTitle(tree.getTitle());
+            tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
+                    .getWidth(), tree.getHeight()));
+            tp.av = av; // af.viewport; // TODO: verify 'associate with all
+            // views'
+            // works still
+            tp.treeCanvas.av = av; // af.viewport;
+            tp.treeCanvas.ap = ap; // af.alignPanel;
+
+          }
+
+          tp.fitToWindow.setState(tree.getFitToWindow());
+          tp.fitToWindow_actionPerformed(null);
+
+          if (tree.getFontName() != null)
+          {
+            tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
+                    .getFontStyle(), tree.getFontSize()));
+          }
+          else
+          {
+            tp.setTreeFont(new java.awt.Font(view.getFontName(), view
+                    .getFontStyle(), tree.getFontSize()));
+          }
+
+          tp.showPlaceholders(tree.getMarkUnlinked());
+          tp.showBootstrap(tree.getShowBootstrap());
+          tp.showDistances(tree.getShowDistances());
+
+          tp.treeCanvas.threshold = tree.getThreshold();
+
+          if (tree.getCurrentTree())
+          {
+            af.viewport.setCurrentTree(tp.getTree());
+          }
+        }
+
+      } catch (Exception ex)
+      {
+        ex.printStackTrace();
+      }
+    }
+
+    // //LOAD STRUCTURES
+    if (loadTreesAndStructures)
+    {
+      for (int i = 0; i < JSEQ.length; i++)
+      {
+        if (JSEQ[i].getPdbidsCount() > 0)
+        {
+          Pdbids[] ids = JSEQ[i].getPdbids();
+          for (int p = 0; p < ids.length; p++)
+          {
+            for (int s = 0; s < ids[p].getStructureStateCount(); s++)
+            {
+              // check to see if we haven't already created this structure view
+              String sviewid = (ids[p].getStructureState(s).getViewId() == null) ? null
+                      : ids[p].getStructureState(s).getViewId()
+                              + uniqueSetSuffix;
+              jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
+              // Originally : ids[p].getFile()
+              // : TODO: verify external PDB file recovery still works in normal
+              // jalview project load
+              jpdb.setFile(loadPDBFile(jprovider, ids[p].getId()));
+              jpdb.setId(ids[p].getId());
+
+              int x = ids[p].getStructureState(s).getXpos();
+              int y = ids[p].getStructureState(s).getYpos();
+              int width = ids[p].getStructureState(s).getWidth();
+              int height = ids[p].getStructureState(s).getHeight();
+              AppJmol comp = null;
+              JInternalFrame[] frames = null;
+              do
+              {
+                try
+                {
+                  frames = Desktop.desktop.getAllFrames();
+                } catch (ArrayIndexOutOfBoundsException e)
+                {
+                  // occasional No such child exceptions are thrown here...
+                  frames = null;
+                  try
+                  {
+                    Thread.sleep(10);
+                  } catch (Exception f)
+                  {
+                  }
+                  ;
+                }
+              } while (frames == null);
+              // search for any Jmol windows already open from other
+              // alignment views that exactly match the stored structure state
+              for (int f = 0; comp == null && f < frames.length; f++)
+              {
+                if (frames[f] instanceof AppJmol)
+                {
+                  if (sviewid != null
+                          && ((AppJmol) frames[f]).getViewId().equals(
+                                  sviewid))
+                  {
+                    // post jalview 2.4 schema includes structure view id
+                    comp = (AppJmol) frames[f];
+                  }
+                  else if (frames[f].getX() == x && frames[f].getY() == y
+                          && frames[f].getHeight() == height
+                          && frames[f].getWidth() == width)
+                  {
+                    comp = (AppJmol) frames[f];
+                  }
+                }
+              }
+              Desktop.desktop.getComponentAt(x, y);
+
+              String pdbFile = loadPDBFile(jprovider, ids[p].getId());
+
+              jalview.datamodel.SequenceI[] seq = new jalview.datamodel.SequenceI[]
+              { (jalview.datamodel.SequenceI) seqRefIds.get(JSEQ[i].getId()
+                      + "") };
+
+              if (comp == null)
+              {
+                // create a new Jmol window
+                String state = ids[p].getStructureState(s).getContent();
+
+                StringBuffer newFileLoc = new StringBuffer(state.substring(
+                        0, state.indexOf("\"", state.indexOf("load")) + 1));
+
+                newFileLoc.append(jpdb.getFile());
+                newFileLoc.append(state.substring(state.indexOf("\"", state
+                        .indexOf("load \"") + 6)));
+
+                new AppJmol(pdbFile, ids[p].getId(), seq, af.alignPanel,
+                        newFileLoc.toString(), new java.awt.Rectangle(x, y,
+                                width, height), sviewid);
+
+              }
+              else
+              // if (comp != null)
+              {
+                // NOTE: if the jalview project is part of a shared session then
+                // view synchronization should/could be done here.
+
+                // add mapping for this sequence to the already open Jmol
+                // instance (if it doesn't already exist)
+                // These
+                StructureSelectionManager.getStructureSelectionManager()
+                        .setMapping(seq, null, pdbFile,
+                                jalview.io.AppletFormatAdapter.FILE);
+
+                ((AppJmol) comp).addSequence(seq);
+              }
+            }
+          }
+        }
+      }
+    }
+
+    return af;
+  }
+
+  AlignFrame loadViewport(String file, JSeq[] JSEQ, Vector hiddenSeqs,
+          Alignment al, boolean hideConsensus, boolean hideQuality,
+          boolean hideConservation, JalviewModelSequence jms,
+          Viewport view, String uniqueSeqSetId, String viewId)
+  {
+    AlignFrame af = null;
+    af = new AlignFrame(al, view.getWidth(), view.getHeight(),
+            uniqueSeqSetId, viewId);
+
+    af.setFileName(file, "Jalview");
+
+    for (int i = 0; i < JSEQ.length; i++)
+    {
+      af.viewport.setSequenceColour(af.viewport.alignment.getSequenceAt(i),
+              new java.awt.Color(JSEQ[i].getColour()));
+    }
+
+    af.viewport.gatherViewsHere = view.getGatheredViews();
+
+    if (view.getSequenceSetId() != null)
+    {
+      jalview.gui.AlignViewport av = (jalview.gui.AlignViewport) viewportsAdded
+              .get(uniqueSeqSetId);
+
+      af.viewport.sequenceSetID = uniqueSeqSetId;
+      if (av != null)
+      {
+        // propagate shared settings to this new view
+        af.viewport.historyList = av.historyList;
+        af.viewport.redoList = av.redoList;
+      }
+      else
+      {
+        viewportsAdded.put(uniqueSeqSetId, af.viewport);
+      }
+      // TODO: check if this method can be called repeatedly without
+      // side-effects if alignpanel already registered.
+      PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
+    }
+    // apply Hidden regions to view.
+    if (hiddenSeqs != null)
+    {
+      for (int s = 0; s < JSEQ.length; s++)
+      {
+        jalview.datamodel.SequenceGroup hidden = new jalview.datamodel.SequenceGroup();
+
+        for (int r = 0; r < JSEQ[s].getHiddenSequencesCount(); r++)
+        {
+          hidden.addSequence(al
+                  .getSequenceAt(JSEQ[s].getHiddenSequences(r)), false);
+        }
+        af.viewport.hideRepSequences(al.getSequenceAt(s), hidden);
+      }
+
+      jalview.datamodel.SequenceI[] hseqs = new jalview.datamodel.SequenceI[hiddenSeqs
+              .size()];
+
+      for (int s = 0; s < hiddenSeqs.size(); s++)
+      {
+        hseqs[s] = (jalview.datamodel.SequenceI) hiddenSeqs.elementAt(s);
+      }
+
+      af.viewport.hideSequence(hseqs);
+
+    }
+    // set visibility of annotation in view
+    if ((hideConsensus || hideQuality || hideConservation)
             && al.getAlignmentAnnotation() != null)
     {
       int hSize = al.getAlignmentAnnotation().length;
@@ -1951,7 +2493,7 @@ public class Jalview2XML
       }
       af.alignPanel.adjustAnnotationHeight();
     }
-
+    // recover view properties and display parameters
     if (view.getViewName() != null)
     {
       af.viewport.viewName = view.getViewName();
@@ -1989,7 +2531,7 @@ public class Jalview2XML
     af.viewport.setStartSeq(view.getStartSeq());
 
     ColourSchemeI cs = null;
-
+    // apply colourschemes
     if (view.getBgColour() != null)
     {
       if (view.getBgColour().startsWith("ucs"))
@@ -2108,7 +2650,7 @@ public class Jalview2XML
     {
       af.viewport.showSequenceFeatures = true;
     }
-
+    // recover featre settings
     if (jms.getFeatureSettings() != null)
     {
       af.viewport.featuresDisplayed = new Hashtable();
@@ -2155,153 +2697,66 @@ public class Jalview2XML
     }
 
     af.setMenusFromViewport(af.viewport);
-
+    // TODO: we don't need to do this if the viewport is aready visible.
     Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(), view
             .getHeight());
+    return af;
+  }
 
-    // LOAD TREES
-    // /////////////////////////////////////
-    if (loadTreesAndStructures && jms.getTreeCount() > 0)
-    {
-      try
-      {
-        for (int t = 0; t < jms.getTreeCount(); t++)
-        {
-
-          Tree tree = jms.getTree(t);
-
-          TreePanel tp = af.ShowNewickTree(new jalview.io.NewickFile(tree
-                  .getNewick()), tree.getTitle(), tree.getWidth(), tree
-                  .getHeight(), tree.getXpos(), tree.getYpos());
-
-          tp.fitToWindow.setState(tree.getFitToWindow());
-          tp.fitToWindow_actionPerformed(null);
-
-          if (tree.getFontName() != null)
-          {
-            tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
-                    .getFontStyle(), tree.getFontSize()));
-          }
-          else
-          {
-            tp.setTreeFont(new java.awt.Font(view.getFontName(), view
-                    .getFontStyle(), tree.getFontSize()));
-          }
-
-          tp.showPlaceholders(tree.getMarkUnlinked());
-          tp.showBootstrap(tree.getShowBootstrap());
-          tp.showDistances(tree.getShowDistances());
-
-          tp.treeCanvas.threshold = tree.getThreshold();
+  Hashtable skipList = null;
 
-          if (tree.getCurrentTree())
-          {
-            af.viewport.setCurrentTree(tp.getTree());
-          }
-        }
+  /**
+   * TODO remove this method
+   * 
+   * @param view
+   * @return AlignFrame bound to sequenceSetId from view, if one exists. private
+   *         AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
+   *         throw new Error("Implementation Error. No skipList defined for this
+   *         Jalview2XML instance."); } return (AlignFrame)
+   *         skipList.get(view.getSequenceSetId()); }
+   */
 
-      } catch (Exception ex)
+  /**
+   * Check if the Jalview view contained in object should be skipped or not.
+   * 
+   * @param object
+   * @return true if view's sequenceSetId is a key in skipList
+   */
+  private boolean skipViewport(JalviewModel object)
+  {
+    if (skipList == null)
+    {
+      return false;
+    }
+    String id;
+    if (skipList.containsKey(id = object.getJalviewModelSequence()
+            .getViewport()[0].getSequenceSetId()))
+    {
+      if (Cache.log != null && Cache.log.isDebugEnabled())
       {
-        ex.printStackTrace();
+        Cache.log.debug("Skipping seuqence set id " + id);
       }
+      return true;
     }
+    return false;
+  }
 
-    // //LOAD STRUCTURES
-    if (loadTreesAndStructures)
+  public void AddToSkipList(AlignFrame af)
+  {
+    if (skipList == null)
     {
-      for (int i = 0; i < JSEQ.length; i++)
-      {
-        if (JSEQ[i].getPdbidsCount() > 0)
-        {
-          Pdbids[] ids = JSEQ[i].getPdbids();
-          for (int p = 0; p < ids.length; p++)
-          {
-            for (int s = 0; s < ids[p].getStructureStateCount(); s++)
-            {
-              jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
-
-              jpdb.setFile(loadPDBFile(ids[p].getFile(), ids[p].getId()));
-              jpdb.setId(ids[p].getId());
-
-              int x = ids[p].getStructureState(s).getXpos();
-              int y = ids[p].getStructureState(s).getYpos();
-              int width = ids[p].getStructureState(s).getWidth();
-              int height = ids[p].getStructureState(s).getHeight();
-
-              java.awt.Component comp = null;
-
-              JInternalFrame[] frames = null;
-              do
-              {
-                try
-                {
-                  frames = Desktop.desktop.getAllFrames();
-                } catch (ArrayIndexOutOfBoundsException e)
-                {
-                  // occasional No such child exceptions are thrown here...
-                  frames = null;
-                  try
-                  {
-                    Thread.sleep(10);
-                  } catch (Exception f)
-                  {
-                  }
-                  ;
-                }
-              } while (frames == null);
-              for (int f = 0; f < frames.length; f++)
-              {
-                if (frames[f] instanceof AppJmol)
-                {
-                  if (frames[f].getX() == x && frames[f].getY() == y
-                          && frames[f].getHeight() == height
-                          && frames[f].getWidth() == width)
-                  {
-                    comp = frames[f];
-                    break;
-                  }
-                }
-              }
-
-              Desktop.desktop.getComponentAt(x, y);
-
-              String pdbFile = loadPDBFile(file, ids[p].getId());
-
-              jalview.datamodel.SequenceI[] seq = new jalview.datamodel.SequenceI[]
-              { (jalview.datamodel.SequenceI) seqRefIds.get(JSEQ[i].getId()
-                      + "") };
-
-              if (comp == null)
-              {
-                String state = ids[p].getStructureState(s).getContent();
-
-                StringBuffer newFileLoc = new StringBuffer(state.substring(
-                        0, state.indexOf("\"", state.indexOf("load")) + 1));
-
-                newFileLoc.append(jpdb.getFile());
-                newFileLoc.append(state.substring(state.indexOf("\"", state
-                        .indexOf("load \"") + 6)));
-
-                new AppJmol(pdbFile, ids[p].getId(), seq, af.alignPanel,
-                        newFileLoc.toString(), new java.awt.Rectangle(x, y,
-                                width, height));
-
-              }
-              else if (comp != null)
-              {
-                StructureSelectionManager.getStructureSelectionManager()
-                        .setMapping(seq, null, pdbFile,
-                                jalview.io.AppletFormatAdapter.FILE);
-
-                ((AppJmol) comp).addSequence(seq);
-              }
-            }
-          }
-        }
-      }
+      skipList = new Hashtable();
     }
+    skipList.put(af.getViewport().getSequenceSetId(), af);
+  }
 
-    return af;
+  public void clearSkipList()
+  {
+    if (skipList != null)
+    {
+      skipList.clear();
+      skipList = null;
+    }
   }
 
   private void recoverDatasetFor(SequenceSet vamsasSet, Alignment al)
@@ -2430,7 +2885,7 @@ public class Jalview2XML
   }
 
   java.util.Hashtable datasetIds = null;
-
+  java.util.IdentityHashMap dataset2Ids = null;
   private Alignment getDatasetFor(String datasetId)
   {
     if (datasetIds == null)
@@ -2453,7 +2908,35 @@ public class Jalview2XML
     }
     datasetIds.put(datasetId, dataset);
   }
-
+  /**
+   * make a new dataset ID for this jalview dataset alignment
+   * @param dataset
+   * @return
+   */
+  private String getDatasetIdRef(jalview.datamodel.Alignment dataset)
+  {
+    if (dataset.getDataset()!=null)
+    {
+      warn("Serious issue!  Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
+    }
+    String datasetId=makeHashCode(dataset, null);
+    if (datasetId==null)
+    {
+      // make a new datasetId and record it
+      if (dataset2Ids == null)
+      {
+        dataset2Ids = new IdentityHashMap();
+      } else {
+        datasetId = (String) dataset2Ids.get(dataset);
+      }
+      if (datasetId==null)
+      {
+        datasetId = "ds"+dataset2Ids.size()+1;
+        dataset2Ids.put(dataset,datasetId);
+      }
+    }
+    return datasetId;
+  }
   private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
   {
     for (int d = 0; d < sequence.getDBRefCount(); d++)
@@ -2497,17 +2980,18 @@ public class Jalview2XML
       MappingChoice mc = m.getMappingChoice();
       if (mc.getDseqFor() != null)
       {
-        if (seqRefIds.containsKey(mc.getDseqFor()))
+        String dsfor = ""+mc.getDseqFor();
+        if (seqRefIds.containsKey(dsfor))
         {
           /**
            * recover from hash
            */
-          jmap.setTo((SequenceI) seqRefIds.get(mc.getDseqFor()));
+          jmap.setTo((SequenceI) seqRefIds.get(dsfor));
         }
         else
         {
           frefedSequence.add(new Object[]
-          { mc.getDseqFor(), jmap });
+          { dsfor, jmap });
         }
       }
       else
@@ -2530,8 +3014,8 @@ public class Jalview2XML
           System.err
                   .println("Warning - making up dataset sequence id for DbRef sequence map reference");
           sqid = ((Object) ms).toString(); // make up a new hascode for
-                                            // undefined dataset sequence hash
-                                            // (unlikely to happen)
+          // undefined dataset sequence hash
+          // (unlikely to happen)
         }
 
         if (djs == null)
@@ -2560,6 +3044,7 @@ public class Jalview2XML
   public jalview.gui.AlignmentPanel copyAlignPanel(AlignmentPanel ap,
           boolean keepSeqRefs)
   {
+    initSeqRefs();
     jalview.schemabinding.version2.JalviewModel jm = SaveState(ap, null,
             null);
 
@@ -2571,11 +3056,16 @@ public class Jalview2XML
     else
     {
       uniqueSetSuffix = "";
+      jm.getJalviewModelSequence().getViewport(0).setId(null); // we don't overwrite the view we just copied
+    }
+    if (this.frefedSequence==null)
+    {
+      frefedSequence = new Vector();
     }
 
     viewportsAdded = new Hashtable();
 
-    AlignFrame af = LoadFromObject(jm, null, false);
+    AlignFrame af = LoadFromObject(jm, null, false, null);
     af.alignPanels.clear();
     af.closeMenuItem_actionPerformed(true);
 
@@ -2589,6 +3079,14 @@ public class Jalview2XML
     return af.alignPanel;
   }
 
+  /**
+   * flag indicating if hashtables should be cleared on finalization TODO this
+   * flag may not be necessary
+   */
+  private boolean _cleartables = true;
+
+  private Hashtable jvids2vobj;
+
   /*
    * (non-Javadoc)
    * 
@@ -2597,10 +3095,145 @@ public class Jalview2XML
   protected void finalize() throws Throwable
   {
     // really make sure we have no buried refs left.
-    clearSeqRefs();
+    if (_cleartables)
+    {
+      clearSeqRefs();
+    }
     this.seqRefIds = null;
     this.seqsToIds = null;
     super.finalize();
   }
 
+  private void warn(String msg)
+  {
+    warn(msg, null);
+  }
+
+  private void warn(String msg, Exception e)
+  {
+    if (Cache.log != null)
+    {
+      if (e != null)
+      {
+        Cache.log.warn(msg, e);
+      }
+      else
+      {
+        Cache.log.warn(msg);
+      }
+    }
+    else
+    {
+      System.err.println("Warning: " + msg);
+      if (e != null)
+      {
+        e.printStackTrace();
+      }
+    }
+  }
+
+  /**
+   * set the object to ID mapping tables used to write/recover objects and XML
+   * ID strings for the jalview project. If external tables are provided then
+   * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
+   * object goes out of scope. - also populates the datasetIds hashtable with
+   * alignment objects containing dataset sequences
+   * 
+   * @param vobj2jv
+   *                Map from ID strings to jalview datamodel
+   * @param jv2vobj
+   *                Map from jalview datamodel to ID strings
+   * 
+   * 
+   */
+  public void setObjectMappingTables(Hashtable vobj2jv,
+          IdentityHashMap jv2vobj)
+  {
+    this.jv2vobj = jv2vobj;
+    this.vobj2jv = vobj2jv;
+    Iterator ds = jv2vobj.keySet().iterator();
+    String id;
+    while (ds.hasNext())
+    {
+      Object jvobj = ds.next();
+      id = jv2vobj.get(jvobj).toString();
+      if (jvobj instanceof jalview.datamodel.Alignment)
+      {
+        if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
+        {
+          addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
+        }
+      }
+      else if (jvobj instanceof jalview.datamodel.Sequence)
+      {
+        // register sequence object so the XML parser can recover it.
+        if (seqRefIds == null)
+        {
+          seqRefIds = new Hashtable();
+        }
+        if (seqsToIds == null)
+        {
+          seqsToIds = new IdentityHashMap();
+        }
+        seqRefIds.put(jv2vobj.get(jvobj).toString(), jvobj);
+        seqsToIds.put(jvobj, id);
+      }
+      else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
+      {
+        if (annotationIds == null)
+        {
+          annotationIds = new Hashtable();
+        }
+        String anid;
+        annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvobj);
+        jalview.datamodel.AlignmentAnnotation jvann = (jalview.datamodel.AlignmentAnnotation) jvobj;
+        if (jvann.annotationId == null)
+        {
+          jvann.annotationId = anid;
+        }
+        if (!jvann.annotationId.equals(anid))
+        {
+          // TODO verify that this is the correct behaviour
+          this.warn("Overriding Annotation ID for " + anid
+                  + " from different id : " + jvann.annotationId);
+          jvann.annotationId = anid;
+        }
+      }
+      else if (jvobj instanceof String)
+      {
+        if (jvids2vobj == null)
+        {
+          jvids2vobj = new Hashtable();
+          jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
+        }
+      }
+      else
+        Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
+    }
+  }
+
+  /**
+   * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
+   * objects created from the project archive. If string is null (default for
+   * construction) then suffix will be set automatically.
+   * 
+   * @param string
+   */
+  public void setUniqueSetSuffix(String string)
+  {
+    uniqueSetSuffix = string;
+
+  }
+
+  /**
+   * uses skipList2 as the skipList for skipping views on sequence sets
+   * associated with keys in the skipList
+   * 
+   * @param skipList2
+   */
+  public void setSkipList(Hashtable skipList2)
+  {
+    skipList = skipList2;
+  }
+
 }
index 5988bb8..9bc1ffa 100644 (file)
 package jalview.gui;
 
 import jalview.bin.Cache;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.io.VamsasAppDatastore;
+import jalview.structure.SelectionListener;
+import jalview.structure.SelectionSource;
 import jalview.structure.StructureSelectionManager;
 import jalview.structure.VamsasListener;
 
@@ -28,8 +33,10 @@ import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.File;
 import java.io.IOException;
+import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.IdentityHashMap;
+import java.util.Iterator;
 
 import javax.swing.JInternalFrame;
 import javax.swing.JOptionPane;
@@ -45,13 +52,17 @@ import uk.ac.vamsas.client.picking.IMessageHandler;
 import uk.ac.vamsas.client.picking.IPickManager;
 import uk.ac.vamsas.client.picking.Message;
 import uk.ac.vamsas.client.picking.MouseOverMessage;
+import uk.ac.vamsas.client.picking.SelectionMessage;
 import uk.ac.vamsas.objects.core.Entry;
+import uk.ac.vamsas.objects.core.Input;
+import uk.ac.vamsas.objects.core.Pos;
+import uk.ac.vamsas.objects.core.Seg;
 
 /**
  * @author jimp
  * 
  */
-public class VamsasApplication
+public class VamsasApplication implements SelectionSource
 {
   IClient vclient = null;
 
@@ -343,11 +354,32 @@ public class VamsasApplication
     ensureJvVamsas();
     VamsasAppDatastore vds = new VamsasAppDatastore(cdoc, vobj2jv, jv2vobj,
             baseProvEntry(), alRedoState);
-    vds.updateToJalview();
+    try
+    {
+      vds.updateToJalview();
+    } catch (Exception e)
+    {
+      Cache.log.error("Failed to update Jalview from vamsas document.", e);
+    }
+    try
+    {
+      if (firstUpdate)
+      {
+        vds.updateJalviewFromAppdata();
+        // Comment this out to repeatedly read in data from JalviewAppData
+        // firstUpdate=false;
+      }
+    } catch (Exception e)
+    {
+      Cache.log.error(
+              "Exception when updating Jalview settings from Appdata.", e);
+    }
     Cache.log.debug(".. finished updating from sesion document.");
 
   }
 
+  boolean firstUpdate = false;
+
   private void ensureJvVamsas()
   {
     if (jv2vobj == null)
@@ -355,6 +387,7 @@ public class VamsasApplication
       jv2vobj = new IdentityHashMap();
       vobj2jv = new Hashtable();
       alRedoState = new Hashtable();
+      firstUpdate = true;
     }
   }
 
@@ -367,9 +400,22 @@ public class VamsasApplication
 
   Hashtable alRedoState = null;
 
+  boolean errorsDuringUpdate = false;
+
+  boolean errorsDuringAppUpdate = false;
+
+  /**
+   * update the document accessed through doc. A backup of the current object
+   * bindings is made.
+   * 
+   * @param doc
+   */
   public void updateVamsasDocument(IClientDocument doc)
   {
     ensureJvVamsas();
+    errorsDuringUpdate = false;
+    errorsDuringAppUpdate = false;
+    backup_objectMapping();
     VamsasAppDatastore vds = new VamsasAppDatastore(doc, vobj2jv, jv2vobj,
             baseProvEntry(), alRedoState);
     // wander through frames
@@ -379,6 +425,8 @@ public class VamsasApplication
     {
       return;
     }
+    Hashtable skipList = new Hashtable();
+    Hashtable viewset = new Hashtable();
 
     try
     {
@@ -388,26 +436,75 @@ public class VamsasApplication
         if (frames[i] instanceof AlignFrame)
         {
           AlignFrame af = (AlignFrame) frames[i];
-
-          // update alignment and root from frame.
-          vds.storeVAMSAS(af.getViewport(), af.getTitle());
+          if (!viewset.containsKey(af.getViewport().getSequenceSetId()))
+          {
+            // update alignment and root from frame.
+            boolean stored = false;
+            try
+            {
+              stored = vds.storeVAMSAS(af.getViewport(), af.getTitle());
+            } catch (Exception e)
+            {
+              errorsDuringUpdate = true;
+              Cache.log.error("Exception synchronizing "
+                      + af.getTitle()
+                      + " "
+                      + (af.getViewport().viewName == null ? "" : " view "
+                              + af.getViewport().viewName)
+                      + " to document.", e);
+              stored = false;
+            }
+            if (!stored)
+            { // record skip in skipList
+              skipList.put(af.getViewport().getSequenceSetId(), af);
+            }
+            else
+            {
+              // could try to eliminate sequenceSetId from skiplist ..
+              // (skipList.containsKey(af.getViewport().getSequenceSetId()))
+              // remember sequenceSetId so we can skip all the other views on
+              // same alignment
+              viewset.put(af.getViewport().getSequenceSetId(), af);
+            }
+          }
         }
       }
       // REVERSE ORDER
-      for (int i = frames.length - 1; i > -1; i--)
+      // for (int i = frames.length - 1; i > -1; i--)
+      // {
+      // if (frames[i] instanceof AlignFrame)
+      // {
+      // AlignFrame af = (AlignFrame) frames[i];
+      Iterator aframes = viewset.values().iterator();
+      while (aframes.hasNext())
       {
-        if (frames[i] instanceof AlignFrame)
-        {
-          AlignFrame af = (AlignFrame) frames[i];
+        AlignFrame af = (AlignFrame) aframes.next();
+        // add any AlignedCodonFrame mappings on this alignment to any other.
+        vds.storeSequenceMappings(af.getViewport(), af.getTitle());
+      }
+    } catch (Exception e)
+    {
+      Cache.log.error("Exception synchronizing Views to Document :", e);
+      errorsDuringUpdate = true;
+    }
 
-          // add any AlignedCodonFrame mappings on this alignment to any other.
-          vds.storeSequenceMappings(af.getViewport(), af.getTitle());
-        }
+    try
+    {
+      if (viewset.size() > 0)
+      {
+        // Alignment views were synchronized, so store their state in the
+        // appData, too.
+        // The skipList ensures we don't write out any alignments not actually
+        // in the document.
+        vds.setSkipList(skipList);
+        vds.updateJalviewClientAppdata();
       }
     } catch (Exception e)
     {
-      Cache.log.error("Vamsas Document store exception", e);
+      Cache.log.error("Client Appdata Write exception", e);
+      errorsDuringAppUpdate = true;
     }
+    vds.clearSkipList();
   }
 
   private Entry baseProvEntry()
@@ -483,6 +580,9 @@ public class VamsasApplication
     {
       System.err.println("Exception whilst updating :");
       ee.printStackTrace(System.err);
+      // recover object map backup, since its probably corrupted with references
+      // to Vobjects that don't exist anymore.
+      this.recover_objectMappingBackup();
     }
     Cache.log.debug("Finished updating from document change.");
     disableGui(false);
@@ -540,10 +640,54 @@ public class VamsasApplication
     Desktop.instance.setVamsasUpdate(b);
   }
 
+  Hashtable _backup_vobj2jv;
+
+  IdentityHashMap _backup_jv2vobj;
+
+  /**
+   * make a backup of the object mappings (vobj2jv and jv2vobj)
+   */
+  public void backup_objectMapping()
+  {
+    _backup_vobj2jv = new Hashtable(vobj2jv);
+    _backup_jv2vobj = new IdentityHashMap(jv2vobj);
+  }
+
+  /**
+   * recover original object mappings from the object mapping backup if document
+   * IO failed
+   * 
+   * @throws Error
+   *                 if backup_objectMapping was not called.
+   */
+  public void recover_objectMappingBackup()
+  {
+    if (_backup_vobj2jv == null)
+    {
+      throw new Error(
+              "IMPLEMENTATION ERROR: Cannot recover vamsas object mappings - no backup was made.");
+    }
+    jv2vobj.clear();
+    Iterator el = _backup_jv2vobj.entrySet().iterator();
+    while (el.hasNext())
+    {
+      java.util.Map.Entry mp = (java.util.Map.Entry) el.next();
+      jv2vobj.put(mp.getKey(), mp.getValue());
+    }
+    el = _backup_vobj2jv.entrySet().iterator();
+    while (el.hasNext())
+    {
+      java.util.Map.Entry mp = (java.util.Map.Entry) el.next();
+      vobj2jv.put(mp.getKey(), mp.getValue());
+    }
+  }
+
   private boolean joinedSession = false;
 
   private VamsasListener picker = null;
 
+  private SelectionListener selecter;
+
   private void startSession()
   {
     if (inSession())
@@ -563,13 +707,19 @@ public class VamsasApplication
         final IPickManager pm = vclient.getPickManager();
         final StructureSelectionManager ssm = StructureSelectionManager
                 .getStructureSelectionManager();
+        final SelectionSource me = this;
         pm.registerMessageHandler(new IMessageHandler()
         {
           String last = null;
 
           public void handleMessage(Message message)
           {
-            if (message instanceof MouseOverMessage && vobj2jv != null)
+            if (vobj2jv == null)
+            {
+              // we are not in a session yet.
+              return;
+            }
+            if (message instanceof MouseOverMessage)
             {
               MouseOverMessage mm = (MouseOverMessage) message;
               String mstring = mm.getVorbaID() + " " + mm.getPosition();
@@ -593,6 +743,161 @@ public class VamsasApplication
                         .getPosition());
               }
             }
+            if (message instanceof uk.ac.vamsas.client.picking.SelectionMessage)
+            {
+              // we only care about AlignmentSequence selections
+              SelectionMessage sm = (SelectionMessage) message;
+              sm.validate();
+              System.err.println("Received\n"+sm.getRawMessage());
+              Object[] jvobjs = sm.getVorbaIDs()==null ? null : new Object[sm.getVorbaIDs().length];
+              if (jvobjs==null)
+              {
+                // TODO: rationalise : can only clear a selection over a referred to object
+                ssm.sendSelection(null,null,me);
+                return;
+              }
+              Class type = null;
+              boolean send = true;
+              for (int o = 0; o < jvobjs.length; o++)
+              {
+                jvobjs[o] = vobj2jv.get(sm.getVorbaIDs()[o]);
+                if (jvobjs[o] == null)
+                {
+                  // can't cope with selections for unmapped objects
+                  continue;
+                }
+                if (type == null)
+                {
+                  type = jvobjs[o].getClass();
+                }
+                ;
+                if (type != jvobjs[o].getClass())
+                {
+                  send=false;
+                  // discard - can't cope with selections over mixed objects
+                  continue;
+                }
+              }
+              SequenceGroup jselection = null;
+              ColumnSelection colsel = null;
+              if (type == jalview.datamodel.Alignment.class)
+              {
+                if (jvobjs.length == 1)
+                {
+                  // TODO if (sm.isNone())// send a message to select the specified columns over the
+                  // given
+                  // alignment
+
+                  send = true;
+                }
+              }
+              if (type == jalview.datamodel.Sequence.class)
+              {
+
+                SequenceI seq;
+                boolean aligned = ((jalview.datamodel.Sequence) jvobjs[0])
+                        .getDatasetSequence() != null;
+                int maxWidth=0;
+                if (aligned)
+                {
+                  jselection = new SequenceGroup();
+                  jselection.addSequence(seq =
+                        (jalview.datamodel.Sequence) jvobjs[0], false);
+                  maxWidth = seq.getLength();
+                }
+                for (int c = 1; aligned && jvobjs.length > 1 && c < jvobjs.length; c++)
+                {
+                  if (((jalview.datamodel.Sequence) jvobjs[c])
+                          .getDatasetSequence() == null)
+                  {
+                    aligned = false;
+                    continue;
+                  }
+                  else
+                  {
+                    jselection.addSequence(
+                            seq = (jalview.datamodel.Sequence) jvobjs[c], false);
+                    if (maxWidth<seq.getLength())
+                    {
+                      maxWidth = seq.getLength();
+                    }
+                    
+                  }
+                }
+                if (!aligned)
+                {
+                  jselection = null;
+                  // if cardinality is greater than one then verify all
+                  // sequences are alignment sequences.
+                  if (jvobjs.length == 1)
+                  {
+                    // find all instances of this dataset sequence in the
+                    // displayed alignments containing the associated range and
+                    // select them.
+                  }
+                }
+                else
+                {
+                  jselection.setStartRes(0);
+                  jselection.setEndRes(maxWidth);
+                  // locate the alignment containing the given sequences and
+                  // select the associated ranges on them.
+                  if (sm.getRanges() != null)
+                  {
+                    int[] prange = uk.ac.vamsas.objects.utils.Range.getBounds(sm.getRanges());
+                    boolean rangeset=false;
+                    colsel = new ColumnSelection();
+                    prange = uk.ac.vamsas.objects.utils.Range.getIntervals(sm.getRanges());
+                    for (int p=0;p<prange.length;p+=2)
+                    {
+                      int d = (prange[p]<=prange[p+1]) ? 1 : -1;
+                      if (!rangeset)
+                      {
+                        if (jselection!=null)
+                        {
+                          // set the bounds of the selected area using the first interval.
+                          jselection.setStartRes(prange[p]-1);
+                          jselection.setEndRes(prange[p+1]-1);
+                          rangeset=true;
+                        }
+                      } else {
+                        // try to join up adjacent columns to make a larger selection
+                        // lower and upper bounds
+                        int l=(d<0) ? 1 : 0;
+                        int u=(d>0) ? 1 : 0;
+                        
+                        if (jselection.getStartRes()>0 && prange[p+l]==jselection.getStartRes())
+                        {
+                          jselection.setStartRes(prange[p+l]-1);
+                        }
+                        if (jselection.getEndRes()<=maxWidth && prange[p+u]==(jselection.getEndRes()+2))
+                        {
+                          jselection.setEndRes(prange[p+u]-1);
+                        }
+                      }
+                      // mark all the columns in the range.
+                      for (int sr=prange[p],er=prange[p+1],de=er+d; sr!=de; sr+=d)
+                      {
+                        colsel.addElement(sr-1);
+                      }
+                    }
+                  }
+                  send = true;
+                }
+              }
+              if (send)
+              {
+                ssm.sendSelection(jselection, colsel, me);
+              }
+              // discard message.
+              for (int c = 0; c < jvobjs.length; c++)
+              {
+                jvobjs[c] = null;
+              }
+              ;
+              jvobjs = null;
+              return;
+            }
           }
         });
         picker = new VamsasListener()
@@ -621,7 +926,94 @@ public class VamsasApplication
             }
           }
         };
+        selecter = new SelectionListener()
+        {
+
+          public void selection(SequenceGroup seqsel,
+                  ColumnSelection colsel, SelectionSource source)
+          {
+            if (source != me)
+            {
+              AlignmentI visal=null;
+              if (source instanceof AlignViewport)
+              {
+                visal = ((AlignViewport) source).getAlignment();
+              }
+              SelectionMessage sm = null;
+              if ((seqsel == null || seqsel.getSize() == 0)
+                      && (colsel == null || colsel.getSelected() == null || colsel
+                              .getSelected().size() == 0))
+              {
+                if (source instanceof AlignViewport)
+                {
+                  // the empty selection.
+                  sm = new SelectionMessage("jalview", new String[] { ((AlignViewport)source).getSequenceSetId()}, null, true);
+                } else {
+                  // the empty selection.
+                  sm = new SelectionMessage("jalview", null, null, true);
+                }
+              }
+              else
+              {
+                String[] vobj = new String[seqsel.getSize()];
+                int o = 0;
+                Enumeration sels = seqsel.getSequences(null).elements();
+                while (sels.hasMoreElements())
+                {
+                  SequenceI sel = (SequenceI) sels.nextElement();
+                  VorbaId v = (VorbaId) jv2vobj.get(sel);
+                  if (v != null)
+                  {
+                    vobj[o++] = v.toString();
+                  }
+                }
+                if (o < vobj.length)
+                {
+                  String t[] = vobj;
+                  vobj = new String[o];
+                  System.arraycopy(t, 0, vobj, 0, o);
+                  t = null;
+                }
+                Input range = null;
+                if (seqsel!=null && colsel != null)
+                {
+                  // deparse the colsel into positions on the vamsas alignment
+                  // sequences
+                  range = new Input();
+                  if (colsel.getSelected()!=null && colsel.getSelected().size()>0 && visal!=null && seqsel.getSize()==visal.getHeight())
+                  {
+                    // gather selected columns outwith the sequence positions too
+                    Enumeration cols = colsel.getSelected().elements();
+                    while (cols.hasMoreElements())
+                    {
+                      int ival = ((Integer) cols.nextElement()).intValue();
+                      Pos p = new Pos();
+                      p.setI(ival+1);
+                      range.addPos(p);
+                    }
+                  } else {
+                    int[] intervals = colsel.getVisibleContigs(seqsel.getStartRes(), seqsel.getEndRes()+1);
+                    for (int iv=0;iv<intervals.length; iv+=2)
+                    {
+                      Seg s = new Seg();
+                      s.setStart(intervals[iv]+1); // vamsas indices begin at 1, not zero.
+                      s.setEnd(intervals[iv+1]+1);
+                      s.setInclusive(true);
+                      range.addSeg(s);
+                    }
+                  }
+                }
+                sm = new SelectionMessage("jalview", vobj, range);
+              }
+              sm.validate(); // debug
+              Cache.log.debug("Pick Message\n"+sm.getRawMessage());
+              pm.sendMessage(sm);
+            }
+          }
+
+        };
         ssm.addStructureViewerListener(picker); // better method here
+        ssm.addSelectionListener(selecter);
       } catch (Exception e)
       {
         Cache.log.error("Failed to init Vamsas Picking", e);