de.enough.polish.ui
Class TextEffect

java.lang.Object
  extended by de.enough.polish.ui.TextEffect
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AlienGlowTextEffect, BitmapFontTextEffect, CyclingTextEffect, DropShadowTextEffect, FadeTextEffect, FadingAlienGlowTextEffect, FlashTextEffect, HtmlTextEffect, LighthouseTextEffect, OutlineTextEffect, RotateTextEffect, RotatingTextEffect, SegmentTextEffect, ShadowTextEffect, SmileyTextEffect, TypeWriterTextEffect, VerticalGradientTextEffect, VerticalMirrorTextEffect, VerticalScrollTextEffect, VerticalSplitTextEffect

public abstract class TextEffect
extends Object
implements Serializable

Allows text effects for StringItems, IconItems and ChoiceItems.

Copyright (c) Enough Software 2005 - 2009

 history
        16-Nov-2005 - rob creation
 

Author:
Robert Virkus, j2mepolish@enough.de

Field Summary
protected  boolean isTextSensitive
          Specifies if this effect needs a lot of texte dependent resources or processing power.
protected  Style style
           
 
Constructor Summary
TextEffect()
          Creates a new effect
 
Method Summary
 boolean animate()
          Animates this effect.
 void animate(Item parent, long currentTime, ClippingRegion repaintRegion)
          Animates this effect.
 int calculateLinesHeight(WrappedText lines, int lineHeight, int paddingVertical)
          Calculates the content height with the lines, the lineheight and the vertical padding
 int charWidth(char c)
          Retrieves the width of the given char
 void drawChar(char c, int x, int y, int anchor, Graphics g)
          Draws the specified character using this effect.
abstract  void drawString(String text, int textColor, int x, int y, int anchor, Graphics g)
          Paints the text and applies the text effect.
 void drawStrings(FakeCustomItem parent, WrappedText textLines, int textColor, int x, int y, int leftBorder, int rightBorder, int lineHeight, int maxWidth, int layout, Graphics g)
          Paints the text and applies the text effect.
 void drawStrings(Item parent, WrappedText textLines, int textColor, int x, int y, int leftBorder, int rightBorder, int lineHeight, int maxWidth, int layout, Graphics g)
          Paints the text and applies the text effect.
 void drawStrings(WrappedText textLines, int textColor, int x, int y, int leftBorder, int rightBorder, int lineHeight, int maxWidth, int layout, Graphics g)
          Paints the text and applies the text effect.
protected  Font getFont()
          Retrieves the font that should be used.
 int getFontHeight()
          Retrieves the font height by default.
 int getLeftX(int x, int anchor, int textWidth)
          Retrieves the left start position for a text.
static int[] getRgbData(String text, int textColor, Font font)
          Retrieves an RGB integer array in which the text is written on MIDP 2.0 devices.
static int[] getRgbData(String text, int textColor, Font font, int x, int y, int width, int height)
          Retrieves an RGB integer array in which the text is written on MIDP 2.0 devices.
static int[] getRgbData(String text, int textColor, Font font, int x, int y, int width, int height, int transparentColor)
          Retrieves an RGB integer array in which the text is written on MIDP 2.0 devices.
 int getTopY(int y, int anchor, Font font)
          Retrieves the top y position for a text.
 int getTopY(int y, int anchor, int height, int baseLine)
          Retrieves the top y position for a text.
 void hideNotify()
          Notifies this effect that the corresponding item is to be hidden.
 void onAttach(Item parent)
          Notifies the text effect that it has been attached to the specified item.
 void onDetach(Item parent)
          Notifies the text effect that it has been detached to the specified item Subclasses can override this method to get access to the parent item.
 void releaseResources()
          Releases any resources this effect might contain.
 void setStyle(Style style)
          Sets the style of this item.
 void setStyle(Style style, boolean resetStyle)
          Sets the style of this item without assuming defaults for non-set style elements.
 void showNotify()
          Notifies this effect that the corresponding item is to be shown.
 int stringWidth(String str)
          Calculates the width of the given text.
 void wrap(FakeStringCustomItem parent, String text, int textColor, Font font, int firstLineWidth, int lineWidth, int maxLines, String maxLinesAppendix, int maxLinesAppendixPosition, WrappedText wrappedText)
          Wraps the text into several lines and adds the result to the specified wrappedText.
 void wrap(StringItem parent, String text, int textColor, Font font, int firstLineWidth, int lineWidth, int maxLines, String maxLinesAppendix, int maxLinesAppendixPosition, WrappedText wrappedText)
          Wraps the text into several lines and adds the result to the specified wrappedText.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

