JAL-2130 call viewer.dispose() on closing Jmol
[jalview.git] / src / jalview / ext / jmol / JalviewJmolBinding.java
index ed0b950..2133f2b 100644 (file)
@@ -28,6 +28,7 @@ import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.io.AppletFormatAdapter;
+import jalview.io.StructureFile;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ResidueProperties;
 import jalview.structure.AtomSpec;
@@ -63,11 +64,6 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
         implements JmolStatusListener, JmolSelectionListener,
         ComponentListener
 {
-  /*
-   * state flag used to check if the Jmol viewer's paint method can be called
-   */
-  private boolean finishedInit = false;
-
   boolean allChainsSelected = false;
 
   /*
@@ -180,6 +176,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     // and shut down jmol
     viewer.evalStringQuiet("zap");
     viewer.setJmolStatusListener(null);
+    viewer.dispose();
     lastCommand = null;
     viewer = null;
     releaseUIResources();
@@ -236,10 +233,9 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   public void superposeStructures(AlignmentI alignment, int refStructure,
           ColumnSelection hiddenCols)
   {
-    superposeStructures(new AlignmentI[]
-    { alignment }, new int[]
-    { refStructure }, new ColumnSelection[]
-    { hiddenCols });
+    superposeStructures(new AlignmentI[] { alignment },
+            new int[] { refStructure },
+            new ColumnSelection[] { hiddenCols });
   }
 
   /**
@@ -259,8 +255,17 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   public void superposeStructures(AlignmentI[] _alignment,
           int[] _refStructure, ColumnSelection[] _hiddenCols)
   {
+    while (viewer.isScriptExecuting())
+    {
+      try
+      {
+        Thread.sleep(10);
+      } catch (InterruptedException i)
+      {
+      }
+      ;
+    }
     String[] files = getPdbFile();
-
     if (!waitForFileLoad(files))
     {
       return;
@@ -272,7 +277,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     String nSeconds = " ";
     if (files.length > 10)
     {
-      nSeconds = " 0.00001 ";
+      nSeconds = " 0.005 ";
     }
     else
     {
@@ -281,7 +286,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     }
     // see JAL-1345 - should really automatically turn off the animation for
     // large numbers of structures, but Jmol doesn't seem to allow that.
-    nSeconds = " ";
+    // nSeconds = " ";
     // union of all aligned positions are collected together.
     for (int a = 0; a < _alignment.length; a++)
     {
@@ -454,8 +459,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
                 + selectioncom.toString() + "); cartoons; ");
         // selcom.append("; ribbons; ");
         String cmdString = command.toString();
-        System.out
-.println("Superimpose command(s):\n" + cmdString);
+        System.out.println("Superimpose command(s):\n" + cmdString);
 
         evalStateCommand(cmdString);
       }
@@ -478,7 +482,6 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     jmolHistory(false);
     if (lastCommand == null || !lastCommand.equals(command))
     {
-      System.out.println(command);
       viewer.evalStringQuiet(command + "\n");
     }
     jmolHistory(true);
@@ -513,7 +516,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     }
     AlignmentI alignment = alignmentv.getAlignment();
 
-    for (jalview.structure.StructureMappingcommandSet cpdbbyseq : getColourBySequenceCommands(files, sr, fr, alignment))
+    for (jalview.structure.StructureMappingcommandSet cpdbbyseq : getColourBySequenceCommands(
+            files, sr, fr, alignment))
     {
       for (String cbyseq : cpdbbyseq.commands)
       {
@@ -533,10 +537,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
           String[] files, SequenceRenderer sr, FeatureRenderer fr,
           AlignmentI alignment)
   {
-    return JmolCommands
-            .getColourBySequenceCommand(getSsm(), files, getSequence(), sr,
-                    fr,
-                    alignment);
+    return JmolCommands.getColourBySequenceCommand(getSsm(), files,
+            getSequence(), sr, fr, alignment);
   }
 
   /**
@@ -552,6 +554,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     System.out.println("JMOL CREATE IMAGE");
   }
 
+  @Override
   public String createImage(String fileName, String type,
           Object textOrBytes, int quality)
   {
@@ -559,6 +562,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     return null;
   }
 
+  @Override
   public String eval(String strEval)
   {
     // System.out.println(strEval);
@@ -569,11 +573,13 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   // End StructureListener
   // //////////////////////////
 
+  @Override
   public float[][] functionXY(String functionName, int x, int y)
   {
     return null;
   }
 
+  @Override
   public float[][][] functionXYZ(String functionName, int nx, int ny, int nz)
   {
     // TODO Auto-generated method stub
@@ -589,8 +595,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     }
     // TODO: verify atomIndex is selecting correct model.
     // return new Color(viewer.getAtomArgb(atomIndex)); Jmol 12.2.4
-    int colour = viewer.ms.at[atomIndex]
-            .atomPropertyInt(T.color);
+    int colour = viewer.ms.at[atomIndex].atomPropertyInt(T.color);
     return new Color(colour);
   }
 
@@ -668,6 +673,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
           // applet path with docroot - discard as format won't match pdbfile
           mset[0] = m;
         }
+        _modelFileNameMap[0] = 0; // filename index for first model is always 0.
       }
       int j = 1;
       for (int i = 1; i < mset.length; i++)
@@ -737,6 +743,11 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   {
     if (atoms != null)
     {
+      if (resetLastRes.length() > 0)
+      {
+        viewer.evalStringQuiet(resetLastRes.toString());
+        resetLastRes.setLength(0);
+      }
       for (AtomSpec atom : atoms)
       {
         highlightAtom(atom.getAtomIndex(), atom.getPdbResNum(),
@@ -768,17 +779,10 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     }
 
     jmolHistory(false);
-    // if (!pdbfile.equals(pdbentry.getFile()))
-    // return;
-    if (resetLastRes.length() > 0)
-    {
-      viewer.evalStringQuiet(resetLastRes.toString());
-    }
 
     StringBuilder cmd = new StringBuilder(64);
     cmd.append("select " + pdbResNum); // +modelNum
 
-    resetLastRes.setLength(0);
     resetLastRes.append("select " + pdbResNum); // +modelNum
 
     cmd.append(":");
@@ -887,8 +891,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
         pdbfilename = modelFileNames[_mp];
         if (pdbfilename == null)
         {
-          pdbfilename = new File(
-                  viewer.ms.getModelFileName(mnumber))
+          pdbfilename = new File(viewer.ms.getModelFileName(mnumber))
                   .getAbsolutePath();
         }
 
@@ -943,7 +946,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     String mdlString = "";
     if ((p = strInfo.indexOf(":")) > -1)
     {
-      picked += strInfo.substring(p + 1, strInfo.indexOf("."));
+      picked += strInfo.substring(p, strInfo.indexOf("."));
     }
 
     if ((p = strInfo.indexOf("/")) > -1)
@@ -1120,7 +1123,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     {
       String fileName = modelfilenames[modelnum];
       boolean foundEntry = false;
-      MCview.PDBfile pdb = null;
+      StructureFile pdb = null;
       String pdbfile = null;
       // model was probably loaded inline - so check the pdb file hashcode
       if (loadedInline)
@@ -1131,8 +1134,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
         pdbfile = viewer.getData("" + (1 + _modelFileNameMap[modelnum])
                 + ".0", "PDB");
       }
-        // search pdbentries and sequences to find correct pdbentry for this
-        // model
+      // search pdbentries and sequences to find correct pdbentry for this
+      // model
       for (int pe = 0; pe < getPdbCount(); pe++)
       {
         boolean matches = false;
@@ -1143,7 +1146,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
           {
             pdb = getSsm().setMapping(getSequence()[pe], getChains()[pe],
                     pdbfile, AppletFormatAdapter.PASTE);
-            getPdbEntry(modelnum).setFile("INLINE" + pdb.id);
+            getPdbEntry(modelnum).setFile("INLINE" + pdb.getId());
             matches = true;
             foundEntry = true;
           }
@@ -1182,10 +1185,10 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
         if (matches)
         {
           // add an entry for every chain in the model
-          for (int i = 0; i < pdb.chains.size(); i++)
+          for (int i = 0; i < pdb.getChains().size(); i++)
           {
-            String chid = new String(pdb.id + ":"
-                    + pdb.chains.elementAt(i).id);
+            String chid = new String(pdb.getId() + ":"
+                    + pdb.getChains().elementAt(i).id);
             chainFile.put(chid, fileName);
             chainNames.addElement(chid);
           }
@@ -1217,7 +1220,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     // }
     if (!isLoadingFromArchive())
     {
-      viewer.evalStringQuiet("model 0; select backbone;restrict;cartoon;wireframe off;spacefill off");
+      viewer.evalStringQuiet("model *; select backbone;restrict;cartoon;wireframe off;spacefill off");
     }
     // register ourselves as a listener and notify the gui that it needs to
     // update itself.
@@ -1265,6 +1268,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
    */
   public abstract void sendConsoleMessage(String strStatus);
 
+  @Override
   public void setCallbackFunction(String callbackType,
           String callbackFunction)
   {
@@ -1402,16 +1406,6 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     return null;
   }
 
-  public boolean isFinishedInit()
-  {
-    return finishedInit;
-  }
-
-  public void setFinishedInit(boolean finishedInit)
-  {
-    this.finishedInit = finishedInit;
-  }
-
   /**
    * 
    */
@@ -1466,4 +1460,3 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     showConsole(false);
   }
 }
-