Package nzilbb.util

Class GuiProgram

  • All Implemented Interfaces:
    ImageObserver, MenuContainer, Serializable, Accessible, RootPaneContainer
    Direct Known Subclasses:
    Converter

    public class GuiProgram
    extends JApplet
    Base class that standardizes various common functions for utilitiy applications - whether they be run as full applications from the command line or via JNLP or as applets within a browser.

    To implement a self-documenting, self-configuring application/applet, simply extend this class, annotate any setXXX() methods with a Switch annotation, and call mainRun(String argv[]) from the derived class's public static void main(String argv[])

    To provide further information, you may use the ProgramDescription annotation on the derived class itself.

    You can also use the setDefaultHeight(int), setDefaultWidth(int), and setDefaultWindowTitle(java.lang.String) methods to influence the configuration of the application window (i.e. for when not invoked as an applet)

    Doing this has the following effect:

    • If the application is run from the command line with the --usage flag then a list of switches (one for each @Switch annotated setter) is listed to stderr, along with description information annotated with ProgramDescription
    • If the application is run from the command line with switches that correspond to @Switch annotated setters, then the setter will be called with the given value - e.g. if --myswitch=myvalue is called, then the equivalent of myObject.setMyswitch("myvalue") is executed
    • If the application is run from the command line and any switches that are marked as compulsory are not set, then execution is halted with an error message and the usage information written to stderr
    • If the application is run from the command line with arguments that don't start with '--' then these are added to a arguments
    • If the application is run as an applet and the derived class's init() method calls interpretAppletParameters() then any @Switch annotated setters are interpreted as possible applet parameters, and the setters are called with the given parameter value if present.
    • When the application is run, if it finds a .properties file that matches the main class name, it is used to call @Switch annotated setter corresponding to the properties defined in the file. Arguments can also be specified by defining properties called arg[0], arg[1], etc.

    e.g.

     import nzilbb.util.GuiProgram;
     import nzilbb.util.ProgramDescription;
     import nzilbb.util.Switch;
     
     @ProgramDescription(value="A very useful utility",arguments="extra-arg-1 extra-arg-2")
     public class Useful extends GuiProgram {
        public Useful() {
           setDefaultWindowTitle("This application is useful");
           setDefaultWidth(800);
           setDefaultHeight(600);
        }
     
        public static void main(String argv[]) {
           new Useful().mainRun(argv);
        }
     
        @Switch(value="This is a compulsory string switch that fulfils some purpose",compulsory=true)
        public void setSomeString(String s) {...}
     
        @Switch("This is an optional boolean switch")
        public void setSomeBoolean(Boolean b) {...}
     
        public void init() {
           interpretAppletParameters();
           ...
        }
        public void start() {
           for (String sArgument: arguments) {
             ...
           }
        }
     }
     

    This could then be invoked as:
    java Useful --SomeString=Hello --SomeBoolean sundryArg1 sundryArg2

    Author:
    Robert Fromont robert@fromont.net.nz
    See Also:
    Serialized Form
    • Constructor Detail

      • GuiProgram

        public GuiProgram()
        Default constructor
    • Method Detail

      • getDefaultHeight

        public int getDefaultHeight()
        Getter for iDefaultHeight: Default height of the application.
        Returns:
        Default height of the application.
      • setDefaultHeight

        public GuiProgram setDefaultHeight​(int iNewDefaultHeight)
        Setter for iDefaultHeight: Default height of the application.
        Parameters:
        iNewDefaultHeight - Default height of the application.
      • getDefaultWidth

        public int getDefaultWidth()
        Getter for iDefaultWidth: Default width of the application.
        Returns:
        Default width of the application.
      • setDefaultWidth

        public GuiProgram setDefaultWidth​(int iNewDefaultWidth)
        Setter for iDefaultWidth: Default width of the application.
        Parameters:
        iNewDefaultWidth - Default width of the application.
      • getDefaultWindowTitle

        public String getDefaultWindowTitle()
        Getter for sDefaultWindowTitle: Default title for the application window.
        Returns:
        Default title for the application window.
      • setDefaultWindowTitle

        public GuiProgram setDefaultWindowTitle​(String sNewDefaultWindowTitle)
        Setter for sDefaultWindowTitle: Default title for the application window.
        Parameters:
        sNewDefaultWindowTitle - Default title for the application window.
      • getUsage

        public Boolean getUsage()
        Getter for usage: Whether or not to display usage information
        Returns:
        Whether or not to display usage information
      • setUsage

        @Switch("Display usage information")
        public GuiProgram setUsage​(Boolean bNewUsage)
        Setter for usage: Whether or not to display usage information
        Parameters:
        bNewUsage - Whether or not to display usage information
      • getV

        public String getV()
        Getter for v: Version information.
        Returns:
        Version information.
      • setV

        public GuiProgram setV​(String newV)
        Setter for v: Version information.
        Parameters:
        newV - Version information.
      • getVersion

        public Boolean getVersion()
        Getter for version: Print version information.
        Returns:
        Print version information.
      • setVersion

        @Switch("Print version information.")
        public GuiProgram setVersion​(Boolean newVersion)
        Setter for version: Print version information.
        Parameters:
        newVersion - Print version information.
      • mainRun

        public void mainRun​(String[] argv)
        Main entrypoint if run as an application - this should be called by the public static void main(String argv[]) of the derived class.
        Parameters:
        argv - Command-line arguments.
      • wrap

        public String wrap​(String s)
        Endeavours to insert line breaks in the given string so that lines are no longer than 80 characters.
        Parameters:
        s -
        Returns:
        The given string, with line breaks inserted.
      • getParameterInfo

        public String[][] getParameterInfo()
        Returns information about the parameters that are understood by this applet. An applet should override this method to return an array of Strings describing these parameters.

        This information is derived by exploring setters that are annotated with Switch

        Overrides:
        getParameterInfo in class Applet
      • interpretAppletParameters

        public void interpretAppletParameters()
        Should be called from the init() method of derived classes, this method interprets parameters passed to the applet, setting bean attributes appropiately.
      • interpretPropertiesParameters

        public void interpretPropertiesParameters()
        This method looks for a .properties file matching the class name, and if it finds one, uses it to set bean attributes and arguments appropiately. Arguments are interpreted is being the values of Properties named arg[0], arg[1], etc.