// URLMenu 1.1
import java.awt.*;
import java.applet.Applet;
import java.net.URL;
import java.util.*;
public class URLMenu extends Applet {
  // Some arrays and status variables
  URL[] URLs;
  String[] names,sites;
  boolean okay=false;
  int[] ycoords, selected;
  int oldindx=-1, oldstat=0,
      times=0;
  long thetime;
  public void init () {
    String stuff,site,name;
    int len,j,curY;
    // Set background color to white
    setBackground(Color.white);
    // Current Y position for the bottom of the name of the link to be drawn
    curY=20;
    // Number of targets
    len=0;
    // From 0 to infinite
    for (int i=0;;i++) {
      // Get parameter
      stuff=getParameter("target"+i);
      if (stuff==null) break; // It's null, we found the last one
      len++;                  // Otherwise, add one to the number of targets
    }
    /* Initialize our names, sites, URLs, bottom's of strings(on the screen),
       and which link is selected and how */
    names=new String[len];
    sites=new String[len];
    URLs=new URL[len];
    ycoords=new int[len];
    selected=new int[len];
    for (int i=0;i<len;i++) {
      // Get target
      stuff=getParameter("target"+i);
      // Get the index of the comma(our separator)
      j=stuff.indexOf(',');
      // The site URL comes before the comma
      site=stuff.substring(0,j);
      // And the name comes after
      name=stuff.substring(j+1,stuff.length());
      // Add the name and site to our lists
      names[i]=name;
      sites[i]=site;
      // It shouldn't be selected
      selected[i]=0;
      // Get a new URL object from our site and add it to the URL list
      try {
        URLs[i]=new URL(site);
      } catch (java.net.MalformedURLException e) {
        URLs[i]=null;
        sites[i] += ", MalformedURLException";
      }
      // Add base Y position
      ycoords[i]=curY;
      // And increment by 20
      curY += 20;
    }
    // If there were more than 0 targets, make the first target selected
    if (selected.length > 0) selected[0]=1;
    // Tell the paint() method it's okay to read all the arrays now
    okay=true;
  }
  public void paint (Graphics g) {
    // If it's not okay to read the arrays yet, return
    if (!okay) return;
    // newfont is for the links, smfont is for my notice
    Font newfont=new Font("Arial",Font.BOLD,25),
         smfont=new Font("Arial",Font.PLAIN,10);
    // Set font to the big one
    g.setFont(newfont);
    // Search the entire names[] array
    for (int i=0;i<names.length;i++) {
      // Is it selected normally?
      if (selected[i]==1)
        // Set color to blue
        g.setColor(Color.blue);
      // Is the mouse pointer held down on it?
      else if (selected[i]==2)
        // Set color to pink
        g.setColor(Color.magenta);
      else
        // Otherwise set color to red
        g.setColor(Color.red);
      // Draw it
      g.drawString(names[i],0,ycoords[i]);
    }
    // Do small font
    g.setFont(smfont);
    // Color: black
    g.setColor(Color.black);
    // Draw it on the bottom of the applet
    g.drawString("URLMenu v1.0 © 2000 Matt Arriola",0,size().height);
  }
  public void update(Graphics g) {
    // Paint
    paint(g);
  }
  public boolean mouseDown(Event evt, int x, int y) {
    // Get Graphics object for drawing
    Graphics g=this.getGraphics();
    long oldtime;
    int indx=-1;
    // Make big font
    Font newfont=new Font("Arial",Font.BOLD,25);
    // Set it
    g.setFont(newfont);
    /* Search for clicked link. If the pointer is below or equal to it's base,
       we've found the clicked one */
    for (int i=0;i<ycoords.length;i++)
      if (y <= ycoords[i]) { indx=i; break; }
    if (indx==-1) return false; // Nothing was found. Exit
    // Set color to magenta
    g.setColor(Color.magenta);
    // Draw it
    g.drawString(names[indx],0,ycoords[indx]);
    // Save old status and index
    oldstat=selected[indx];
    oldindx=indx;
    oldtime=thetime;
    thetime=System.currentTimeMillis();
    times++;
    if (thetime-oldtime > 500)
      times=0;
    System.out.println(times);
    selected[indx]=2;
    return true;
  }
  public boolean mouseUp(Event evt, int x, int y) {
    Graphics g=this.getGraphics();
    int indx=-1;
    Font newfont=new Font("Arial",Font.BOLD,25);
    g.setFont(newfont);
    for (int i=0;i<ycoords.length;i++)
      if (y <= ycoords[i]) { indx=i; break; }
    if (indx==-1) {
      if (oldindx != -1) { selected[oldindx]=oldstat; repaint(); }
      return false;
    }
    if (indx != oldindx) {
      selected[oldindx]=oldstat;
      oldindx=-1;
    } else {
      for (int i=0;i<selected.length;i++) selected[i]=0;
      selected[indx]=1;
    }
    // Show the site URL for the link
    getAppletContext().showStatus(sites[indx]);
    repaint();
    if (System.currentTimeMillis()-thetime <= 500 && times==1) {
      times=0;
      for (int i=0;i<selected.length;i++)
        if (selected[i]==1) { indx=i; break; }
      if (URLs[indx]==null) {
        getAppletContext().showStatus("There was a MalformedURLException on generating this URL");
      } else {
        getAppletContext().showStatus("Hang on a sec...");
        if (indx != -1) getAppletContext().showDocument(URLs[indx]);
      }
    }      
    return true;
  }
  public boolean mouseEnter(Event evt, int x, int y) {
    int indx=-1;
    for (int i=0;i<selected.length;i++)
      if (selected[i]==1) { indx=i; break; }
    if (indx != -1) getAppletContext().showStatus(sites[indx]);
    return true;
  }
}

