JAL-1588 restoring saved Chimera session(s)
[jalview.git] / src / jalview / gui / Jalview2XML.java
index bb4ef8b..728d2fc 100644 (file)
@@ -27,6 +27,8 @@ import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
+import jalview.datamodel.ViewerData;
+import jalview.datamodel.ViewerData.StructureData;
 import jalview.schemabinding.version2.AlcodMap;
 import jalview.schemabinding.version2.Alcodon;
 import jalview.schemabinding.version2.AlcodonFrame;
@@ -100,6 +102,7 @@ import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.IdentityHashMap;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -128,47 +131,6 @@ import org.exolab.castor.xml.Unmarshaller;
  */
 public class Jalview2XML
 {
-
-  /**
-   * A data bean to hold stored data about a structure viewer.
-   */
-  public class ViewerData
-  {
-
-    private int x;
-
-    private int y;
-
-    private int width;
-
-    private int height;
-
-    public boolean alignWithPanel;
-
-    public boolean colourWithAlignPanel;
-
-    public boolean colourByViewer;
-
-    String stateData = "";
-
-    // todo: java bean in place of Object []
-    private Map<File, Object[]> fileData = new HashMap<File, Object[]>();
-
-    public ViewerData(int x, int y, int width, int height,
-            boolean alignWithPanel, boolean colourWithAlignPanel,
-            boolean colourByViewer)
-    {
-      this.x = x;
-      this.y = y;
-      this.width = width;
-      this.height = height;
-      this.alignWithPanel = alignWithPanel;
-      this.colourWithAlignPanel = colourWithAlignPanel;
-      this.colourByViewer = colourByViewer;
-    }
-
-  }
-
   /*
    * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
    * of sequence objects are created.
@@ -3073,9 +3035,11 @@ public class Jalview2XML
   protected void loadStructures(jarInputStreamProvider jprovider,
           JSeq[] jseqs, AlignFrame af, AlignmentPanel ap)
   {
-    // run through all PDB ids on the alignment, and collect mappings between
-    // jmol view ids and all sequences referring to it
-    Map<String, ViewerData> jmolViewIds = new HashMap<String, ViewerData>();
+    /*
+     * Run through all PDB ids on the alignment, and collect mappings between
+     * distinct view ids and all sequences referring to that view.
+     */
+    Map<String, ViewerData> structureViewers = new LinkedHashMap<String, ViewerData>();
 
     for (int i = 0; i < jseqs.length; i++)
     {
@@ -3115,9 +3079,9 @@ public class Jalview2XML
               sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width
                       + "," + height;
             }
-            if (!jmolViewIds.containsKey(sviewid))
+            if (!structureViewers.containsKey(sviewid))
             {
-              jmolViewIds.put(sviewid, new ViewerData(x, y, width, height,
+              structureViewers.put(sviewid, new ViewerData(x, y, width, height,
                       false, false, true));
               // Legacy pre-2.7 conversion JAL-823 :
               // do not assume any view has to be linked for colour by
@@ -3128,44 +3092,53 @@ public class Jalview2XML
             // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
             // seqs_file 2}, boolean[] {
             // linkAlignPanel,superposeWithAlignpanel}} from hash
-            ViewerData jmoldat = jmolViewIds.get(sviewid);
-            jmoldat.alignWithPanel |= structureState
-                    .hasAlignwithAlignPanel() ? structureState.getAlignwithAlignPanel() : false;
-            // never colour by linked panel if not specified
-            jmoldat.colourWithAlignPanel |= structureState
-                    .hasColourwithAlignPanel() ? structureState.getColourwithAlignPanel()
-                    : false;
-            // default for pre-2.7 projects is that Jmol colouring is enabled
-            jmoldat.colourByViewer &= structureState
+            ViewerData jmoldat = structureViewers.get(sviewid);
+            jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
+                    | (structureState.hasAlignwithAlignPanel() ? structureState
+                            .getAlignwithAlignPanel() : false));
+
+            /*
+             * Default colour by linked panel to false if not specified (e.g.
+             * for pre-2.7 projects)
+             */
+            boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
+            colourWithAlignPanel |= (structureState
+                    .hasColourwithAlignPanel() ? structureState
+                    .getColourwithAlignPanel() : false);
+            jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
+
+            /*
+             * Default colour by viewer to true if not specified (e.g. for
+             * pre-2.7 projects)
+             */
+            boolean colourByViewer = jmoldat.isColourByViewer();
+            colourByViewer &= structureState
                     .hasColourByJmol() ? structureState
                     .getColourByJmol() : true;
+            jmoldat.setColourByViewer(colourByViewer);
 
-            if (jmoldat.stateData.length() < structureState.getContent()
-                    .length())
+            if (jmoldat.getStateData().length() < structureState
+                    .getContent().length())
             {
               {
-                jmoldat.stateData = structureState.getContent();
+                jmoldat.setStateData(structureState.getContent());
               }
             }
             if (ids[p].getFile() != null)
             {
               File mapkey = new File(ids[p].getFile());
-              Object[] seqstrmaps = jmoldat.fileData.get(mapkey);
+              StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
               if (seqstrmaps == null)
               {
-                jmoldat.fileData.put(mapkey,
-                        seqstrmaps = new Object[]
-                        { pdbFile, ids[p].getId(), new Vector(),
-                            new Vector() });
+                jmoldat.getFileData().put(
+                        mapkey,
+                        seqstrmaps = jmoldat.new StructureData(pdbFile,
+                                ids[p].getId()));
               }
-              if (!((Vector) seqstrmaps[2]).contains(seq))
+              if (!seqstrmaps.getSeqList().contains(seq))
               {
-                ((Vector) seqstrmaps[2]).addElement(seq);
-                // ((Vector)seqstrmaps[3]).addElement(n) :
-                // in principle, chains
-                // should be stored here : do we need to
-                // TODO: store and recover seq/pdb_id :
-                // chain mappings
+                seqstrmaps.getSeqList().add(seq);
+                // TODO and chains?
               }
             }
             else
@@ -3178,7 +3151,7 @@ public class Jalview2XML
       }
     }
       // Instantiate the associated structure views
-      for (Entry<String, ViewerData> entry : jmolViewIds.entrySet())
+      for (Entry<String, ViewerData> entry : structureViewers.entrySet())
       {
         createOrLinkStructureViewer(entry, af, ap);
       }
@@ -3212,7 +3185,7 @@ public class Jalview2XML
      * Pending an XML element for ViewerType, just check if stateData contains
      * "chimera" (part of the chimera session filename).
      */
-    if (svattrib.stateData.indexOf("chimera") > -1)
+    if (svattrib.getStateData().indexOf("chimera") > -1)
     {
       createChimeraViewer(viewerData, af);
     }
@@ -3232,36 +3205,34 @@ public class Jalview2XML
           AlignFrame af)
   {
     final ViewerData data = viewerData.getValue();
-    String chimeraSession = data.stateData;
-    List<String> pdbfilenames = new ArrayList<String>();
-    List<SequenceI[]> seqmaps = new ArrayList<SequenceI[]>();
-    List<String> pdbids = new ArrayList<String>();
+    String chimeraSession = data.getStateData();
 
     if (new File(chimeraSession).exists())
     {
-      Set<Entry<File, Object[]>> fileData = data.fileData.entrySet();
+      Set<Entry<File, StructureData>> fileData = data.getFileData()
+              .entrySet();
       List<PDBEntry> pdbs = new ArrayList<PDBEntry>();
       List<SequenceI[]> allseqs = new ArrayList<SequenceI[]>();
-      for (Entry<File, Object[]> pdb : fileData)
-      {
-        String filePath = (String) pdb.getValue()[0];
-        String pdbId = (String) pdb.getValue()[1];
-        final Vector seqList = (Vector) pdb.getValue()[2];
-        PDBEntry pdbentry = new PDBEntry();
-        pdbentry.setFile(filePath);
-        pdbentry.setId(pdbId);
-        pdbs.add(pdbentry);
-        SequenceI[] seqs = new SequenceI[seqList.size()];
-        seqList.copyInto(seqs);
+      for (Entry<File, StructureData> pdb : fileData)
+      {
+        String filePath = pdb.getValue().getFilePath();
+        String pdbId = pdb.getValue().getPdbId();
+        pdbs.add(new PDBEntry(filePath, pdbId));
+        final List<SequenceI> seqList = pdb.getValue().getSeqList();
+        SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
         allseqs.add(seqs);
       }
 
+      boolean colourByChimera = data.isColourByViewer();
+      boolean colourBySequence = data.isColourWithAlignPanel();
+
       // TODO can/should this be done via StructureViewer (like Jmol)?
       final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs
               .size()]);
       final SequenceI[][] seqsArray = allseqs.toArray(new SequenceI[allseqs.size()][]);
-      ChimeraViewFrame cvf = // new ChimeraViewFrame(data, af);
-      new ChimeraViewFrame(af.alignPanel, pdbArray, seqsArray);
+      new ChimeraViewFrame(chimeraSession, af.alignPanel, pdbArray,
+              seqsArray,
+              colourByChimera, colourBySequence);
     }
     else
     {
@@ -3282,13 +3253,13 @@ public class Jalview2XML
           final Entry<String, ViewerData> viewerData, AlignFrame af)
   {
     final ViewerData svattrib = viewerData.getValue();
-    String state = svattrib.stateData;
+    String state = svattrib.getStateData();
     List<String> pdbfilenames = new ArrayList<String>();
     List<SequenceI[]> seqmaps = new ArrayList<SequenceI[]>();
     List<String> pdbids = new ArrayList<String>();
     StringBuilder newFileLoc = new StringBuilder(64);
     int cp = 0, ncp, ecp;
-    Map<File, Object[]> oldFiles = svattrib.fileData;
+    Map<File, StructureData> oldFiles = svattrib.getFileData();
     while ((ncp = state.indexOf("load ", cp)) > -1)
     {
       do
@@ -3302,11 +3273,11 @@ public class Jalview2XML
         // have to normalize filename - since Jmol and jalview do
         // filename
         // translation differently.
-        Object[] filedat = oldFiles.get(new File(oldfilenam));
-        newFileLoc.append(Platform.escapeString((String) filedat[0]));
-        pdbfilenames.add((String) filedat[0]);
-        pdbids.add((String) filedat[1]);
-        seqmaps.add(((Vector<SequenceI>) filedat[2])
+        StructureData filedat = oldFiles.get(new File(oldfilenam));
+        newFileLoc.append(Platform.escapeString(filedat.getFilePath()));
+        pdbfilenames.add(filedat.getFilePath());
+        pdbids.add(filedat.getPdbId());
+        seqmaps.add(filedat.getSeqList()
                 .toArray(new SequenceI[0]));
         newFileLoc.append("\"");
         cp = ecp + 1; // advance beyond last \" and set cursor so we can
@@ -3327,15 +3298,14 @@ public class Jalview2XML
       {
         // add this and any other pdb files that should be present in
         // the viewer
-        Object[] filedat = oldFiles.get(id);
-        String nfilename;
-        newFileLoc.append(((String) filedat[0]));
-        pdbfilenames.add((String) filedat[0]);
-        pdbids.add((String) filedat[1]);
-        seqmaps.add(((Vector<SequenceI>) filedat[2])
+        StructureData filedat = oldFiles.get(id);
+        newFileLoc.append(filedat.getFilePath());
+        pdbfilenames.add(filedat.getFilePath());
+        pdbids.add(filedat.getPdbId());
+        seqmaps.add(filedat.getSeqList()
                 .toArray(new SequenceI[0]));
         newFileLoc.append(" \"");
-        newFileLoc.append((String) filedat[0]);
+        newFileLoc.append(filedat.getFilePath());
         newFileLoc.append("\"");
 
       }
@@ -3373,8 +3343,8 @@ public class Jalview2XML
       final String fileloc = newFileLoc.toString();
       final String sviewid = viewerData.getKey();
       final AlignFrame alf = af;
-      final java.awt.Rectangle rect = new java.awt.Rectangle(svattrib.x,
-              svattrib.y, svattrib.width, svattrib.height);
+      final Rectangle rect = new Rectangle(svattrib.getX(),
+              svattrib.getY(), svattrib.getWidth(), svattrib.getHeight());
       try
       {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable()
@@ -3448,9 +3418,10 @@ public class Jalview2XML
         /*
          * Otherwise test for matching position and size of viewer frame
          */
-        else if (frame.getX() == svattrib.x && frame.getY() == svattrib.y
-                && frame.getHeight() == svattrib.height
-                && frame.getWidth() == svattrib.width)
+        else if (frame.getX() == svattrib.getX()
+                && frame.getY() == svattrib.getY()
+                && frame.getHeight() == svattrib.getHeight()
+                && frame.getWidth() == svattrib.getWidth())
         {
           comp = (AppJmol) frame;
           // todo: break?
@@ -3476,10 +3447,10 @@ public class Jalview2XML
     // NOTE: if the jalview project is part of a shared session then
     // view synchronization should/could be done here.
 
-    final boolean useinViewerSuperpos = svattrib.alignWithPanel;
-    final boolean usetoColourbyseq = svattrib.colourWithAlignPanel;
-    final boolean viewerColouring = svattrib.colourByViewer;
-    Map<File, Object[]> oldFiles = svattrib.fileData;
+    final boolean useinViewerSuperpos = svattrib.isAlignWithPanel();
+    final boolean usetoColourbyseq = svattrib.isColourWithAlignPanel();
+    final boolean viewerColouring = svattrib.isColourByViewer();
+    Map<File, StructureData> oldFiles = svattrib.getFileData();
 
     /*
      * Add mapping for sequences in this view to an already open viewer
@@ -3489,13 +3460,10 @@ public class Jalview2XML
     {
       // add this and any other pdb files that should be present in the
       // viewer
-      Object[] filedat = oldFiles.get(id);
-      String pdbFile = (String) filedat[0];
-      SequenceI[] seq = ((Vector<SequenceI>) filedat[2])
-              .toArray(new SequenceI[0]);
-      binding
-              .getSsm()
-              .setMapping(seq, null, pdbFile,
+      StructureData filedat = oldFiles.get(id);
+      String pdbFile = filedat.getFilePath();
+      SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
+      binding.getSsm().setMapping(seq, null, pdbFile,
               jalview.io.AppletFormatAdapter.FILE);
       binding.addSequenceForStructFile(pdbFile, seq);
     }