de.enough.polish.ui
Class ChoiceItem

java.lang.Object
  extended by de.enough.polish.ui.Item
      extended by de.enough.polish.ui.StringItem
          extended by de.enough.polish.ui.IconItem
              extended by de.enough.polish.ui.ChoiceItem
All Implemented Interfaces:
ImageConsumer

public class ChoiceItem
extends IconItem
implements ImageConsumer

Paints a single item of a choice group.

As for IconItem, ChoiceItem is a de.enough.polish extension to group in a same Object text and image, ChoiceItems being aimed at choice items of lists, e.g. List or ChoiceGroup.  Hence, arguments of type ChoiceItem are found in the constructors of these lists, and in all methods for adding items to them, or retrieving items from them.

This is a much better alternative to methods passing separately string and image parts, for (at least) 2 reasons:

  1. for methods taking an array of Strings and Images, such as:
  2. the caller must ensure that both arrays have the same length.  Providing an unique array of ChoiceItem, each of them containing its image and text, solves this error-prone constraint.
  3. there is no easy way to associate data to a given choice. One must create a parallel array containing the associated data and ensure the synchronism between the data array and the displayed choices array.  Extending ChoiceItem is a much robust approach, as shown in this example:
 import javax.microedition.lcdui.*;
 import de.enough.polish.ui.ChoiceItem;
 
 class ListExample extends List implements CommandListener
 {
    static class Entry extends ChoiceItem {
       String data;
       Entry(String text, Image img, String data) {
          //#style default
          super(text, img, List.IMPLICIT);
          this.data = data;
       }
    }
 
    static final Entry[] entries = {
       new Entry("Item #1", null, "data 1"),
       new Entry("Item #2", null, "data 2"),
       new Entry("Item #3", null, "data 3")
    };
 
    ListExample() {
       //#style default
       super("A List", List.IMPLICIT, entries);
       setCommandListener(this);
       display.setCurrent(this);
    }
 
    public void commandAction(Command c, Displayable d)
    {
       int sel;
       if ((c == List.SELECT_COMMAND) && (-1 != (sel = getSelectedIndex()))) {
          System.out.println("Selected data: " + ((Entry)getItem(sel)).data);
       }
    }
 }
 
(Example and JavaDoc provided by Pierre G. Richard)

Copyright Enough Software 2004 - 2008

 history
        05-May-2004 - rob creation
 

Author:
Robert Virkus, robert@enough.de

Field Summary
 boolean drawBox
          determines whether an image is drawn before the item
 boolean isSelected
          determines whether this item is currently selected
protected  javax.microedition.lcdui.Font preferredFont
           
 
Fields inherited from class de.enough.polish.ui.IconItem
horizontalAdjustment, image, imageAlign, imageHeight, imageWidth, isTextVisible, relativeIconX, relativeIconY, scaleData, scaleFinished, verticalAdjustment, yAdjustText
 
Fields inherited from class de.enough.polish.ui.StringItem
animateTextWrap, bitMapFont, bitMapFontViewer, clipText, font, text, textColor, textEffect, textHorizontalAdjustment, textLines, textVerticalAdjustment, textWrapDirection, textWrapSpeed, useSingleLine, xOffset
 
Fields inherited from class de.enough.polish.ui.Item
_bbField, _bbFieldAdded, appearanceMode, background, backgroundHeight, backgroundWidth, backgroundYOffset, border, borderWidth, BUTTON, colSpan, commands, completeBackground, completeBackgroundPadding, completeBorder, contentHeight, contentWidth, contentX, contentY, cssSelector, defaultCommand, focusedStyle, HORIZONTAL, HYPERLINK, includeLabel, INTERACTIVE, internalHeight, internalWidth, internalX, internalY, isFocused, isInitialized, isInvisible, isLayoutCenter, isLayoutExpand, isLayoutRight, isPressed, isShown, isStyleInitialised, itemCommandListener, itemHeight, itemWidth, label, labelStyle, layout, LAYOUT_2, LAYOUT_BOTTOM, LAYOUT_CENTER, LAYOUT_DEFAULT, LAYOUT_EXPAND, LAYOUT_LEFT, LAYOUT_NEWLINE_AFTER, LAYOUT_NEWLINE_BEFORE, LAYOUT_RIGHT, LAYOUT_SHRINK, LAYOUT_TOP, LAYOUT_VCENTER, LAYOUT_VEXPAND, LAYOUT_VSHRINK, marginBottom, marginLeft, marginRight, marginTop, maximumHeight, maximumWidth, minimumHeight, minimumWidth, NO_POSITION_SET, opacity, opacityPaintNormally, opacityRgbData, paddingBottom, paddingHorizontal, paddingLeft, paddingRight, paddingTop, paddingVertical, parent, PLAIN, preferredHeight, preferredWidth, preserveViewType, relativeX, relativeY, rowSpan, screen, style, TRANSPARENT, useSingleRow, VERTICAL, view
 
