AlertMessage.java

/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 
package com.nokia.example.bcexchanger.ui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import com.nokia.example.bcexchanger.*;

/**
 * 
 * This class represents a screen displaying an alert message. The
 * class is extending an example.BCExchanger.ui.Screen class
 * 
 * @see example.BCExchanger.ui.Screen Design patterns: State
 */
public class AlertMessage extends Screen {

    Alert alert;
    Screen nextScreen = null;
    boolean quitAfterDismiss = false;

    /**
     * Constructor
     * 
     * @param _midlet -
     *          the parent class which keeps the current UI state
     * @param message -
     *          text to display
     * @param _nextScreen -
     *          UI screen should be displayed after the alarm message is
     *          dismissed
     */
    public AlertMessage(BCExchangerMIDlet _midlet, String message,
            Screen _nextScreen) {
        super(_midlet);

        nextScreen = _nextScreen;
        init(message);
    }

    /**
     * Constructor
     * 
     * If this constructor is used, the application will exit after the
     * alert is dismissed
     * 
     * @param _midlet -
     *          the parent class which keeps the current UI state
     * @param message -
     *          text to display
     */
    public AlertMessage(BCExchangerMIDlet _midlet, String message) {
        super(_midlet);

        //quitAfterDismiss = true;
        init(message);
    }

    /**
     * Initialize the displayable
     * 
     * @param message -
     *          text to display
     */
    private void init(String message) {

        alert = new Alert("", message, null, null);
        alert.setCommandListener(this);

        displayable = alert;
    }

    public void commandAction(Command command, Displayable _displayable) {
        if (_displayable != alert) {
            // wrong displayable
            throw new RuntimeException("Internal error #28");
        }

        if (command == Alert.DISMISS_COMMAND) {
            if (quitAfterDismiss) {
                midlet.quit();
            } else {
                if (nextScreen != null) {
                    midlet.changeScreen(nextScreen);
                }
            }
        } else {
            // unknown command
            throw new RuntimeException("Internal error #29");
        }
    }
}