com.miginfocom.calendar.decorators
Class GridCellDecorator

java.lang.Object
  extended by com.miginfocom.calendar.decorators.AbstractGridDecorator
      extended by com.miginfocom.calendar.decorators.GridCellDecorator
All Implemented Interfaces:
Decorator, GridDecorator, java.lang.Comparable
Direct Known Subclasses:
CellLabelDecorator, NoFitShapeDecorator

public abstract class GridCellDecorator
extends AbstractGridDecorator

Abstract base class for Decorators that wish to decorate cells in a grid. Uses basic caching of all cell's bounds so those doesn't have to be recalculated every repaint. The cache is revalidated if grid or the grid's bounds change.

Here's some sample code to create a decorator that paints sundays in red. It should be trivial to modify it to implement any algorithm.

final SimpleDateFormat sdf = new SimpleDateFormat("d/M");
defaultDateArea.addDecorator(new GridCellDecorator(defaultDateArea, 200)
{
   protected void paintCell(Graphics2D g2, int r, int c, Rectangle bounds)
   {
      DateGrid grid = (DateGrid) getGrid();
      Calendar cal = grid.getDateRangeForCell(r, c).getStart();
      int day = cal.get(Calendar.DAY_OF_WEEK);
      Color col = day == Calendar.SUNDAY ? Color.RED : Color.BLACK;
      String s = sdf.format(cal.getTime());
      GfxUtil.drawStringInRect(g2, s, bounds, AtFraction.CENTER, AtFraction.CENTER, 0, true, GfxUtil.AA_HINT_PLATFORM);
   }
   protected boolean prePaint() {return true;} // Always paint
   protected void postPaint() {} // No cleanup
});
 


Field Summary
protected  PlaceRect placeRect
          Which part of the cells to paint.
 
Constructor Summary
protected GridCellDecorator(GridContainer gridContainer, int layerIndex)
          Constructor with AbsRect.FILL and Grid.SIZE_MODE_INSIDE.
protected GridCellDecorator(GridContainer gridContainer, int layerIndex, PlaceRect placeRect, int gridCellSizeMode)
          Constructor.
 
Method Summary
protected  java.awt.Rectangle[][] createCellRects(Grid grid)
          Creates a rectangle for every cell.
 void doPaint(java.awt.Graphics2D g2, java.awt.Rectangle bounds)
          Paint what should be painted.
 PlaceRect getPlaceRect()
          Retuns the relative rectangle used for denoting the part of the cell to draw in.
 int getSizeMode()
          Returns if the inside, middle or outside of the grid cell should be targeted.
 void gridChanged(java.beans.PropertyChangeEvent e)
          Called when the grid changes.
protected abstract  void paintCell(java.awt.Graphics2D g2, int r, int c, java.awt.Rectangle bounds)
          Paints the cell, or part thereof.
protected abstract  void postPaint()
          Called after the last cell has been painted.
protected abstract  boolean prePaint()
          Called before the first cell is painted.
 void setPlaceRect(PlaceRect placeRect)
          Sets the relative rectangle used for denoting the part of the cell to draw in.
 void setSizeMode(int sizeMode)
          Sets if the inside, middle or outside of the grid cell should be targeted.
 
Methods inherited from class com.miginfocom.calendar.decorators.AbstractGridDecorator
compareTo, dispose, getGrid, getGridContainer, getInvalidEnd, getInvalidStart, getLayerIndex, hasInvalidCells, invalidateCells, processEvent, setCellsValid, setLayerIndex
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

placeRect

protected PlaceRect placeRect
Which part of the cells to paint. Can be null.

Constructor Detail

GridCellDecorator

protected GridCellDecorator(GridContainer gridContainer,
                            int layerIndex)
Constructor with AbsRect.FILL and Grid.SIZE_MODE_INSIDE.

Parameters:
gridContainer - The grid container to decorate. Not null.
layerIndex - A number to indicate the order in which to paint something. Lower if painted before. I.e. under.

GridCellDecorator

protected GridCellDecorator(GridContainer gridContainer,
                            int layerIndex,
                            PlaceRect placeRect,
                            int gridCellSizeMode)
Constructor.

Parameters:
gridContainer - The grid container to decorate. Not null.
layerIndex - A number to indicate the order in which to paint something. Lower if painted before. I.e. under.
placeRect - The rectangle within the cell to paint/draw label for.
gridCellSizeMode - If the inside, middle or outside of the grid cell should be targeted. E.g Grid.SIZE_MODE_INSIDE.
Method Detail

doPaint

public final void doPaint(java.awt.Graphics2D g2,
                          java.awt.Rectangle bounds)
Description copied from interface: Decorator
Paint what should be painted. Color, paints, fonts, hints and similar should be same as when called.

Parameters:
g2 - The graphics object to paint in
bounds - The bounds to stay within (a must). If width and/or height is 0 that dimension is not scaled to fit and should be ignored.

prePaint

protected abstract boolean prePaint()
Called before the first cell is painted. Possible to do setup.

Returns:
If the paint should continue. If false paintCell(java.awt.Graphics2D, int, int, java.awt.Rectangle) will not be called this round. postPaint() will be called though.

paintCell

protected abstract void paintCell(java.awt.Graphics2D g2,
                                  int r,
                                  int c,
                                  java.awt.Rectangle bounds)
Paints the cell, or part thereof.

Parameters:
g2 - The object to paint in. Paint may be changed and not restored for every call.
r - The row beeing painted.
c - The column beeing painted
bounds - The bounds of the cell, or part there of, to be painted.

postPaint

protected abstract void postPaint()
Called after the last cell has been painted. Possible to dispose of resources.


createCellRects

protected java.awt.Rectangle[][] createCellRects(Grid grid)
Creates a rectangle for every cell. Uses absRect (if != null) to specify a part of the cell.
Note! The method CAN change the graphics' color, and font and not set it back.

Parameters:
grid - The grid. Not null.
Returns:
The created rectangles. Same size as grid. Never null.

getPlaceRect

public PlaceRect getPlaceRect()
Retuns the relative rectangle used for denoting the part of the cell to draw in.

Returns:
The relative rectangle. May be null.

setPlaceRect

public void setPlaceRect(PlaceRect placeRect)
Sets the relative rectangle used for denoting the part of the cell to draw in.

Parameters:
placeRect - The relative rectangle. May be null.

getSizeMode

public int getSizeMode()
Returns if the inside, middle or outside of the grid cell should be targeted. E.g Grid.SIZE_MODE_INSIDE.

Returns:
If the inside, middle or outside of the grid cell should be targeted.

setSizeMode

public void setSizeMode(int sizeMode)
Sets if the inside, middle or outside of the grid cell should be targeted. E.g Grid.SIZE_MODE_INSIDE.

Parameters:
sizeMode - If the inside, middle or outside of the grid cell should be targeted.

gridChanged

public void gridChanged(java.beans.PropertyChangeEvent e)
Description copied from class: AbstractGridDecorator
Called when the grid changes.

Specified by:
gridChanged in class AbstractGridDecorator
Parameters:
e - The event.


Copyright © 2009 MiG InfoCom AB. All Rights Reserved.