📄 calendardemo3.java
字号:
//CalendarDemo3.java file
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import calendar.*;
public class CalendarDemo3 extends MIDlet
implements CommandListener,ImportDateFilter,DateSelectListener
{
private Command exitCommand= new Command("Exit",Command.EXIT,1);
//用于日程列表Form上的菜单
private Command backCmd= new Command("Back",Command.BACK,1);
private Command editCmd =new Command("Edit",Command.SCREEN,1);
private Command addCmd = new Command("Add",Command.SCREEN,1);
private Command delCmd = new Command("Del",Command.SCREEN,2);
//用于修改日程Form上的菜单
private Command okCmd = new Command("OK",Command.OK,1);
private Command cancelCmd = new Command("Cancel",Command.EXIT,1);
private CalendarListForm listForm =null;
private CalendarEditForm editForm =null;
private CalendarCanvas myCanvas;
private int [][]holiday={{1,1},{3,8},{5,1},{6,1},{10,1}};
private Displayable lastDis=null;//保存最后显示的窗口对象
private Calendar dateSelected = null;
//private Hashtable itemTable = null;
private ItemTable itemTable = null; //将对象改为 ItemTable
private CalendarStore store = null; //新增加对象记录文件的管理对象
private boolean firstRun = true; //新增加变量记录程序是否第一次运行
public CalendarDemo3()
{
//创建日历对象
myCanvas =new CalendarCanvas(new Date());
myCanvas.setImportDateFilter(this);
myCanvas.setDateSelectListener(this);
//在日历中添加菜单命令
myCanvas.addCommand(exitCommand);
//替换日历对象的命令处理对象
myCanvas.setCommandListener(this);
lastDis = myCanvas;
//创建日程列表Form对象
listForm = new CalendarListForm();
listForm.addCommand(backCmd);
listForm.addCommand(addCmd);
listForm.addCommand(editCmd);
listForm.addCommand(delCmd);
listForm.setCommandListener(this);
//初始化日程条目表
//itemTable = new Hashtable();
itemTable = new ItemTable();
//创建记录文件的管理对象
store = new CalendarStore(Display.getDisplay(this));
}
protected void startApp( ) throws MIDletStateChangeException
{
if(firstRun)
{//第一次运行时读入数据
itemTable.loadData(store);
firstRun =false;
}
Display.getDisplay(this).setCurrent(lastDis);
}
protected void pauseApp( )
{//暂停时保存当前显示的对象
lastDis = Display.getDisplay(this).getCurrent();
}
protected void destroyApp( boolean p1 )
{//在退出程序时保存数据
itemTable.saveData(store);
}
public void commandAction(Command c,Displayable d)
{//处理命令
if (c ==exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
else if (d == myCanvas)
{//对于来自CalendarCanvas 对象的命令发送给对象自己处理
myCanvas.commandAction(c, d);
}
else if (c ==backCmd)
{//切换到日历界面
Display.getDisplay(this).setCurrent(myCanvas);
}
else if (c ==addCmd)
{//增加新日程
editForm = new CalendarEditForm(dateSelected.getTime());//创建日程修改Form对象
editForm.addCommand(okCmd);
editForm.addCommand(cancelCmd);
editForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(editForm);
}
else if (c ==editCmd)
{//修改现有日程
ScheduleItem item = listForm.getSelectedItem();
if( null != item)
{
editForm = new CalendarEditForm(item);//创建日程修改Form对象
editForm.addCommand(okCmd);
editForm.addCommand(cancelCmd);
editForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(editForm);
}
else
{//当前没有被选中的日程,显示错误信息
Alert alert=new Alert("Empty","No Schedule is selected",null,AlertType.ERROR );
Display.getDisplay(this).setCurrent(alert,listForm);
}
}
else if(c == delCmd)
{
ScheduleItem item = listForm.delSelectItem();//更新CalendarListForm 对象中数据
if (item != null)
{
delItem(item);
item = null;
}
else
{//当前没有被选中的日程,显示错误信息
Alert alert=new Alert("Empty","No Schedule is selected",null,AlertType.ERROR );
Display.getDisplay(this).setCurrent(alert,listForm);
}
}
else if(c == okCmd)
{
if(editForm.getType() == CalendarEditForm.ADD)
{//增加日程
ScheduleItem item = editForm.getScheduleItem();
addItem(item);
Display.getDisplay(this).setCurrent(listForm);
listForm.add(item);//更新CalendarListForm 对象中数据
}
else
{//更改原有日程
ScheduleItem item = editForm.getScheduleItem();
Display.getDisplay(this).setCurrent(listForm);
listForm.update(item);//更新CalendarListForm 对象中数据
}
editForm = null;
}
else if(c == cancelCmd)
{//从 CalendarEditForm 中取消增加或修改
Display.getDisplay(this).setCurrent(listForm);
editForm = null;
}
}
//添加新的日程记录
protected void addItem(ScheduleItem item)
{
Calendar date=Calendar.getInstance();
date.setTime(item.itemTime);
String key=date.get(Calendar.YEAR)+"/"+(date.get(Calendar.MONTH)+1)+"/"+date.get(Calendar.DAY_OF_MONTH);
Vector array = (Vector)itemTable.get(key);
if(array == null)
{
array = new Vector();
itemTable.put(key, array);
}
array.addElement(item);
}
//删除日程记录
protected void delItem(ScheduleItem item)
{
Calendar date=Calendar.getInstance();
date.setTime(item.itemTime);
String key=date.get(Calendar.YEAR)+"/"+(date.get(Calendar.MONTH)+1)+"/"+date.get(Calendar.DAY_OF_MONTH);
Vector array = (Vector)itemTable.get(key);
array.removeElement(item);
if(array.size() ==0)
{
itemTable.remove(key);
}
}
//实现 ImportDateFilter 接口,判断日期类型
public int isImportantDate(Calendar date)
{
int dayType = NORMALDAY;
String key=date.get(Calendar.YEAR)+"/"+(date.get(Calendar.MONTH)+1)+"/"+date.get(Calendar.DAY_OF_MONTH);
if(itemTable.containsKey(key))
{
dayType = IMPORTANTDAY;
}
int dayOW = date.get(Calendar.DAY_OF_WEEK );
if(dayOW == Calendar.SATURDAY || dayOW == Calendar.SUNDAY)
{//周末作为假日
return HOLIDAY + dayType;
}
int dayOM = date.get(Calendar.DAY_OF_MONTH);
int month = date.get(Calendar.MONTH);
for(int i=0;i<holiday.length;i++)
{//判断是否法定节日
if(dayOM == holiday[i][1] && month+1== holiday[i][0])
return HOLIDAY + dayType;
}
return dayType;
}
//实现 DateSelectListener 接口,处理日期被选择的事件
public void dateSelected(CalendarCanvas c, Calendar date)
{
dateSelected = date;
String key=date.get(Calendar.YEAR)+"/"+(date.get(Calendar.MONTH)+1)+"/"+date.get(Calendar.DAY_OF_MONTH);
Vector array = (Vector)itemTable.get(key);
listForm.reset(array,dateSelected);
Display.getDisplay(this).setCurrent(listForm);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -