edu.kzoo.grid.display
Class ScrollableGridDisplay

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JPanel
                          |
                          +--edu.kzoo.grid.display.ScrollableGridDisplay
All Implemented Interfaces:
javax.accessibility.Accessible, GridBackgroundDisplay, GridChangeListener, GridDisplay, java.awt.image.ImageObserver, java.awt.MenuContainer, PseudoInfiniteViewport.Pannable, javax.swing.Scrollable, java.io.Serializable

public class ScrollableGridDisplay
extends javax.swing.JPanel
implements GridDisplay, GridBackgroundDisplay, GridChangeListener, javax.swing.Scrollable, PseudoInfiniteViewport.Pannable

Grid Display Package:
A ScrollableGridDisplay is a panel containing a scrollable graphical display of a grid.

Version:
13 February 2004
Author:
Alyce Brady (based on MBSDisplay by Julie Zelenski)
See Also:
Serialized Form

Field Summary
protected  GridBackgroundDisplay backgroundDisplay
           
protected  java.awt.Color bgColor
           
static int DEFAULT_MIN_CELL_SIZE
           
static int DEFAULT_VIEWABLE_SIZE
           
protected  int gridLineWidth
           
protected static int LOCATION_TOOL_TIPS
           
protected  int minCellSize
           
protected  int numCols
           
protected  int numRows
           
protected static int OBJECT_STRING_TOOL_TIPS
           
static java.awt.Color OCEAN_BLUE
           
protected  int originCol
           
protected  int originRow
           
protected  int outerCellSize
           
protected  java.awt.Dimension preferredVPSize
           
protected  Grid theGrid
           
protected  boolean toolTipsEnabledFlag
           
protected  int toolTipsType
           
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
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
ScrollableGridDisplay()
          Constructs a new ScrollableGridDisplay object with no grid and an empty view.
ScrollableGridDisplay(java.awt.Color backgroundColor)
          Constructs a new ScrollableGridDisplay object with no grid and an empty view.
ScrollableGridDisplay(int minimumCellSize)
          Constructs a new ScrollableGridDisplay object with no grid and an empty view.
ScrollableGridDisplay(int minimumCellSize, java.awt.Color backgroundColor)
          Constructs a new ScrollableGridDisplay object with no grid and an empty view.
ScrollableGridDisplay(int width, int height)
          Constructs a new ScrollableGridDisplay object with no grid and an empty view.
ScrollableGridDisplay(int width, int height, java.awt.Color backgroundColor)
          Constructs a new ScrollableGridDisplay object with no grid and an empty view.
ScrollableGridDisplay(int width, int height, int minimumCellSize)
          Constructs a new ScrollableGridDisplay object with no grid and an empty view.
ScrollableGridDisplay(int viewingWidth, int viewingHeight, int minimumCellSize, java.awt.Color backgroundColor)
          Constructs a new ScrollableGridDisplay object with no grid and an empty view.
 
Method Summary
 java.awt.Color backgroundColor()
          Gets the background color for displaying the grid.
protected  int calculateGridLineWidth()
          Returns the width of each grid line.
 int colToXCoord(int col)
          Returns X-coordinate of left side of given column.
 void drawBackground(java.awt.Graphics2D g2)
          Draws the grid background.
protected  void drawGridlines(java.awt.Graphics2D g2)
          Draws the gridlines for the grid.
protected  void drawGridObject(java.awt.Graphics2D g2, GridObject obj)
          Draws one GridObject instance.
protected  int extraHeight()
           
protected  int extraWidth()
           
 void fillBackground(java.awt.Graphics2D g2, java.awt.Color fillColor)
          Fills the grid background with the specified color.
 javax.swing.JViewport getEnclosingViewport()
          Gets our parent viewport, if we are in one.
 java.awt.Dimension getMinimumSize()
          Returns the minimum size of the display, for use by layout manager.
 java.lang.String getPannableTipText()
           
 java.awt.Dimension getPreferredScrollableViewportSize()
           
 java.awt.Dimension getPreferredSize()
          Returns the desired size of the display, for use by layout manager.
 int getScrollableBlockIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
           
 boolean getScrollableTracksViewportHeight()
           
 boolean getScrollableTracksViewportWidth()
           
 int getScrollableUnitIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
           
 java.lang.String getToolTipText(java.awt.event.MouseEvent evt)
          Given a MouseEvent, determines what text to place in the floating tool tip when the the mouse hovers over this location.
 Grid grid()
          Gets the grid.
 boolean gridLinesAreVisible()
          Returns true if the grid lines are visible, false otherwise.
 int innerCellSize()
          Returns the size of each cell, not including the width of a grid line.
 boolean isPannableUnbounded()
           
 Location locationForPoint(java.awt.Point p)
          Given a Point, determines which grid location (if any) is under the mouse.
 void makeGridLinesVisible(boolean visible)
          Makes the gridlines visible or invisible, depending on the value of the visible parameter.
 void makeToolTipsReportLocation()
          Sets tool tips to provide information about the locations of cells in the grid.
 void makeToolTipsReportObject()
          Sets tool tips to provide information about the contents of cells in the grid.
 int minimumCellSize()
          Gets the minimum cell size.
 void paintComponent(java.awt.Graphics g)
          Paints this component.
 void panBy(int hDelta, int vDelta)
           
 void reactToNewGrid(Grid newGrid)
          Reacts to a change in grids being used.
