
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.awt.event.*;

/** A hypertext Link Class; adapted from:
    Rice & Salisbury, "Advanced Java 1.1 Programming", 1997, McGraw-hill

**/

public class HypertextLink extends Panel implements MouseListener {
    
    GraphicText text;      // internal object to use
    Applet      parent;    // the parent applet
    URL         url;
    String      urlString; // URL as a string
    Cursor      waitCursor;
    Cursor      handCursor;
    Cursor      defaultCursor;
    boolean     visited = false;
    Color       visitedColor = new Color( 0, 0, 80 );
    Color       unvisitedColor = Color.blue;
    String      frameName = null; 
                // for if we are displaying in an alternate frame
    Font        font = null;

    /** a new HypertextLink object......
     **/
    public HypertextLink( String urlString,
			  String frameName,
			  Applet applet ) throws MalformedURLException {
	this( new URL( urlString ), frameName, applet );
    }

    public HypertextLink( String urlString,
			  Applet applet ) throws MalformedURLException {
	this( new URL( urlString ), null, applet );
    }
	
    public HypertextLink( URL url,
			  Applet parent ) throws MalformedURLException {
	this( url, null, parent );
    }

    public HypertextLink( URL url,
			  String frameName,
			  Applet parent ) throws MalformedURLException {
	super();
	// register for mouse events
	addMouseListener( this );
	
	waitCursor = Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR );
	handCursor = Cursor.getPredefinedCursor( Cursor.HAND_CURSOR );
	defaultCursor = Cursor.getDefaultCursor( );

	int port; // the port the server is listening to

	// set up instance variables
	this.url = url;
	this.parent = parent;
	this.frameName = frameName;
	text = new GraphicText();
	text.setText("");
	// DEPRICATED
	// String fonts[] = Toolkit.getDefaultToolkit().getFontList();

	urlString = new String( url.getProtocol() + "://" + url.getHost());
	if ((port = url.getPort()) > 0) {
	    urlString = urlString + url.getFile();
	}
    }

    // set the font and text color
    public void addNotify() {
	super.addNotify();
	text.setBGColor( getBackground());
	if (font == null) {
	    setFont(getFont());
	}
    }

    // set the default color of the link
    public void setColor( Color color ) {
	unvisitedColor = color;
	text.setColor( unvisitedColor );
	repaint();
    }

    // set the "visited" color of the link
    public void setVisitedColor( Color color ) {
	visitedColor = color;
	text.setColor( color );
	repaint();
    }

    // set the font used by the link
    public void setFont( Font font ) {
	this.font = font;
	text.setFont( font );
	repaint();
    }

    // a mouse click happened. Change the cursor and visit the page.
    public void mouseClicked( MouseEvent evt ) {
	setCursor( waitCursor );

	if (frameName != null) {
	    parent.getAppletContext().showDocument( url, frameName );
	}
	else {
	    parent.getAppletContext().showDocument( url );
	}
	visited = true;
	setColor( visitedColor );
    }

    // entered the hypertext link
    public void mouseEntered( MouseEvent evt ) {
	setCursor( handCursor );
	parent.getAppletContext().showStatus( urlString );
    }

    // exited the hypertext link
    public void mouseExited( MouseEvent evt ) {
	setCursor( defaultCursor );
	parent.getAppletContext().showStatus( "" );
    }

    public void mousePressed( MouseEvent evt ) {}
    public void mouseReleased( MouseEvent evt ) {}

    public GraphicText getGraphicText() {
	return text;
    }

    public void paint( Graphics g) {
	g.drawImage( text.getAsImage( this, getSize()), 0, 0, this );
    }

    public void setText( String message ) {
	text.setText( message );
	repaint();
    }
    
}
	

			  
