/*
    Go to a specified URL by clicking a button.
    Adapted from code by Mylène Reiners, CyberCenter RaboSite
 */

import java.awt.*;
import java.applet.*;
import java.net.URL;

public class URLButton extends Applet {

    int x    = 12;
    int y    = 12;
    int high = 26;
    int wide = 84;
    String label = "Visit Muq Docs";
    String url   = "http://muq.org/~cynbe/muq/muq.html";

    Button button1;

    public String getAppletInfo() {
	return "URLButton 1.0 example applet by Cynbe ru Taren: Public Domain";
    }

    public String [][] getParameterInfo() {
        String info[][] = {
	    { "x",        "int",                     "x start for string" },
	    { "y",        "int",                     "y start for string" },
	    { "high",     "int",                     "height of button"   },
	    { "wide",     "int",                     "width of button"    },
	    { "label",    "string",                  "text to display"    },
	    { "url",      "string",                  "url to visit"       }
	};
	return info;
    }

    private String getStrParam( String name, String defaultVal ) {
	String param = this.getParameter(name);
	if (param != null)   return param;
	return defaultVal;
    }

    private int getIntParam( String name, int defaultVal ) {
	String param = this.getParameter(name);
	if (param != null) {
	    try {
	        int i = Integer.parseInt( param );
		if (i >= 0)   return i;
	    } catch (NumberFormatException e) {
            }
	}
	return defaultVal;
    }

    public void init() {
        super.init();

	x      = getIntParam( "x",     x      );
	y      = getIntParam( "y",     y      );
	high   = getIntParam( "high",  high   );
	wide   = getIntParam( "wide",  wide   );
	label  = getStrParam( "label", label  );
	url    = getStrParam( "url",   url    );

        setLayout(null);
        button1 = new Button(label);
        button1.reshape( x, y, wide, high );
        add(button1);
    }

    void URLButton_Clicked(Event event) {
            
        // Visit the specified URL:
        try {
            URL u = new URL(url);
            AppletContext c = getAppletContext();
            if (c!=null)   c.showDocument(u);
        } catch(Exception url){}
    }       

    public boolean handleEvent(Event event) {
        if (event.target == button1
        &&  event.id     == Event.ACTION_EVENT
        ){
            URLButton_Clicked(event);
            return true;
        }
        return super.handleEvent(event);
    }
}

