home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / dek17tau / classes / spt / applets / imagebuttonapplet.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  7.1 KB  |  246 lines

  1.  
  2. /*
  3.  * ImageButtonApplet.java
  4.  *
  5.  * Copyright (C) 1996 Shaun Terry. All Rights Reserved.
  6.  */
  7.  
  8. package spt.applets;
  9.  
  10. import java.io.FileNotFoundException;
  11. import java.awt.Color;
  12. import java.awt.Font;
  13. import java.awt.Event;
  14. import java.awt.Image;
  15. import java.net.URL;
  16. import java.net.MalformedURLException;
  17. import java.applet.*;
  18.  
  19. import spt.gui.ImageButton;
  20. import spt.gui.ImagePanel;
  21. import spt.applets.StdApplet;
  22. import spt.applets.AppletParamParser;
  23.  
  24.  
  25. /**
  26.  * A button applet that displays an image and optional text label.
  27.  * <p>
  28.  *
  29.  * <strong>Parameters</strong>
  30.  * <pre>
  31.  * IMG        URL    Button image (optional)
  32.  * IMGSCALE    float    Scale the image to (1.0 = normal) (optional)
  33.  * CLICKAUDIO    URL    Sound played when the button is pushed (optional)
  34.  * TEXT        String    Button text (optional)
  35.  * TEXTALIGN    String    Alignment of the text in relation to the image: LEFT|RIGHT|TOP|BOTTOM (optional)
  36.  * FONT        Font    Font for the text: Family,Style,PointSize (optional)
  37.  * ENABLED        bool    Start the button enable? TRUE|FALSE or 1|0 or YES|NO (optional)
  38.  * SHOWBORDER    bool    Show the button's border? TRUE|FALSE or 1|0 or YES|NO (optional)
  39.  * DRAWPUSHEDIN    bool    Change look when button depressed? TRUE|FALSE or 1|0 or YES|NO (optional)
  40.  * PADDING        int    Pixel buffer around inside of button border (optional)
  41.  * IMAGELABELGAP    int    Pixel buffer between image and text (optional)
  42.  * TARGETURL    URL    URL to go to when button pushed (optional)
  43.  * TARGETFRAME    String    Frame to display the TARGETURL in (see AppletContext.showDocument()) (optional)
  44.  *
  45.  * </pre>
  46.  * Plus StdApplet parameters.
  47.  *
  48.  * @see spt.gui.ImageButton
  49.  *
  50.  * @author Shaun Terry
  51.  */
  52.  
  53. public class ImageButtonApplet extends StdApplet {
  54.  
  55.     ImageButton imgButton;
  56.     private URL targetURL;
  57.     private String targetFrame;
  58.  
  59.     public void init() {
  60.         super.init();
  61.  
  62.         URL img=null;
  63.         try
  64.             img = paramParser.getURL("IMG");
  65.         catch (MalformedURLException e) {
  66.             System.err.println("Param(IMG) Error: " + e);
  67.             return;
  68.         }
  69.  
  70.         Image ip = null;
  71.  
  72.         if (img != null) {
  73.             ip = ImagePanel.loadImage(this, img);
  74.         }
  75.  
  76.         String text = paramParser.getParameter("TEXT");
  77.  
  78.         imgButton = new ImageButton(ip, text);
  79.  
  80.         setParams();
  81.  
  82.         add(imgButton);
  83.     }
  84.  
  85.     void setParams() {
  86.         setFontParam(this, imgButton);
  87.         setClickAudioParam(this, imgButton);
  88.         setTextAlignParam(this, imgButton);
  89.         setImgScaleParam(this, imgButton);
  90.         setShowBorderParam(this, imgButton);
  91.         setDrawPushedInParam(this, imgButton);
  92.         setPaddingParam(this, imgButton);
  93.         setImgLabelGapParam(this, imgButton);
  94.         setEnabledParam(this, imgButton);
  95.  
  96.         try {
  97.             URL url = paramParser.getURL("TARGETURL");
  98.             setTarget(url);
  99.         }
  100.         catch (MalformedURLException e) {
  101.             System.err.println("Param(TARGETURL) Error: " + e);
  102.             return;
  103.         }
  104.  
  105.         String text;
  106.  
  107.         if ((text = getParameter("TARGETFRAME")) != null) {
  108.             setTargetFrame(text);
  109.         }
  110.     }
  111.  
  112.     static public void setTextAlignParam(Applet app, ImageButton b) {
  113.         String textAlign = app.getParameter("TEXTALIGN");
  114.         if (textAlign != null) {
  115.             if (textAlign.equalsIgnoreCase("LEFT")) b.setLabelPosition(ImageButton.LEFT);
  116.             else if (textAlign.equalsIgnoreCase("RIGHT")) b.setLabelPosition(ImageButton.RIGHT);
  117.             else if (textAlign.equalsIgnoreCase("TOP")) b.setLabelPosition(ImageButton.TOP);
  118.             else if (textAlign.equalsIgnoreCase("BOTTOM")) b.setLabelPosition(ImageButton.BOTTOM);
  119.         }
  120.     }
  121.  
  122.     static public void setFontParam(Applet applet, ImageButton b) {
  123.         AppletParamParser app = new AppletParamParser(applet);
  124.  
  125.         Font f;
  126.         if ((f = app.getFont("FONT")) != null) b.setFont(f);
  127.     }
  128.  
  129.     static public void setClickAudioParam(Applet applet, ImageButton b) {
  130.         AppletParamParser app = new AppletParamParser(applet);
  131.  
  132.         URL url = null;
  133.         try
  134.             url = app.getURL("CLICKAUDIO");
  135.         catch (MalformedURLException e) {
  136.             System.err.println("Param(AUDIO) Error: " + e);
  137.             return;
  138.         }
  139.  
  140.         if (url != null) {
  141.             AudioClip a = applet.getAudioClip(url);
  142.             if (a != null) b.setAudioClip(a);
  143.         }
  144.     }
  145.  
  146.     static public void setImgScaleParam(Applet applet, ImageButton b) {
  147.         AppletParamParser app = new AppletParamParser(applet);
  148.  
  149.         Float flt;
  150.         if ((flt = app.getFloat("IMGSCALE")) != null) {
  151.             b.setImageScale(flt.floatValue());
  152.         }
  153.     }
  154.  
  155.     static public void setShowBorderParam(Applet applet, ImageButton b) {
  156.         AppletParamParser app = new AppletParamParser(applet);
  157.  
  158.         Boolean bool;
  159.         if ((bool = app.getBoolean("SHOWBORDER")) != null) {
  160.             b.setShowBorder(bool.booleanValue());
  161.         }
  162.     }
  163.  
  164.     static public void setDrawPushedInParam(Applet applet, ImageButton b) {
  165.         AppletParamParser app = new AppletParamParser(applet);
  166.  
  167.         Boolean bool;
  168.         if ((bool = app.getBoolean("DRAWPUSHEDIN")) != null) {
  169.             b.setDrawPushedIn(bool.booleanValue());
  170.         }
  171.     }
  172.  
  173.     static public void setPaddingParam(Applet applet, ImageButton b) {
  174.         AppletParamParser app = new AppletParamParser(applet);
  175.  
  176.         Integer i;
  177.         if ((i = app.getInteger("PADDING")) != null) {
  178.             b.setPadding(i.intValue());
  179.         }
  180.     }
  181.  
  182.     static public void setImgLabelGapParam(Applet applet, ImageButton b) {
  183.         AppletParamParser app = new AppletParamParser(applet);
  184.  
  185.         Integer i;
  186.         if ((i = app.getInteger("IMAGELABELGAP")) != null) {
  187.             b.setImageLabelGap(i.intValue());
  188.         }
  189.     }
  190.  
  191.     static public void setEnabledParam(Applet applet, ImageButton b) {
  192.         AppletParamParser app = new AppletParamParser(applet);
  193.  
  194.         Boolean bool;
  195.         if ((bool = app.getBoolean("ENABLED")) != null) {
  196.             if (!bool.booleanValue()) b.disable();
  197.         }
  198.     }
  199.  
  200.     public void setTarget(URL url) { targetURL = url; }
  201.  
  202.     public void setTargetFrame(String f) { targetFrame = f; }
  203.  
  204.     public ImageButton getImageButton() { return imgButton; }
  205.  
  206.     public boolean mouseEnter(Event e, int x, int y) {
  207.         if (helpText != null) showStatus(helpText);
  208.         else if (targetURL != null) showStatus(targetURL.toString());
  209.         return true;
  210.     }
  211.  
  212.     public boolean action(Event e, Object o) {
  213.         if (targetURL != null) {
  214.             AppletContext c = getAppletContext();
  215.             imgButton.disable();    // Prevents lots of user clicks if
  216.                                     // the doc loading is slow
  217.             if (targetFrame != null) c.showDocument(targetURL, targetFrame);
  218.             else c.showDocument(targetURL);
  219.             imgButton.enable();
  220.         }
  221.         return true;
  222.     }
  223.  
  224.     /** Parameters */
  225.     public String[][] getParameterInfo() {
  226.         String[][] info = {
  227.             { "IMG",             "URL",         "Button image" },
  228.             { "IMGSCALE",        "float",     "Scale the image to (1.0 = normal)" },
  229.             { "CLICKAUDIO",     "URL",         "Sound played when the button is pushed" },
  230.             { "TEXT",             "String",     "Button text" },
  231.             { "TEXTALIGN",        "String",     "Alignment of the text in relation to the image: LEFT|RIGHT|TOP|BOTTOM" },
  232.             { "FONT",             "Font",     "Font for the text: Family,Style,PointSize" },
  233.             { "ENABLED",         "boolean",     "Start the button enable? TRUE|FALSE or 1|0 or YES|NO" },
  234.             { "SHOWBORDER",        "boolean",     "Show the button's border? TRUE|FALSE or 1|0 or YES|NO" },
  235.             { "DRAWPUSHEDIN",    "boolean",     "Change look when button depressed? TRUE|FALSE or 1|0 or YES|NO" },
  236.             { "PADDING",        "int",         "Pixel buffer around inside of button border" },
  237.             { "IMAGELABELGAP",    "int",         "Pixel buffer between image and text" },
  238.             { "TARGETURL",        "URL",         "URL to go to when button pushed" },
  239.             { "TARGETFRAME",    "String",     "Frame to display the TARGETURL in (see AppletContext.showDocument())" },
  240.         };
  241.  
  242.         return mergeParameters(super.getParameterInfo(), info);
  243.     }
  244. }
  245.  
  246.