style

protected transient Style style

isTextSensitive

protected boolean isTextSensitive
Specifies if this effect needs a lot of texte dependent resources or processing power. When this is the case, StringItems will create a StringItem specific copy of this effect instead of using the effect from the Style (which may be used several times).

Constructor Detail

TextEffect

public TextEffect()
Creates a new effect

Method Detail

setStyle

public void setStyle(Style style)
Sets the style of this item. The implementation sets the style field and then calls setStyle( style, false ). Subclasses can override this method for getting specific settings.

Parameters:
style - the new style for this item.
Throws:
NullPointerException - when style is null
See Also:
setStyle(Style, boolean)

onAttach

public void onAttach(Item parent)
Notifies the text effect that it has been attached to the specified item. Subclasses can override this method to get access to the parent item.

Parameters:
parent - the parent item

onDetach

public void onDetach(Item parent)
Notifies the text effect that it has been detached to the specified item Subclasses can override this method to get access to the parent item.

Parameters:
parent - the parent item

setStyle

public void setStyle(Style style,
                     boolean resetStyle)
Sets the style of this item without assuming defaults for non-set style elements. Subclasses can override this method for getting specific settings.

Parameters:
style - the new style for this item.
resetStyle - true when all style elements should be reset to their default when no CSS attributes are defined.
Throws:
NullPointerException - when style is null

animate

public boolean animate()
Animates this effect. Subclasses can override this method to create animations.

Returns:
true when this effect has been animated.

animate

public void animate(Item parent,
                    long currentTime,
                    ClippingRegion repaintRegion)
Animates this effect. Subclasses can override this method to create animations. The default implementation calls the animate() method and adds the full content area to the repaint region.

Parameters:
parent - the parent item
currentTime - the current time in milliseconds
repaintRegion - the repaint area that needs to be updated when this item is animated
See Also:
Item.addRelativeToContentRegion(ClippingRegion, int, int, int, int)

drawStrings

public void drawStrings(FakeCustomItem parent,
                        WrappedText textLines,
                        int textColor,
                        int x,
                        int y,
                        int leftBorder,
                        int rightBorder,
                        int lineHeight,
                        int maxWidth,
                        int layout,
                        Graphics g)
Paints the text and applies the text effect. The default implementation calls drawText( String, int, int, int, int, int, Graphics)

Parameters:
parent - the parent item
textLines - the text
textColor - the color of the text
x - horizontal start coordinate
y - vertical start coordinate
leftBorder - the left border, nothing must be painted left of this position
rightBorder - the right border, nothing must be painted right of this position
lineHeight - the height of a single text line
maxWidth - the width of the longest line
layout - the anchor or the text, e.g. Item.LAYOUT_CENTER or Item.LAYOUT_RIGHT
g - the graphics context
See Also:
drawString( String,int,int,int,int,Graphics)

drawStrings

public void drawStrings(Item parent,
                        WrappedText textLines,
                        int textColor,
                        int x,
                        int y,
                        int leftBorder,
                        int rightBorder,
                        int lineHeight,
                        int maxWidth,
                        int layout,
                        Graphics g)
Paints the text and applies the text effect. The default implementation calls drawStrings(String[], int, int, int, int, int, int, int, int, Graphics)

Parameters:
parent - the parent item
textLines - the text
textColor - the color of the text
x - horizontal start coordinate
y - vertical start coordinate
leftBorder - the left border, nothing must be painted left of this position
rightBorder - the right border, nothing must be painted right of this position
lineHeight - the height of a single text line
maxWidth - the width of the longest line
layout - the anchor or the text, e.g. Item.LAYOUT_CENTER or Item.LAYOUT_RIGHT
g - the graphics context
See Also:
drawStrings(WrappedText, int, int, int, int, int, int, int, int, Graphics)

