予定の表示・保存(1)〜スケジュール管理ソフトをS!アプリで作ってみよう(その12)

前回までで、予定の入力のフォームの表示はできた。次は入力された予定をカレンダーの下に表示するようにする。

まず、EventFormクラスを修正して、予定の入力内容を充実させる。


package com.ettem.scheduler;

import javax.microedition.lcdui.*;

/**
* 予定の入力フォーム。
* Saveのボタンが押されたら、呼び出し側に戻る。
*/
public class EventForm extends Form implements CommandListener, ItemStateListener {
private Display display;
private Displayable callerDisplayable;

private Command saveCommand;
private TextField what; // 件名の入力文字列
private final int WHAT_LEN = 256;
private DateField startTime; // 開始日時
private DateField endTime; // 終了日時
private TextField where; // 場所
private final int WHERE_LEN = WHAT_LEN;
private TextField discription; // 説明
private final int DISCRIPTION_LEN = 1024;

private Event event; // 予定

public EventForm(Display display, Displayable callerDisplayable, Event event) {
super("Event");
this.display = display;
this.callerDisplayable = callerDisplayable;
this.event = event;

saveCommand = new Command("Save", Command.OK, 1);
addCommand(saveCommand);
setCommandListener(this);
setItemStateListener(this);

what = new TextField("What", "", WHAT_LEN, TextField.ANY);
append(what);

startTime = new DateField("Start", DateField.DATE_TIME);
startTime.setDate(event.startTime);
append(startTime);
endTime = new DateField("End", DateField.DATE_TIME);
endTime.setDate(event.endTime);
append(endTime);

where = new TextField("Where", "", WHERE_LEN, TextField.ANY);
append(where);
discription = new TextField("Discription", "", DISCRIPTION_LEN, TextField.ANY);
append(discription);
}

public void commandAction(Command command, Displayable displayable) {
if (command == saveCommand) {
Events.setEvent(event);
display.setCurrent(callerDisplayable);
}
}

public void itemStateChanged(Item changedItem) {
if (changedItem == what) {
event.what = ((TextField) what).getString();
} else if (changedItem == startTime) {
event.startTime = ((DateField) changedItem).getDate();
} else if (changedItem == endTime) {
event.endTime = ((DateField) changedItem).getDate();
} else if (changedItem == where) {
event.where = ((TextField) where).getLabel();
} else if (changedItem == discription) {
event.discription = ((TextField) discription).getString();
}
}
}

EventFormクラスのコンストラクタを変更したので、MainCanvasクラスでEventFormのインスタンスを生成している部分を修正する。


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, new Event());
display.setCurrent(eventForm);
} else {
dateToDisplay.setTime(calendar.getMovedDateByAction(action).getTime());
}
calendar.setDate(dateToDisplay);

repaint();
}
}



それから、Eventクラスを修正する。と言うか、名前はそのままで、用途をまるっきり変更する。このクラスが1件の予定を保持する。(もはや画面の表示とは関係がなくなる。)


package com.ettem.scheduler;

import java.util.Calendar;
import java.util.Date;

import javax.microedition.rms.RecordStoreException;

public class Event {
public String what;
public String where;
public String discription;
public Date startTime;
public Date endTime;
public int recordID;
public static final int newRecordID = 0;

public Event() {
what = "";
where = "";
discription = "";
startTime = Calendar.getInstance().getTime();

endTime = Calendar.getInstance().getTime();
recordID = newRecordID;
}

public Event(Date date) {
this();
startTime = date;
endTime = date;
}
}

カレンダーの下に、予定を表示するのは、EventViewと言うクラスで行う。


package com.ettem.scheduler;

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

/**
* 予定を表示する。
*/
public class EventView {
public int x, y; // 予定欄の開始位置。
public int width, height; // 予定欄の幅と高さ。
public Calendar dateToDisplay = Calendar.getInstance();

public EventView(Canvas canvas, int x, int y, int width, int height, Calendar dateToDisplay) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.dateToDisplay.setTime(dateToDisplay.getTime());
}

public void setDate(Calendar date) {
dateToDisplay.setTime(date.getTime());
}

public void paint(Graphics graphics) {
// 描画領域のクリア
graphics.setColor(0xFFFFFF);
graphics.fillRect(x, y, x + width, y + height);

graphics.setColor(0x000000);
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
graphics.setFont(font);

Event events[] = Events.getEvents(dateToDisplay.getTime());
for (int i = 0, disp_y = y; i < events.length; i++, disp_y = disp_y + font.getHeight()) {
graphics.drawString(events[i].what, x, disp_y, Graphics.TOP | Graphics.LEFT);
}
}
}

他の修正は、次回に。