import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.URL;

public class URLChoice extends Applet {

    Button goButton;
    Choice ourChoice;
    Vector urls;
    URL currentUrl;    

    public String getAppletInfo() {
	return "URLChoice 1.0 example applet by Cynbe ru Taren: Public Domain";
    }

    public String [][] getParameterInfo() {
        String info[][] = {
	    { "label1", "string", "label for first url" },
	    { "url1",   "string", "first url" },
	    { "label2", "string", "label for second url" },
	    { "url2",   "string", "second url" },
	};
	return info;
    }

    public void init() {
	super.init();

        setLayout(null);

	urls = new Vector();

	// Add a button to fetch chosen page:
        goButton = new java.awt.Button("Go");
        goButton.reshape(180,5,30,20);
        add(goButton);

        // Create choice widget with specified entries:
        ourChoice = new Choice();
	{   String url;
	    String tag;
            for (int i = 1;
                (tag = getParameter("label"+i)) != null   &&
                (url = getParameter("url"  +i)) != null;
		++i
            ) {
                ourChoice.addItem (tag);
	        try{
		    urls.addElement(new URL(url));
		} catch (Exception e) {
		    urls.addElement(null);
		};
	}   }
        ourChoice.reshape( 12, 4, 160, 20 );
        add(ourChoice);

	currentUrl = (URL) urls.elementAt(0);
    }

    public boolean action(Event e, Object o) {

        // Handle choice events:
        if (e.target == ourChoice) {
	    // Note change in selected URL:
            int    index = ourChoice.getSelectedIndex();
            currentUrl   = (URL)urls.elementAt(index);
            return true;
        }

        // Handle button events:
        if (e.target == goButton
        &&  e.id     == Event.ACTION_EVENT
        ){
	    try {
		AppletContext a = getAppletContext();
		if (a!=null && currentUrl!=null) {
		    a.showDocument(currentUrl);
		}
	    } catch(Exception x){}
            return true;
	}

        return false;
    }
}

