001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.progress;
003
004import java.util.concurrent.LinkedBlockingQueue;
005import java.util.concurrent.ThreadPoolExecutor;
006import java.util.concurrent.TimeUnit;
007
008import org.openstreetmap.josm.Main;
009import org.openstreetmap.josm.tools.Utils;
010
011/**
012 * Executor that displays the progress monitor to the user.
013 *
014 * Similar to Executors.newSingleThreadExecutor(), but displays the
015 * progress monitor whenever a new task is executed.
016 */
017public class ProgressMonitorExecutor extends ThreadPoolExecutor {
018
019    /**
020     * Creates a new {@code ProgressMonitorExecutor}
021     * @param nameFormat see {@link Utils#newThreadFactory(String, int)}
022     * @param threadPriority see {@link Utils#newThreadFactory(String, int)}
023     */
024    public ProgressMonitorExecutor(final String nameFormat, final int threadPriority) {
025        super(1, 1, 0L, TimeUnit.MILLISECONDS,
026                new LinkedBlockingQueue<Runnable>(),
027                Utils.newThreadFactory(nameFormat, threadPriority));
028    }
029
030    @Override
031    public void execute(Runnable command) {
032        if (Main.currentProgressMonitor != null) {
033            //TODO show only if this can't be in background or better if always in background is not checked
034            Main.currentProgressMonitor.showForegroundDialog();
035        }
036        super.execute(command);
037    }
038
039}