JAL-4217 wrap imageExport CLI method's call to imageExport in an invokeAndWait
authorJames Procter <j.procter@dundee.ac.uk>
Thu, 6 Jul 2023 14:23:29 +0000 (15:23 +0100)
committerJames Procter <j.procter@dundee.ac.uk>
Thu, 6 Jul 2023 14:23:29 +0000 (15:23 +0100)
src/jalview/gui/AppJmol.java

index 1758b5b..a14fabb 100644 (file)
@@ -28,6 +28,7 @@ import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
 import java.io.File;
+import java.lang.reflect.InvocationTargetException;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -472,11 +473,49 @@ public class AppJmol extends StructureViewerBase
     };
     String view = MessageManager.getString("action.view")
             .toLowerCase(Locale.ROOT);
-    ImageExporter exporter = new ImageExporter(writer,
+    final ImageExporter exporter = new ImageExporter(writer,
             getProgressIndicator(), type, getTitle());
-    
-    exporter.doExport(file, this, width, height, view, renderer, userBis);
-    
+    // hack to pass back and throw to caller
+    final Throwable[] exceptions = new Throwable[1];
+    exceptions[0] = null;
+    final AppJmol us = this;
+    try
+    {
+      SwingUtilities.invokeAndWait(new Runnable()
+      {
+        @Override
+        public void run()
+        {
+          try
+          {
+            exporter.doExport(file, us, width, height, view, renderer,
+                    userBis);
+          } catch (Throwable t)
+          {
+            exceptions[0] = t;
+          }
+        }
+      });
+    } catch (InvocationTargetException e)
+    {
+      throw new ImageOutputException(
+              "Unexpected error when generating image", e);
+    } catch (InterruptedException e)
+    {
+      Console.debug("Interrupted whilst waiting for image export", e);
+    }
+    if (exceptions[0] != null)
+    {
+      if (exceptions[0] instanceof ImageOutputException)
+      {
+        throw ((ImageOutputException) exceptions[0]);
+      }
+      else
+      {
+        throw new ImageOutputException(
+                "Unexpected error when generating image", exceptions[0]);
+      }
+    }
   }
 
   @Override