仕様・設計と実装 (improve の機能仕様と MIDP のつまずきどころ)
001:import javax.microedition.lcdui.*;
002:
003:/**
004: * 確認ダイアログ 2 の画面を提供します。
005: */
006:public class DialogTest2Form extends Form implements CommandListener {
007:
008: /** コマンド処理ロック */
009: private static boolean lock;
010:
011: /** 「Yes」と「No」 */
012: private static final String DIALOG_YES = "Yes";
013: private static final String DIALOG_NO = "No";
014:
015: /** 「結果」のラベルとテキスト */
016: private StringItem itemResult;
017:
018: /** 「表示」コマンド */
019: private Command cmdShow;
020:
021: /**
022: * コンストラクタ。画面の準備をします。
023: */
024: public DialogTest2Form() {
025: super("DialogTest2");
026:
027: // コマンドの作成とリスナーの登録
028: cmdShow = new Command("表示", Command.SCREEN, 1);
029: addCommand(cmdShow);
030:
031: setCommandListener(this);
032:
033: // コンポーネントの初期化
034: itemResult = new StringItem("ダイアログの結果:\n", " ");
035: append(itemResult);
036: append("----- [説明] -----\n「表示」で確認ダイアログを表示します。");
037: }
038:
039: /**
040: * コマンドハンドラ。各コマンドが発行された時の処理を行います。
041: * @param c コマンド
042: * @param d ディスプレイ
043: */
044: public void commandAction(Command c, Displayable d) {
045: System.out.println("DialogTest2Form#commandAction() "
+ Thread.currentThread());
046: if (lockCommandAction()) {
047: Thread thread = new Thread(new ProcessCommand(c, d));
048: thread.start();
049: }
050: }
051:
052: /**
053: * コマンド処理のロック
054: * @return ロックが取得できたかどうか
055: */
056: private static synchronized boolean lockCommandAction() {
057: if (lock) {
058: return false;
059: }
060: lock = true;
061: return true;
062: }
063:
064: /**
065: * コマンド処理のロック解除
066: */
067: private static synchronized void unlockCommandAction() {
068: lock = false;
069: }
070:
071: /**
072: * コマンド処理スレッドクラス
073: */
074: private class ProcessCommand implements Runnable {
075: private Command command;
076: private Displayable displayable;
077:
078: /**
079: * コンストラクタ
080: */
081: public ProcessCommand(Command c, Displayable d) {
082: command = c;
083: displayable = d;
084: }
085:
086: /**
087: * ここで実際のコマンド処理を行います。
088: */
089: public void run() {
090: try {
091: if (command == cmdShow) {
092: // 「表示」が押された時の処理
093: // 確認ダイアログを表示
094: int result = showDialog("確認ダイアログ2", "OK ですか?");
095:
096: // 結果を画面に表示
097: switch (result) {
098: case DialogForm.STATUS_LEFT:
099: itemResult.setText(DIALOG_YES);
100: break;
101:
102: case DialogForm.STATUS_RIGHT:
103: itemResult.setText(DIALOG_NO);
104: break;
105:
106: default:
107: itemResult.setText("");
108: break;
109: }
110: }
111: } catch (Exception e) {
112: e.printStackTrace();
113: }
114:
115: // コマンド処理が終了したのでロック解除
116: DialogTest2Form.unlockCommandAction();
117: }
118:
119: /**
120: * 確認ダイアログを表示
121: * @param title ダイアログのタイトル
122: * @param message ダイアログのメッセージ
123: */
124: public int showDialog(String title, String message) {
125: // ダイアログを作成、表示
126: DialogForm dialog = new DialogForm(title, message);
127: DialogTest2MIDlet.display.setCurrent(dialog);
128:
129: // ソフトキーが押されるのを待つ
130: synchronized (dialog) {
131: try {
132: dialog.wait();
133: } catch (InterruptedException e) {
134: e.printStackTrace();
135: }
136: }
137:
138: // 元の画面に切り替える
139: DialogTest2MIDlet.display.setCurrent(displayable);
140:
141: return dialog.getCommandStatus();
142: }
143: }
144:
145: /**
146: * 確認ダイアログクラス
147: */
148: private class DialogForm extends Form implements CommandListener {
149:
150: /** ステータス定数 */
151: public static final int STATUS_NONE = 0;
152: public static final int STATUS_LEFT = 1;
153: public static final int STATUS_RIGHT = 2;
154:
155:
156: /** ステータス */
157: private int status;
158:
159: /**
160: * コンストラクタ
161: * @param title ダイアログのタイトル
162: * @param message ダイアログのメッセージ
163: */
164: public DialogForm(String title, String message) {
165: super(title);
166:
167: status = STATUS_NONE;
168:
169: // コマンドの作成とリスナーの登録
170: addCommand(new Command(DIALOG_YES, Command.BACK, 1));
171: addCommand(new Command(DIALOG_NO, Command.OK, 1));
172:
173: setCommandListener(this);
174:
175: // コンポーネントの初期化
176: append(message);
177: }
178:
179: /**
180: * コマンドステータス取得
181: * @return どのキーが押されたかを表わす定数
182: */
183: public int getCommandStatus() {
184: return status;
185: }
186:
187: /**
188: * コマンドハンドラ。各コマンドが発行された時の処理を行います。
189: * @param c コマンド
190: * @param d ディスプレイ
191: */
192: public synchronized void commandAction(Command c, Displayable d) {
193: System.out.println("DialogForm#commandAction() " +
Thread.currentThread());
194: switch (c.getCommandType()) {
195: case Command.BACK:
196: status = STATUS_LEFT;
197: notify();
198: break;
199:
200: case Command.OK:
201: status = STATUS_RIGHT;
202: notify();
203: break;
204:
205: default:
206: break;
207: }
208: }
209: }
210:}