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

AlertLFImpl

public class AlertLFImpl extends DisplayableLFImpl implements AlertLF
Look & Feel implementation of Alert based on platform widget.

Fields Summary
private static com.sun.midp.security.SecurityToken
classSecurityToken
Security token to allow access to implementation APIs
private static final Command
DISMISS_COMMAND
Internal command used to visually represent Alert.DISMISS_COMMAND.
private static final int
DEFAULT_TIMEOUT
The default timeout of all alerts.
private static Timer
timeoutTimer
A Timer which serves all Alert objects to schedule their timeout tasks.
private Alert
alert
Alert associated with this view.
private TimerTask
timerTask
A TimerTask which will be set to expire this Alert after its timeout period has elapsed.
private int
isContentScroll
A flag that indicates whether the content of the alert needs scrolling. Valid values are: -1: unknown, 0: no scrolling, 1: scrolling needed.
private static Image
ALERT_ALRM
An image to be drawn in Alert when it was created with AlertType ALARM.
private static Image
ALERT_CFM
An image to be drawn in Alert when it was created with AlertType CONFIRMATION..
private static Image
ALERT_ERR
An image to be drawn in Alert when it was created with AlertType ERROR.
private static Image
ALERT_INFO
An image to be drawn in Alert when it was created with AlertType INFO.
private static Image
ALERT_WARN
An image to be drawn in Alert when it was created with AlertType WARNING.
Constructors Summary
AlertLFImpl(Alert a)
Creates an AlertLF for the passed in Alert instance.

param
a The Alert associated with this look & feel

        super(a);
        alert = a;
    
Methods Summary
voidcreateNativeResource()
Create native resource for this Alert. Gauge resource will not be created.


        nativeId = createNativeResource0(alert.title,
                        alert.ticker == null ? null : alert.ticker.getString(),
                        alert.type == null ? 0 : alert.type.getType());
    
private native intcreateNativeResource0(java.lang.String title, java.lang.String tickerText, int type)
Create native dialog with image and text widget for this Alert.

param
title the title being passed to native
param
tickerText text to be displayed on the Ticker
param
type the type of Alert
return
native resource id

private ImagegetAlertImage(AlertType alertType)
Get the corresponding image for a given alert type.

param
alertType type defined in AlertType
return
image object to be displayed. Null if type is invalid.

        if (alertType != null) {
            if (alertType.equals(AlertType.INFO)) {
                if (ALERT_INFO == null) {
                    ALERT_INFO = getSystemImage("alert.image_icon_info");
                }
                return ALERT_INFO;
            } else if (alertType.equals(AlertType.WARNING)) {
                if (ALERT_WARN == null) {
                    ALERT_WARN = getSystemImage("alert.image_icon_warn");
                }
                return ALERT_WARN;
            } else if (alertType.equals(AlertType.ERROR)) {
                if (ALERT_ERR == null) {
                    ALERT_ERR = getSystemImage("alert.image_icon_errr");
                }
                return ALERT_ERR;
            } else if (alertType.equals(AlertType.ALARM)) {
                if (ALERT_ALRM == null) {
                    ALERT_ALRM = getSystemImage("alert.image_icon_alrm");
                }
                return ALERT_ALRM;
            } else if (alertType.equals(AlertType.CONFIRMATION)) {
                if (ALERT_CFM == null) {
                    ALERT_CFM = getSystemImage("alert.image_icon_cnfm");
                }
                return ALERT_CFM;
            }
        }

        return null;
    
private ImagegetSystemImage(java.lang.String imageName)
Obtain system image resource and create Image object from it.

param
imageName image name
return
icon image

        byte[] imageData = ResourceHandler.getSystemImageResource(
                classSecurityToken, imageName);
        if (imageData != null) {
            return Image.createImage(imageData, 0, imageData.length);
        } else {
            // Use a empty immutable image as placeholder
            return Image.createImage(Image.createImage(16, 16));
        }
    
