X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FDesktop.java;h=d357e4f6240e16bdd1565266badb3ea184f55ae1;hb=10866cba33b339de23e5a4e3121ab86c08b5ff2a;hp=d6ab3ae3ce7a3a04c61270089b363127a04a1794;hpb=3a124a6618aca04d87fa89a76d79cc03c4d45e47;p=jalview.git diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index d6ab3ae..d357e4f 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -59,11 +59,15 @@ 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; 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; @@ -326,19 +330,6 @@ public class Desktop extends jalview.jbgui.GDesktop implements showConsole(showjconsole); showNews.setVisible(false); - final Desktop me = this; - // Thread off the news reader, in case there are connection problems. - new Thread( new Runnable() { - @Override - public void run() - { - Cache.log.debug("Starting news thread."); - - jvnews = new BlogReader(me); - showNews.setVisible(true); - Cache.log.debug("Completed news thread."); - } - }).start(); this.addWindowListener(new WindowAdapter() { @@ -421,6 +412,22 @@ public class Desktop extends jalview.jbgui.GDesktop implements }); } + public void checkForNews() + { + final Desktop me = this; + // Thread off the news reader, in case there are connection problems. + addDialogThread(new Runnable() { + @Override + public void run() + { + Cache.log.debug("Starting news thread."); + + jvnews = new BlogReader(me); + showNews.setVisible(true); + Cache.log.debug("Completed news thread."); + } + }); + } protected void showNews_actionPerformed(ActionEvent e) { showNews(showNews.isSelected()); @@ -1038,6 +1045,10 @@ public class Desktop extends jalview.jbgui.GDesktop implements storeLastKnownDimensions("JALVIEW_RSS_WINDOW_", jvnews.getBounds()); } + if (dialogExecutor!=null) + { + dialogExecutor.shutdownNow(); + } System.exit(0); } @@ -2604,6 +2615,51 @@ public class Desktop extends jalview.jbgui.GDesktop implements } } + /** + * single thread that handles display of dialogs to user. + */ + 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 void addDialogThread(final Runnable prompter) + { + dialogExecutor.submit(new Runnable() + { + 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(); + } }