FileDocCategorySizeDatePackage
LinkButton.javaAPI DocExample7391Sun Dec 27 15:20:32 GMT 1998None

LinkButton

public class LinkButton extends Applet implements MouseListener
Synthetic button, not using Native toolkit. This is slightly fancier than the XButton* classes, but still does not do everything you might want in a button. It does both support actionListeners AND have a TARGET URL that it jumps to (after notifying the actionListeners).
author
Copyright 1995, 1997 Ian F. Darwin, ian@darwinsys.com, http://www.darwinsys.com.
version
$Id: LinkButton.java,v 1.1.1.1 1998/12/27 20:20:32 ian Exp $

Fields Summary
protected String
label
The label that is to appear in the button
protected String
imName
The name of the image
protected Image
im
The Image to display in the button
protected int
width
The width and height, based on the Image
protected int
height
protected Vector
l
The list of ActionListeners. Usually short, so a Vector is fine. TODO use new AwtMulticastBroadcaster
protected final int
MIN_PAD
Padding around the text for getMinimumSize()
protected final int
PREF_PAD
Padding around the text for getPreferredSize()
protected int
state
State: at first CALM; AWAKE when the mouse enters, ARMED if the mouse has been clicked but not yet released in our window; FIRED if activated,
public static final int
CALM
public static final int
AWAKE
public static final int
ARMED
public static final int
FIRED
String
target
The string form of the URL to jump to
URL
targetURL
The URL to jump to when the button is pushed.
protected String
fontName
The font name
protected int
fontSize
The font size
protected Font
textFont
The font itself
Constructors Summary
Methods Summary
public voidaddActionListener(java.awt.event.ActionListener listener)
Add an ActionListener to our list of listeners.

		l.addElement(listener);
	
protected voidfireButton()
The mouse was clicked, so notify all our ActionListeners.

		repaint();
		for (int i=0; i<l.size(); i++)
			((ActionListener)(l.elementAt(i))).actionPerformed(
				new ActionEvent(this,
					ActionEvent.ACTION_PERFORMED,
					label == null? "A LinkButton" : label));
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// nothing to do!
		}
		if (targetURL != null) {
			showStatus("Going to " + target);
			getAppletContext().showDocument(targetURL);
		}
	
public java.lang.String[][]getParameterInfo()
Give Parameter info the the AppletViewer, just for those writing HTML without hardcopy documentation :-)

		String info[][] = {
			{ "label",		"string",	"Text to display" },
			{ "fontname",	"name",		"Font to display it in" },
			{ "fontsize",	"10-30?",	"Size to display it at" },
			{ "image",		"img file", "Alternate image" },
			{ "target",		"URL",		"Where do you want to go tomorrow?" },
		};
		return info;
	
protected java.awt.DimensiongetStrSize(java.lang.String l)
Compute the size of a String

		if (textFont == null) 
			throw new IllegalArgumentException("No Font!");
		FontMetrics fm = getFontMetrics(textFont);
		return new Dimension(fm.stringWidth(l), fm.getAscent());
	
public voidinit()
Called from the browser to set up. We want to throw various kinds of exceptions but the API predefines that we don't, so we limit ourselves to the ubiquitous IllegalArgumentException.


	                              	 
	   
		String s;

		System.out.println("In LinkButton::init");
		try {
			if ((target = getParameter("target")) == null)
				throw new IllegalArgumentException("TARGET parameter REQUIRED");
			targetURL = new URL(target);
			if ((imName = getParameter("image")) != null)
				setImage(getImage(new URL(imName)));
		} catch (MalformedURLException rsi) {
			throw new IllegalArgumentException("MalformedURLException " +
				rsi.getMessage());
		}
		label = getParameter("label");

		// last-minute checking: must be an Image or text
		if (imName == null && label == null)
				throw new IllegalArgumentException("LABEL or IMAGE is REQUIRED");
		// Now handle font stuff.
		fontName = getParameter("fontname");
		if ((s = getParameter("fontsize")) != null)
			fontSize = Integer.parseInt(s);
		if (fontName != null || fontSize != 0) {
			System.out.println("Name " + fontName + ", size " + fontSize);
			textFont = new Font(fontName, Font.BOLD, fontSize);
		}
		
		// Applets don't do this; application components may:
		// setSize(getPreferredSize());

		// N.B. Must say we want to handle *mouse* events!
		addMouseListener(this);

		// set up the list of action handlers
		l = new Vector();
	
public voidmouseClicked(java.awt.event.MouseEvent e)
mouseClicked is defined by MouseListener, but not used here

	
public voidmouseEntered(java.awt.event.MouseEvent e)
Called by AWT when the mouse walks into our den.

		// System.out.println("mouseEntered");
		state = AWAKE;
		repaint();
		showStatus(target);
	
public voidmouseExited(java.awt.event.MouseEvent e)
Called by AWT when the mouse escapes from our den.

		// System.out.println("mouseExited");
		state = CALM;
		repaint();
		showStatus("");
	
public voidmousePressed(java.awt.event.MouseEvent e)

		state = ARMED;
		repaint();
	
public voidmouseReleased(java.awt.event.MouseEvent e)
The mouse was released in our window. If also pressed there, consider it a "click", and fire the event.

		// System.out.println("MouseUp");

		if (state==ARMED) {
			state = FIRED;
			fireButton();
		}
		state = CALM;
		repaint();
	
public voidpaint(java.awt.Graphics g)
Paint -- draw the text

		Dimension bd = getSize();
		// System.out.println("In paint, im=" + im + "; label=" + label);

		if (im != null) {
			g.drawImage(im, 0, 0, bd.width, bd.height, this);
			g.draw3DRect(0, 0, bd.width-2, bd.height-2, true);
			return;
		}

		switch(state) {
		case CALM:
			g.setColor(Color.black);
			break;
		case AWAKE:
			g.setColor(Color.blue);
			break;
		case ARMED:
			g.setColor(Color.red);
			break;
		case FIRED:
			g.setColor(Color.green);
			break;
		default:
			showStatus("LinkButton BUG: state=" + state);
		}

		// draw the box
		g.draw3DRect(0, 0, bd.width-2, bd.height-2, true);

		if (label == null)
			return;

		// Compute where to draw the text.
		g.setFont(textFont);
		Dimension td = getStrSize(label);
		int x = (bd.width-td.width)/2;
		if (x<0)	// what if text is too wide? Cram at left margin.
			x=0;
		int y = ((bd.height-td.height)/2)+td.height;
		// System.out.println("drawString("+label+", "+x+", "+y+");");

		// Now draw it.
		g.drawString(label, x, y);
		return;
	
public voidremoveActionListener(java.awt.event.ActionListener listener)
Remove an ActionListener; we'll no longer bother it.

		l.removeElement(listener);
	
public voidsetImage(java.awt.Image i)
Set the Image to appear. You must have done the appropriate (applet or applicatoin) form of getImage before this call.


	                    	 
	    
		im = i;
		MediaTracker mt = new MediaTracker(this);
		mt.addImage(im, 0);
		try {
			mt.waitForID(0);
		} catch(InterruptedException e) {
			System.err.println("Wonkey! INTR in waitForID!");
			return;
		}
		if (mt.isErrorID(0)) {
			throw new IllegalArgumentException("Couldn't load image");
		}
		width = im.getWidth(this);
		height = im.getHeight(this);
		setSize(width, height);
	
public voidstart()

		System.out.println("In LinkButton::start");