protected  void recalculateCellSize()
          Calculates the cell size to use given the current viewable region and the number of rows and columns in the grid.
 void recenterOnOrigin()
          Pans the display back to the origin, so that 0, 0 is at the the upper left of the visible viewport.
 int rowToYCoord(int row)
          Returns Y-coordinate of top of given row.
 void setBackgroundColor(java.awt.Color newBackgroundColor)
          Sets the background color for displaying the grid.
 void setBackgroundDisplay(GridBackgroundDisplay bgDisplay)
          Sets the object used to draw the background.
 void setGrid(Grid grid)
          Sets the Grid being displayed.
 void setToolTipsEnabled(boolean flag)
          Enables/disables showing of tooltip giving information about the grid object beneath the mouse.
 void showGrid()
          Shows the grid.
 boolean toolTipsEnabled()
          Indicates whether tool tips have been enabled.
 void updateLocation(Location loc)
          Updates the display of just a single location on the grid.
protected  int xCoordToCol(int xCoord)
          Returns column corresponding to given X-coordinate.
protected  int yCoordToRow(int yCoord)
          Returns row corresponding to given Y-coordinate.
 void zoomIn()
          Zooms in the display by doubling the current cell size.
 void zoomOut()
          Zooms out the display by halving the current cell size.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getNextFocusableComponent, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, 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, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, 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

DEFAULT_MIN_CELL_SIZE

public static final int DEFAULT_MIN_CELL_SIZE
See Also:
Constant Field Values

DEFAULT_VIEWABLE_SIZE

public static final int DEFAULT_VIEWABLE_SIZE
See Also:
Constant Field Values

OCEAN_BLUE

public static final java.awt.Color OCEAN_BLUE

LOCATION_TOOL_TIPS

protected static final int LOCATION_TOOL_TIPS
See Also:
Constant Field Values

OBJECT_STRING_TOOL_TIPS

protected static final int OBJECT_STRING_TOOL_TIPS
See Also:
Constant Field Values

theGrid

protected Grid theGrid

preferredVPSize

protected java.awt.Dimension preferredVPSize

gridLineWidth

protected int gridLineWidth

minCellSize

protected int minCellSize

outerCellSize

protected int outerCellSize

numRows

protected int numRows

numCols

protected int numCols

originRow

protected int originRow

originCol

protected int originCol

backgroundDisplay

protected GridBackgroundDisplay backgroundDisplay

bgColor

protected java.awt.Color bgColor

toolTipsEnabledFlag

protected boolean toolTipsEnabledFlag

toolTipsType

protected int toolTipsType
Constructor Detail

ScrollableGridDisplay

public ScrollableGridDisplay()
Constructs a new ScrollableGridDisplay object with no grid and an empty view.


ScrollableGridDisplay

public ScrollableGridDisplay(int width,
                             int height)
Constructs a new ScrollableGridDisplay object with no grid and an empty view. (Precondition: width and height must be at least as large as the default minimum cell size.)

Parameters:
width - the width of the viewing area
height - the height of the viewing area

ScrollableGridDisplay

public ScrollableGridDisplay(int minimumCellSize)
Constructs a new ScrollableGridDisplay object with no grid and an empty view.

Parameters:
minimumCellSize - minimum cell side length

ScrollableGridDisplay

public ScrollableGridDisplay(int width,
                             int height,
                             int minimumCellSize)
Constructs a new ScrollableGridDisplay object with no grid and an empty view. (Precondition: width and height must be at least as large as minimumCellSize.)

Parameters:
width - the width of the viewing area
height - the height of the viewing area
minimumCellSize - minimum cell side length

