RecordStoreクラスを使ってみる〜スケジュール管理ソフトをS!アプリで作ってみよう(その10)

スケジュール管理ソフトなのだから、予定を保存できないといけない。

予定のデータを保存するにはいくつか方法があるが、一番手軽なRecordStoreクラスを使って保存する事にする。

RecordStoreクラスを使用するにはjadファイルに使用するデータのサイズを定義しなければいけない。サイズの指定はバイト単位で指定する。今回は256KBを指定する事にする。

以下の内容をjadファイルに記述する。


MIDlet-Data-Size: 262144

記述を追加したScheduler.jadファイルの内容は以下の通り


MIDlet-Vendor: Ettem
MIDlet-Name: Scheduler
MIDlet-Version: 1.0.0
MIDlet-1: Scheduler,,com.ettem.scheduler.Scheduler
MIDlet-Jar-URL: Scheduler.jar
MIDlet-Jar-Size: 0
MIDlet-Data-Size: 262144
MIDxlet-ScreenSize: 480,520

これで、RecordStoreクラスを使ってデータを保存できるようになる。

いきなり、予定を登録するのは大変なのでアプリを起動した回数を保存できるようする。また、カレンダーの表示の下(ここに予定を表示するつもり)に保存した値を表示してみる。

まず、SchedulerクラスでRecordStoreをopenし、1番目のレコードに起動回数を記録するようにする


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();
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) {

}
}
}
}

カレンダーを表示している部分の下に、起動回数を表示するようする。(ここの部分は後で、予定を表示するエリアにする) そのためにEventクラスを作成する。


package com.ettem.scheduler;

import java.util.*;
import javax.microedition.lcdui.*;

/*=
* 予定を表示する。
*/
public class Event {
public int x, y; // 予定欄の開始位置。
public int width, height; // 予定欄の幅と高さ。

public Event(Canvas canvas, int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

public void paint(Graphics graphics) {
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
graphics.setFont(font);
graphics.drawString(new Byte(Scheduler.bootCount).toString(), x, y,
Graphics.TOP | Graphics.LEFT);
}
}

Eventクラスを呼び出すように、MainCanvas クラスを修正する。


package com.ettem.scheduler;

import java.util.Calendar;
import javax.microedition.lcdui.*;

/**
* メインの画面
* カレンダーを表示する。
* (その日の予定、ToDoも表示する予定)
*/
public class MainCanvas extends Canvas {
private Calendar now;
private Calendar dateToDisplay;
private MonthlyCalendar calendar;
private Event event;

public MainCanvas() {
now = Calendar.getInstance();
dateToDisplay = Calendar.getInstance();
dateToDisplay.setTime(now.getTime());
calendar = new MonthlyCalendar(this, 0, 0, getWidth(), 0, dateToDisplay);
event = new Event(this, 0, calendar.height, getWidth(), 0);
}

protected void paint(Graphics graphics) {
calendar.paint(graphics);
event.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 {
dateToDisplay.setTime(calendar.getMovedDateByAction(action).getTime());
}
calendar.setDate(dateToDisplay);

repaint();
}
}

しかし、コードの部分にタブを使うのはよくないよなあ... でも、修正してアップロードするのも面倒だし...