net.pacbell.jfai.toh.util
Class AbstractJMainFrame

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by java.applet.Applet
                  extended by javax.swing.JApplet
                      extended by net.pacbell.jfai.toh.util.AbstractJMainFrame
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, RootPaneContainer
Direct Known Subclasses:
TowerOfHanoi

public abstract class AbstractJMainFrame
extends JApplet

Inherit from this class to enable a program to run as an applet and as an application. Loosely cloned from JMainFrame , which seems to have problems with redraws, probably because it manipulates Swing components outside of the event dispatch thread.

Applets : you can override init().

For an application , implement a main method in YourClass like:

public abstract main(String[] args) { new YourClass(args); }

You may also call YourClass(args, width, height) if either width or height should have a default value. In either case, width and height may be overridden using width= or height= as invocation arguments.

Constructors with arguments parse the invocation arguments, and then call buildWindow() to construct a root container, and display it.

Override initialize to execute setup code after argument parsing but before anything else happens during instance creation, like:

                       protected void initialize(String[] args) { 
                           super initialize(args); // parse arguments 
                           // YourClass initialization goes here 
                       }
 

Use setErrorComponent if a problem is detected during setup. If the error component is not null, then buildContentPane is skipped and the error component is displayed instead.

Author:
Jürgen Failenschmid
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JApplet
JApplet.AccessibleJApplet
 
Nested classes/interfaces inherited from class java.applet.Applet
Applet.AccessibleApplet
 
Nested classes/interfaces inherited from class java.awt.Panel
Panel.AccessibleAWTPanel
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
static String ARGUMENT_HEIGHT
          Invocation argument name for the window height.
static String ARGUMENT_WIDTH
          Invocation argument name for the window width.
 
Fields inherited from class javax.swing.JApplet
accessibleContext, rootPane, rootPaneCheckingEnabled
 
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
AbstractJMainFrame()
          (Applet) Creates a new instance.
AbstractJMainFrame(String[] args)
          (Application) Creates a new instance, parses the given arguments, creates a root container, constructs its content pane (or uses the error component), and displays the window with default size.
AbstractJMainFrame(String[] args, int width, int height)
          (Application) Creates a new instance, parses the given arguments, creates a root container, constructs its content pane (or uses the error component), and displays the window with the given size.
 
Method Summary
protected  void addListeners(JFrame frame)
          (Application) Registers listeners with the root container, such as window event listeners.
protected abstract  void buildContentPane(Container contentPane)
          (Application and Applet) Constructs the contents of the given content pane of a root container.
protected  void buildWindow(int width, int height)
          (Application) Creates a frame as root container and adds a panel.
static String getArgument(String name)
          (Application) Returns the value of the named argument, if it was found in the properties list.
static String getArgument(String name, Properties someProperties)
          (Application) Returns the value of the named argument, if it was found in the given properties list.
protected  Component getErrorComponent()
          Gets the errorComponent.
protected  boolean hasErrorComponent()
          Checks if an error component is present.
 void init()
          (Applet) Performs initialization.
protected  void initialize(String[] args)
          (Application and Applet) Parses the given arguments.
static void parseArguments(String[] args, Properties someProperties)
          (Application) Parses the list of argument strings and adds key-value pairs into the specified properties list.
protected static int parseHeightArgument(int height, Properties someProperties)
          Checks if height was specified as an invocation argument, e.g.
protected static int parseWidthArgument(int width, Properties someProperties)
          Checks if width was specified as an invocation argument, e.g.
static void resize(Container frame, int width, int height, Properties someProperties)
          (Application) Resizes the given container.
protected  void setErrorComponent(Component errorComponent)
          Sets the errorComponent.
 void usage()
          Writes the usage messageto System.err, then exitswith code -1.
 void usage(String message)
          Writes the given string to System.err, followed by the usage message, then exitswith code -1.
abstract  String usageMessage()
          Answers a string describing how to invoke the application correctly.
 String windowTitle()
          (Application) Answers a String for the title bar of the main window.
 
Methods inherited from class javax.swing.JApplet
addImpl, createRootPane, getAccessibleContext, getContentPane, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, isRootPaneCheckingEnabled, paramString, remove, setContentPane, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, update
 
Methods inherited from class java.applet.Applet
destroy, getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameter, getParameterInfo, isActive, newAudioClip, play, play, resize, resize, setStub, showStatus, start, stop
 
Methods inherited from class java.awt.Panel
addNotify
 
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, processContainerEvent, processEvent, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, 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, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ARGUMENT_HEIGHT

public static final String ARGUMENT_HEIGHT
Invocation argument name for the window height.

See Also:
Constant Field Values

ARGUMENT_WIDTH

public static final String ARGUMENT_WIDTH
Invocation argument name for the window width.

See Also:
Constant Field Values
Constructor Detail

AbstractJMainFrame

public AbstractJMainFrame()
(Applet) Creates a new instance.


AbstractJMainFrame

public AbstractJMainFrame(String[] args)
(Application) Creates a new instance, parses the given arguments, creates a root container, constructs its content pane (or uses the error component), and displays the window with default size.

