FileDocCategorySizeDatePackage
StringItemLFImpl.javaAPI DocphoneME MR2 API (J2ME)11639Wed May 02 18:00:22 BST 2007javax.microedition.lcdui

StringItemLFImpl

public class StringItemLFImpl extends ItemLFImpl implements StringItemLF
Look and feel implementation for StringItem using platform widget.

Fields Summary
private StringItem
strItem
The StringItem associated with this view.
private int
appearanceMode
The appearance mode. The actual appearance of a StringItem could be different to the one set in StringItem. A StringItem created with PLAIN appearance will look like a HYPERLINK if commands were added.
Constructors Summary
StringItemLFImpl(StringItem strItem)
Creates a look&feel for a StringItem.

param
strItem The StringItem associated with this look&feel

        
        super(strItem);
        
        this.strItem = strItem;

        // when no commands are added, the actual appearance
        // is PLAIN; the actual appearance will be the same
        // as appearance set in StringItem if a command is added
        // to this StringItem
        appearanceMode = Item.PLAIN;
    
Methods Summary
voidcreateNativeResource(int ownerId)
Create native resource for current StringItem. Override function in ItemLFImpl.

param
ownerId Owner screen's native resource id

        nativeId = createNativeResource0(ownerId,
                                         strItem.label, strItem.layout,
                                         strItem.str, 
                                         appearanceMode, strItem.font);
    
private native intcreateNativeResource0(int ownerId, java.lang.String label, int layout, java.lang.String text, int appearanceMode, Font font)
KNI function that create native resource for current StringItem.

param
ownerId Owner screen's native resource id (MidpDisplayable *)
param
label label to be used for this Item
param
layout layout directive associated with this Item
param
text text to be used for this StringItem
param
appearanceMode should be PLAIN, HYPERLINK or BUTTON
param
font font face to be used for rendering StringItem content
return
native resource id (MidpItem *) of this StringItem

booleanequateNLA()
Determine if this Item should have a newline after it.

return
true if it should have a newline after

        
        String label = item.label;
        String str = strItem.str;

        // If content ends with a \n, there is a newline after 
        // this StringItem
        if (str != null && str.length() > 0) {
            if (str.charAt(str.length() - 1) == '\n") {
                return true;
            }
        } else if (label != null && label.length() > 0) {
            // If there is no content and our label ends with a \n, 
            // there is a newline after this StringItem
            if (label.charAt(label.length() - 1) == '\n") {
                return true;
            }
        } else {
            // empty StringItem
            return false;
        }
        
        if ((strItem.layout & Item.LAYOUT_2) == Item.LAYOUT_2) {
            return ((item.layout & Item.LAYOUT_NEWLINE_AFTER)
                    == Item.LAYOUT_NEWLINE_AFTER);
        }
        return false;
    
booleanequateNLB()
Determine if this Item should have a newline before it.

return
true if it should have a newline before

        String label = strItem.label;
        String str   = strItem.str;

        // If label starts with a \n, put this StringItem on a newline
        if (label != null && label.length() > 0) {
            if (label.charAt(0) == '\n") {
                return true;
            }
        } else if (str != null && str.length() > 0) {
            // If there is no label and our content starts with a \n,
            // this StringItem starts on a newline
            if (str.charAt(0) == '\n") {
                return true;
            }
        } else {
            // empty StringItem
            return false;
        }
        
        if ((strItem.layout & Item.LAYOUT_2) == Item.LAYOUT_2) {
            return ((strItem.layout & Item.LAYOUT_NEWLINE_BEFORE)
                    == Item.LAYOUT_NEWLINE_BEFORE);
        }
        
        // LAYOUT_2 was not set, hence we need to provide backward 
        // compatibility with MIDP1.0 where any StringItem with a 
        // non-null label would go on a new line.
        return label != null && label.length() > 0;
    
public FontgetDefaultFont()
Gets default font to render text in StringItem if it was not set by the application.

return
- the font to render text if it was not set by the app

        return Theme.curContentFont;
    
public voidlAddCommand(Command cmd, int i)
Notifies L&F of a command addition in the corresponding StringItem.

param
cmd the newly added command
param
i the index of the added command in the StringItem's commands[] array

        super.lAddCommand(cmd, i);

        if ((strItem.numCommands >= 1) && (appearanceMode == Item.PLAIN)) {
            appearanceMode = strItem.appearanceMode == Item.BUTTON ?
                             Item.BUTTON : Item.HYPERLINK;
            if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
                setContent0(nativeId, strItem.str, appearanceMode);
            }
            lRequestInvalidate(true, true);
        }
    
public voidlRemoveCommand(Command cmd, int i)
Notifies L&F of a command removal in the corresponding StringItem.

param
cmd the newly removed command
param
i the index of the removed command in the StringItem's commands[] array

        super.lRemoveCommand(cmd, i);

        // restore the value of the original appearanceMode
        if (strItem.numCommands < 1) {
            appearanceMode = Item.PLAIN;
            if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
                setContent0(nativeId, strItem.str, appearanceMode);
            }
            lRequestInvalidate(true, true);
        }
    
public voidlSetFont(Font font)
Notifies L&F of a font change in the corresponding StringItem.

param
font the new font set in the StringItem

        // Only update native resource if it exists.
        if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
            setFont0(nativeId, 
                     font.getFace(), font.getStyle(), font.getSize());
        }

        lRequestInvalidate(true, true);
    
public voidlSetText(java.lang.String str)
Notifies L&F of a string change in the corresponding StringItem.

param
str the new string set in the StringItem

        // Only update native resource if it exists.
        if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
            setContent0(nativeId, str, appearanceMode);
        }

        lRequestInvalidate(true, true);
    
private native voidsetContent0(int nativeId, java.lang.String text, int appearanceMode)
KNI function that sets text on the native resource corresponding to the current StringItem.

param
nativeId native resource id for this item
param
text new text set on the current StringItem
param
appearanceMode the appearance mode of the text passed in

private native voidsetFont0(int nativeId, int face, int style, int size)
KNI function that sets font on the native resource corresponding to the current StringItem.

param
nativeId native resource id for this item
param
face face of the new font
param
style style of the new font
param
size size of the new font

booleanuCallPeerStateChanged(int hint)
Called by event delivery to notify an ItemLF in current FormLF of a change in its peer state. Handle special gesture of default command.

param
hint -1 signals that user performed the special gesture of default command
return
always false so ItemStateListener will not be notified

 
        // activate default command if hint is -1
        if (hint == -1) {

            Command defaultCommand;
            ItemCommandListener commandListener;

            synchronized (Display.LCDUILock) {

                defaultCommand  = strItem.defaultCommand;
                commandListener = strItem.commandListener;
            }

            if (defaultCommand != null && commandListener != null) {

                // Protect from any unexpected application exceptions
                try {
                    synchronized (Display.calloutLock) {
                        commandListener.commandAction(defaultCommand, strItem);
                    }
                } catch (Throwable thr) {
                    Display.handleThrowable(thr);
                }
            }
        }

        // Indicate to Form to not notify ItemStateListener
        return false;