home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 7.0 KB | 278 lines |
-
- /*
- * AppletParamParser.java
- *
- * Copyright (C) 1996 Shaun Terry. All Rights Reserved.
- */
-
- package spt.applets;
-
- import java.lang.String;
- import java.lang.Boolean;
- import java.util.StringTokenizer;
- import java.awt.Font;
- import java.awt.Color;
- import java.net.URL;
- import java.net.MalformedURLException;
- import java.applet.*;
-
- import spt.gui.BorderedComponent;
- import spt.util.StringUtil;
-
-
- /**
- * A utility class that knows how to parse a wide variety of applet
- * parameters (int, Colors, URLs, etc.)
- *
- * @author Shaun Terry
- */
-
- public class AppletParamParser {
-
- Applet applet;
-
- public AppletParamParser(Applet a) { applet = a; }
-
- /** Was the parameter supplied?
- */
- public boolean parameterExists(String name) {
- return applet.getParameter(name) != null;
- }
-
- /** Get the value of a parameter in String format. Returns
- * null if no parameter exists. (Really just calls
- * the applet version of the function).
- * @see java.applet.Applet#getParameter
- */
- public String getParameter(String name) {
- return applet.getParameter(name);
- }
-
- /** Get a String parameter or return a default value if it
- * doesn't exist.
- */
- public String getParameter(String name, String defaultVal) {
- String s = getParameter(name);
- return s != null ? s : defaultVal;
- }
-
- /** Get a Boolean parameter. A true value is the string "TRUE",
- * "YES" or "1", all case insensitive. False is anything else.
- * null is returned if it doesn't exist.
- * @see java.lang.Boolean
- */
- public Boolean getBoolean(String name) {
- String s = getParameter(name);
- if (s == null) return null;
-
- if (s.equalsIgnoreCase("TRUE") ||
- s.equalsIgnoreCase("YES") || s.equalsIgnoreCase("1"))
- return new Boolean(true);
- else return new Boolean(false);
- }
-
- /** Return a Boolean or default value if it doesn't exist.
- */
- public Boolean getBoolean(String name, boolean defaultVal) {
- Boolean b = getBoolean(name);
- return b != null ? b : new Boolean(defaultVal);
- }
-
- /** Get an Integer parameter. null is returned if it doesn't exist.
- * @see java.lang.Integer
- */
- public Integer getInteger(String name) {
- String s = getParameter(name);
- if (s == null) return null;
-
- try {
- int i = Integer.parseInt(s);
- return new Integer(i);
- }
- catch (NumberFormatException e) {
- return null;
- }
- }
-
- /** Return an Integer or default value if it doesn't exist.
- */
- public Integer getInteger(String name, int defaultVal) {
- Integer i = getInteger(name);
- return i != null ? i : new Integer(defaultVal);
- }
-
- /** Get a Float parameter. null is returned if it doesn't exist.
- * @see java.lang.Float
- */
- public Float getFloat(String name) {
- String s = getParameter(name);
- if (s == null) return null;
-
- try {
- return Float.valueOf(s);
- }
- catch (NumberFormatException e) {
- return null;
- }
- }
-
- /** Return a Float or default value if it doesn't exist.
- */
- public Float getFloat(String name, float defaultVal) {
- Float i = getFloat(name);
- return i != null ? i : new Float(defaultVal);
- }
-
- /** Get a Double parameter. null is returned if it doesn't exist.
- * @see java.lang.Double
- */
- public Double getDouble(String name) {
- String s = getParameter(name);
- if (s == null) return null;
-
- try {
- return Double.valueOf(s);
- }
- catch (NumberFormatException e) {
- return null;
- }
- }
-
- /** Return a Double or default value if it doesn't exist.
- */
- public Double getDouble(String name, float defaultVal) {
- Double i = getDouble(name);
- return i != null ? i : new Double(defaultVal);
- }
-
- /** Get a Color parameter. null is returned if it doesn't exist.
- * Colors can be in several formats.
- * @see spt.util.StringUtil
- * @see java.awt.Color
- */
- public Color getColor(String name) {
- String s = getParameter(name);
- if (s == null) return null;
-
- s = s.trim();
-
- return StringUtil.stringToColor(s);
- }
-
- /** Return a Color or default value if it doesn't exist.
- */
- public Color getColor(String name, Color defaultVal) {
- Color c = getColor(name);
- return c != null ? c : defaultVal;
- }
-
- /** Get a URL parameter. null is returned if it doesn't exist.
- * URLs can be relative or absolute.
- * @see java.net.URL
- */
- public URL getURL(String name) throws MalformedURLException {
- String s = getParameter(name);
- return (s == null) ? null : new URL(applet.getCodeBase(), s);
- }
-
- /** Return a URL or default value if it doesn't exist.
- */
- public URL getURL(String name, URL defaultVal) throws MalformedURLException {
- URL u = getURL(name);
- return u != null ? u : defaultVal;
- }
-
- /** Get a Font parameter. null is returned if it doesn't exist.
- * Fonts are specified as: "FontName,Style,Point"
- * (e.g. "Helvetica,BOLD,14"). Style can be one of: PLAIN, BOLD,
- * ITALIC or BOLDITALIC.
- * @see java.awt.Font
- */
- public Font getFont(String name) {
- String s = getParameter(name);
- if (s == null) return null;
-
- StringTokenizer tok = new StringTokenizer(s, " ,");
-
- int i=0;
- String family=null;
- int style=(-1), pointSize=0;
-
- while (tok.hasMoreTokens()) {
- s = tok.nextToken();
- if (i==0) family = s;
- else if (i==1) {
- if (s.equalsIgnoreCase("PLAIN")) style = Font.PLAIN;
- else if (s.equalsIgnoreCase("BOLD")) style = Font.BOLD;
- else if (s.equalsIgnoreCase("ITALIC")) style = Font.ITALIC;
- else if (s.equalsIgnoreCase("BOLDITALIC"))
- style = Font.BOLD|Font.ITALIC;
- }
- else if (i==2) {
- try
- pointSize = Integer.parseInt(s, 10);
- catch (NumberFormatException e) {}
- }
- ++i;
- }
-
- if (family != null && style >= 0 && pointSize > 0)
- return new Font(family, style, pointSize);
- return null;
- }
-
- /** Return a Font or default value if it doesn't exist.
- */
- public Font getFont(String name, Font defaultVal) {
- Font f = getFont(name);
- return f != null ? f : defaultVal;
- }
-
- /** Gets border style useful for the BorderComponent class.
- * @see spt.gui.BorderedComponent
- */
- public int getBorderStyle() {
- String s = getParameter("BORDERSTYLE");
- if (s == null) return (-1);
-
- if (s.equalsIgnoreCase("NONE")) return BorderedComponent.NONE;
- else if (s.equalsIgnoreCase("IN")) return BorderedComponent.IN;
- else if (s.equalsIgnoreCase("OUT")) return BorderedComponent.OUT;
- else if (s.equalsIgnoreCase("BORDER_IN")) return BorderedComponent.BORDER_IN;
- else if (s.equalsIgnoreCase("BORDER_OUT")) return BorderedComponent.BORDER_OUT;
- return (-1);
- }
-
- // These are some common params you'd want to set for applets
-
- /** Gets the color specified by the BGCOLOR parameter and sets
- * the applet background to it.
- */
- public void applyBGColorParam() {
- Color c;
- if ((c = getColor("BGCOLOR")) != null) applet.setBackground(c);
- }
-
- /** Gets the color specified by the FGCOLOR parameter and sets
- * the applet foreground to it.
- */
- public void applyFGColorParam() {
- Color c;
- if ((c = getColor("FGCOLOR")) != null) applet.setForeground(c);
- }
-
- /** Processes FGCOLOR and BGCOLOR at once.
- */
- public void applyFGBGColorParam() {
- applyFGColorParam();
- applyBGColorParam();
- }
-
- /** Gets the font specified by the FONT parameter and sets
- * the applet font to it.
- */
- public void applyFontParam() {
- Font f = getFont("FONT");
- if (f != null) applet.setFont(f);
- }
- }