prefuse.render
Class LabelRenderer

java.lang.Object
  extended by prefuse.render.AbstractShapeRenderer
      extended by prefuse.render.LabelRenderer
All Implemented Interfaces:
Renderer

public class LabelRenderer
extends AbstractShapeRenderer

Renderer that draws a label, which consists of a text string, an image, or both.

When created using the default constructor, the renderer attempts to use text from the "label" field. To use a different field, use the appropriate constructor or use the setTextField(String) method. To perform custom String selection, subclass this Renderer and override the getText(VisualItem) method. When the text field is null, no text label will be shown. Labels can span multiple lines of text, determined by the presence of newline characters ('\n') within the text string.

By default, no image is shown. To show an image, the image field needs to be set, either using the appropriate constructor or the setImageField(String) method. The value of the image field should be a text string indicating the location of the image file to use. The string should be either a URL, a file located on the current classpath, or a file on the local filesystem. If found, the image will be managed internally by an ImageFactory instance, which maintains a cache of loaded images.

The position of the image relative to text can be set using the setImagePosition(int) method. Images can be placed to the left, right, above, or below the text. The horizontal and vertical alignments of either the text or the image can be set explicitly using the appropriate methods of this class (e.g., setHorizontalTextAlignment(int)). By default, both the text and images are centered along both the horizontal and vertical directions.

Author:
jeffrey heer

Field Summary
protected  int m_arcHeight
           
protected  int m_arcWidth
           
protected  java.awt.geom.RectangularShape m_bbox
          The holder for the currently computed bounding box
protected  java.lang.String m_delim
           
protected  java.awt.Font m_font
           
protected  int m_hImageAlign
           
protected  int m_horizBorder
           
protected  int m_hTextAlign
           
protected  int m_imageMargin
           
protected  java.lang.String m_imageName
           
protected  int m_imagePos
           
protected  ImageFactory m_images
           
protected  java.lang.String m_labelName
           
protected  int m_maxTextWidth
           
protected  java.awt.geom.Point2D m_pt
           
protected  java.lang.String m_text
           
protected  java.awt.Dimension m_textDim
           
protected  int m_vertBorder
           
protected  int m_vImageAlign
           
protected  int m_vTextAlign
           
protected  int m_xAlign
           
protected  int m_yAlign
           
 
Fields inherited from class prefuse.render.AbstractShapeRenderer
m_manageBounds, RENDER_TYPE_DRAW, RENDER_TYPE_DRAW_AND_FILL, RENDER_TYPE_FILL, RENDER_TYPE_NONE
 
Fields inherited from interface prefuse.render.Renderer
DEFAULT_GRAPHICS
 
Constructor Summary
LabelRenderer()
          Create a new LabelRenderer.
LabelRenderer(java.lang.String textField)
          Create a new LabelRenderer.
LabelRenderer(java.lang.String textField, java.lang.String imageField)
          Create a new LabelRenderer.
 
Method Summary
protected static void getAlignedPoint(java.awt.geom.Point2D p, VisualItem item, double w, double h, int xAlign, int yAlign)
          Helper method, which calculates the top-left co-ordinate of an item given the item's alignment.
 int getHorizontalAlignment()
          Get the horizontal alignment of this node with respect to its x, y coordinates.
 int getHorizontalImageAlignment()
          Get the horizontal image alignment within the layout.
 int getHorizontalPadding()
          Returns the amount of padding in pixels between the content and the border of this item along the horizontal dimension.
 int getHorizontalTextAlignment()
          Get the horizontal text alignment within the layout.
protected  java.awt.Image getImage(VisualItem item)
          Get the image to include in the label for the given VisualItem.
 ImageFactory getImageFactory()
          Returns the image factory used by this renderer.
 java.lang.String getImageField()
          Get the data field for image locations.
protected  java.lang.String getImageLocation(VisualItem item)
          Returns a location string for the image to draw.
 int getImagePosition()
          Get the image position, determining where the image is placed with respect to the text.
 int getImageTextPadding()
          Get the padding, in pixels, between an image and text.
protected  java.awt.Shape getRawShape(VisualItem item)
          Return a non-transformed shape for the visual representation of the item.
