⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendardemo4.java

📁 J2ME MIDP 2.0 无线设备编程的一些源码
💻 JAVA
字号:
//CalendarDemo4.java file
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import calendar.*;

public class CalendarDemo4 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; //新增加变量记录程序是否第一次运行
    //下载日程记录的命令
    private Command downloadCmd = new Command("Download",Command.SCREEN, 1);
    //日程下载窗口对象,在需要使用时再创建
    private CalendarDownloadForm downloadForm=null;
    public CalendarDemo4()
    {
        //创建日历对象
        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(downloadCmd);
        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 ==downloadCmd)
        {//显示日程记录下载窗口,并开始日程下载
            StringBuffer sb = new StringBuffer(30);
            sb.append("http://localhost/");
            sb.append(dateSelected.get(Calendar.YEAR));
            if(dateSelected.get(Calendar.MONTH)+1<10)
                sb.append('0');
            sb.append(dateSelected.get(Calendar.MONTH)+1);
            if(dateSelected.get(Calendar.DAY_OF_MONTH)<10)
                sb.append('0');
            sb.append(dateSelected.get(Calendar.DAY_OF_MONTH));
            sb.append(".txt");
            
            downloadForm = new CalendarDownloadForm(sb.toString(),dateSelected);
            sb = null;
            downloadForm.setCommandListener(this);
            Display.getDisplay(this).setCurrent(downloadForm);
        }
        else if (c ==CalendarDownloadForm.exitCmd || c ==CalendarDownloadForm.cancelCmd)
        {//下载日程记录不成功,或者是取消
            Display.getDisplay(this).setCurrent(listForm);
            downloadForm = null;
        }
        else if (c ==CalendarDownloadForm.okCmd)
        {//下载日程记录成功
            Display.getDisplay(this).setCurrent(listForm);
            for(int i=0;i<downloadForm.array.size();i++)
            {
                addItem((ScheduleItem)downloadForm.array.elementAt(i));
                listForm.add((ScheduleItem)downloadForm.array.elementAt(i));
            }
            downloadForm.array.removeAllElements();
            downloadForm = null;
        }
        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 + -