drawStrings

public void drawStrings(WrappedText textLines,
                        int textColor,
                        int x,
                        int y,
                        int leftBorder,
                        int rightBorder,
                        int lineHeight,
                        int maxWidth,
                        int layout,
                        Graphics g)
Paints the text and applies the text effect. The default implementation calls drawText( String, int, int, int, int, int, Graphics)

Parameters:
textLines - the text
textColor - the color of the text
x - horizontal start coordinate
y - vertical start coordinate
leftBorder - the left border, nothing must be painted left of this position
rightBorder - the right border, nothing must be painted right of this position
lineHeight - the height of a single text line
maxWidth - the width of the longest line
layout - the anchor or the text, e.g. Item.LAYOUT_CENTER or Item.LAYOUT_RIGHT
g - the graphics context
See Also:
drawString( String,int,int,int,int,Graphics)

drawString

public abstract void drawString(String text,
                                int textColor,
                                int x,
                                int y,
                                int anchor,
                                Graphics g)
Paints the text and applies the text effect.

Parameters:
text - the text
textColor - the color of the text
x - x coordinate
y - y coordinate
anchor - the orientation, e.g. Graphics.TOP | Graphics.LEFT or Graphics.TOP | Graphics.HCENTER
g - the graphics context

getLeftX

public int getLeftX(int x,
                    int anchor,
                    int textWidth)
Retrieves the left start position for a text.

Parameters:
x - the x position given in drawString()
anchor - the orientation given in drawString()
textWidth - the width of the text given in drawString()
Returns:
the left x position

getTopY

public int getTopY(int y,
                   int anchor,
                   Font font)
Retrieves the top y position for a text.

Parameters:
y - the y position given in drawString()
anchor - the orientation given in drawString()
font - the used font, usually g.getFont()
Returns:
the top y position.

getTopY

public int getTopY(int y,
                   int anchor,
                   int height,
                   int baseLine)
Retrieves the top y position for a text.

Parameters:
y - the y position given in drawString()
anchor - the orientation given in drawString()
height - the height of the used font
baseLine - the base line of the used font
Returns:
the top y position.

getRgbData

public static int[] getRgbData(String text,
                               int textColor,
                               Font font)
Retrieves an RGB integer array in which the text is written on MIDP 2.0 devices. NOTE: this method is not available on MIDP 1.0 devices! You can determine the height and width of the produced RGB data with this code:
 int[] rgbData = getRgbData(text, textColor, font);
 int height = font.getHeight();
 int width = rgbData.length / height;
 

Parameters:
text - the text
textColor - the color of the text
font - the font of the text
Returns:
the RGB data that contains the given text

getRgbData

public static int[] getRgbData(String text,
                               int textColor,
                               Font font,
                               int x,
                               int y,
                               int width,
                               int height)
Retrieves an RGB integer array in which the text is written on MIDP 2.0 devices. NOTE: this method is not available on MIDP 1.0 devices!

Parameters:
text - the text
textColor - the color of the text
font - the font of the text
x - the left corner of the text in the created rgb data
y - the top corner of the text in the created rgb data
width - the desired width of the data array array, e.g. font.stringWidth(text)
height - the desired height of the data array, e.g. font.getHeight()
Returns:
the RGB data that contains the given text
See Also:
DrawUtil.getComplementaryColor(int)

getRgbData

public static int[] getRgbData(String text,
                               int textColor,
                               Font font,
                               int x,
                               int y,
                               int width,
                               int height,
                               int transparentColor)
Retrieves an RGB integer array in which the text is written on MIDP 2.0 devices. NOTE: this method is not available on MIDP 1.0 devices!

