001/*
002 * Copyright (c) 2012 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-07-19 19:19:59 +0000 (Thu, 19 Jul 2012) $' 
007 * '$Revision: 30239 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029package org.kepler.util;
030
031import java.util.LinkedList;
032import java.util.Queue;
033
034import javax.swing.SwingUtilities;
035
036import ptolemy.util.MessageHandler;
037
038/** A queue that executes Runnables.
039 * 
040 *  @author Daniel Crawl
041 *  @version $Id: RunnableExecutionQueue.java 30239 2012-07-19 19:19:59Z crawl $
042 */
043
044public class RunnableExecutionQueue {
045
046    /** Submit a Runnable for execution. */
047    public static void submit(Runnable runnable) {
048        _queue.add(new QueueItem(runnable, false));
049    }
050    
051    /** Submit a Runnable for execution in the Swing Thread. */
052    public static void submitToSwing(Runnable runnable) {
053        _queue.add(new QueueItem(runnable, true));
054    }
055    
056    /** Execute all the submitted Runnables. */
057    public static void execute() {
058        QueueItem item;
059        while((item = _queue.poll()) != null) {
060            
061            if(item.swing) {
062                try {
063                    SwingUtilities.invokeAndWait(item.r);
064                } catch (Exception e) {
065                    MessageHandler.error("Error executing runnable in Swing Thread.", e);
066                }
067            } else {
068                item.r.run();
069            }
070        }
071    }
072    
073    /** An item for the queue. */
074    private static class QueueItem {
075        
076        QueueItem(Runnable r, boolean swing) {
077            this.r = r;
078            this.swing = swing;
079        }
080        
081        public Runnable r;
082        public boolean swing;
083    }
084    
085    /** A queue of items to be executed. */
086    private static Queue<QueueItem> _queue = new LinkedList<QueueItem>(); 
087}