voidlCallHide()
Notify this Alert that it will no longer be displayed. Override the version in DisplayableLFImpl.


        // Stop the timer
        if (timerTask != null) {
            try {
                timerTask.cancel();
                timerTask = null;
            } catch (Throwable t) { }
        }

        // Hide and delete gauge resource
        if (alert.indicator != null) {
            GaugeLFImpl gaugeLF = (GaugeLFImpl)alert.indicator.gaugeLF;

            gaugeLF.lHideNativeResource();

            gaugeLF.deleteNativeResource();

            if (gaugeLF.visibleInViewport) {
                gaugeLF.lCallHideNotify();
            }
        }

        // Hide and delete alert dialog window including title and ticker
        super.lCallHide();
    
voidlCallShow()
Notify this Alert that it is being displayed. Override the version in DisplayableLFImpl.


        // Create native resource with title and ticker
        super.lCallShow();

        // Play sound
        if (alert.type != null) {
            currentDisplay.playAlertSound(alert.type);
        }

        // Setup contained items and show them
        showContents();

        // Show the Alert dialog window
        showNativeResource0(nativeId);

        // Start Java timer
        // If native dialog will cause VM to freeze, this timer
        // needs to be moved to native.
        if (alert.time != Alert.FOREVER
            && alert.numCommands == 1
            && isContentScroll == 0) {

            if (timeoutTimer == null) {
                timeoutTimer = new Timer();
            }
            timerTask = new TimeoutTask();
            timeoutTimer.schedule(timerTask, alert.time);
        }
    
public intlGetDefaultTimeout()
Gets default timeout for the Alert associated with this view.

return
the default timeout

        return DEFAULT_TIMEOUT;
    
public CommandlGetDismissCommand()
Return the command that should be mapped to Alert.DISMISS_COMMAND.

return
command that maps to Alert.DISMISS_COMMAND

        return DISMISS_COMMAND;
    
public booleanlIsModal()
Determines if Alert associated with this view is modal.

return
true if this AlertLF should be displayed as modal

        if (alert.numCommands > 1) {
            return true;
        }

        if (isContentScroll < 0) {
            layout();
        }

        return (isContentScroll == 1);
    
voidlRequestInvalidate()
Called upon content change to schedule a request for relayout and repaint.

        super.lRequestInvalidate();
        isContentScroll = -1; // Unknown scrolling state
    
public voidlSetImage(Image oldImg, Image newImg)
Notifies image change.

param
oldImg the old image set in the corresponding Alert.
param
newImg the new image set in the corresponding Alert.

        lRequestInvalidate();
    
public voidlSetIndicator(Gauge oldIndicator, Gauge newIndicator)
Notifies indicator change.

param
oldIndicator the old indicator set in the corresponding Alert.
param
newIndicator the new indicator set in the corresponding Alert.

        lRequestInvalidate();
    
public voidlSetString(java.lang.String oldString, java.lang.String newString)
Notifies string change.

param
oldString the old string set in the corresponding Alert.
param
newString the new string set in the corresponding Alert.

        lRequestInvalidate();
    
public voidlSetTimeout(int timeout)
Notifies timeout change. Changing timeout on an already visible Alert will restart the timer, but has no effect on current layout.

param
timeout the new timeout set in the corresponding Alert.

        if (timerTask != null) {
            try {
                timerTask.cancel();
                if (timeout == Alert.FOREVER) {
                    timerTask = null;
                } else {
                    timerTask = new TimeoutTask();
                    timeoutTimer.schedule(timerTask, timeout);
                }
            } catch (Throwable t) { }
        }
    
public voidlSetType(AlertType type)
Notifies Alert type change. Changing type on an already visible Alert will only update the default icon. No sound will be played.

param
type the new AlertType set in the corresponding Alert.

        lRequestInvalidate();
    
