<?xml version="1.0" encoding="Big5"?>
<window title="待辦事項列表" width="640px" border="normal" mode="highlighted">
<zscript>
import java.util.*;
import java.text.*;
// 抓取所有待辦事項
EventDAO evtdao = new EventDAO();
List allEvents = evtdao.findAll();
void add() {
// 從輸入欄位取得資料
Event newEvt = new Event(UUID.randomUUID().toString(),
name.value,priority.value.intValue(),date.value);
// 將資料新增至資料庫
evtdao.insert(newEvt);
// 確保 allEvents 物件有最新的資料
allEvents = evtdao.findAll();
// 將新增資料形成一個 Listitem 物件(或者節點)
Listitem li = new Listitem();
li.setValue(newEvt); // 在之後的 update, delete 會用到
li.appendChild(new Listcell(name.value));
li.appendChild(new Listcell(priority.value.toString()));
li.appendChild(new Listcell(new SimpleDateFormat("yyyy-MM-dd").format(date.value)));
// 將 Listitem 物件變成 box 的子節點
// box 是 listbox 的 id 值
box.appendChild(li);
// 清除輸入欄位
clearData();
}
void move(){
name.value = ((Event) box.selectedItem.value).getName();
priority.value = ((Event) box.selectedItem.value).getPriority();
date.value = ((Event) box.selectedItem.value).getDate();
}
void update(){
// 修改待辦事項物件的內容以及資料庫的內容
Event editEvt = (Event) box.selectedItem.value;
editEvt.setName(name.value);
editEvt.setPriority(priority.value);
editEvt.setDate(date.value);
evtdao.update(editEvt);
// 修改 listbox 的內容
List children = box.selectedItem.children;
((Listcell)children.get(0)).label = name.value;
((Listcell)children.get(1)).label = priority.value.toString();
((Listcell)children.get(2)).label = new SimpleDateFormat("yyyy-MM-dd").format(date.value);
}
void delete(){
evtdao.delete((Event)box.selectedItem.value);
box.removeItemAt(box.getSelectedIndex());
clearData();
}
void clearData(){
// 清除輸入欄位
name.value = null;
priority.value = null;
date.value = null;
}
</zscript>
<listbox id="box" multiple="true" rows="4" onSelect="move()">
<listhead>
<listheader label="待辦事項" />
<listheader label="重要性" width="50px" />
<listheader label="日期" width="90px" />
</listhead>
<listitem forEach="${allEvents}" value="${each}">
<listcell label="${each.name}"/>
<listcell label="${each.priority}"/>
<listcell label="${each.date}"/>
</listitem>
</listbox>
<groupbox>
<caption label="待辦事項管理" />
待辦事項: <textbox id="name" cols="25" />
重要性: <intbox id="priority" cols="1" />
日期: <datebox id="date" cols="8" />
<button label="新增" width="46px" height="24px" onClick="add()"/>
<button label="修改" width="46px" height="24px" onClick="update()"/>
<button label="刪除" width="46px" height="24px" onClick="delete()"/>
</groupbox>
</window>