JAL-3551 pull up Close Viewer dialog to base class
[jalview.git] / src / jalview / structures / models / AAStructureBindingModel.java
index db523f9..6e926df 100644 (file)
  */
 package jalview.structures.models;
 
+import java.awt.Color;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.swing.SwingUtilities;
+
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
 import jalview.api.StructureSelectionManagerProvider;
 import jalview.api.structures.JalviewStructureDisplayI;
+import jalview.bin.Cache;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.PDBEntry;
@@ -46,17 +60,6 @@ import jalview.structure.StructureSelectionManager;
 import jalview.util.Comparison;
 import jalview.util.MessageManager;
 
-import java.awt.Color;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.BitSet;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.swing.SwingUtilities;
-
 /**
  * 
  * A base class to hold common function for protein structure model binding.
@@ -1371,7 +1374,6 @@ public abstract class AAStructureBindingModel
   
     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
     {
-      // todo indirect this resolution / allow override
       final String modelId = getModelIdForFile(files[pdbfnum]);
       StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
   
@@ -1513,4 +1515,83 @@ public abstract class AAStructureBindingModel
   
     atomSpec.addRange(model, startPos, endPos, chain);
   }
+
+  /**
+   * Returns the file extension (including '.' separator) to use for a saved
+   * viewer session file. Default is to return null (not supported), override as
+   * required.
+   * 
+   * @return
+   */
+  public String getSessionFileExtension()
+  {
+    return null;
+  }
+
+  /**
+   * If supported, saves the state of the structure viewer to a temporary file
+   * and returns the file. Returns null and logs an error on any failure.
+   * 
+   * @return
+   */
+  public File saveSession()
+  {
+    String prefix = getViewerType().toString();
+    String suffix = getSessionFileExtension();
+    File f = null;
+    try
+    {
+      f = File.createTempFile(prefix, suffix);
+      saveSession(f);
+    } catch (IOException e)
+    {
+      Cache.log.error(String.format("Error saving %s session: %s",
+              prefix, e.toString()));
+    }
+
+    return f;
+  }
+
+  /**
+   * Saves the structure viewer session to the given file
+   * 
+   * @param f
+   */
+  protected void saveSession(File f)
+  {
+    StructureCommandI cmd = commandGenerator
+            .saveSession(f.getPath());
+    if (cmd != null)
+    {
+      executeCommand(cmd, false);
+    }
+  }
+
+  /**
+   * Returns true if the viewer is an external structure viewer for which the
+   * process is still alive, else false (for Jmol, or an external viewer which
+   * the user has independently closed)
+   * 
+   * @return
+   */
+  public boolean isViewerRunning()
+  {
+    return false;
+  }
+
+  /**
+   * Closes Jalview's structure viewer panel and releases associated resources.
+   * If it is managing an external viewer program, and {@code forceClose} is
+   * true, also shuts down that program.
+   * 
+   * @param forceClose
+   */
+  public void closeViewer(boolean forceClose)
+  {
+    getSsm().removeStructureViewerListener(this, this.getStructureFiles());
+    releaseUIResources();
+
+    // add external viewer shutdown in overrides
+    // todo - or can maybe pull up to here
+  }
 }