import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.URL;

public class URLList extends Applet {

    List    ourList;
    boolean allowMultipleSelections = false;
    int     visibleSlots            = 3;
    Vector  urls;

    public String getAppletInfo() {
	return "URLList 1.0 example applet by Cynbe ru Taren: Public Domain";
    }

    public String [][] getParameterInfo() {
        String info[][] = {
	    { "visibleslots", "int",    "max slots visible at once" },
	    { "label1",       "string", "label for first url" },
	    { "url1",         "string", "first url" },
	    { "label2",       "string", "label for second url" },
	    { "url2",         "string", "second url" },
	};
	return info;
    }

    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() {

	visibleSlots = getIntParam( "visibleslots", visibleSlots );

	urls = new Vector();

        // Create list widget with specified entries:
        ourList = new List( visibleSlots, allowMultipleSelections );
	{   String url;
	    String tag;
            for (int i = 1;
                (tag = getParameter("label"+i)) != null   &&
                (url = getParameter("url"  +i)) != null;
		++i
            ) {
                ourList.addItem(tag);
	        try{
		    urls.addElement(new URL(url));
		} catch (Exception e) {
		    urls.addElement(null);
		};
	}   }
        add(ourList);
    }

    public boolean handleEvent( Event event ) {
        if (event.target == ourList) {
            if (event.id == Event.LIST_SELECT) {
		int    index = ourList.getSelectedIndex();
		URL    url   = (URL)urls.elementAt(index);
		try {
		    AppletContext a = getAppletContext();
		    if (a!=null && url!=null) {
			a.showDocument(url);
		    }
		} catch(Exception x){}
	        return true;
	    }
	}
	return false;
    }
}