Constructor Summary
ChoiceItem(java.lang.String text, javax.microedition.lcdui.Image image, int choiceType)
          Creates a new ChoiceItem.
ChoiceItem(java.lang.String text, javax.microedition.lcdui.Image image, int choiceType, Style style)
          Creates a new ChoiceItem.
 
Method Summary
 void adjustProperties(Item lastItem)
           
protected  java.lang.String createCssSelector()
          Retrieves the CSS selector for this item.
 javax.microedition.lcdui.Image getPlainImage()
          Gets the image representing the "not selected" state of this choice item.
 javax.microedition.lcdui.Image getSelectedImage()
          Gets the image representing the "selected" state of this choice item.
protected  void initContent(int firstLineWidth, int lineWidth)
          Initialises this item.
 void paintContent(int x, int y, int leftBorder, int rightBorder, javax.microedition.lcdui.Graphics g)
          Paints the content of this item.
 void select(boolean select)
          Selects or deselects this item.
 void setChoiceType(int choiceType)
          Changes the type of this ChoiceItem
 void setImage(java.lang.String name, javax.microedition.lcdui.Image image)
          sets the image which has been loaded in the background.
 void setPlainImage(javax.microedition.lcdui.Image image)
          Sets the image representing the "not selected" state of this choice item.
 void setPreferredFont(javax.microedition.lcdui.Font font)
          Sets the preferred font.
 void setSelectedImage(javax.microedition.lcdui.Image image)
          Sets the image representing the "selected" state of this choice item.
 void setStyle(Style style)
          Sets the style of this item.
 void toggleSelect()
          Changes the selected-state to the opposite state.
 
Methods inherited from class de.enough.polish.ui.IconItem
animate, defocus, getImage, isTextVisible, releaseResources, setImage, setImage, setImage, setImageAlign, setTextVisible, toString
 
Methods inherited from class de.enough.polish.ui.StringItem
getFont, getFontHeight, getText, hideNotify, setFont, setText, setText, setTextColor, showNotify, stringWidth
 
Methods inherited from class de.enough.polish.ui.Item
addCommand, addCommand, addCommands, addRelativeToBackgroundRegion, addRelativeToBackgroundRegion, addRelativeToContentRegion, animate, containsCommand, focus, getAbsoluteX, getAbsoluteY, getAppearanceMode, getAttribute, getAttributes, getBackgroundHeight, getBackgroundWidth, getBackgroundX, getBackgroundY, getContentHeight, getContentWidth, getContentX, getContentY, getDefaultCommand, getFocusedStyle, getItemAt, getItemCommandListener, getItemCommands, getItemHeight, getItemStateListener, getItemWidth, getLabel, getLabelItem, getLayout, getMinimumHeight, getMinimumWidth, getParent, getPreferredHeight, getPreferredWidth, getScreen, getStyle, handleCommand, handleKeyPressed, handleKeyReleased, handleKeyRepeated, handlePointerPressed, handlePointerReleased, init, initStyle, isInContentArea, isInItemArea, isInItemArea, isInitialized, isVisible, notifyItemPressedEnd, notifyItemPressedStart, notifyStateChanged, paint, paintBackground, paintBackgroundAndBorder, paintBorder, removeCommand, repaint, repaint, repaintFully, requestInit, setAppearanceMode, setAttribute, setDefaultCommand, setItemCommandListener, setItemCommandListener, setItemStateListener, setLabel, setLayout, setParent, setParent, setPreferredSize, setVisible, show, showCommands
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

isSelected

public boolean isSelected
determines whether this item is currently selected


drawBox

public boolean drawBox
determines whether an image is drawn before the item


preferredFont

protected javax.microedition.lcdui.Font preferredFont
Constructor Detail

ChoiceItem

public ChoiceItem(java.lang.String text,
                  javax.microedition.lcdui.Image image,
                  int choiceType)
Creates a new ChoiceItem.

