001
002 package com.nokia.midp.examples.lcdui.helloworldplus;
003
004 import javax.microedition.lcdui.*;
005
006 /**
007 * The TextEditor class allows the user to edit the message displayed.
008 * <p>
009 * This class extends the Form class and uses an instance of TextField to
010 * provide an editable text field.
011 * <p>
012 * The initial text message can set via the constructor or via setText() method.
013 */
014
015 public class TextEditor extends Form implements CommandListener {
016
017 /** Reference to the instance of the TextField class created and appended to the form. */
018 private TextField textField;
019
020 /**
021 * Back reference to parent MIDlet class enabling this class to access
022 * to access the callback methods provided by HelloWorldMIDlet.
023 */
024 private final HelloWorldPlusMIDlet midlet;
025
026 /** Priority of commands relative to others of the same type. */
027 private static final int COMMAND_PRIORITY = 1;
028
029 /** Command specified to dismiss the TextEditor form and accept the edited text. */
030 private static final Command CMD_OK =
031 new Command("OK", Command.OK, COMMAND_PRIORITY);
032
033 /** Command specified to dismiss the TextEditor form and reject the edited text. */
034 private static final Command CMD_CANCEL =
035 new Command("Cancel", Command.CANCEL, COMMAND_PRIORITY);
036
037 /** The maximum size of the text allowed in the text field. */
038 private static final int MAX_TEXT_SIZE = 40;
039
040 /**
041 * The constructor initializes the class making it ready for use.
042 * <p>
043 * It initializes the reference to the parent MIDlet class with the paramater
044 * value 'midlet'.
045 * <br>It creates an instance of TextField initialized with the parameter 'message'.
046 * <br>It appends the created instance of TextField 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 the opening text in the text field.
053 */
054 TextEditor(HelloWorldPlusMIDlet midlet, String message) {
055 super("Hello World Plus MIDlet");
056 this.midlet = midlet;
057
058 // Create and append an instance of TextField to the form.
059 textField = new TextField("Edit message",
060 message,
061 MAX_TEXT_SIZE,
062 TextField.ANY);
063 append(textField);
064
065 // Add the Commands define to the command handling framework
066 addCommand(CMD_OK);
067 addCommand(CMD_CANCEL);
068 setCommandListener(this);
069 }
070
071 /**
072 * Handles commands received.
073 * <p>
074 * The CMD_OK command instructs the MIDlet to dismiss the form and accept
075 * the edited text. The method handles this command by calling the parent
076 * method textEditorDone() passing an initialised instance of String.
077 * <p>
078 * The CMD_CANCEL command instructs the MIDlet to dismiss the form and
079 * reject the edited text. The method handles this command by calling the
080 * parent method textEditorDone() passing an uninitialised reference.
081 *
082 * @param cmd is the identity of command received from the framework.
083 * @param source is the identity of the UI area displayed which generated the
084 * command.
085 */
086 public void commandAction(Command cmd, Displayable source) {
087 if (cmd == CMD_OK) {
088 midlet.textEditorDone(textField.getString());
089 } else if (cmd == CMD_CANCEL) {
090 midlet.textEditorDone(null);
091 } else {
092 // Functionality to handle for unexpected commands may be added here...
093 }
094 }
095
096 /**
097 * Sets the opening text in the textfield.
098 *
099 * @param message is the opening text to be displayed
100 */
101 public void setText(String message) {
102 textField.setString(message);
103 }
104
105 }