Parameters:
text - the text
textColor - the color of the text
font - the font of the text
x - the left corner of the text in the created rgb data
y - the top corner of the text in the created rgb data
width - the desired width of the data array array, e.g. font.stringWidth(text)
height - the desired height of the data array, e.g. font.getHeight()
transparentColor - the color that should be used to flag transparent parts, using DrawUtil.getComplementaryColor( textColor ) might be a good idea
Returns:
the RGB data that contains the given text
See Also:
DrawUtil.getComplementaryColor(int)

showNotify

public void showNotify()
Notifies this effect that the corresponding item is to be shown. The default implementation is empty.


hideNotify

public void hideNotify()
Notifies this effect that the corresponding item is to be hidden. The default implementation is empty.


releaseResources

public void releaseResources()
Releases any resources this effect might contain. For staying future proof subclasses should call super.releaseResources() first, when overriding this method.


stringWidth

public int stringWidth(String str)
Calculates the width of the given text. By default getFont().stringWidth(text) is returned.

Parameters:
str - the text of which the width should be determined
Returns:
the width of the text

charWidth

public int charWidth(char c)
Retrieves the width of the given char

Parameters:
c - the char
Returns:
the width of that char

getFontHeight

public int getFontHeight()
Retrieves the font height by default.

Returns:
the height of the font

getFont

protected Font getFont()
Retrieves the font that should be used.

Returns:
the font

wrap

public void wrap(StringItem parent,
                 String text,
                 int textColor,
                 Font font,
                 int firstLineWidth,
                 int lineWidth,
                 int maxLines,
                 String maxLinesAppendix,
                 int maxLinesAppendixPosition,
                 WrappedText wrappedText)
Wraps the text into several lines and adds the result to the specified wrappedText. The default implementation just calls TextUtil.wrap(text, font, firstLineWidth, lineWidth, maxLines, maxLinesAppendix).

Parameters:
parent - the parent of this effect
text - the text
textColor - color of the text
font - used font
firstLineWidth - width of the first line
lineWidth - width of following lines
maxLines - the maximum number of lines
maxLinesAppendix - the appendix that should be added to the last line when the line number is greater than maxLines
maxLinesAppendixPosition - either TextUtil.MAXLINES_APPENDIX_POSITION_AFTER or TextUtil.MAXLINES_APPENDIX_POSITION_BEFORE
wrappedText - the wrapped text object to which the single text lines should be added
See Also:
TextUtil.wrap(String, Font, int, int, int, String, int)

wrap

public void wrap(FakeStringCustomItem parent,
                 String text,
                 int textColor,
                 Font font,
                 int firstLineWidth,
                 int lineWidth,
                 int maxLines,
                 String maxLinesAppendix,
                 int maxLinesAppendixPosition,
                 WrappedText wrappedText)
Wraps the text into several lines and adds the result to the specified wrappedText. The default implementation just calls TextUtil.wrap(text, font, firstLineWidth, lineWidth, maxLines, maxLinesAppendix).

Parameters:
parent - the parent of this effect
text - the text
textColor - color of the text
font - used font
firstLineWidth - width of the first line
lineWidth - width of following lines
maxLines - the maximum number of lines
maxLinesAppendix - the appendix that should be added to the last line when the line number is greater than maxLines
maxLinesAppendixPosition - either TextUtil.MAXLINES_APPENDIX_POSITION_AFTER or TextUtil.MAXLINES_APPENDIX_POSITION_BEFORE
wrappedText - the wrapped text object to which the single text lines should be added
See Also:
TextUtil.wrap(String, Font, int, int, int, String, int)

drawChar

public void drawChar(char c,
                     int x,
                     int y,
                     int anchor,
                     Graphics g)
Draws the specified character using this effect. Subclasses may override this - by default the character is just painted.

Parameters:
c - the character
x - horizontal position
y - vertical position
anchor - anchor, e.g. Graphics.TOP | Graphics.LEFT
g - the graphics context

calculateLinesHeight

public int calculateLinesHeight(WrappedText lines,
                                int lineHeight,
                                int paddingVertical)
Calculates the content height with the lines, the lineheight and the vertical padding

Parameters:
lines - the lines
lineHeight - the lineheight
paddingVertical - the vertical padding
Returns:
the height in pixels, normally (lines.size() * lineHeight) - paddingVertical;