Class GuiProgram
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- java.awt.Panel
-
- java.applet.Applet
-
- javax.swing.JApplet
-
- nzilbb.util.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 callmainRun(String argv[])
from the derived class'spublic 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)
, andsetDefaultWindowTitle(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 callsinterpretAppletParameters()
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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.awt.Component
Component.BaselineResizeBehavior
-
-
Field Summary
-
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
-
Constructor Summary
Constructors Constructor Description GuiProgram()
Default constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getDefaultHeight()
Getter foriDefaultHeight
: Default height of the application.int
getDefaultWidth()
Getter foriDefaultWidth
: Default width of the application.String
getDefaultWindowTitle()
Getter forsDefaultWindowTitle
: Default title for the application window.String[][]
getParameterInfo()
Returns information about the parameters that are understood by this applet.Boolean
getUsage()
Getter forusage
: Whether or not to display usage informationString
getV()
Getter forv
: Version information.Boolean
getVersion()
Getter forversion
: Print version information.void
interpretAppletParameters()
Should be called from the init() method of derived classes, this method interprets parameters passed to the applet, setting bean attributes appropiately.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.void
mainRun(String[] argv)
Main entrypoint if run as an application - this should be called by thepublic static void main(String argv[])
of the derived class.GuiProgram
setDefaultHeight(int iNewDefaultHeight)
Setter foriDefaultHeight
: Default height of the application.GuiProgram
setDefaultWidth(int iNewDefaultWidth)
Setter foriDefaultWidth
: Default width of the application.GuiProgram
setDefaultWindowTitle(String sNewDefaultWindowTitle)
Setter forsDefaultWindowTitle
: Default title for the application window.GuiProgram
setUsage(Boolean bNewUsage)
Setter forusage
: Whether or not to display usage informationGuiProgram
setV(String newV)
Setter forv
: Version information.GuiProgram
setVersion(Boolean newVersion)
Setter forversion
: Print version information.String
wrap(String s)
Endeavours to insert line breaks in the given string so that lines are no longer than 80 characters.-
Methods inherited from class javax.swing.JApplet
getAccessibleContext, getContentPane, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, remove, repaint, setContentPane, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setTransferHandler, update
-
Methods inherited from class java.applet.Applet
destroy, getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameter, init, isActive, isValidateRoot, newAudioClip, play, play, resize, resize, setStub, showStatus, start, stop
-
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusDownCycle, validate
-
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, dispatchEvent, enable, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Method Detail
-
getDefaultHeight
public int getDefaultHeight()
Getter foriDefaultHeight
: Default height of the application.- Returns:
- Default height of the application.
-
setDefaultHeight
public GuiProgram setDefaultHeight(int iNewDefaultHeight)
Setter foriDefaultHeight
: Default height of the application.- Parameters:
iNewDefaultHeight
- Default height of the application.
-
getDefaultWidth
public int getDefaultWidth()
Getter foriDefaultWidth
: Default width of the application.- Returns:
- Default width of the application.
-
setDefaultWidth
public GuiProgram setDefaultWidth(int iNewDefaultWidth)
Setter foriDefaultWidth
: Default width of the application.- Parameters:
iNewDefaultWidth
- Default width of the application.
-
getDefaultWindowTitle
public String getDefaultWindowTitle()
Getter forsDefaultWindowTitle
: Default title for the application window.- Returns:
- Default title for the application window.
-
setDefaultWindowTitle
public GuiProgram setDefaultWindowTitle(String sNewDefaultWindowTitle)
Setter forsDefaultWindowTitle
: Default title for the application window.- Parameters:
sNewDefaultWindowTitle
- Default title for the application window.
-
getUsage
public Boolean getUsage()
Getter forusage
: 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 forusage
: Whether or not to display usage information- Parameters:
bNewUsage
- Whether or not to display usage information
-
getV
public String getV()
Getter forv
: Version information.- Returns:
- Version information.
-
setV
public GuiProgram setV(String newV)
Setter forv
: Version information.- Parameters:
newV
- Version information.
-
getVersion
public Boolean getVersion()
Getter forversion
: Print version information.- Returns:
- Print version information.
-
setVersion
@Switch("Print version information.") public GuiProgram setVersion(Boolean newVersion)
Setter forversion
: 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 thepublic 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 classApplet
-
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.
-
-