Parameters:
text - the text of the item
image - the image of the item
choiceType - the type of this item, either Choice.EXCLUSIVE, Choice.MULTIPLE, Choice.IMPLICIT or Choice.POPUP. When IMPLICIT or POPUP is used, no selection-box will be drawn.

ChoiceItem

public ChoiceItem(java.lang.String text,
                  javax.microedition.lcdui.Image image,
                  int choiceType,
                  Style style)
Creates a new ChoiceItem.

Parameters:
text - the text of the item
image - the image of the item
choiceType - the type of this item, either Choice.EXCLUSIVE, Choice.MULTIPLE, Choice.IMPLICIT or Choice.POPUP. When IMPLICIT or POPUP is used, no selection-box will be drawn.
style - the CSS style of this item
Method Detail

setChoiceType

public void setChoiceType(int choiceType)
Changes the type of this ChoiceItem

Parameters:
choiceType - eithe Choice.EXCLUSIVE, Choice.MULTPIPLE or Choice.IMPLICIT

createCssSelector

protected java.lang.String createCssSelector()
Description copied from class: Item
Retrieves the CSS selector for this item. The CSS selector is used for the dynamic assignment of styles - that is the styles are assigned by the usage of the item and not by a predefined style-name. With the #style preprocessing command styles are set fix, this method yields in a faster GUI and is recommended. When in a style-sheet dynamic styles are used, e.g. "Form>p", than the selector of the item is needed.
This abstract method needs only be implemented, when dynamic styles are used: #ifdef polish.useDynamicStyles
The returned selector needs to be in lower case.

Overrides:
createCssSelector in class IconItem
Returns:
the appropriate CSS selector for this item. The selector needs to be in lower case.

initContent

protected void initContent(int firstLineWidth,
                           int lineWidth)
Description copied from class: Item
Initialises this item. The implementation needs to calculate and set the contentWidth and contentHeight fields. The implementation should take the fields preferredWidth and preferredHeight into account.

Overrides:
initContent in class IconItem
Parameters:
firstLineWidth - the maximum width of the first line
lineWidth - the maximum width of any following lines
See Also:
Item.contentWidth, Item.contentHeight, Item.preferredWidth, Item.preferredHeight

paintContent

public void paintContent(int x,
                         int y,
                         int leftBorder,
                         int rightBorder,
                         javax.microedition.lcdui.Graphics g)
Description copied from class: Item
Paints the content of this item. The background has already been painted and the border will be added after this method returns.

Overrides:
paintContent in class IconItem
Parameters:
x - the left start position
y - the upper start position
leftBorder - the left border, nothing must be painted left of this position
rightBorder - the right border, nothing must be painted right of this position
g - the Graphics on which this item should be painted.

setStyle

public void setStyle(Style style)
Description copied from class: Item
Sets the style of this item.

Overrides:
setStyle in class IconItem
Parameters:
style - the new style for this item.

setImage

public void setImage(java.lang.String name,
                     javax.microedition.lcdui.Image image)
Description copied from interface: ImageConsumer
sets the image which has been loaded in the background.

Specified by:
setImage in interface ImageConsumer
Overrides:
setImage in class IconItem
Parameters:
name - the name of the image. This allows the image-consumer to differentiate between several images.
image - the image which was loaded in the background thread.

select

public void select(boolean select)
Selects or deselects this item.

Parameters:
select - true when this item should be selected

toggleSelect

public void toggleSelect()
Changes the selected-state to the opposite state.


setPreferredFont

public void setPreferredFont(javax.microedition.lcdui.Font font)
Sets the preferred font. This setting is ignored, since the J2ME Polish implementation uses the CSS settings.

Parameters:
font - the preferred font

adjustProperties

public void adjustProperties(Item lastItem)
Parameters:
lastItem - the item to use the values from for adjustoing

setPlainImage

public void setPlainImage(javax.microedition.lcdui.Image image)
Sets the image representing the "not selected" state of this choice item.

Parameters:
image - the image representing the "not selected" state

getPlainImage

public javax.microedition.lcdui.Image getPlainImage()
Gets the image representing the "not selected" state of this choice item.

Returns:
the image representing the "not selected" state

setSelectedImage

public void setSelectedImage(javax.microedition.lcdui.Image image)
Sets the image representing the "selected" state of this choice item.

Parameters:
image - the image representing the "selected" state

getSelectedImage

public javax.microedition.lcdui.Image getSelectedImage()
Gets the image representing the "selected" state of this choice item.

Returns:
the image representing the "selected" state