JAL-3551 null pointer defence while structure files loading; comments feature/JAL-3551Pymol
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 29 May 2020 15:23:05 +0000 (16:23 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 29 May 2020 15:23:05 +0000 (16:23 +0100)
src/jalview/ext/jmol/JalviewJmolBinding.java
src/jalview/ext/pymol/PymolCommands.java
src/jalview/ext/rbvi/chimera/ChimeraCommands.java
src/jalview/ext/rbvi/chimera/ChimeraXCommands.java
src/jalview/gui/AppJmolBinding.java
src/jalview/gui/StructureViewerBase.java
src/jalview/project/Jalview2XML.java
src/jalview/structures/models/AAStructureBindingModel.java

index 63c1fd3..eee48df 100644 (file)
@@ -118,7 +118,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   private String jmolScript(String script)
   {
     Cache.log.debug(">>Jmol>> " + script);
-    String s = jmolViewer.scriptWait(script);
+    String s = jmolViewer.evalStringQuiet(script); // scriptWait(script); BH
     Cache.log.debug("<<Jmol<< " + s);
 
     return s;
index 1e4885e..3493d03 100644 (file)
@@ -313,6 +313,7 @@ public class PymolCommands extends StructureCommandsBase
   public StructureCommandI openSession(String filepath)
   {
     // https://pymolwiki.org/index.php/Load
+    // this version of the command has no dependency on file extension
     return new StructureCommand("load", filepath, "", "0", "pse");
   }
 
index 8e72255..5beee56 100644 (file)
@@ -402,6 +402,7 @@ public class ChimeraCommands extends StructureCommandsBase
   public StructureCommandI openSession(String filepath)
   {
     // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
+    // this version of the command has no dependency on file extension
     return new StructureCommand("open chimera:" + filepath);
   }
 
index 6df5ab9..a596da9 100644 (file)
@@ -227,6 +227,7 @@ public class ChimeraXCommands extends ChimeraCommands
   public StructureCommandI openSession(String filepath)
   {
     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html#composite
+    // this version of the command has no dependency on file extension
     return new StructureCommand("open " + filepath + " format session");
   }
 }
index 3298509..98787cb 100644 (file)
@@ -93,6 +93,11 @@ public class AppJmolBinding extends JalviewJmolBinding
   @Override
   public void refreshGUI()
   {
+    if (getMappedStructureCount() == 0)
+    {
+      // too soon!
+      return;
+    }
     // appJmolWindow.repaint();
     javax.swing.SwingUtilities.invokeLater(new Runnable()
     {
index fccd9bf..33a122c 100644 (file)
@@ -970,7 +970,7 @@ public abstract class StructureViewerBase extends GStructureViewer
      * enable 'Superpose with' if more than one mapped structure
      */
     viewSelectionMenu.setEnabled(false);
-    if (getBinding().getStructureFiles().length > 1
+    if (getBinding().getMappedStructureCount() > 1
             && getBinding().getSequence().length > 1)
     {
       viewSelectionMenu.setEnabled(true);
index 8bbe20c..ccd9ab0 100644 (file)
@@ -29,7 +29,6 @@ import java.awt.Font;
 import java.awt.Rectangle;
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
-import java.io.DataOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -1089,13 +1088,14 @@ public class Jalview2XML
             if (frames[f] instanceof StructureViewerBase)
             {
               StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
-              matchedFile = saveStructureState(ap, jds, pdb, entry, viewIds,
+              matchedFile = saveStructureViewer(ap, jds, pdb, entry, viewIds,
                       matchedFile, viewFrame);
               /*
                * Only store each structure viewer's state once in the project
                * jar. First time through only (storeDS==false)
                */
               String viewId = viewFrame.getViewId();
+              String viewerType = viewFrame.getViewerType().toString();
               if (!storeDS && !viewIds.contains(viewId))
               {
                 viewIds.add(viewId);
@@ -1103,13 +1103,13 @@ public class Jalview2XML
                 if (viewerState != null)
                 {
                   copyFileToJar(jout, viewerState.getPath(),
-                          getViewerJarEntryName(viewId));
+                          getViewerJarEntryName(viewId), viewerType);
                 }
                 else
                 {
                   Cache.log.error("Failed to save viewer state for "
                           +
-                          viewFrame.getViewerType().toString());
+                          viewerType);
                 }
               }
             }
@@ -1131,7 +1131,7 @@ public class Jalview2XML
             if (!pdbfiles.contains(pdbId))
             {
               pdbfiles.add(pdbId);
-              copyFileToJar(jout, matchedFile, pdbId);
+              copyFileToJar(jout, matchedFile, pdbId, pdbId);
             }
           }
 
@@ -1678,7 +1678,7 @@ public class Jalview2XML
       // using save and then load
       try
       {
-       fileName = fileName.replace('\\', '/');
+        fileName = fileName.replace('\\', '/');
         System.out.println("Writing jar entry " + fileName);
         JarEntry entry = new JarEntry(fileName);
         jout.putNextEntry(entry);
@@ -1979,7 +1979,7 @@ public class Jalview2XML
 
                 String varnaStateFile = varna.getStateInfo(model.rna);
                 jarEntryName = RNA_PREFIX + viewId + "_" + nextCounter();
-                copyFileToJar(jout, varnaStateFile, jarEntryName);
+                copyFileToJar(jout, varnaStateFile, jarEntryName, "Varna");
                 rnaSessions.put(model, jarEntryName);
               }
               SecondaryStructure ss = new SecondaryStructure();
@@ -2003,16 +2003,19 @@ public class Jalview2XML
    * @param jout
    * @param infilePath
    * @param jarEntryName
+   * @param msg
+   *          additional identifying info to log to the console
    */
   protected void copyFileToJar(JarOutputStream jout, String infilePath,
-          String jarEntryName)
+          String jarEntryName, String msg)
   {
     try (InputStream is = new FileInputStream(infilePath))
     {
       File file = new File(infilePath);
       if (file.exists() && jout != null)
       {
-        System.out.println("Writing jar entry " + jarEntryName);
+        System.out.println(
+                "Writing jar entry " + jarEntryName + " (" + msg + ")");
         jout.putNextEntry(new JarEntry(jarEntryName));
         copyAll(is, jout);
         jout.closeEntry();
@@ -2028,29 +2031,6 @@ public class Jalview2XML
   }
 
   /**
-   * Write the data to a new entry of given name in the output jar file
-   * 
-   * @param jout
-   * @param jarEntryName
-   * @param data
-   * @throws IOException
-   */
-  protected void writeJarEntry(JarOutputStream jout, String jarEntryName,
-          byte[] data) throws IOException
-  {
-    if (jout != null)
-    {
-      jarEntryName = jarEntryName.replace('\\','/');
-      System.out.println("Writing jar entry " + jarEntryName);
-      jout.putNextEntry(new JarEntry(jarEntryName));
-      DataOutputStream dout = new DataOutputStream(jout);
-      dout.write(data, 0, data.length);
-      dout.flush();
-      jout.closeEntry();
-    }
-  }
-
-  /**
    * Copies input to output, in 4K buffers; handles any data (text or binary)
    * 
    * @param in
@@ -2081,7 +2061,7 @@ public class Jalview2XML
    * @param viewFrame
    * @return
    */
-  protected String saveStructureState(AlignmentPanel ap, SequenceI jds,
+  protected String saveStructureViewer(AlignmentPanel ap, SequenceI jds,
           Pdbids pdb, PDBEntry entry, List<String> viewIds,
           String matchedFile, StructureViewerBase viewFrame)
   {
@@ -6198,8 +6178,8 @@ public class Jalview2XML
     String state = svattrib.getStateData(); // Jalview < 2.9
     if (state == null || state.isEmpty()) // Jalview >= 2.9
     {
-      state = readJarEntry(jprovider,
-              getViewerJarEntryName(svattrib.getViewId()));
+      String jarEntryName = getViewerJarEntryName(svattrib.getViewId());
+      state = readJarEntry(jprovider, jarEntryName);
     }
     // TODO or simpler? for each key in oldFiles,
     // replace key.getPath() in state with oldFiles.get(key).getFilePath()
@@ -6246,8 +6226,7 @@ public class Jalview2XML
       {
         // add pdb files that should be present in the viewer
         StructureData filedat = oldFiles.get(id);
-        rewritten.append(filedat.getFilePath()).append(" \"")
-                .append(filedat.getFilePath()).append("\"");
+        rewritten.append(" \"").append(filedat.getFilePath()).append("\"");
       }
       rewritten.append(";");
     }
index dcd6da8..5949847 100644 (file)
@@ -1855,4 +1855,17 @@ public abstract class AAStructureBindingModel
       }
     }
   }
+
+  /**
+   * Returns the number of structure files in the structure viewer and mapped to
+   * Jalview. This may be zero if the files are still in the process of loading
+   * in the viewer.
+   * 
+   * @return
+   */
+  public int getMappedStructureCount()
+  {
+    String[] files = getStructureFiles();
+    return files == null ? 0 : files.length;
+  }
 }