予定の入力画面の作成〜スケジュール管理ソフトをS!アプリで作ってみよう(その11)
今回は、予定の入力画面を作ってみる。
Formクラスを継承して、予定の入力画面を表示する。今回は単純に入力のフォームを表示するだけ。保存も何もされない。(なんじゃそりゃ...) まあ、少しずつ機能拡張していく方針なので少しずつ進めていく。
EventFormと言うクラスを作成する。
package com.ettem.scheduler;import javax.microedition.lcdui.*;
/**
* 予定の入力フォーム
*/
public class EventForm extends Form implements CommandListener {
private Display display;
private Displayable callerDisplayable;
private Command saveCommand;
private TextField what;
final int WHAT_LEN = 256;
public EventForm(Display display, Displayable callerDisplayable) {
super("Event");
this.display = display;
this.callerDisplayable = callerDisplayable;
saveCommand = new Command("Save", Command.OK, 1);
addCommand(saveCommand);
setCommandListener(this);
what = new TextField("What", "", WHAT_LEN, TextField.ANY);
append(what);
}public void commandAction(Command command, Displayable displayable) {
if (command == saveCommand) {
display.setCurrent(callerDisplayable);
}
}
}
MainCanvasクラスを修正して、Fireキー(普通は方向キーの真ん中にあるボタン)を押されたら予定の入力フォームを呼び出すようにする。
package com.ettem.scheduler;import java.util.Calendar;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;/**
* メインの画面
* カレンダーを表示する。
* (その日の予定、ToDoも表示する予定)
*/
public class MainCanvas extends Canvas {
private Display display;
private Calendar now;
private Calendar dateToDisplay;
private MonthlyCalendar calendar;
public MainCanvas(Display display) {
this.display = display;
now = Calendar.getInstance();
dateToDisplay = Calendar.getInstance();
dateToDisplay.setTime(now.getTime());
calendar = new MonthlyCalendar(this, 0, 0, getWidth(), 0, dateToDisplay);
}protected void paint(Graphics graphics) {
calendar.paint(graphics);
}protected void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
// action == 0 で判定できるかと思ったけど、
// KEY_START、KEY_POUNDはGameActionが割当てられていた。
if (keyCode == KEY_STAR
|| keyCode == KEY_POUND) {
dateToDisplay.setTime(calendar.getMovedDateByKeyCode(keyCode).getTime());
} else if (action == FIRE){
EventForm eventForm = new EventForm(display, this);
display.setCurrent(eventForm);
} else {
dateToDisplay.setTime(calendar.getMovedDateByAction(action).getTime());
}
calendar.setDate(dateToDisplay);repaint();
}
}
MainCanvasクラスのコンストラクタを修正したので、MainCanvasクラスを生成しているSchedulerクラスを修正する。画面の遷移を制御するためにDisplayクラスのインスタンスをあちこちのクラスで持ちまわってしまっている。うーん、こんな実装でいいのだろうか? なんか、変な実装のような気がするが、他に案がないのでこんな風にしてしまっている。
package com.ettem.scheduler;import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;/**
* 任意の月の月間カレンダーを表示する。
* STARキー(*)で前月、POUNDキー(#)で次月へ移動する。
* UPキーで1週間前、DOWNキーで1週間後、LEFTキーで1日前、RIGHTキーで1日後へ
* 移動する。
*/
public class Scheduler extends MIDlet implements CommandListener {
private Command exitCommand;
private MainCanvas mainScr;
private RecordStore scheduleRecordStore;
public static byte bootCount = 0;public Scheduler() {
exitCommand = new Command("Exit", Command.EXIT, 1);
mainScr = new MainCanvas(Display.getDisplay(this));
mainScr.addCommand(exitCommand);
mainScr.setCommandListener(this);try {
scheduleRecordStore= RecordStore.openRecordStore("ScheduleBook", true);
if (scheduleRecordStore.getNumRecords() == 0) {
bootCount = 1;
byte bootCountData[] = {bootCount};
scheduleRecordStore.addRecord(bootCountData, 0, 1);
} else {
byte bootCountData[] = scheduleRecordStore.getRecord(1);
bootCount = bootCountData[0];
bootCountData[0] = ++bootCount;
scheduleRecordStore.setRecord(1, bootCountData, 0, 1);
}
scheduleRecordStore.closeRecordStore();
} catch (RecordStoreException e) {
scheduleRecordStore = null;
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException midletStChanExc) {
}
}}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(mainScr);
}public void commandAction(Command command, Displayable display) {
if (command == exitCommand) {
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException e) {
}
}
}
}