ScrollableGridDisplay

public ScrollableGridDisplay(java.awt.Color backgroundColor)
Constructs a new ScrollableGridDisplay object with no grid and an empty view.

Parameters:
backgroundColor - color to paint background of grid

ScrollableGridDisplay

public ScrollableGridDisplay(int width,
                             int height,
                             java.awt.Color backgroundColor)
Constructs a new ScrollableGridDisplay object with no grid and an empty view. (Precondition: width and height must be at least as large as the default minimum cell size.)

Parameters:
width - the width of the viewing area
height - the height of the viewing area
backgroundColor - color to paint background of grid

ScrollableGridDisplay

public ScrollableGridDisplay(int minimumCellSize,
                             java.awt.Color backgroundColor)
Constructs a new ScrollableGridDisplay object with no grid and an empty view.

Parameters:
minimumCellSize - minimum cell side length
backgroundColor - color to paint background of grid

ScrollableGridDisplay

public ScrollableGridDisplay(int viewingWidth,
                             int viewingHeight,
                             int minimumCellSize,
                             java.awt.Color backgroundColor)
Constructs a new ScrollableGridDisplay object with no grid and an empty view. (Precondition: width and height must be at least as large as minimumCellSize.)

Parameters:
viewingWidth - the width of the viewing area
viewingHeight - the height of the viewing area
minimumCellSize - minimum cell side length
backgroundColor - color to paint background of grid
Method Detail

setGrid

public void setGrid(Grid grid)
Sets the Grid being displayed. Sets the cell size to be the largest that fits the entire grid in the current viewing area (uses a default minimum if grid is too large).

Specified by:
setGrid in interface GridDisplay
Parameters:
grid - the Grid to display

reactToNewGrid

public void reactToNewGrid(Grid newGrid)
Description copied from interface: GridChangeListener
Reacts to a change in grids being used.

Specified by:
reactToNewGrid in interface GridChangeListener
Parameters:
newGrid - the new grid being used

getEnclosingViewport

public javax.swing.JViewport getEnclosingViewport()
Gets our parent viewport, if we are in one.


recalculateCellSize

protected void recalculateCellSize()
Calculates the cell size to use given the current viewable region and the number of rows and columns in the grid. We use the largest cellSize that will fit in the viewable region, bounded to be at least the parameter minSize.


calculateGridLineWidth

protected int calculateGridLineWidth()
Returns the width of each grid line.


innerCellSize

public int innerCellSize()
Returns the size of each cell, not including the width of a grid line.


grid

public Grid grid()
Gets the grid.


minimumCellSize

public int minimumCellSize()
Gets the minimum cell size.


setBackgroundDisplay

public void setBackgroundDisplay(GridBackgroundDisplay bgDisplay)
Sets the object used to draw the background.


backgroundColor

public java.awt.Color backgroundColor()
Gets the background color for displaying the grid.


setBackgroundColor

public void setBackgroundColor(java.awt.Color newBackgroundColor)
Sets the background color for displaying the grid.


makeGridLinesVisible

public void makeGridLinesVisible(boolean visible)
Makes the gridlines visible or invisible, depending on the value of the visible parameter. The gridlines are visible by default.

Parameters:
visible - whether to make the gridlines visible (true) or invisible (false)

gridLinesAreVisible

public boolean gridLinesAreVisible()
Returns true if the grid lines are visible, false otherwise.


showGrid

public void showGrid()
Shows the grid. Invoking the repaint method is the standard way to ask a Swing component to redraw itself. This eventually turns into a call back to our version of the standard paintComponent method where we do the actual drawing.

Specified by:
showGrid in interface GridDisplay

updateLocation

public void updateLocation(Location loc)
Updates the display of just a single location on the grid. Does not redraw the gridlines.


paintComponent

public void paintComponent(java.awt.Graphics g)
Paints this component.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - the Graphics object to use to render this component

drawBackground

public void drawBackground(java.awt.Graphics2D g2)
Draws the grid background.

Specified by:
drawBackground in interface GridBackgroundDisplay
Parameters:
g2 - the Graphics2 object to use to render

fillBackground

public void fillBackground(java.awt.Graphics2D g2,
                           java.awt.Color fillColor)
Fills the grid background with the specified color. At the end of this method, the graphics context is set to draw whatever color it was set to draw when the method was called, not the fill color.

Parameters:
g2 - the Graphics2 object to use to render
fillColor - the color with which to fill the background

drawGridlines

