JAL-1764 save Chimera/Jmol state as separate entries in project Jar
[jalview.git] / src / jalview / gui / ChimeraViewFrame.java
index b933345..1e6da78 100644 (file)
@@ -26,9 +26,11 @@ import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.List;
@@ -55,6 +57,7 @@ import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
+import jalview.gui.StructureViewer.ViewerType;
 import jalview.io.AppletFormatAdapter;
 import jalview.io.JalviewFileChooser;
 import jalview.io.JalviewFileView;
@@ -445,23 +448,25 @@ public class ChimeraViewFrame extends StructureViewerBase
 
   /**
    * Create a new viewer from saved session state data including Chimera session
-   * file.
-   * 
-   * @param chimeraSession
+   * file
    * 
+   * @param chimeraSessionFile
    * @param alignPanel
    * @param pdbArray
    * @param seqsArray
    * @param colourByChimera
    * @param colourBySequence
+   * @param newViewId
    */
-  public ChimeraViewFrame(String chimeraSession, AlignmentPanel alignPanel,
+  public ChimeraViewFrame(String chimeraSessionFile,
+          AlignmentPanel alignPanel,
           PDBEntry[] pdbArray,
           SequenceI[][] seqsArray, boolean colourByChimera,
-          boolean colourBySequence)
+          boolean colourBySequence, String newViewId)
   {
     super();
-    this.chimeraSessionFile = chimeraSession;
+    setViewId(newViewId);
+    this.chimeraSessionFile = chimeraSessionFile;
     openNewChimera(alignPanel, pdbArray, seqsArray);
     if (colourByChimera)
     {
@@ -1310,31 +1315,74 @@ public class ChimeraViewFrame extends StructureViewerBase
   }
 
   /**
-   * Ask Chimera to save its session to the designated file path. Returns true
-   * if successful, else false.
+   * Ask Chimera to save its session to the designated file path, or to a
+   * temporary file if the path is null. Returns the file path if successful,
+   * else null.
    * 
    * @param filepath
    * @see getStateInfo
    */
-  public boolean saveSession(String filepath)
+  protected String saveSession(String filepath)
   {
-    boolean result = jmb.saveSession(filepath);
-    if (result)
+    String pathUsed = filepath;
+    try
+    {
+      if (pathUsed == null)
+      {
+        File tempFile = File.createTempFile("chimera", ".py");
+        tempFile.deleteOnExit();
+        pathUsed = tempFile.getPath();
+      }
+      boolean result = jmb.saveSession(pathUsed);
+      if (result)
+      {
+        this.chimeraSessionFile = pathUsed;
+        return pathUsed;
+      }
+    } catch (IOException e)
     {
-      this.chimeraSessionFile = filepath;
     }
-    return result;
+    return null;
   }
 
   /**
-   * Returns the file path of the Chimera session file the last time it was
-   * saved. If it was never saved, returns an empty string. There is no
-   * guarantee that the Chimera session has not changed since it was saved.
+   * Returns a string representing the state of the Chimera session. This is
+   * done by requesting Chimera to save its session to a temporary file, then
+   * reading the file contents. Returns an empty string on any error.
    */
   @Override
   public String getStateInfo()
   {
-    return this.chimeraSessionFile == null ? "" : chimeraSessionFile;
+    String sessionFile = saveSession(null);
+    if (sessionFile == null)
+    {
+      return "";
+    }
+    InputStream is = null;
+    try
+    {
+      File f = new File(sessionFile);
+      byte[] bytes = new byte[(int) f.length()];
+      is = new FileInputStream(sessionFile);
+      is.read(bytes);
+      return new String(bytes);
+    } catch (IOException e)
+    {
+      return "";
+    } finally
+    {
+      if (is != null)
+      {
+        try
+        {
+          is.close();
+        } catch (IOException e)
+        {
+          // ignoreß
+        }
+      }
+    }
+    // return this.chimeraSessionFile == null ? "" : chimeraSessionFile;
   }
 
   @Override
@@ -1342,4 +1390,10 @@ public class ChimeraViewFrame extends StructureViewerBase
   {
     jmb.focusView();
   }
+
+  @Override
+  public ViewerType getViewerType()
+  {
+    return ViewerType.CHIMERA;
+  }
 }