JAL-3626 use Viewer.scriptWait() to send Jmol commands (Bob Hanson)
[jalview.git] / src / jalview / gui / AppJmol.java
index 83ec177..ffc8053 100644 (file)
  */
 package jalview.gui;
 
-import jalview.api.AlignmentViewPanel;
-import jalview.bin.Cache;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.PDBEntry;
-import jalview.datamodel.SequenceI;
-import jalview.gui.StructureViewer.ViewerType;
-import jalview.structures.models.AAStructureBindingModel;
-import jalview.util.BrowserLauncher;
-import jalview.util.MessageManager;
-import jalview.util.Platform;
-import jalview.ws.dbsources.Pdb;
-
 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Rectangle;
-import java.awt.event.ActionEvent;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Vector;
 
-import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JPanel;
 import javax.swing.JSplitPane;
 import javax.swing.SwingUtilities;
 import javax.swing.event.InternalFrameAdapter;
 import javax.swing.event.InternalFrameEvent;
 
+import jalview.api.AlignmentViewPanel;
+import jalview.bin.Cache;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceI;
+import jalview.gui.ImageExporter.ImageWriterI;
+import jalview.gui.StructureViewer.ViewerType;
+import jalview.structure.StructureCommand;
+import jalview.structures.models.AAStructureBindingModel;
+import jalview.util.BrowserLauncher;
+import jalview.util.ImageMaker;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
+import jalview.ws.dbsources.Pdb;
+
 public class AppJmol extends StructureViewerBase
 {
   // ms to wait for Jmol to load files
@@ -184,7 +184,10 @@ public class AppJmol extends StructureViewerBase
     useAlignmentPanelForColourbyseq(ap);
 
     alignAddedStructures = alignAdded;
-    useAlignmentPanelForSuperposition(ap);
+    if (pdbentrys.length > 1)
+    {
+      useAlignmentPanelForSuperposition(ap);
+    }
 
     jmb.setColourBySequence(true);
     setSize(400, 400); // probably should be a configurable/dynamic default here
@@ -247,47 +250,12 @@ public class AppJmol extends StructureViewerBase
     {
       command = "";
     }
-    jmb.evalStateCommand(command);
-    jmb.evalStateCommand("set hoverDelay=0.1");
+    jmb.executeCommand(new StructureCommand(command), false);
+    jmb.executeCommand(new StructureCommand("set hoverDelay=0.1"), false);
     jmb.setFinishedInit(true);
   }
 
   @Override
-  void showSelectedChains()
-  {
-    Vector<String> toshow = new Vector<>();
-    for (int i = 0; i < chainMenu.getItemCount(); i++)
-    {
-      if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
-      {
-        JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
-        if (item.isSelected())
-        {
-          toshow.addElement(item.getText());
-        }
-      }
-    }
-    jmb.centerViewer(toshow);
-  }
-
-  @Override
-  public void closeViewer(boolean closeExternalViewer)
-  {
-    // Jmol does not use an external viewer
-    if (jmb != null)
-    {
-      jmb.closeViewer();
-    }
-    setAlignmentPanel(null);
-    _aps.clear();
-    _alignwith.clear();
-    _colourwith.clear();
-    // TODO: check for memory leaks where instance isn't finalised because jmb
-    // holds a reference to the window
-    jmb = null;
-  }
-
-  @Override
   public void run()
   {
     _started = true;
@@ -335,6 +303,8 @@ public class AppJmol extends StructureViewerBase
       } catch (Exception ex)
       {
         Cache.log.error("Couldn't open Jmol viewer!", ex);
+        ex.printStackTrace();
+        return;
       }
     }
     else
@@ -343,20 +313,23 @@ public class AppJmol extends StructureViewerBase
       cmd.append("loadingJalviewdata=true\nload APPEND ");
       cmd.append(filesString);
       cmd.append("\nloadingJalviewdata=null");