protected  java.lang.String getText(VisualItem item)
          Returns the text to draw.
 java.lang.String getTextField()
          Get the field name to use for text labels.
 int getVerticalAlignment()
          Get the vertical alignment of this node with respect to its x, y coordinates.
 int getVerticalImageAlignment()
          Get the vertical image alignment within the layout.
 int getVerticalPadding()
          Returns the amount of padding in pixels between the content and the border of this item along the vertical dimension.
 int getVerticalTextAlignment()
          Get the vertical text alignment within the layout.
 void render(java.awt.Graphics2D g, VisualItem item)
          Render item into a Graphics2D context.
 void setHorizontalAlignment(int align)
          Set the horizontal alignment of this node with respect to its x, y coordinates.
 void setHorizontalImageAlignment(int halign)
          Set the horizontal image alignment within the layout.
 void setHorizontalPadding(int xpad)
          Sets the amount of padding in pixels between the content and the border of this item along the horizontal dimension.
 void setHorizontalTextAlignment(int halign)
          Set the horizontal text alignment within the layout.
 void setImageFactory(ImageFactory ifact)
          Sets the image factory used by this renderer.
 void setImageField(java.lang.String imageField)
          Set the data field for image locations.
 void setImagePosition(int pos)
          Set the image position, determining where the image is placed with respect to the text.
 void setImageTextPadding(int pad)
          Set the padding, in pixels, between an image and text.
 void setMaxImageDimensions(int width, int height)
          Sets the maximum image dimensions, used to control scaling of loaded images.
 void setMaxTextWidth(int maxWidth)
          Sets the maximum width that should be allowed of the text label.
 void setRoundedCorner(int arcWidth, int arcHeight)
          Rounds the corners of the bounding rectangle in which the text string is rendered.
 void setTextField(java.lang.String textField)
          Set the field name to use for text labels.
 void setVerticalAlignment(int align)
          Set the vertical alignment of this node with respect to its x, y coordinates.
 void setVerticalImageAlignment(int valign)
          Set the vertical image alignment within the layout.
 void setVerticalPadding(int ypad)
          Sets the amount of padding in pixels between the content and the border of this item along the vertical dimension.
 void setVerticalTextAlignment(int valign)
          Set the vertical text alignment within the layout.
 
Methods inherited from class prefuse.render.AbstractShapeRenderer
drawShape, getRenderType, getShape, getStroke, getTransform, locatePoint, setBounds, setManageBounds, setRenderType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_images

protected ImageFactory m_images

m_delim

protected java.lang.String m_delim

m_labelName

protected java.lang.String m_labelName

m_imageName

protected java.lang.String m_imageName

m_xAlign

protected int m_xAlign

m_yAlign

protected int m_yAlign

m_hTextAlign

protected int m_hTextAlign

m_vTextAlign

protected int m_vTextAlign

m_hImageAlign

protected int m_hImageAlign

m_vImageAlign

protected int m_vImageAlign

m_imagePos

protected int m_imagePos

m_horizBorder

protected int m_horizBorder

m_vertBorder

protected int m_vertBorder

m_imageMargin

protected int m_imageMargin

m_arcWidth

protected int m_arcWidth

m_arcHeight

protected int m_arcHeight

m_maxTextWidth

protected int m_maxTextWidth

m_bbox

protected java.awt.geom.RectangularShape m_bbox
The holder for the currently computed bounding box


m_pt

protected java.awt.geom.Point2D m_pt

m_font

protected java.awt.Font m_font

m_text

protected java.lang.String m_text

m_textDim

protected java.awt.Dimension m_textDim
Constructor Detail

LabelRenderer

public LabelRenderer()
Create a new LabelRenderer. By default the field "label" is used as the field name for looking up text, and no image is used.


LabelRenderer

public LabelRenderer(java.lang.String textField)
Create a new LabelRenderer. Draws a text label using the given text data field and does not draw an image.

Parameters:
textField - the data field for the text label.

LabelRenderer

public LabelRenderer(java.lang.String textField,
                     java.lang.String imageField)
Create a new LabelRenderer. Draws a text label using the given text data field, and draws the image at the location reported by the given image data field.

Parameters:
textField - the data field for the text label
imageField - the data field for the image location. This value in the data field should be a URL, a file within the current classpath, a file on the filesystem, or null for no image. If the imageField parameter is null, no images at all will be drawn.
Method Detail

