package gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A Jlabel that blinks (in an annoying way)
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class BlinkingLabel extends JLabel implements ActionListener
{
    private boolean           status;    
    private Color             textColor;    
    private Timer             timer;
    

    /**
     * Explicit Value Constructor
     */
    public BlinkingLabel(String text)
    {
       super(text);
       performCommonConstructionTasks();
    }

    /**
     * Explicit Value Constructor
     */
    public BlinkingLabel(String text, Icon icon, int horizontalAlignment)
    {
       super(text, icon, horizontalAlignment);
       performCommonConstructionTasks();
    }

    /**
     * Explicit Value Constructor
     */
    public BlinkingLabel(String text, int horizontalAlignment)
    {
       super(text, horizontalAlignment);
       performCommonConstructionTasks();
    }

    /**
     * Handle actionPerformed() message (required by ActionListener)
     *
     * @param evt  The ActionEvent that generated the message
     */
    public void actionPerformed(ActionEvent evt)
    {
       if (status) setForeground(textColor);
       else        setForeground(Color.RED);
       
       status = !status;
    }

    /**
     * Perform common construction tasks
     */
    private void performCommonConstructionTasks()
    {
       timer = new Timer(500, this);
       status = false;
    }
    
    /**
     * Start the blinking
     */
    public void start()
    {
       textColor = getForeground();
       timer.restart();
    }
    
    /**
     * Stop the blinking
     */
    public void stop()
    {
       timer.stop();
       setForeground(textColor);
    }
    
    
}
