/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usda.weru.erosion;

import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class BlinkingButton extends JButton {
    private static final long serialVersionUID = 1L;

    private static Logger LOGGER = LogManager.getLogger(BlinkingButton.class);
    private Thread c_blinker;
    private long c_sleep = 250l; //That's 100 long, not 1001
    private Color c_blinkColor = Color.YELLOW.brighter();
    private boolean c_blinking;

	/**
	 *
	 * @return
	 */
	public long getSleep() {
        return c_sleep;
    }

	/**
	 *
	 * @param sleep
	 */
	public void setSleep(long sleep) {
        c_sleep = sleep;
    }

	/**
	 *
	 * @return
	 */
	public Color getBlinkColor() {
        return c_blinkColor;
    }

	/**
	 *
	 * @param color
	 */
	public void setBlinkColor(Color color) {
        c_blinkColor = color;
    }

	/**
	 *
	 * @return
	 */
	public boolean isBlinking() {
        return c_blinking;
    }

	/**
	 *
	 * @param blinking
	 */
	public void setBlinking(boolean blinking) {
        c_blinking = blinking;
        if (isBlinking()) {
            //Turn off the blink.
            
        /**
                 * Note:  Assertions are not enabled.  These will be useless items
                 * unless assertions are enabled.  Thus, they will be commented out unless
                 * the user wishes to enable insertions (feed the virtual machine the -ea
                 * argument.
                 */
//assert c_blinker == null : "Blinker thread is not null.";
            c_blinker = new Blinker();
            c_blinker.start();
        } else {
            //Turn off the blink
            if (c_blinker != null) {
                c_blinker.interrupt();
                c_blinker = null;
            }
        }
    }

    private class Blinker extends Thread {

        private Color c_previousColor;
        private Color c_oldColor;

        public Blinker() {
            super("Button Blinker");
        }

        @Override
        public void run() {
            c_oldColor = getBackground();
            c_previousColor = c_blinkColor;

            while (isBlinking()) {
                try {
                    Color temp = getBackground();
                    SwingUtilities.invokeAndWait(new Runnable() {

                        @Override
                        public void run() {
                            setBackground(c_previousColor);
                        }
                    });
                    c_previousColor = temp;
                    sleep(c_sleep);
                } catch (InterruptedException ex) {
                    //Leave the thread.
                    setBackground(c_oldColor);
                    return;
                } catch (InvocationTargetException ex) {
                    LOGGER.log(org.apache.logging.log4j.Level.ERROR, ex.getMessage());
                }
            }
        }
    }
}

