/*
 * @(#)FrameBlinker.java
 *
 * Copyright 2001 Thomas P. Hayes, All Rights Reserved.
 *
 * This software is the proprietary information of Thomas P. Hayes.
 * Use outside the CSPP 535 course taught by the author is subject
 * to license agreement.
 */

package saveopen.presentation;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * A special-purpose Timer which can be used to make a Frame
 * blink on and off at a regular interval.  A distinct
 * FrameBlinker is needed for every Frame you wish to blink.
 * A number of experiments indicated that the original version
 * of this class, which called the Frame's hide() and show()
 * methods, interacted poorly with the enclosing window-managing
 * environment.  For instance, CTWM flashed the window, but first
 * moved it to virtual screen 1, and minimized it.  After some
 * thought, I decided that blinking the title was the best option.
 */
public class FrameBlinker extends javax.swing.Timer implements ActionListener {
    /** The Frame to be blinked. */
    protected Frame target;
    /** The current state of blinkitude. */
    protected boolean on;
    /** The original title of {@link #target}; the title blinks
        between this and "Selected Frame". */
    protected String targetTitle;
    
    /** 
     * Creates a new FrameBlinker, with target frame <code>targetFrame</code>.
     * The default delay is 500 milliseconds.
     */
    public FrameBlinker(Frame targetFrame) {
        super(500,null);
        target = targetFrame;
        addActionListener(this);
        on = false;
        targetTitle = target.getTitle();
    }
    
    /**
     * With every tick of the Timer, switches the frame title.
     */
    public void actionPerformed(ActionEvent e) {
        if (on) {
            on = false;
            target.setTitle("Selected Frame");
        } else {
            on = true;
            target.setTitle(targetTitle);
        }
    }
    
    /**
     * Makes the frame stop blinking and restores the original
     * frame title.
     */
    public void stop() {
        super.stop();
        target.setTitle(targetTitle);
    }

    /**
     * For test purposes.  Creates and displays a Frame, then blinks it.
     */    
    public static void main(String[] args) {
        System.out.println("Test code for class apt.presentation.FrameBlinker running...");
        JFrame f = new JFrame();
        f.getContentPane().add(new JButton("ButtonText"));
        f.setTitle("Demo Frame for FrameBlinker class");
        f.setSize(300,200);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }});
        FrameBlinker blinker = new FrameBlinker(f);
        blinker.start();
    }
    
}