Parameters:
args - the invocation arguments

AbstractJMainFrame

public AbstractJMainFrame(String[] args,
                          int width,
                          int height)
(Application) Creates a new instance, parses the given arguments, creates a root container, constructs its content pane (or uses the error component), and displays the window with the given size.

Parameters:
args - the invocation arguments
width - the window width (if >= 0)
height - the window height (if >= 0)
Method Detail

getArgument

public static String getArgument(String name)
(Application) Returns the value of the named argument, if it was found in the properties list. Otherwise null is returned.

Parameters:
name - a string - case is insignificant
Returns:
the value of the named argument (or null)

getArgument

public static String getArgument(String name,
                                 Properties someProperties)
(Application) Returns the value of the named argument, if it was found in the given properties list. Otherwise null is returned.

Parameters:
name - a string - case is insignificant
someProperties - a Properties
Returns:
the value of the named argument (or null)

parseArguments

public static void parseArguments(String[] args,
                                  Properties someProperties)
(Application) Parses the list of argument strings and adds key-value pairs into the specified properties list. Valid argument syntax: The name string is converted to lower case characters.

Parameters:
args - the argument strings
someProperties - a Properties

resize

public static void resize(Container frame,
                          int width,
                          int height,
                          Properties someProperties)
(Application) Resizes the given container. If width= and/or height= argument properties are found in the given properties list, then the property values are used instead of the given parameters.

If width and/or height argument properties were specified or the last two parameters are greater than -1, set the size of the given container.

If the parameter values are invalid integers, a NumberFormatExceptionis thrown.

Parameters:
frame - a Container
width - the width of the frame (if >= 0)
height - the height of the frame (if >= 0)
someProperties - a Properties

parseHeightArgument

protected static int parseHeightArgument(int height,
                                         Properties someProperties)
Checks if height was specified as an invocation argument, e.g. "height=600", and parses the argument value. If it is a valid number, it returns that number, otherwise an error message is printed to System.errand the result is the given height parameter. If there is no height invocation argument, the result is the given height parameter.

Parameters:
height - the default window height
someProperties - a Properties
Returns:
height the resulting window height

parseWidthArgument

protected static int parseWidthArgument(int width,
                                        Properties someProperties)
Checks if width was specified as an invocation argument, e.g. "width=600", and parses the argument value. If it is a valid number, it returns that number, otherwise an error message is printed to System.errand the result is the given width parameter. If there is no width invocation argument, the result is the given width parameter.

Parameters:
width - the default window width
someProperties - a Properties
Returns:
width argument value or -1

init

public void init()
(Applet) Performs initialization. By default, a new panel is used as the content pane. If an error component was set during initialize, then the error component is added to the panel. If there is no error component, then buildContentPane is called.

Overrides:
init in class Applet
See Also:
hasErrorComponent(), setErrorComponent(Component), getErrorComponent(), Applet.init()

usage

public void usage()
Writes the usage messageto System.err, then exitswith code -1.


usage

public void usage(String message)
Writes the given string to System.err, followed by the usage message, then exitswith code -1.

Parameters:
message - a string

usageMessage

public abstract String usageMessage()
Answers a string describing how to invoke the application correctly.

Override to provide something like:

Usage: YourClass arg1 arg2, where arg1 is...

Returns:
a string

windowTitle

public String windowTitle()
(Application) Answers a String for the title bar of the main window. By default, the title is the class name.

Returns:
a window title

addListeners

protected void addListeners(JFrame frame)
(Application) Registers listeners with the root container, such as window event listeners. By default, registers for the window-close event. On window close, calls destroy and exits with code 0.

Parameters:
frame - the frame

buildContentPane

protected abstract void buildContentPane(Container contentPane)
(Application and Applet) Constructs the contents of the given content pane of a root container.

Parameters:
contentPane - the content pane of a container

buildWindow

protected void buildWindow(int width,
                           int height)
(Application) Creates a frame as root container and adds a panel. If an error component was set during initialize, then the error component is added to the panel. If there is no error component, then buildContentPane is called to construct the content pane. The window is made visible. If width and/or height are >= 0, they'll determine the window size.

Parameters:
width - window width (if >= 0)
height - window height (if >= 0)
See Also:
windowTitle(), addListeners(JFrame), resize(Container,int,int,Properties), hasErrorComponent(), setErrorComponent(Component), getErrorComponent()

initialize

protected void initialize(String[] args)
(Application and Applet) Parses the given arguments. This method is called before anything else happens during instance creation.

Parameters:
args - The invocation arguments. If args parameter is null, then the instance is an applet.

hasErrorComponent

protected boolean hasErrorComponent()
Checks if an error component is present.

Returns:
Is an error component present?

getErrorComponent

protected Component getErrorComponent()
Gets the errorComponent.

Returns:
the errorComponent

setErrorComponent

protected void setErrorComponent(Component errorComponent)
Sets the errorComponent.

Parameters:
errorComponent - The errorComponent to set.