setRoundedCorner

public void setRoundedCorner(int arcWidth,
                             int arcHeight)
Rounds the corners of the bounding rectangle in which the text string is rendered. This will only be seen if either the stroke or fill color is non-transparent.

Parameters:
arcWidth - the width of the curved corner
arcHeight - the height of the curved corner

getTextField

public java.lang.String getTextField()
Get the field name to use for text labels.

Returns:
the data field for text labels, or null for no text

setTextField

public void setTextField(java.lang.String textField)
Set the field name to use for text labels.

Parameters:
textField - the data field for text labels, or null for no text

setMaxTextWidth

public void setMaxTextWidth(int maxWidth)
Sets the maximum width that should be allowed of the text label. A value of -1 specifies no limit (this is the default).

Parameters:
maxWidth - the maximum width of the text or -1 for no limit

getText

protected java.lang.String getText(VisualItem item)
Returns the text to draw. Subclasses can override this class to perform custom text selection.

Parameters:
item - the item to represent as a String
Returns:
a String to draw

getImageField

public java.lang.String getImageField()
Get the data field for image locations. The value stored in the data field should be a URL, a file within the current classpath, a file on the filesystem, or null for no image.

Returns:
the data field for image locations, or null for no images

setImageField

public void setImageField(java.lang.String imageField)
Set the data field for image locations. The value stored in the data field should be a URL, a file within the current classpath, a file on the filesystem, or null for no image. If the imageField parameter is null, no images at all will be drawn.

Parameters:
imageField - the data field for image locations, or null for no images

setMaxImageDimensions

public void setMaxImageDimensions(int width,
                                  int height)
Sets the maximum image dimensions, used to control scaling of loaded images. This scaling is enforced immediately upon loading of the image.

Parameters:
width - the maximum width of images (-1 for no limit)
height - the maximum height of images (-1 for no limit)

getImageLocation

protected java.lang.String getImageLocation(VisualItem item)
Returns a location string for the image to draw. Subclasses can override this class to perform custom image selection beyond looking up the value from a data field.

Parameters:
item - the item for which to select an image to draw
Returns:
the location string for the image to use, or null for no image

getImage

protected java.awt.Image getImage(VisualItem item)
Get the image to include in the label for the given VisualItem.

Parameters:
item - the item to get an image for
Returns:
the image for the item, or null for no image

getRawShape

protected java.awt.Shape getRawShape(VisualItem item)
Description copied from class: AbstractShapeRenderer
Return a non-transformed shape for the visual representation of the item. Subclasses must implement this method.

Specified by:
getRawShape in class AbstractShapeRenderer
Parameters:
item - the VisualItem being drawn
Returns:
the "raw", untransformed shape.
See Also:
AbstractShapeRenderer.getRawShape(prefuse.visual.VisualItem)

getAlignedPoint

protected static void getAlignedPoint(java.awt.geom.Point2D p,
                                      VisualItem item,
                                      double w,
                                      double h,
                                      int xAlign,
                                      int yAlign)
Helper method, which calculates the top-left co-ordinate of an item given the item's alignment.


render

public void render(java.awt.Graphics2D g,
                   VisualItem item)
Description copied from interface: Renderer
Render item into a Graphics2D context.

Specified by:
render in interface Renderer
Overrides:
render in class AbstractShapeRenderer
Parameters:
g - the Graphics2D context
item - the visual item to draw
See Also:
Renderer.render(java.awt.Graphics2D, prefuse.visual.VisualItem)

getImageFactory

public ImageFactory getImageFactory()
Returns the image factory used by this renderer.

Returns:
the image factory

setImageFactory

public void setImageFactory(ImageFactory ifact)
Sets the image factory used by this renderer.

Parameters:
ifact - the image factory

getHorizontalTextAlignment

public int getHorizontalTextAlignment()
Get the horizontal text alignment within the layout. One of Constants.LEFT, Constants.RIGHT, or Constants.CENTER. The default is centered text.

Returns:
the horizontal text alignment

setHorizontalTextAlignment

public void setHorizontalTextAlignment(int halign)
Set the horizontal text alignment within the layout. One of Constants.LEFT, Constants.RIGHT, or Constants.CENTER. The default is centered text.

