仕様・設計と実装 (improve の機能仕様と MIDP のつまずきどころ)
001:import javax.microedition.lcdui.*;
002:
003:/**
004: * 確認ダイアログ 1 の画面を提供します。
005: */
006:public class DialogTest1Form extends Form implements CommandListener {
007:
008: /** 「Yes」と「No」 */
009: private static final String DIALOG_YES = "Yes";
010: private static final String DIALOG_NO = "No";
011:
012: /** 「結果」のラベルとテキスト */
013: private StringItem itemResult;
014:
015: /** 「表示」コマンド */
016: private Command cmdShow;
017:
018: /**
019: * コンストラクタ。画面の準備をします。
020: */
021: public DialogTest1Form() {
022: super("DialogTest1");
023:
024: // コマンドの作成とリスナーの登録
025: cmdShow = new Command("表示", Command.SCREEN, 1);
026: addCommand(cmdShow);
027:
028: setCommandListener(this);
029:
030: // コンポーネントの初期化
031: itemResult = new StringItem("ダイアログの結果:\n", " ");
032: append(itemResult);
033: append("----- [説明] -----\n「表示」で確認ダイアログを表示します。");
034: }
035:
036: /**
037: * コマンドハンドラ。各コマンドが発行された時の処理を行います。
038: * @param c コマンド
039: * @param d ディスプレイ
040: */
041: public void commandAction(Command c, Displayable d) {
042: if (c == cmdShow) {
043: // 「表示」が押された時の処理
044: // 確認ダイアログを表示
045: int result = showDialog("確認ダイアログ1", "OK ですか?", d);
046:
047: // 結果を画面に表示
048: switch (result) {
049: case DialogForm.STATUS_LEFT:
050: itemResult.setText(DIALOG_YES);
051: break;
052:
053: case DialogForm.STATUS_RIGHT:
054: itemResult.setText(DIALOG_NO);
055: break;
056:
057: default:
058: itemResult.setText("");
059: break;
060: }
061: }
062: }
063:
064: /**
065: * 確認ダイアログを表示
066: * @param title ダイアログのタイトル
067: * @param message ダイアログのメッセージ
068: * @param disp 現在の Displayable
069: */
070: public int showDialog(String title, String message, Displayable disp) {
071: // ダイアログを作成、表示
072: DialogForm dialog = new DialogForm(title, message);
073: DialogTest1MIDlet.display.setCurrent(dialog);
074:
075: // ソフトキーが押されるのを待つ
076: synchronized (dialog) {
077: try {
078: dialog.wait();
079: } catch (InterruptedException e) {
080: e.printStackTrace();
081: }
082: }
083:
084: // 元の画面に切り替える
085: DialogTest1MIDlet.display.setCurrent(disp);
086:
087: return dialog.getCommandStatus();
088: }
089:
090: /**
091: * 確認ダイアログクラス
092: */
093: private class DialogForm extends Form implements CommandListener {
094: /** ステータス定数 */
095: public static final int STATUS_NONE = 0;
096: public static final int STATUS_LEFT = 1;
097: public static final int STATUS_RIGHT = 2;
098:
099: /** ステータス */
100: private int status;
101:
102: /**
103: * コンストラクタ
104: * @param title ダイアログのタイトル
105: * @param message ダイアログのメッセージ
106: */
107: public DialogForm(String title, String message) {
108: super(title);
109:
110: status = STATUS_NONE;
111:
112: // コマンドの作成とリスナーの登録
113: addCommand(new Command(DIALOG_YES, Command.BACK, 1));
114: addCommand(new Command(DIALOG_NO, Command.OK, 1));
115:
116: setCommandListener(this);
117:
118: // コンポーネントの初期化
119: append(message);
120: }
121:
122: /**
123: * コマンドステータス取得
124: * @return どのキーが押されたかを表わす定数
125: */
126: public int getCommandStatus() {
127: return status;
128: }
129:
130: /**
131: * コマンドハンドラ。各コマンドが発行された時の処理を行います。
132: * @param c コマンド
133: * @param d ディスプレイ
134: */
135: public synchronized void commandAction(Command c, Displayable d) {
136: switch (c.getCommandType()) {
137: case Command.BACK:
138: status = STATUS_LEFT;
139: notify();
140: break;
141:
142: case Command.OK:
143: status = STATUS_RIGHT;
144: notify();
145: break;
146:
147: default:
148: break;
149: }
150: }
151: }
152:}