package be.msyben.vinylManager.tools;

import be.msyben.vinylManager.gui.WaitWindow;
import be.msyben.vinylManager.interfaces.WaitListener;

/**
 * Thread d'attente.
 * @author Mathieu Syben
 */
public class WaitThread extends Thread {

    private boolean running = false;
    private WaitWindow ww = null;
    
    /**
     * Le listener pour cancel.
     */
    private WaitListener waitListener = null;

    public WaitThread(){
        this(false);
    }
    public WaitThread(boolean showProgress){
        ww = new WaitWindow(showProgress);
    }

    /**
     * Ajoute un listener.
     * @param listener Le listener.
     */
    public void addWaitListener(WaitListener listener){
        ww.addWaitListener(listener);
    }

    /**
     * Démarrage
     */
    @Override
    public void run() {
        running = true;
        ww.setVisible(true);
        ww.toFront();
        if (waitListener == null) {
            ww.addWaitListener(waitListener);
        }
    }

    /**
     * Arret
     */
    public void arrete() {
        running = false;
        if (ww != null) {
            ww.dispose();
        }
        ww = null;
    }

    public boolean isRunning() {
        return running;
    }
    
    public void setProgressMax(int max) {
        if (ww != null) {
            ww.setProgressMax(max); 
        }
    }
    public void setProgressCurrent(String prefix, int current) {
        if (ww != null) {
            ww.setProgressCurrent(prefix, current); 
        }
    }
}