home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 7.1 KB | 246 lines |
-
- /*
- * ImageButtonApplet.java
- *
- * Copyright (C) 1996 Shaun Terry. All Rights Reserved.
- */
-
- package spt.applets;
-
- import java.io.FileNotFoundException;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Event;
- import java.awt.Image;
- import java.net.URL;
- import java.net.MalformedURLException;
- import java.applet.*;
-
- import spt.gui.ImageButton;
- import spt.gui.ImagePanel;
- import spt.applets.StdApplet;
- import spt.applets.AppletParamParser;
-
-
- /**
- * A button applet that displays an image and optional text label.
- * <p>
- *
- * <strong>Parameters</strong>
- * <pre>
- * IMG URL Button image (optional)
- * IMGSCALE float Scale the image to (1.0 = normal) (optional)
- * CLICKAUDIO URL Sound played when the button is pushed (optional)
- * TEXT String Button text (optional)
- * TEXTALIGN String Alignment of the text in relation to the image: LEFT|RIGHT|TOP|BOTTOM (optional)
- * FONT Font Font for the text: Family,Style,PointSize (optional)
- * ENABLED bool Start the button enable? TRUE|FALSE or 1|0 or YES|NO (optional)
- * SHOWBORDER bool Show the button's border? TRUE|FALSE or 1|0 or YES|NO (optional)
- * DRAWPUSHEDIN bool Change look when button depressed? TRUE|FALSE or 1|0 or YES|NO (optional)
- * PADDING int Pixel buffer around inside of button border (optional)
- * IMAGELABELGAP int Pixel buffer between image and text (optional)
- * TARGETURL URL URL to go to when button pushed (optional)
- * TARGETFRAME String Frame to display the TARGETURL in (see AppletContext.showDocument()) (optional)
- *
- * </pre>
- * Plus StdApplet parameters.
- *
- * @see spt.gui.ImageButton
- *
- * @author Shaun Terry
- */
-
- public class ImageButtonApplet extends StdApplet {
-
- ImageButton imgButton;
- private URL targetURL;
- private String targetFrame;
-
- public void init() {
- super.init();
-
- URL img=null;
- try
- img = paramParser.getURL("IMG");
- catch (MalformedURLException e) {
- System.err.println("Param(IMG) Error: " + e);
- return;
- }
-
- Image ip = null;
-
- if (img != null) {
- ip = ImagePanel.loadImage(this, img);
- }
-
- String text = paramParser.getParameter("TEXT");
-
- imgButton = new ImageButton(ip, text);
-
- setParams();
-
- add(imgButton);
- }
-
- void setParams() {
- setFontParam(this, imgButton);
- setClickAudioParam(this, imgButton);
- setTextAlignParam(this, imgButton);
- setImgScaleParam(this, imgButton);
- setShowBorderParam(this, imgButton);
- setDrawPushedInParam(this, imgButton);
- setPaddingParam(this, imgButton);
- setImgLabelGapParam(this, imgButton);
- setEnabledParam(this, imgButton);
-
- try {
- URL url = paramParser.getURL("TARGETURL");
- setTarget(url);
- }
- catch (MalformedURLException e) {
- System.err.println("Param(TARGETURL) Error: " + e);
- return;
- }
-
- String text;
-
- if ((text = getParameter("TARGETFRAME")) != null) {
- setTargetFrame(text);
- }
- }
-
- static public void setTextAlignParam(Applet app, ImageButton b) {
- String textAlign = app.getParameter("TEXTALIGN");
- if (textAlign != null) {
- if (textAlign.equalsIgnoreCase("LEFT")) b.setLabelPosition(ImageButton.LEFT);
- else if (textAlign.equalsIgnoreCase("RIGHT")) b.setLabelPosition(ImageButton.RIGHT);
- else if (textAlign.equalsIgnoreCase("TOP")) b.setLabelPosition(ImageButton.TOP);
- else if (textAlign.equalsIgnoreCase("BOTTOM")) b.setLabelPosition(ImageButton.BOTTOM);
- }
- }
-
- static public void setFontParam(Applet applet, ImageButton b) {
- AppletParamParser app = new AppletParamParser(applet);
-
- Font f;
- if ((f = app.getFont("FONT")) != null) b.setFont(f);
- }
-
- static public void setClickAudioParam(Applet applet, ImageButton b) {
- AppletParamParser app = new AppletParamParser(applet);
-
- URL url = null;
- try
- url = app.getURL("CLICKAUDIO");
- catch (MalformedURLException e) {
- System.err.println("Param(AUDIO) Error: " + e);
- return;
- }
-
- if (url != null) {
- AudioClip a = applet.getAudioClip(url);
- if (a != null) b.setAudioClip(a);
- }
- }
-
- static public void setImgScaleParam(Applet applet, ImageButton b) {
- AppletParamParser app = new AppletParamParser(applet);
-
- Float flt;
- if ((flt = app.getFloat("IMGSCALE")) != null) {
- b.setImageScale(flt.floatValue());
- }
- }
-
- static public void setShowBorderParam(Applet applet, ImageButton b) {
- AppletParamParser app = new AppletParamParser(applet);
-
- Boolean bool;
- if ((bool = app.getBoolean("SHOWBORDER")) != null) {
- b.setShowBorder(bool.booleanValue());
- }
- }
-
- static public void setDrawPushedInParam(Applet applet, ImageButton b) {
- AppletParamParser app = new AppletParamParser(applet);
-
- Boolean bool;
- if ((bool = app.getBoolean("DRAWPUSHEDIN")) != null) {
- b.setDrawPushedIn(bool.booleanValue());
- }
- }
-
- static public void setPaddingParam(Applet applet, ImageButton b) {
- AppletParamParser app = new AppletParamParser(applet);
-
- Integer i;
- if ((i = app.getInteger("PADDING")) != null) {
- b.setPadding(i.intValue());
- }
- }
-
- static public void setImgLabelGapParam(Applet applet, ImageButton b) {
- AppletParamParser app = new AppletParamParser(applet);
-
- Integer i;
- if ((i = app.getInteger("IMAGELABELGAP")) != null) {
- b.setImageLabelGap(i.intValue());
- }
- }
-
- static public void setEnabledParam(Applet applet, ImageButton b) {
- AppletParamParser app = new AppletParamParser(applet);
-
- Boolean bool;
- if ((bool = app.getBoolean("ENABLED")) != null) {
- if (!bool.booleanValue()) b.disable();
- }
- }
-
- public void setTarget(URL url) { targetURL = url; }
-
- public void setTargetFrame(String f) { targetFrame = f; }
-
- public ImageButton getImageButton() { return imgButton; }
-
- public boolean mouseEnter(Event e, int x, int y) {
- if (helpText != null) showStatus(helpText);
- else if (targetURL != null) showStatus(targetURL.toString());
- return true;
- }
-
- public boolean action(Event e, Object o) {
- if (targetURL != null) {
- AppletContext c = getAppletContext();
- imgButton.disable(); // Prevents lots of user clicks if
- // the doc loading is slow
- if (targetFrame != null) c.showDocument(targetURL, targetFrame);
- else c.showDocument(targetURL);
- imgButton.enable();
- }
- return true;
- }
-
- /** Parameters */
- public String[][] getParameterInfo() {
- String[][] info = {
- { "IMG", "URL", "Button image" },
- { "IMGSCALE", "float", "Scale the image to (1.0 = normal)" },
- { "CLICKAUDIO", "URL", "Sound played when the button is pushed" },
- { "TEXT", "String", "Button text" },
- { "TEXTALIGN", "String", "Alignment of the text in relation to the image: LEFT|RIGHT|TOP|BOTTOM" },
- { "FONT", "Font", "Font for the text: Family,Style,PointSize" },
- { "ENABLED", "boolean", "Start the button enable? TRUE|FALSE or 1|0 or YES|NO" },
- { "SHOWBORDER", "boolean", "Show the button's border? TRUE|FALSE or 1|0 or YES|NO" },
- { "DRAWPUSHEDIN", "boolean", "Change look when button depressed? TRUE|FALSE or 1|0 or YES|NO" },
- { "PADDING", "int", "Pixel buffer around inside of button border" },
- { "IMAGELABELGAP", "int", "Pixel buffer between image and text" },
- { "TARGETURL", "URL", "URL to go to when button pushed" },
- { "TARGETFRAME", "String", "Frame to display the TARGETURL in (see AppletContext.showDocument())" },
- };
-
- return mergeParameters(super.getParameterInfo(), info);
- }
- }
-
-