Copyright 2012-02-25 Werner Randelshofer

ch.randelshofer.util
Class Applets

java.lang.Object
  extended by ch.randelshofer.util.Applets

public class Applets
extends java.lang.Object

Usefull methods for applets.

Version:
3.0 2009-11-23 Added support for command line arguments.
2.0 2004-12-27 Enhanced to support the special applet parameters of Sun's Java plug in. See http://java.sun.com/j2se/1.5.0/docs/guide/plugin/developer_guide/special_attributes.html
1.5 2008-06-14 Added debugging support.
1.4 2007-06-10 Methog getParamete(Applet,String,boolean) added.
1.3 2003-07-25 Method getIndexedKeyValueParameters(Applet,String,Hashtable) added.
1.2 2002-06-26 Method getParameter(Applet,String,double) added.
1.1 2001-12-27 Method getParameters did not work.
1.0 2000-03-06
Author:
Werner Randelshofer

Method Summary
static java.lang.Integer decode(java.lang.String nm)
          Decodes a String into an Integer.
static java.util.Hashtable getIndexedKeyValueParameters(java.applet.Applet applet, java.lang.String name, java.util.Hashtable defaultValue)
          Returns the parameter value of the specified applet or the default value if the parameter is null, the value is returned as a Hashtable.
static java.lang.String getParameter(java.applet.Applet applet, java.lang.String name)
          Returns the parameter value for the specified applet or the command line parameter (main args) when the applet is run as an application.
static boolean getParameter(java.applet.Applet applet, java.lang.String name, boolean defaultValue)
          Reads a parameter as a boolean value.
static java.awt.Color getParameter(java.applet.Applet applet, java.lang.String name, java.awt.Color defaultValue)
          Reads a parameter as a color value.
static double getParameter(java.applet.Applet applet, java.lang.String name, double defaultValue)
          Reads a parameter as a double value.
static int getParameter(java.applet.Applet applet, java.lang.String name, int defaultValue)
          Reads a parameter as an int value.
static java.lang.String getParameter(java.applet.Applet applet, java.lang.String name, java.lang.String defaultValue)
          Returns the String value of an applet parameter or the default value if the parameter is null.
static int[] getParameters(java.applet.Applet applet, java.lang.String name, int[] defaultValue)
          Reads a parameter as an array of comma separated int values.
static java.lang.String[] getParameters(java.applet.Applet applet, java.lang.String name, java.lang.String[] defaultValue)
          Returns the parameter value for the specified applet or the default value if the parameter is null, the value is returned as an array.
static void setDebug(boolean newValue)
           
static void setMainArgs(java.applet.Applet applet, java.lang.String[] args)
          Sets command line arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setDebug

public static void setDebug(boolean newValue)

setMainArgs

public static void setMainArgs(java.applet.Applet applet,
                               java.lang.String[] args)
Sets command line arguments. These arguments are used as fallback, when the applet is run as an application.

Parameters:
args -

getParameter

public static java.awt.Color getParameter(java.applet.Applet applet,
                                          java.lang.String name,
                                          java.awt.Color defaultValue)
Reads a parameter as a color value.

The color can be represented in the following formats:

Parameters:
applet - The applet.
name - The name of the parameter.
defaultValue - The default value.
Returns:
Returns the value of the parameter, or the default value, if the parameter does not exist, or does not represent a double.

getParameter

public static java.lang.String getParameter(java.applet.Applet applet,
                                            java.lang.String name,
                                            java.lang.String defaultValue)
Returns the String value of an applet parameter or the default value if the parameter is null.

Parameters:
applet - The applet from which we want to get the parameter.
name - The name of the parameter.
defaultValue - This value is returned when the applet returns null for the parameter.

getParameters

public static java.lang.String[] getParameters(java.applet.Applet applet,
                                               java.lang.String name,
                                               java.lang.String[] defaultValue)
Returns the parameter value for the specified applet or the default value if the parameter is null, the value is returned as an array. The values of the array consist of each word found in the value. The words are delimited by spaces or by commas.

Parameters:
applet - The applet from which we want to get the parameter.
name - The name of the parameter.
defaultValue - This value is returned when the applet returns null for the parameter.

getParameter

public static java.lang.String getParameter(java.applet.Applet applet,
                                            java.lang.String name)
Returns the parameter value for the specified applet or the command line parameter (main args) when the applet is run as an application.

Parameters:
applet - The applet from which we want to get the parameter.
name - The name of the parameter.

getParameter

public static int getParameter(java.applet.Applet applet,
                               java.lang.String name,
                               int defaultValue)
Reads a parameter as an int value.

Parameters:
applet - The applet.
name - The name of the parameter.
defaultValue - The default value.
Returns:
Returns the value of the parameter, or the default value, if the parameter does not exist, or does not represent an int.

getParameter

public static boolean getParameter(java.applet.Applet applet,
                                   java.lang.String name,
                                   boolean defaultValue)
Reads a parameter as a boolean value.

Parameters:
applet - The applet.
name - The name of the parameter.
defaultValue - The default value.
Returns:
Returns the value of the parameter, or the default value, if the parameter does not exist, or does not represent a boolean.

getParameters

public static int[] getParameters(java.applet.Applet applet,
                                  java.lang.String name,
                                  int[] defaultValue)
Reads a parameter as an array of comma separated int values.

Parameters:
applet - The applet.
name - The name of the parameter.
defaultValue - The default value.
Returns:
Returns the value of the parameter, or the default value, if the parameter does not exist, or does not represent an array of int's.

getParameter

public static double getParameter(java.applet.Applet applet,
                                  java.lang.String name,
                                  double defaultValue)
Reads a parameter as a double value.

Parameters:
applet - The applet.
name - The name of the parameter.
defaultValue - The default value.
Returns:
Returns the value of the parameter, or the default value, if the parameter does not exist, or does not represent a double.

getIndexedKeyValueParameters

public static java.util.Hashtable getIndexedKeyValueParameters(java.applet.Applet applet,
                                                               java.lang.String name,
                                                               java.util.Hashtable defaultValue)
Returns the parameter value of the specified applet or the default value if the parameter is null, the value is returned as a Hashtable.

The parameter value is parsed according to the following EBNF production:

 list ::= entry {"," entry}
 entry ::= [key "="] value
 key ::= String
 value ::= String
 

The returned Hashtable contains up to two key value pairs for each entry.
If an entry does specify a key, it is put using the specified key into the hashtable.
For each entry a key is generated which denotes the index of the entry. If the Hashtable does not contain an entry for the generated key. Then an entry is put using the generated key as well.

Parameters:
applet - The applet from which we want to get the parameter.
name - The name of the parameter.
defaultValue - This value is returned when the applet returns null for the parameter.
Returns:
A hashtable containing a key value pair for each entry.

decode

public static java.lang.Integer decode(java.lang.String nm)
                                throws java.lang.NumberFormatException
Decodes a String into an Integer. Accepts decimal, hexadecimal, and octal numbers, in the following formats:
     [-]    
     [-] 0x     
     [-] #      
     [-] 0    
 
The constant following an (optional) negative sign and/or "radix specifier" is parsed as by the Integer.parseInt method with the specified radix (10, 8 or 16). This constant must be positive or a NumberFormatException will result. The result is made negative if first character of the specified String is the negative sign. No whitespace characters are permitted in the String.

Parameters:
nm - the String to decode.
Returns:
the Integer represented by the specified string.
Throws:
java.lang.NumberFormatException - if the String does not contain a parsable integer.
See Also:
Integer.parseInt(String, int)

Copyright 2012-02-25 Werner Randelshofer