Parameters:
halign - the desired horizontal text alignment

getVerticalTextAlignment

public int getVerticalTextAlignment()
Get the vertical text alignment within the layout. One of Constants.TOP, Constants.BOTTOM, or Constants.CENTER. The default is centered text.

Returns:
the vertical text alignment

setVerticalTextAlignment

public void setVerticalTextAlignment(int valign)
Set the vertical text alignment within the layout. One of Constants.TOP, Constants.BOTTOM, or Constants.CENTER. The default is centered text.

Parameters:
valign - the desired vertical text alignment

getHorizontalImageAlignment

public int getHorizontalImageAlignment()
Get the horizontal image alignment within the layout. One of Constants.LEFT, Constants.RIGHT, or Constants.CENTER. The default is a centered image.

Returns:
the horizontal image alignment

setHorizontalImageAlignment

public void setHorizontalImageAlignment(int halign)
Set the horizontal image alignment within the layout. One of Constants.LEFT, Constants.RIGHT, or Constants.CENTER. The default is a centered image.

Parameters:
halign - the desired horizontal image alignment

getVerticalImageAlignment

public int getVerticalImageAlignment()
Get the vertical image alignment within the layout. One of Constants.TOP, Constants.BOTTOM, or Constants.CENTER. The default is a centered image.

Returns:
the vertical image alignment

setVerticalImageAlignment

public void setVerticalImageAlignment(int valign)
Set the vertical image alignment within the layout. One of Constants.TOP, Constants.BOTTOM, or Constants.CENTER. The default is a centered image.

Parameters:
valign - the desired vertical image alignment

getImagePosition

public int getImagePosition()
Get the image position, determining where the image is placed with respect to the text. One of Constants.LEFT, Constants.RIGHT, Constants.TOP, or Constants.BOTTOM. The default is left.

Returns:
the image position

setImagePosition

public void setImagePosition(int pos)
Set the image position, determining where the image is placed with respect to the text. One of Constants.LEFT, Constants.RIGHT, Constants.TOP, or Constants.BOTTOM. The default is left.

Parameters:
pos - the desired image position

getHorizontalAlignment

public int getHorizontalAlignment()
Get the horizontal alignment of this node with respect to its x, y coordinates.

Returns:
the horizontal alignment, one of Constants.LEFT, Constants.RIGHT, or Constants.CENTER.

getVerticalAlignment

public int getVerticalAlignment()
Get the vertical alignment of this node with respect to its x, y coordinates.

Returns:
the vertical alignment, one of Constants.TOP, Constants.BOTTOM, or Constants.CENTER.

setHorizontalAlignment

public void setHorizontalAlignment(int align)
Set the horizontal alignment of this node with respect to its x, y coordinates.

Parameters:
align - the horizontal alignment, one of Constants.LEFT, Constants.RIGHT, or Constants.CENTER.

setVerticalAlignment

public void setVerticalAlignment(int align)
Set the vertical alignment of this node with respect to its x, y coordinates.

Parameters:
align - the vertical alignment, one of Constants.TOP, Constants.BOTTOM, or Constants.CENTER.

getHorizontalPadding

public int getHorizontalPadding()
Returns the amount of padding in pixels between the content and the border of this item along the horizontal dimension.

Returns:
the horizontal padding

setHorizontalPadding

public void setHorizontalPadding(int xpad)
Sets the amount of padding in pixels between the content and the border of this item along the horizontal dimension.

Parameters:
xpad - the horizontal padding to set

getVerticalPadding

public int getVerticalPadding()
Returns the amount of padding in pixels between the content and the border of this item along the vertical dimension.

Returns:
the vertical padding

setVerticalPadding

public void setVerticalPadding(int ypad)
Sets the amount of padding in pixels between the content and the border of this item along the vertical dimension.

Parameters:
ypad - the vertical padding

getImageTextPadding

public int getImageTextPadding()
Get the padding, in pixels, between an image and text.

Returns:
the padding between an image and text

setImageTextPadding

public void setImageTextPadding(int pad)
Set the padding, in pixels, between an image and text.

Parameters:
pad - the padding to use between an image and text


Copyright © 2007 Regents of the University of California