localizes Thread colourby so that it does not cause ThreadStateException
[jalview.git] / src / jalview / ext / jmol / JalviewJmolBinding.java
index 258d806..fa31fd9 100644 (file)
@@ -167,7 +167,9 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     // remove listeners for all structures in viewer
     getSsm().removeStructureViewerListener(this, this.getStructureFiles());
     if (viewer != null)
+    {
       viewer.dispose();
+    }
     lastCommand = null;
     viewer = null;
     releaseUIResources();
@@ -473,13 +475,14 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     jmolHistory(false);
     if (lastCommand == null || !lastCommand.equals(command))
     {
-      viewer.evalStringQuiet(command + "\n");
+      jmolScript(command + "\n");
     }
     jmolHistory(true);
     lastCommand = command;
   }
 
   Thread colourby = null;
+
   /**
    * Sends a set of colour commands to the structure viewer
    * 
@@ -494,7 +497,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
       colourby.interrupt();
       colourby = null;
     }
-    colourby = new Thread(new Runnable()
+    Thread colourby = new Thread(new Runnable()
     {
       @Override
       public void run()
@@ -509,6 +512,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
       }
     });
     colourby.start();
+    this.colourby = colourby;
   }
 
   /**
@@ -739,7 +743,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     {
       if (resetLastRes.length() > 0)
       {
-        viewer.evalStringQuiet(resetLastRes.toString());
+        jmolScript(resetLastRes.toString());
         resetLastRes.setLength(0);
       }
       for (AtomSpec atom : atoms)
@@ -797,7 +801,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
 
     cmd.append("spacefill 200;select none");
 
-    viewer.evalStringQuiet(cmd.toString());
+    jmolScript(cmd.toString());
     jmolHistory(true);
 
   }
@@ -806,7 +810,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
 
   private void jmolHistory(boolean enable)
   {
-    viewer.evalStringQuiet("History " + ((debug || enable) ? "on" : "off"));
+    jmolScript("History " + ((debug || enable) ? "on" : "off"));
   }
 
   public void loadInline(String string)
@@ -935,7 +939,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   {
     /**
      * this implements the toggle label behaviour copied from the original
-     * structure viewer, MCView
+     * structure viewer, mc_view
      */
     if (strData != null)
     {
@@ -966,7 +970,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
 
     if (!atomsPicked.contains(picked))
     {
-      viewer.evalStringQuiet("select " + picked + ";label %n %r:%c");
+      jmolScript("select " + picked + ";label %n %r:%c");
       atomsPicked.addElement(picked);
     }
     else
@@ -1154,8 +1158,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
           // see JAL-623 - need method of matching pasted data up
           {
             pdb = getSsm().setMapping(getSequence()[pe], getChains()[pe],
-                    pdbfile, DataSourceType.PASTE,
-                    getIProgressIndicator());
+                    pdbfile, DataSourceType.PASTE, getIProgressIndicator());
             getPdbEntry(modelnum).setFile("INLINE" + pdb.getId());
             matches = true;
             foundEntry = true;
@@ -1230,7 +1233,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     // }
     if (!isLoadingFromArchive())
     {
-      viewer.evalStringQuiet(
+      jmolScript(
               "model *; select backbone;restrict;cartoon;wireframe off;spacefill off");
     }
     // register ourselves as a listener and notify the gui that it needs to
@@ -1255,7 +1258,10 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     return chainNames;
   }
 
-  protected abstract IProgressIndicator getIProgressIndicator();
+  protected IProgressIndicator getIProgressIndicator()
+  {
+    return null;
+  }
 
   public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
   {
@@ -1327,7 +1333,9 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
 
   public void showHelp()
   {
-    showUrl("http://jmol.sourceforge.net/docs/JmolUserGuide/", "jmolHelp");
+    showUrl("http://wiki.jmol.org"
+    // BH 2018 "http://jmol.sourceforge.net/docs/JmolUserGuide/"
+            , "jmolHelp");
   }
 
   /**
@@ -1351,7 +1359,16 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
    */
   public abstract void showConsole(boolean show);
 
+  public static Viewer getJmolData(JmolParser jmolParser)
+  {
+    return (Viewer) JmolViewer.allocateViewer(null, null, null, null, null,
+            "-x -o -n", jmolParser);
+  }
+
   /**
+   * 
+   * 
+   * 
    * @param renderPanel
    * @param jmolfileio
    *          - when true will initialise jmol's file IO system (should be false
@@ -1382,13 +1399,16 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
    * @param consolePanel
    *          - panel to contain Jmol console
    * @param buttonsToShow
-   *          - buttons to show on the console, in ordr
+   *          - buttons to show on the console, in order
    */
   public void allocateViewer(Container renderPanel, boolean jmolfileio,
           String htmlName, URL documentBase, URL codeBase,
           String commandOptions, final Container consolePanel,
           String buttonsToShow)
   {
+
+    System.err.println("Allocating Jmol Viewer: " + commandOptions);
+
     if (commandOptions == null)
     {
       commandOptions = "";
@@ -1400,7 +1420,15 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
 
     viewer.setJmolStatusListener(this); // extends JmolCallbackListener
 
-    console = createJmolConsole(consolePanel, buttonsToShow);
+    try
+    {
+      console = createJmolConsole(consolePanel, buttonsToShow);
+    } catch (Throwable e)
+    {
+      System.err.println("Could not create Jmol application console. "
+              + e.getMessage());
+      e.printStackTrace();
+    }
     if (consolePanel != null)
     {
       consolePanel.addComponentListener(this);
@@ -1412,17 +1440,32 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   protected abstract JmolAppConsoleInterface createJmolConsole(
           Container consolePanel, String buttonsToShow);
 
+  // BH 2018 -- Jmol console is not working due to problems with styled
+  // documents.
+
   protected org.jmol.api.JmolAppConsoleInterface console = null;
 
   @Override
   public void setBackgroundColour(java.awt.Color col)
   {
     jmolHistory(false);
-    viewer.evalStringQuiet("background [" + col.getRed() + ","
-            + col.getGreen() + "," + col.getBlue() + "];");
+    jmolScript("background [" + col.getRed() + "," + col.getGreen() + ","
+            + col.getBlue() + "];");
     jmolHistory(true);
   }
 
+  private String jmolScript(String script)
+  {
+
+    System.err.println(">>Jmol>> " + script);
+
+    String s = viewer.scriptWait(script);
+
+    System.err.println("<<Jmol<< " + s);
+
+    return s;
+  }
+
   @Override
   public int[] resizeInnerPanel(String data)
   {
@@ -1483,4 +1526,5 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   {
     showConsole(false);
   }
+
 }