/**
 File:      KaiButtonApplet.java
 Author:    Angus McIntyre <angus@pobox.com>
 Date:      10.06.96
 Updated:   11.06.99
 
 Variant of KaiButton that takes a URL as an argument, and tells the parent
 browser to go to that URL when clicked.

    HISTORY
    -------
    
    11.06.99	Documented. Removed 'getDirectoryURL' method, which is
    			defined by the 'ExtendedApplet' superclass.
    05.07.98	Used BorderLayout to overcome button placement problems.
    			Added code to set background color so that it can be
    			used by contained buttons. Made KaiButtonApplet inherit
    			from ExtendedApplet so that I could get parameter
    			parsing. 
    26.08.96    First release
    
    TO-DO
    -----
    
    Figure out why the browser insists on drawing images 5 pixels down
    from their correct location, and see if there's a workaround I can
    implement that won't be too baroque. Until this is fixed, simply
    specify the HEIGHT of the applet as being 5 pixels taller than it
    needs to be.
    
    LEGAL
    -----
    
    This software is free. It can be used and modified in any way you 
    choose, but it may not be sold, either separately or as part of a 
    collection without explicit prior permission from the author. The 
    author assumes no liability for any loss, damage or mental or 
    physical trauma you may incur through use of or inability to use 
    this software. This disclaimer must appear on any modified or 
    unmodified version of the software in which the name of the author
    also appears.
    
**/

/* ----------------------------------------------------------------------
 *                              IMPORTS
 * ---------------------------------------------------------------------- */

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.util.*;
import WebKaiButton;
import ExtendedApplet;

/**
 * Class of applets designed to contain a single clickable button.
 */
 
public class KaiButtonApplet extends ExtendedApplet {
    protected   WebKaiButton    button;
    
	/**
     * Set up the applet. All the image downloading, URL checking etc. is done
     * here, because the button itself isn't smart enough to do any of it.
     */
     
    public  void init() {
        URL             docBase = getDirectoryURL(this.getDocumentBase());
        URL             buttonURL;
        String          buttonURLString;
        Image           inactiveImage,activeImage,clickedImage;
        MediaTracker    tracker = new MediaTracker(this);
        Color			background_color;
        Color			default_background_color;
        Component		browser;

        // First, get all the images. We use a MediaTracker object for this
        // to ensure that we don't start drawing before we're completely
        // ready. Each image required is added to the tracker for monitoring.
        
        inactiveImage = this.getImage(docBase,this.getParameter("inactive"));
        tracker.addImage(inactiveImage,0);
        activeImage = this.getImage(docBase,this.getParameter("active"));
        tracker.addImage(activeImage,1);
        clickedImage = this.getImage(docBase,this.getParameter("clicked"));
        tracker.addImage(clickedImage,2);
    
        // Let the user know that this is likely to take a while, and wait
        // for the tracker to confirm that all the images are present and
        // correct.
        
        showStatus("Waiting for button images");
        try {
            tracker.waitForAll();
        } catch (InterruptedException e) {
            showStatus("Image loading interrupted");
        }

        // Next we have to try to do something with the URL. We'll begin
        // by assuming that it's relative to the document base. If that
        // fails to produce a valid URL, then we can try to interpret it
        // as a complete URL in its own right. If that fails, we print a
        // warning on the console, but don't otherwise worry about it.
        
        buttonURL = null;
        buttonURLString = this.getParameter("url");
        try {
            buttonURL = new URL(docBase,buttonURLString);
        } catch (MalformedURLException e1) {
            try {
                buttonURL = new URL(buttonURLString);
            }
            catch (MalformedURLException e2) {
                System.err.println("Warning: malformed URL " + buttonURLString);
            }
        }

		// Set up the background color for this item
		
		browser = this.getParent();
		if (browser != null)
			default_background_color = browser.getBackground();
		else
			default_background_color = Color.gray;
		background_color = 
         this.parseColorParameter("background-color",
                                  default_background_color);
		this.setBackground(background_color);

        // Now, create the button, and add it to the applet.
            
        button = new WebKaiButton(inactiveImage,activeImage,clickedImage,
                            	  bounds().x,bounds().y,
                                  bounds().width,bounds().height,
                                  buttonURL);
        this.setLayout(new BorderLayout(0,0));
        this.add("Center",button);
    }
    
}