private voidlayout()
Layout the content of this Alert. Query native resource for two informations: - Whether the content needs scrolling, in 'isContentScroll' - Location of the gauge indicator SYNC NOTE: Caller of this function should hold LCDUILock around this call.


        boolean wasNoNative = (nativeId == INVALID_NATIVE_ID);

        // If no native resource yet, create it temporarily
        if (wasNoNative) {
            createNativeResource();
        }

        Image img = alert.image;

        // If no image is specified, default icon for that type should be used
        if (img == null && alert.type != null) {
            img = getAlertImage(alert.type);
        }

        // Bounds array of gauge
        // The reason gauge bounds is passed back from native is to be
        // consistent with Form's Java layout code.
        int[] gaugeBounds;
        GaugeLFImpl gaugeLF;

        if (alert.indicator == null) {
            gaugeLF = null;
            gaugeBounds = null;
        } else {
            // We temporarily use bounds array in gauge
            // The real values will be set later by setSize() and setLocation()
            gaugeLF = (GaugeLFImpl)alert.indicator.gaugeLF;
            gaugeBounds = new int[4];

            // Pass gauge's preferred size to native layout code
            gaugeBounds[WIDTH]  = gaugeLF.lGetPreferredWidth(-1);
            gaugeBounds[HEIGHT] = gaugeLF.lGetPreferredHeight(-1);
        }

        ImageData imageData = null;

        if (img != null) {
            imageData = img.getImageData();
        }

        // Set content to native dialog and get layout information back
        if (setNativeContents0(nativeId, imageData,
                               gaugeBounds, alert.text)) {
            isContentScroll = 1; // scrolling needed
        } else {
            isContentScroll = 0; // no scrolling
        }

        // Set gauge location and size based on return from native layout code
        if (gaugeBounds != null) {
            gaugeLF.lSetSize(gaugeBounds[WIDTH], gaugeBounds[HEIGHT]);
            gaugeLF.lSetLocation(gaugeBounds[X], gaugeBounds[Y]);
        }

        // Native resource should only be kept alive if it's visible
        // Free temporarily created native resource here
        if (wasNoNative) {
            deleteNativeResource();
        }
    
private native booleansetNativeContents0(int nativeId, ImageData imgId, int[] indicatorBounds, java.lang.String text)
Set content to native dialog.

param
nativeId IN this alert's resource id (MidpDisplayable *)
param
imgId IN icon image native id. 0 if no image.
param
indicatorBounds a 4 integer array for indicator gauge [0] : OUT x coordinate in alert dialog [1] : OUT y coordinate in alert dialog [2] : IN/OUT width of the gauge, in pixels [3] : IN/OUT height of the gauge, in pixels null if no indicator gauge present.
param
text IN alert text string
return
true if content requires scrolling

private voidshowContents()
Show or update contents on a visible Alert. SYNC NOTE: Caller must hold LCDUILock around this call.


        // Make sure gauge has native resource ready
        GaugeLFImpl gaugeLF = (alert.indicator == null)
                                        ? null
                                        : (GaugeLFImpl)alert.indicator.gaugeLF;

        if (gaugeLF != null && gaugeLF.nativeId == INVALID_NATIVE_ID) {
            gaugeLF.createNativeResource(nativeId);
        }

        // Re-populate the alert with updated contents
        layout();

        // Make sure gauge is shown
        if (gaugeLF != null) {
            gaugeLF.lShowNativeResource();

            // SYNC NOTE: Since Gauge show and showNotify does not involve
            // application code, we can call it while holding LCDUILock
            gaugeLF.lCallShowNotify();

            // IMPLEMENTATION NOTE: when gauge is present in the Alert
            // its visibleInViewport will always be set to true.
            // If dynamic update of gauge's visibleInViewport flag is
            // required in AlertLFImpl
            // uViewportChanged() can be moved up from FormLFImpl to
            // DisplayableLFImpl
        }
    
private native voidshowNativeResource0(int nativeId)
(Re)Show native dialog with image and text widget for this Alert.

param
nativeId native resource id

public voiduCallInvalidate()
Called by the event handler to perform a re-layout on this AlertLF.

        synchronized (Display.LCDUILock) {
            showContents();
        }
    
public voiduCallSizeChanged(int w, int h)
Notify return screen about screen size change

        super.uCallSizeChanged(w,h);
        Displayable returnScreen = alert.getReturnScreen();
        if (returnScreen != null) {
            (returnScreen.displayableLF).uCallSizeChanged(w,h);
        }