protected void drawGridlines(java.awt.Graphics2D g2)
Draws the gridlines for the grid. We only draw the portion of the lines that intersect the current clipping bounds.

Parameters:
g2 - the Graphics2 object to use to render

drawGridObject

protected void drawGridObject(java.awt.Graphics2D g2,
                              GridObject obj)
Draws one GridObject instance. First verifies that the object is actually visible before any drawing, sets up the clip appropriately and uses the DisplayMap to determine which object to call upon to render this particular GridObject. Note that we save and restore the graphics transform to restore back to normalcy no matter what the renderer did to to the coordinate system.

Parameters:
g2 - the Graphics2D object to use to render
obj - the GridObject object to draw

getScrollableUnitIncrement

public int getScrollableUnitIncrement(java.awt.Rectangle visibleRect,
                                      int orientation,
                                      int direction)
Specified by:
getScrollableUnitIncrement in interface javax.swing.Scrollable

getScrollableBlockIncrement

public int getScrollableBlockIncrement(java.awt.Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Specified by:
getScrollableBlockIncrement in interface javax.swing.Scrollable

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Specified by:
getScrollableTracksViewportWidth in interface javax.swing.Scrollable

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Specified by:
getScrollableTracksViewportHeight in interface javax.swing.Scrollable

getPreferredScrollableViewportSize

public java.awt.Dimension getPreferredScrollableViewportSize()
Specified by:
getPreferredScrollableViewportSize in interface javax.swing.Scrollable

zoomIn

public void zoomIn()
Zooms in the display by doubling the current cell size.


zoomOut

public void zoomOut()
Zooms out the display by halving the current cell size.


recenterOnOrigin

public void recenterOnOrigin()
Pans the display back to the origin, so that 0, 0 is at the the upper left of the visible viewport.


panBy

public void panBy(int hDelta,
                  int vDelta)
Specified by:
panBy in interface PseudoInfiniteViewport.Pannable

isPannableUnbounded

public boolean isPannableUnbounded()
Specified by:
isPannableUnbounded in interface PseudoInfiniteViewport.Pannable

getPannableTipText

public java.lang.String getPannableTipText()
Specified by:
getPannableTipText in interface PseudoInfiniteViewport.Pannable

setToolTipsEnabled

public void setToolTipsEnabled(boolean flag)
Enables/disables showing of tooltip giving information about the grid object beneath the mouse.

Parameters:
flag - whether to enable/disable tool tips

toolTipsEnabled

public boolean toolTipsEnabled()
Indicates whether tool tips have been enabled.

Returns:
true if tool tips are enabled; false otherwise

makeToolTipsReportLocation

public void makeToolTipsReportLocation()
Sets tool tips to provide information about the locations of cells in the grid.


makeToolTipsReportObject

public void makeToolTipsReportObject()
Sets tool tips to provide information about the contents of cells in the grid.


getToolTipText

public java.lang.String getToolTipText(java.awt.event.MouseEvent evt)
Given a MouseEvent, determines what text to place in the floating tool tip when the the mouse hovers over this location. If the mouse is over a valid cell in the grid, we provide some information about the cell and its contents. This method is automatically called on mouse-moved events if we register for tool tips.

Overrides:
getToolTipText in class javax.swing.JComponent
Parameters:
evt - the MouseEvent in question
Returns:
the tool tip string for this location

locationForPoint

public Location locationForPoint(java.awt.Point p)
Given a Point, determines which grid location (if any) is under the mouse. This method is used by the GUI when generating text tips.

Parameters:
p - the Point in question (in display's coordinate system)
Returns:
the grid Location beneath the event, or null if no cell is beneath the mouse

xCoordToCol

protected int xCoordToCol(int xCoord)
Returns column corresponding to given X-coordinate.


yCoordToRow

protected int yCoordToRow(int yCoord)
Returns row corresponding to given Y-coordinate.


colToXCoord

public int colToXCoord(int col)
Returns X-coordinate of left side of given column.


rowToYCoord

public int rowToYCoord(int row)
Returns Y-coordinate of top of given row.


getPreferredSize

public java.awt.Dimension getPreferredSize()
Returns the desired size of the display, for use by layout manager.

Overrides:
getPreferredSize in class javax.swing.JComponent
Returns:
preferred size

getMinimumSize

public java.awt.Dimension getMinimumSize()
Returns the minimum size of the display, for use by layout manager.

Overrides:
getMinimumSize in class javax.swing.JComponent
Returns:
minimum size

extraWidth

protected int extraWidth()

extraHeight

protected int extraHeight()