JAL-1130 more robust loading code and different logos for desktop icon and splashscre...
[jalview.git] / src / jalview / gui / Desktop.java
index d1e8b6a..d357e4f 100644 (file)
@@ -59,6 +59,7 @@ import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Hashtable;
@@ -66,6 +67,7 @@ import java.util.StringTokenizer;
 import java.util.Vector;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.Semaphore;
 
 import javax.swing.DefaultDesktopManager;
 import javax.swing.DesktopManager;
@@ -2616,19 +2618,48 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   /**
    * single thread that handles display of dialogs to user.
    */
-  ExecutorService dialogExecutor=null;
+  ExecutorService dialogExecutor=Executors.newSingleThreadExecutor();
+  /**
+   * flag indicating if dialogExecutor should try to acquire a permit
+   */
+  private volatile boolean dialogPause=true;
+  /**
+   * pause the queue 
+   */
+  private java.util.concurrent.Semaphore block=new Semaphore(0);
   
   /**
    * add another dialog thread to the queue
    * @param prompter
    */
-  public synchronized void addDialogThread(Runnable prompter)
+  public void addDialogThread(final Runnable prompter)
   {
-    if (dialogExecutor==null)
+    dialogExecutor.submit(new Runnable()
     {
-      dialogExecutor = Executors.newSingleThreadExecutor();
-    }
-    dialogExecutor.submit(prompter);
+      public void run()
+      {
+        if (dialogPause) {
+          try { block.acquire(); } catch (InterruptedException x){};
+        }
+        if (instance==null)
+        {
+          return;
+        }
+        try
+        {
+          SwingUtilities.invokeAndWait(prompter);
+        } catch (Exception q)
+        {
+          Cache.log.warn("Unexpected Exception in dialog thread.", q);
+        }
+      }
+    });
   }
 
+  public void startDialogQueue()
+  {
+    // set the flag so we don't pause waiting for another permit and semaphore the current task to begin
+    dialogPause=false;
+    block.release();
+  }
 }