001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.util;
025    
026    import java.awt.event.ActionEvent;
027    import java.awt.event.ActionListener;
028    import java.util.concurrent.Future;
029    import java.util.concurrent.LinkedBlockingQueue;
030    import java.util.concurrent.ThreadPoolExecutor;
031    import java.util.concurrent.TimeUnit;
032    import java.util.logging.Level;
033    import java.util.logging.Logger;
034    
035    import javax.swing.event.EventListenerList;
036    
037    import org.ujmp.gui.actions.ObjectAction;
038    
039    public abstract class TaskQueue {
040    
041            private static final Logger logger = Logger.getLogger(TaskQueue.class.getName());
042    
043            private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
044                            new LinkedBlockingQueue<Runnable>());
045    
046            private static double progress = 1.0;
047    
048            private static EventListenerList listenerList = new EventListenerList();
049    
050            private static String status = "";
051    
052            public static final String getStatus() {
053                    if (getWaitingCount() == 0 && getProgress() == 1.0) {
054                            return "Ready";
055                    } else if (getWaitingCount() == 0 && getProgress() != 1.0) {
056                            return status;
057                    } else {
058                            return status + " (" + getWaitingCount() + " tasks waiting)";
059                    }
060            }
061    
062            public static final Future<?> submit(ObjectAction c) {
063                    logger.log(Level.INFO, "New task added: " + c);
064                    fireActionPerformed(new ActionEvent(c, 0, c.toString()));
065                    return executor.submit(c);
066            }
067    
068            public static void setStatus(String s) {
069                    status = s;
070                    logger.log(Level.FINER, "Status: " + s);
071            }
072    
073            public static final void setProgress(double p) {
074                    progress = p;
075                    logger.log(Level.FINER, "Progress: " + p);
076            }
077    
078            public static final double getProgress() {
079                    return progress;
080            }
081    
082            public static final int getWaitingCount() {
083                    return executor.getQueue().size();
084            }
085    
086            public static final void addActionListener(ActionListener l) {
087                    listenerList.add(ActionListener.class, l);
088            }
089    
090            public static final void removeActionListener(ActionListener l) {
091                    listenerList.remove(ActionListener.class, l);
092            }
093    
094            public static void fireActionPerformed(ActionEvent e) {
095                    for (Object o : listenerList.getListenerList()) {
096                            if (o instanceof ActionListener) {
097                                    ((ActionListener) o).actionPerformed(e);
098                            }
099                    }
100            }
101    
102            public static void submit(Runnable runnable) {
103                    executor.submit(runnable);
104            }
105    
106    }