001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.progress.swing;
003
004import java.util.concurrent.LinkedBlockingQueue;
005import java.util.concurrent.ThreadPoolExecutor;
006import java.util.concurrent.TimeUnit;
007
008import org.openstreetmap.josm.tools.Utils;
009
010/**
011 * Executor that displays the progress monitor to the user.
012 *
013 * Similar to Executors.newSingleThreadExecutor(), but displays the
014 * progress monitor whenever a new task is executed.
015 * @since 12675 (moved from {@code gui.progress} package}
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 (PleaseWaitProgressMonitor.currentProgressMonitor != null) {
033            //TODO show only if this can't be in background or better if always in background is not checked
034            PleaseWaitProgressMonitor.currentProgressMonitor.showForegroundDialog();
035        }
036        super.execute(command);
037    }
038
039}