-      final String command = cmd.toString();
+      final StructureCommand command = new StructureCommand(cmd.toString());
       lastnotify = jmb.getLoadNotifiesHandled();
 
       try
       {
-        jmb.evalStateCommand(command);
+        jmb.executeCommand(command, false);
       } catch (OutOfMemoryError oomerror)
       {
         new OOMWarning("When trying to add structures to the Jmol viewer!",
                 oomerror);
         Cache.log.debug("File locations are " + filesString);
+        return;
       } catch (Exception ex)
       {
         Cache.log.error("Couldn't add files to Jmol viewer!", ex);
+        ex.printStackTrace();
+        return;
       }
     }
 
@@ -371,8 +344,10 @@ public class AppJmol extends StructureViewerBase
       try
       {
         Cache.log.debug("Waiting around for jmb notify.");
-        Thread.sleep(waitFor);
         waitTotal += waitFor;
+
+        // Thread.sleep() throws an exception in JS
+        Thread.sleep(waitFor);
       } catch (Exception e)
       {
       }
@@ -424,7 +399,7 @@ public class AppJmol extends StructureViewerBase
         }
         else
         {
-          alignStructs_withAllAlignPanels();
+          alignStructsWithAllAlignPanels();
         }
       }
     });
@@ -535,59 +510,39 @@ public class AppJmol extends StructureViewerBase
     return files;
   }
 
+  /**
+   * Outputs the Jmol viewer image as an image file, after prompting the user to
+   * choose a file and (for EPS) choice of Text or Lineart character rendering
+   * (unless a preference for this is set)
+   * 
+   * @param type
+   */
   @Override
-  public void eps_actionPerformed(ActionEvent e)
-  {
-    makePDBImage(jalview.util.ImageMaker.TYPE.EPS);
-  }
-
-  @Override
-  public void png_actionPerformed(ActionEvent e)
-  {
-    makePDBImage(jalview.util.ImageMaker.TYPE.PNG);
-  }
-
-  void makePDBImage(jalview.util.ImageMaker.TYPE type)
+  public void makePDBImage(ImageMaker.TYPE type)
   {
     int width = getWidth();
     int height = getHeight();
-
-    jalview.util.ImageMaker im;
-
-    if (type == jalview.util.ImageMaker.TYPE.PNG)
-    {
-      im = new jalview.util.ImageMaker(this,
-              jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from view",
-              width, height, null, null, null, 0, false);
-    }
-    else if (type == jalview.util.ImageMaker.TYPE.EPS)
-    {
-      im = new jalview.util.ImageMaker(this,
-              jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from view",
-              width, height, null, this.getTitle(), null, 0, false);
-    }
-    else
-    {
-
-      im = new jalview.util.ImageMaker(this,
-              jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
-              width, height, null, this.getTitle(), null, 0, false);
-    }
-
-    if (im.getGraphics() != null)
+    ImageWriterI writer = new ImageWriterI()
     {
-      jmb.jmolViewer.renderScreenImage(im.getGraphics(), width, height);
-      im.writeImage();
-    }
+      @Override
+      public void exportImage(Graphics g) throws Exception
+      {
+        jmb.jmolViewer.renderScreenImage(g, width, height);
+      }
+    };
+    String view = MessageManager.getString("action.view").toLowerCase();
+    ImageExporter exporter = new ImageExporter(writer,
+            getProgressIndicator(), type, getTitle());
+    exporter.doExport(null, this, width, height, view);
   }
 
   @Override
-  public void showHelp_actionPerformed(ActionEvent actionEvent)
+  public void showHelp_actionPerformed()
   {
     try
     {
-      BrowserLauncher
-              .openURL("http://jmol.sourceforge.net/docs/JmolUserGuide/");
+      BrowserLauncher // BH 2018
+              .openURL("http://wiki.jmol.org");//http://jmol.sourceforge.net/docs/JmolUserGuide/");
     } catch (Exception ex)
     {
       System.err.println("Show Jmol help failed with: " + ex.getMessage());
@@ -686,12 +641,6 @@ public class AppJmol extends StructureViewerBase
   }
 
   @Override
-  public String getStateInfo()
-  {
-    return jmb == null ? null : jmb.jmolViewer.getStateInfo();
-  }
-
-  @Override
   public ViewerType getViewerType()
   {
     return ViewerType.JMOL;