LinkButtonpublic class LinkButton extends Applet implements MouseListenerSynthetic 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). |
Fields Summary |
---|
protected String | labelThe label that is to appear in the button | protected String | imNameThe name of the image | protected Image | imThe Image to display in the button | protected int | widthThe width and height, based on the Image | protected int | height | protected Vector | lThe list of ActionListeners. Usually short, so a Vector is fine.
TODO use new AwtMulticastBroadcaster | protected final int | MIN_PADPadding around the text for getMinimumSize() | protected final int | PREF_PADPadding around the text for getPreferredSize() | protected int | stateState: 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 | targetThe string form of the URL to jump to | URL | targetURLThe URL to jump to when the button is pushed. | protected String | fontNameThe font name | protected int | fontSizeThe font size | protected Font | textFontThe font itself |
Methods Summary |
---|
public void | addActionListener(java.awt.event.ActionListener listener)Add an ActionListener to our list of listeners.
l.addElement(listener);
| protected void | fireButton()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.Dimension | getStrSize(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 void | init()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 void | mouseClicked(java.awt.event.MouseEvent e)mouseClicked is defined by MouseListener, but not used here
| public void | mouseEntered(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 void | mouseExited(java.awt.event.MouseEvent e)Called by AWT when the mouse escapes from our den.
// System.out.println("mouseExited");
state = CALM;
repaint();
showStatus("");
| public void | mousePressed(java.awt.event.MouseEvent e)
state = ARMED;
repaint();
| public void | mouseReleased(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 void | paint(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 void | removeActionListener(java.awt.event.ActionListener listener)Remove an ActionListener; we'll no longer bother it.
l.removeElement(listener);
| public void | setImage(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 void | start()
System.out.println("In LinkButton::start");
|
|