/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.utils.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JComponent;

//import org.apache.log4j.Logger;

/**
 * This class is designed to drive users patient when a long process is running.
 * It is really awfull, by its implementation and by its drawing. Artists contributors
 * are welcome to re-implment it ;-))
 * @author chm
 */
public class JWaiter extends JComponent {
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 3256444694262394930L;
//    private static Logger logger = Logger.getLogger(JWaiter.class);
    public static final Color EXT_COLOR = Color.WHITE;
    public static final Color IN_COLOR = new Color(163,184,204);
    public static final int PAINT_MODE_NORMAL = 1;	// an empty rect
    public static final int PAINT_MODE_PROGRESS = 2;
    public static final int LEFT_TO_RIGHT = 1;
    public static final int RIGHT_TO_LEFT = 2;
    
    private int inPosition = 0;		// may change from 0 to 10
    private int inDirection = LEFT_TO_RIGHT;
    
    private int paintMode = PAINT_MODE_NORMAL;
    private Waiter waiter = null;
    
    public JWaiter() {
        super();
        setPreferredSize(new Dimension(75,16));
        setOpaque(true);
    }
    
    public void startProgress() {
        inPosition = 0;
        inDirection = LEFT_TO_RIGHT;
        paintMode = PAINT_MODE_PROGRESS;
        waiter = new Waiter();
        waiter.start();
    }
    
    private void pushProgress() {
        if(inDirection==LEFT_TO_RIGHT) {
            inPosition++;
            if(inPosition==10) inDirection = RIGHT_TO_LEFT;
        } else {
            inPosition--;
            if(inPosition==0) inDirection = LEFT_TO_RIGHT;
        }
        //paintImmediately(getBounds());
        Graphics g = getGraphics();
        g.setClip(0,0,getWidth(),getHeight());
        paintComponent(g);
    }

    public void stopProgress() {
        if(waiter!=null) {
            waiter.stop = true;
        }
        paintMode = PAINT_MODE_NORMAL;
        Graphics g = getGraphics();
        g.setClip(0,0,getWidth(),getHeight());
        paintComponent(g);
        waiter = null;
    }

    @Override
    protected void paintComponent(Graphics g) {
        //super.paintComponent(g);
        Rectangle rect = getBounds();
        if(paintMode==PAINT_MODE_NORMAL) {
            g.setColor(getBackground());
            g.fillRect(1,1,rect.width-2,rect.height-2);
        } else {
            int w = rect.width;
            int left = (inPosition*w/30);
            int right = (left + w/3);
            g.setColor(EXT_COLOR);
            g.fillRect(0,0,left,rect.height-1);
            g.fillRect(right,0,w-1,rect.height-1);
            g.setColor(IN_COLOR);
            g.fillRect(left+1,0,right-1,rect.height-1);
        }
        g.setColor(Color.DARK_GRAY);
        g.drawRect(0,0,rect.width-1,rect.height-1);
    }
    private class Waiter extends Thread {
        public boolean stop = false;
        public Waiter() {
            super("Waiter");
        }
        @Override
        public void run() {
            setPriority(MAX_PRIORITY);
            while(!stop) {
                pushProgress();
                try { 
                    sleep(100);
                } catch(InterruptedException iEx) {
                    stopProgress();
                }
            }
        }
    }
}
