package be.nikiroo.utils.ui; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.TreeSet; import javax.swing.SwingWorker; /** * This class helps you delay some graphical actions and execute the most recent * ones when under contention. *
* How does it work? *
* Can be called even if already paused, will just do nothing in that * context. */ public void pause() { paused = true; } /** * Check if the {@link DelayWorker} is currently paused. * * @return TRUE if it is */ public boolean isPaused() { return paused; } /** * Resume the system after a pause. *
* Can be called even if already running, will just do nothing in that * context. */ public void resume() { synchronized (waiter) { paused = false; wakeup(); } } /** * Stop the system. *
* Note: this is final, you MUST NOT call {@link DelayWorker#start()} * a second time (but see {@link DelayWorker#pause()} and * {@link DelayWorker#resume()} instead). */ public void stop() { synchronized (waiter) { cont = false; wakeup(); } } /** * Clear all the processes that were put on the queue but not yet scheduled * to be executed -- note that it will still continue on the processes * currently scheduled to run. */ public void clear() { synchronized (lazyEnCoursLock) { lazyEnCours.clear(); wip.clear(); } } /** * Put a new process in the delay queue. * * @param id * the ID of this process (if you want to skip workers when they * are superseded by a new one, you need to use the same ID key) * @param worker * the process to delay */ public void delay(String id, SwingWorker worker) { synchronized (lazyEnCoursLock) { lazyEnCours.put(id, worker); } wakeup(); } /** * Wake up the loop thread. */ private void wakeup() { synchronized (waiter) { waiter.notifyAll(); } } }