JAL-4086 commented out attempt to create measurement lines between aligned residues...
[jalview.git] / src / jalview / ext / jmol / JalviewJmolBinding.java
index e607ef8..21a19ae 100644 (file)
@@ -20,7 +20,6 @@
  */
 package jalview.ext.jmol;
 
-import java.awt.Color;
 import java.awt.Container;
 import java.awt.event.ComponentEvent;
 import java.awt.event.ComponentListener;
@@ -32,6 +31,8 @@ import java.util.Map;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
+import javax.swing.SwingUtilities;
+
 import org.jmol.adapter.smarter.SmarterJmolAdapter;
 import org.jmol.api.JmolAppConsoleInterface;
 import org.jmol.api.JmolSelectionListener;
@@ -44,7 +45,7 @@ import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureRenderer;
 import jalview.api.FeatureSettingsModelI;
 import jalview.api.SequenceRenderer;
-import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AppJmol;
@@ -90,8 +91,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     setStructureCommands(new JmolCommands());
     /*
      * viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter(),
-     * "jalviewJmol", ap.av.applet .getDocumentBase(),
-     * ap.av.applet.getCodeBase(), "", this);
+     * "jalviewJmol", ap.av.applet .getDocumentBase(), ap.av.applet.getCodeBase(),
+     * "", this);
      * 
      * jmolpopup = JmolPopup.newJmolPopup(viewer, true, "Jmol", true);
      */
@@ -121,9 +122,22 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
 
   private String jmolScript(String script)
   {
-    Cache.log.debug(">>Jmol>> " + script);
-    String s = jmolViewer.evalStringQuiet(script); // scriptWait(script); BH
-    Cache.log.debug("<<Jmol<< " + s);
+    return jmolScript(script, false);
+  }
+
+  private String jmolScript(String script, boolean useScriptWait)
+  {
+    Console.debug(">>Jmol>> " + script);
+    String s;
+    if (useScriptWait)
+    {
+      s = jmolViewer.scriptWait(script);
+    }
+    else
+    {
+      s = jmolViewer.evalStringQuiet(script); // scriptWait(script); BH
+    }
+    Console.debug("<<Jmol<< " + s);
 
     return s;
   }
@@ -208,8 +222,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
       for (int i = 0; i < modelCount; ++i)
       {
         /*
-         * defensive check for null as getModelFileName can return null
-         * even when model count ms.mc is > 0
+         * defensive check for null as getModelFileName can return null even when model
+         * count ms.mc is > 0
          */
         filePath = jmolViewer.ms.getModelFileName(i);
         if (filePath != null && !mset.contains(filePath))
@@ -253,22 +267,47 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   {
     if (atoms != null)
     {
+      boolean useScriptWait = atoms.size() > 1;
       if (resetLastRes.length() > 0)
       {
-        jmolScript(resetLastRes.toString());
+        jmolScript(resetLastRes.toString(), useScriptWait);
         resetLastRes.setLength(0);
       }
       for (AtomSpec atom : atoms)
       {
         highlightAtom(atom.getAtomIndex(), atom.getPdbResNum(),
-                atom.getChain(), atom.getPdbFile());
+                atom.getChain(), atom.getPdbFile(), useScriptWait);
       }
+      // Highlight distances between atoms with a 'measure' command - not yet
+      // working
+      // if (atoms.size() >= 2)
+      // {
+      // StringBuilder sb = new StringBuilder();
+      // for (int a = 0; a < atoms.size(); a++)
+      // {
+      // AtomSpec speca = atoms.get(a);
+      // String a_model = getModelIdForFile(speca.getPdbFile());
+      // for (int b = a + 1; b < atoms.size(); b++)
+      // {
+      // AtomSpec specb = atoms.get(b);
+      // String b_model = getModelIdForFile(speca.getPdbFile());
+      // sb.append("measure ALL (" + speca.getAtomIndex() + " and */"
+      // + a_model + ") (" + specb.getAtomIndex() + " and */"
+      // + b_model + ");");
+      // }
+      // }
+      // jmolHistory(false, useScriptWait);
+      // jmolScript(sb.toString(), useScriptWait);
+      // jmolHistory(true, useScriptWait);
+      // }
+
     }
+
   }
 
   // jmol/ssm only
   public void highlightAtom(int atomIndex, int pdbResNum, String chain,
-          String pdbfile)
+          String pdbfile, boolean useScriptWait)
   {
     String modelId = getModelIdForFile(pdbfile);
     if (modelId.isEmpty())
@@ -276,7 +315,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
       return;
     }
 
-    jmolHistory(false);
+    jmolHistory(false, useScriptWait);
 
     StringBuilder selection = new StringBuilder(32);
     StringBuilder cmd = new StringBuilder(64);
@@ -294,15 +333,21 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     resetLastRes.append(selection).append(";wireframe 0;").append(selection)
             .append(" and not hetero; spacefill 0;");
 
-    jmolScript(cmd.toString());
-    jmolHistory(true);
+    jmolScript(cmd.toString(), useScriptWait);
+    jmolHistory(true, useScriptWait);
   }
 
   private boolean debug = true;
 
   private void jmolHistory(boolean enable)
   {
-    jmolScript("History " + ((debug || enable) ? "on" : "off"));
+    jmolHistory(enable, false);
+  }
+
+  private void jmolHistory(boolean enable, boolean useScriptWait)
+  {
+    jmolScript("History " + ((debug || enable) ? "on" : "off"),
+            useScriptWait);
   }
 
   public void loadInline(String string)
@@ -402,8 +447,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     }
 
     /*
-     * highlight position on alignment(s); if some text is returned, 
-     * show this as a second line on the structure hover tooltip
+     * highlight position on alignment(s); if some text is returned, show this as a
+     * second line on the structure hover tooltip
      */
     String label = getSsm().mouseOverStructure(pdbResNum, chainId,
             pdbfilename);
@@ -439,8 +484,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
 
   /*
    * { if (history != null && strStatus != null &&
-   * !strStatus.equals("Script completed")) { history.append("\n" + strStatus);
-   * } }
+   * !strStatus.equals("Script completed")) { history.append("\n" + strStatus); }
+   * }
    */
 
   public void notifyAtomPicked(int atomIndex, String strInfo,
@@ -501,6 +546,28 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   @Override
   public void notifyCallback(CBK type, Object[] data)
   {
+    /*
+     * ensure processed in AWT thread to avoid risk of deadlocks
+     */
+    SwingUtilities.invokeLater(new Runnable()
+    {
+
+      @Override
+      public void run()
+      {
+        processCallback(type, data);
+      }
+    });
+  }
+
+  /**
+   * Processes one callback notification from Jmol
+   * 
+   * @param type
+   * @param data
+   */
+  protected void processCallback(CBK type, Object[] data)
+  {
     try
     {
       switch (type)
@@ -749,9 +816,6 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
       FeatureRenderer fr = getFeatureRenderer(null);
       if (fr != null)
       {
-        // JAL-1842 ensure FeatureRenderer knows about RESNUM
-        fr.featuresAdded();
-        // ... apply feature settings (e.g. to hide RESNUM)
         FeatureSettingsModelI colours = new Pdb().getFeatureColourScheme();
         ((AppJmol) getViewer()).getAlignmentPanel().av
                 .applyFeaturesStyle(colours);
@@ -783,8 +847,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   public abstract void sendConsoleEcho(String strEcho); /*
                                                          * { showConsole(true);
                                                          * 
-                                                         * history.append("\n" +
-                                                         * strEcho); }
+                                                         * history.append("\n" + strEcho); }
                                                          */
 
   // /End JmolStatusListener