001
002 package com.nokia.midp.examples.lcdui.helloworldplus;
003
004 import javax.microedition.lcdui.*;
005
006 /**
007 * The TextScreen class displays a message on the screen.
008 * <p>
009 * This class extends the Form class and uses an instance of StringItem to
010 * display the text message. The Form with StringItem implementation was chosen
011 * because it allows a non-editable text message to be written onto the screen.
012 * <p>
013 * The text message can set via the constructor and setCurrentText() method.
014 */
015
016 public class TextScreen extends Form implements CommandListener {
017
018 /** Reference to the string item added to the form for displaying a message. */
019 private StringItem stringItem;
020
021 /**
022 * Reference to the parent MIDlet class enabling this class
023 * to access the callback methods provided by HelloWorldMIDlet.
024 */
025 private final HelloWorldPlusMIDlet midlet;
026
027 /** Priority of commands relative to others of the same type. */
028 private static final int COMMAND_PRIORITY = 1;
029
030 /** Command specified to initiate the termination of the MIDlet. */
031
032 private static final Command CMD_EXIT =
033 new Command("Exit", Command.EXIT, COMMAND_PRIORITY);
034
035 /** Command specified to initiate the launch of the TextEditor screen. */
036 private static final Command CMD_EDIT =
037 new Command("Edit Text", Command.ITEM, COMMAND_PRIORITY);
038
039 /**
040 * The constructor initializes the class making it ready for use.
041 * <p>
042 * It initializes the reference to the parent MIDlet class with the paramater
043 * value 'midlet'.
044 * <br>It creates an instance of the class ItemString and initializes it with
045 * the parameter value 'message'.
046 * <br>It appends the created instance of ItemString to the form.
047 * <br>It adds the Command objects defined to the command handling framework.
048 * <br>It calls setCommandListener() to register itself with the command framework
049 * as the command listener and will be notified when a command is activated.
050 *
051 * @param midlet is a reference to the parent MIDlet. Enables callback to parent object.
052 * @param message sets displayed text.
053 */
054 TextScreen(HelloWorldPlusMIDlet midlet, String message) {
055 super("Hello World Plus MIDlet");
056 this.midlet = midlet;
057
058 // Create and append a StringItem to the form.
059 stringItem = new StringItem(null,message);
060 stringItem.setLayout(Item.LAYOUT_CENTER);
061 append(stringItem);
062
063 // Add Commands objects to the command handling framework
064 addCommand(CMD_EXIT);
065 addCommand(CMD_EDIT);
066 setCommandListener(this);
067 }
068
069 /**
070 * Sets the text to be displayed.
071 */
072 public void setCurrentText(String message) {
073 stringItem.setText(message);
074 }
075
076 /**
077 * Returns the text currently displayed
078 */
079 public String getCurrentText() {
080 return stringItem.getText();
081 }
082
083 /**
084 * Handles commands received.
085 * <p>
086 * The CMD_EXIT command initiates the termination of the MIDlet. The method
087 * handles this command by calling the parent method exitRequested().
088 * <p>
089 * The CMD_EDIT command initiates the launch of the TextEditor screen. The
090 * method handles this command by calling the parent method textEditorRequested().
091 *
092 * @param cmd is the identity of command received from the framework.
093 * @param source is the identity of the UI area displayed which generated the
094 * command.
095 */
096 public void commandAction(Command cmd, Displayable source) {
097 if (cmd == CMD_EXIT) {
098 midlet.exitRequested();
099 } else if (cmd == CMD_EDIT) {
100 midlet.textEditorRequested();
101 } else {
102 // Functionality to handle for unexpected commands may be added here...
103 }
104 }
105 }