JAL-3551 save structure viewer session refactorings, PyMol added
[jalview.git] / src / jalview / gui / AppJmolBinding.java
index db698ac..34ff7b3 100644 (file)
  */
 package jalview.gui;
 
-import jalview.api.AlignmentViewPanel;
-import jalview.api.structures.JalviewStructureDisplayI;
-import jalview.bin.Cache;
-import jalview.datamodel.PDBEntry;
-import jalview.datamodel.SequenceI;
-import jalview.ext.jmol.JalviewJmolBinding;
-import jalview.io.DataSourceType;
-import jalview.structure.StructureSelectionManager;
-
 import java.awt.Container;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.util.Map;
 
 import javax.swing.JComponent;
@@ -38,6 +32,15 @@ import org.jmol.api.JmolAppConsoleInterface;
 import org.jmol.java.BS;
 import org.openscience.jmol.app.jmolpanel.console.AppConsole;
 
+import jalview.api.AlignmentViewPanel;
+import jalview.api.structures.JalviewStructureDisplayI;
+import jalview.bin.Cache;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceI;
+import jalview.ext.jmol.JalviewJmolBinding;
+import jalview.io.DataSourceType;
+import jalview.structure.StructureSelectionManager;
+
 public class AppJmolBinding extends JalviewJmolBinding
 {
   public AppJmolBinding(AppJmol appJmol, StructureSelectionManager sSm,
@@ -165,4 +168,32 @@ public class AppJmolBinding extends JalviewJmolBinding
     // TODO Auto-generated method stub
     return null;
   }
+
+  /**
+   * Overrides the default method to save a session to file, in order to
+   * guarantee it is done synchronously. Jmol command 'write STATE path' would
+   * execute asynchronously, so instead we get the state and write it directly
+   * here.
+   */
+  @Override
+  protected void saveSession(File f)
+  {
+    String state = jmolViewer.getStateInfo();
+    if (state != null)
+    {
+      try
+      {
+        FileWriter fw = new FileWriter(f);
+        fw.write(state);
+        fw.close();
+      } catch (IOException e)
+      {
+        Cache.log.error("Error writing Jmol state: " + e.toString());
+      }
+    }
+    else
+    {
+      Cache.log.error("Error requesting Jmol state to save");
+    }
+  }
 }