JAL-3541 selectively merged build.gradle and gradle.properties
[jalview.git] / src / jalview / ext / jmol / JalviewJmolBinding.java
index 1ceabd1..453152e 100644 (file)
@@ -166,7 +166,10 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   {
     // remove listeners for all structures in viewer
     getSsm().removeStructureViewerListener(this, this.getStructureFiles());
-    viewer.dispose();
+    if (viewer != null)
+    {
+      viewer.dispose();
+    }
     lastCommand = null;
     viewer = null;
     releaseUIResources();
@@ -472,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
    * 
@@ -493,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()
@@ -508,6 +512,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
       }
     });
     colourby.start();
+    this.colourby = colourby;
   }
 
   /**
@@ -670,7 +675,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)
@@ -728,7 +733,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
 
     cmd.append("spacefill 200;select none");
 
-    viewer.evalStringQuiet(cmd.toString());
+    jmolScript(cmd.toString());
     jmolHistory(true);
 
   }
@@ -737,7 +742,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)
@@ -884,7 +889,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)
     {
@@ -915,7 +920,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
@@ -1103,8 +1108,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;
@@ -1179,7 +1183,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
@@ -1279,7 +1283,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");
   }
 
   /**
@@ -1303,7 +1309,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
@@ -1334,13 +1349,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 = "";
@@ -1352,7 +1370,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);
@@ -1364,17 +1390,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)
   {
@@ -1435,4 +1476,5 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   {
     showConsole(false);
   }
+
 }