public class GridAppFrame extends javax.swing.JFrame implements GridDisplay
GridAppFrame
class provides a window in which
to display a grid and its contents. Options include menus, a
speed slider bar, and a control panel containing buttons such as
a Start/Restart/Reset button. Menus and buttons are usually
enabled (clickable) except when one of them is executing, when
they are generally disabled (grayed-out and not clickable). It
is possible, though, to create components that are always enabled
(such as the speed slider bar), enabled only when the application
is actively executing (such as a stop button), or always disabled
(useful when the program is under construction and the behavior for
a particular component has not yet been implemented).Modifier and Type | Class and Description |
---|---|
class |
GridAppFrame.DisplayMouseListener
Nested class that handles simple mouse presses over the grid
display.
|
static class |
GridAppFrame.GUIExceptionHandler
Nested class that is registered as the handler for exceptions
on the Swing event thread.
|
javax.swing.JFrame.AccessibleJFrame
java.awt.Window.AccessibleAWTWindow, java.awt.Window.Type
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MAX_DELAY_MSECS
Default maximum value for speed slider bar (1000 milliseconds).
|
static int |
DEFAULT_MIN_DELAY_MSECS
Default minimum value for speed slider bar (10 milliseconds).
|
accessibleContext, EXIT_ON_CLOSE, rootPane, rootPaneCheckingEnabled
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Constructor and Description |
---|
GridAppFrame()
Constructs an empty GridAppFrame window object that will
display a grid.
|
Modifier and Type | Method and Description |
---|---|
void |
addGridChangeListener(GridChangeListener listener)
Registers the specified object as interested in being notified
of changes in the identify of the grid being modeled.
|
protected boolean |
componentRequiresGrid(javax.swing.JComponent component)
Returns
true if the given component requires the grid
to have been set in order to be enabled; false otherwise. |
protected java.util.Collection<javax.swing.JComponent> |
componentsEnabledOnlyIfGridSet()
Returns the set of components that should be enabled only when the
current grid is not null.
|
protected java.util.Collection<javax.swing.JComponent> |
componentsEnabledOnlyWhenRunning()
Returns the set of components that should be enabled only when the
application is actively running, and should be disabled when the
application is ready and waiting for user input.
|
protected java.util.Collection<javax.swing.JComponent> |
componentsEnabledOnlyWhenWaiting()
Returns the set of components that should be enabled only when the
application is ready and waiting for user input.
|
protected ScrollableGridDisplay |
constructDisplay(int viewingWidth,
int viewingHeight,
int minCellSize,
java.awt.Color bgColor)
Constructs the grid display at the heart of the graphical
user interface.
|
void |
constructWindowContents(java.lang.String title,
java.awt.Color bgColor,
int viewingWidth,
int viewingHeight,
int minCellSize)
Constructs the body of a window containing a scrollable
display for a grid and its contents.
|
void |
constructWindowContents(java.lang.String title,
int viewingWidth,
int viewingHeight,
int minCellSize)
Constructs the body of a window containing a scrollable
display for a grid and its contents.
|
protected javax.swing.JPanel |
defineContent()
Defines contents of main window panel.
|
protected void |
enableAndDisable()
Enables and disables components appropriately.
|
void |
enableOnlyIfGridSet(javax.swing.JComponent component)
Enables the given component only when the application is ready
and waiting for user input.
|
void |
enableOnlyWhenRunning(javax.swing.JComponent component)
Enables the given component only when the application is actively
executing (not just waiting for user input).
|
void |
enableOnlyWhenWaiting(javax.swing.JComponent component)
Enables the given component only when the application is ready
and waiting for user input.
|
void |
enterNotRunningMode()
Enables and disables GUI components as necessary when an application
is not running.
|
void |
enterRunningMode()
Enables and disables GUI components as necessary while the application
is running.
|
protected javax.swing.JPanel |
getControlPanel()
Returns the control panel (
null if this
graphical user interface has no control panel). |
int |
getDelay()
Returns the current delay value from the speed slider or a previous
call to
setDelay ; defaults to 0 if there is no speed
slider included with this graphical user interface and if
setDelay has never been called. |
ScrollableGridDisplay |
getDisplay()
Gets the grid display.
|
Grid |
getGrid()
Returns the grid at the center of this graphical user interface.
|
protected java.awt.event.MouseAdapter |
getMouseListenerForDisplay()
Returns a mouse adapter that responds to mouse clicks, presses, and
releases over the grid display.
|
protected javax.swing.JSlider |
getSpeedSlider()
Returns the speed slider bar (
null if no speed slider
is included with this graphical user interface). |
void |
includeControlComponent(javax.swing.JComponent component,
int enableDisableIndicator)
Includes the specified control component in a list of control
buttons.
|
void |
includeControlComponent(javax.swing.JComponent component,
int enableDisableIndicator,
boolean initiallyEnabled)
Includes the specified control component in a list of control
buttons.
|
void |
includeControlComponents(java.util.ArrayList<? extends javax.swing.JComponent> componentList,
int enableDisableIndicator)
Includes the control components in the given list in the control
panel.
|
void |
includeMenu(javax.swing.JMenu menu)
Includes the specified menu.
|
void |
includeMenu(javax.swing.JMenu menu,
int enableDisableIndicator)
Includes the specified menu.
|
void |
includeSpeedSlider()
Includes a speed adjustment slider bar with default values.
|
void |
includeSpeedSlider(int maxDelayMsecs,
int minDelayMsecs)
Includes a speed adjustment slider bar with the specified
extreme values and an initial delay halfway between them.
|
void |
includeSpeedSlider(int maxDelayMsecs,
int minDelayMsecs,
int initialDelayMsecs)
Includes a speed adjustment slider bar with the specified
properties.
|
boolean |
isInRunningMode()
Returns
true if the application is in "running mode"
(executing the behavior associated with a control button, for example);
returns false otherwise. |
protected javax.swing.JPanel |
makeControlPanel(java.lang.String title)
Creates a control panel that lays out control buttons starting
from the top.
|
protected javax.swing.JComponent |
makeDisplayPanel()
Creates the panel for displaying a grid application.
|
protected javax.swing.JPanel |
makeSliderPanel()
Creates a speed slider for controling the speed of the animation.
|
void |
notifyGridChangeListeners()
Notifies all registered model change listeners that the model
has been replaced.
|
protected void |
onMouseClickOverDisplay(Location loc)
Handles a mouse click over the grid display.
|
protected void |
onMousePressOverDisplay(Location loc)
Handles a mouse press over the grid display.
|
protected void |
onMouseReleaseOverDisplay(Location loc)
Handles a mouse release over the grid display.
|
void |
resetDelay()
Resets the current delay value to the initial delay value and adjusts
the speed slider, if there is one.
|
void |
setDelay(int delayMsecs)
Sets the current delay value and adjusts the speed slider, if there
is one.
|
protected void |
setEnabledStatus(javax.swing.JComponent component,
int enableDisableIndicator)
Specifies when a given component should be enabled or disabled.
|
void |
setGrid(Grid grid)
Sets the grid being displayed (and displays it without any pause).
|
void |
showGrid()
Shows the grid (and pauses if a delay has been set either
by a speed slider bar or by a call to the
setDelay
method). |
addImpl, createRootPane, frameInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, update
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setBackground, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setOpacity, setResizable, setShape, setState, setTitle, setUndecorated
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBackground, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOpacity, getOwnedWindows, getOwner, getOwnerlessWindows, getShape, getToolkit, getType, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isAutoRequestFocus, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isOpaque, isShowing, isValidateRoot, pack, paint, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setAutoRequestFocus, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocation, setLocation, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setType, setVisible, show, toBack, toFront
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusDownCycle, validate, validateTree
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, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resize, resize, revalidate, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setMaximumSize, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final int DEFAULT_MIN_DELAY_MSECS
public static final int DEFAULT_MAX_DELAY_MSECS
public GridAppFrame()
includeStartRestart
and
includeSpeedSlider
to include components on the
window other than the basic grid display.
Use the constructWindowContents method to set the properties of the
window and make it visible.public void includeMenu(javax.swing.JMenu menu)
JMenu
subclass, should implement the
GridChangeListener
interface, and should register
itself as a grid change listener.
This method will have no effect unless it is
called before the constructWindowContents method.menu
- the menu to includeBasicGridFileMenu
,
BasicHelpMenu
,
MinimalFileMenu
,
addGridChangeListener(edu.kzoo.grid.gui.GridChangeListener)
public void includeMenu(javax.swing.JMenu menu, int enableDisableIndicator)
JMenu
subclass, should implement the
GridChangeListener
interface, and should register
itself as a grid change listener.
This method will have no effect unless it is
called before the constructWindowContents method.menu
- the menu to includeenableDisableIndicator
- indicates when the menu should
enabled or disabledBasicGridFileMenu
,
BasicHelpMenu
,
MinimalFileMenu
,
addGridChangeListener(edu.kzoo.grid.gui.GridChangeListener)
protected void setEnabledStatus(javax.swing.JComponent component, int enableDisableIndicator)
component
- the component to includeenableDisableIndicator
- indicates when the component should
enabled or disabledpublic void enableOnlyIfGridSet(javax.swing.JComponent component)
public void enableOnlyWhenWaiting(javax.swing.JComponent component)
public void enableOnlyWhenRunning(javax.swing.JComponent component)
public void includeControlComponent(javax.swing.JComponent component, int enableDisableIndicator)
component
- the component to includeenableDisableIndicator
- indicates when the component should
be enabled or disabledpublic void includeControlComponent(javax.swing.JComponent component, int enableDisableIndicator, boolean initiallyEnabled)
initiallyEnabled
parameter;
it later switches to enabled or disabled under the conditions
indicated by the enableDisableIndicator
parameter.
Note, though, that it will only switch if there is a state change
that would normally cause it to switch. It will never switch,
for example, if the enableDisableIndicator
parameter
is ALWAYS_ENABLED or ALWAYS_DISABLED, because there is no defined
state change that would cause such components to switch. Thus,
a component that is specified as ALWAYS_ENABLED but initially
disabled will remain disabled throughout the application.
This method will have no effect unless it is called before the constructWindowContents method.
component
- the component to includeenableDisableIndicator
- indicates when the component should
be enabled or disabledinitiallyEnabled
- true if button should initially be enabled;
false if button should initially be disabledpublic void includeControlComponents(java.util.ArrayList<? extends javax.swing.JComponent> componentList, int enableDisableIndicator)
componentList
- the list of components to include in the
control panelenableDisableIndicator
- indicates when the components should
be enabled or disabledpublic void includeSpeedSlider()
DEFAULT_MAX_DELAY_MSECS
to
DEFAULT_MIN_DELAY_MSECS
(maximum delay is
slowest; minimum dalay is fastest), with an initial
delay halfway between them.public void includeSpeedSlider(int maxDelayMsecs, int minDelayMsecs)
maxDelayMsecs
to minDelayMsecsY_MSECS
(maximum delay is slowest; minimum dalay is fastest).
(Precondition: minDelayMsecs <= <= maxDelayMsecs)maxDelayMsecs
- maximum delay value for slider, in millisecondsminDelayMsecs
- minimum delay value for slider, in millisecondspublic void includeSpeedSlider(int maxDelayMsecs, int minDelayMsecs, int initialDelayMsecs)
maxDelayMsecs
to minDelayMsecsY_MSECS
(maximum delay is slowest; minimum dalay is fastest).
(Precondition: minDelayMsecs <= initialDelayMsecs <= maxDelayMsecs)maxDelayMsecs
- maximum delay value for slider, in millisecondsminDelayMsecs
- minimum delay value for slider, in millisecondsinitialDelayMsecs
- initial value for slider, in millisecondspublic void constructWindowContents(java.lang.String title, int viewingWidth, int viewingHeight, int minCellSize)
title
- frame titleviewingWidth
- the width of the viewing areaviewingHeight
- the height of the viewing areaminCellSize
- minimum grid cell side lengthpublic void constructWindowContents(java.lang.String title, java.awt.Color bgColor, int viewingWidth, int viewingHeight, int minCellSize)
title
- frame titlebgColor
- color to paint background of gridviewingWidth
- the width of the viewing areaviewingHeight
- the height of the viewing areaminCellSize
- minimum grid cell side lengthprotected ScrollableGridDisplay constructDisplay(int viewingWidth, int viewingHeight, int minCellSize, java.awt.Color bgColor)
ScrollableGridDisplay
(for
example, to modify the way tool tips are displayed, or some
other aspect of the scrollable display).viewingWidth
- the width of the viewing areaviewingHeight
- the height of the viewing areaminCellSize
- minimum grid cell side lengthbgColor
- color to paint background of gridpublic void setGrid(Grid grid)
setGrid
in interface GridDisplay
grid
- the Grid to displaypublic Grid getGrid()
public void showGrid()
setDelay
method).
(Precondition: must have called setGrid
.)showGrid
in interface GridDisplay
public ScrollableGridDisplay getDisplay()
protected java.awt.event.MouseAdapter getMouseListenerForDisplay()
protected javax.swing.JPanel getControlPanel()
null
if this
graphical user interface has no control panel).protected boolean componentRequiresGrid(javax.swing.JComponent component)
true
if the given component requires the grid
to have been set in order to be enabled; false
otherwise.protected java.util.Collection<javax.swing.JComponent> componentsEnabledOnlyIfGridSet()
protected java.util.Collection<javax.swing.JComponent> componentsEnabledOnlyWhenWaiting()
protected java.util.Collection<javax.swing.JComponent> componentsEnabledOnlyWhenRunning()
public boolean isInRunningMode()
true
if the application is in "running mode"
(executing the behavior associated with a control button, for example);
returns false
otherwise.protected javax.swing.JSlider getSpeedSlider()
null
if no speed slider
is included with this graphical user interface).public void setDelay(int delayMsecs)
delayMsecs
- the length of time the application should pause
after displaying the grid to allow users time
to see it (in milliseconds)public void resetDelay()
public int getDelay()
setDelay
; defaults to 0 if there is no speed
slider included with this graphical user interface and if
setDelay
has never been called.protected javax.swing.JPanel defineContent()
protected javax.swing.JComponent makeDisplayPanel()
protected javax.swing.JPanel makeControlPanel(java.lang.String title)
title
- a title to put in the border of this control
panel, or null
if no title is desiredprotected javax.swing.JPanel makeSliderPanel()
public void addGridChangeListener(GridChangeListener listener)
listener
- the object that should be notified of
changes to the identity of the gridpublic void notifyGridChangeListeners()
protected void onMouseClickOverDisplay(Location loc)
protected void onMousePressOverDisplay(Location loc)
protected void onMouseReleaseOverDisplay(Location loc)
public void enterRunningMode()
public void enterNotRunningMode()
protected void enableAndDisable()