UIController.java
上传用户:heyongping
上传日期:2022-07-01
资源大小:95k
文件大小:5k
- /*
- * 创建于 2009-12-04
- * 实现控制屏幕及相关事件监听
- *
- * 更改注释格式可以:Window - Preferences - Java - Code Style - Code Templates
- */
- package net.garrey.util;
- import net.garrey.midlet.PhoneTest;
- import net.garrey.model.PhoneItem;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Alert;
- import net.garrey.util.PhoneRecordStore;
- import net.garrey.view.About;
- import net.garrey.view.PhoneAdd;
- import net.garrey.view.PhoneEdit;
- import net.garrey.view.PhoneFind;
- import net.garrey.view.PhoneList;
- import net.garrey.view.PhoneView;
- /**
- * 作者: 张三
- * 初始化,控制事件监听
- */
- public class UIController {
- private PhoneTest phoneMidlet;
- private PhoneRecordStore phoneRecords;
- private PhoneList list;
- private PhoneAdd addForm;
- private PhoneView viewForm;
- private PhoneEdit editForm;
- private Alert alert;
- private About about;
- private PhoneFind findForm;
-
- public UIController(PhoneTest phone){
- phoneMidlet=phone;
- }
-
- public void init(){
- //PhoneRecordStore类中要使用UIController对象,要传一个当前对象
- phoneRecords=new PhoneRecordStore(this);
- System.out.println("bbbbbbbbbbbbbbbbbbbb:"+"init()方法");
- list=new PhoneList("列表",this,phoneRecords.getPhones());
- addForm=new PhoneAdd("添加",this);
- viewForm=new PhoneView("查看",this);
- editForm=new PhoneEdit("修改",this);
- about=new About("关于");
-
- findForm=new PhoneFind("查询",this);
- setCurrent(list); //默认第一个屏幕,查询并显示所有记录
- }
- //get method
- public PhoneRecordStore getPhoneRecords(){
- return phoneRecords;
- }
-
- public void setCurrent(Displayable disp){
- phoneMidlet.setCurrent(disp);
- }
- public void setCurrent(Alert alert, Displayable disp){
- phoneMidlet.setCurrent(alert, disp);
- }
-
- //定义事件ID内部类
- public static class EventID{
- private EventID(){
- }
-
- public static final byte EVENT_EXIT=0;//退出
- public static final byte EVENT_NEW_RECORD =1;//增加
- public static final byte EVENT_NEW_SAVE =2;//保存记录
- public static final byte EVENT_NEW_BACK =3;//保存记录
- public static final byte EVENT_DELETE =4;//删除
- public static final byte EVENT_VIEW_DETAIL =5;//察看详细
- public static final byte EVENT_VIEW_BACK= 6;//返回察看
- public static final byte EVENT_EDIT=7;//修改
- public static final byte EVENT_EDIT_BACK=8;//修改返回
- public static final byte EVENT_EDIT_SAVE=9;//保存修改
- public static final byte EVENT_ABOUT=10;//保存修改
-
- public static final byte EVENT_FIND=11; //进入查找页面
- public static final byte EVENT_VIEW_FINDOK=12; //开始查询
- }
-
- //事件处理
- public void handleEvent( int eventID,Object[] args){
- switch (eventID)
- {
- case EventID.EVENT_EXIT:
- {
- phoneMidlet.exit(false);
- break;
- }
- case EventID.EVENT_VIEW_DETAIL:
- {
- viewForm.setPhone((PhoneItem)args[0]);
- setCurrent(viewForm);
- break;
- }
- case EventID.EVENT_NEW_RECORD:
- {
- //添加新一条信息时,要先清除原来信息
- addForm.clear();
- setCurrent(addForm);
- break;
- }
- case EventID.EVENT_NEW_BACK:
- case EventID.EVENT_VIEW_BACK:
- case EventID.EVENT_EDIT_BACK:
- {
- setCurrent(list);
- break;
- }
- case EventID.EVENT_NEW_SAVE:
- {
- PhoneItem item=new PhoneItem((String)args[0],(String)args[1]);
- int id=phoneRecords.addPhone(item);
- list.refresh(phoneRecords.getPhones());
- setCurrent(list);
- break;
- }
- case EventID.EVENT_EDIT:
- {
- editForm.setPhone((PhoneItem)args[0]);
- setCurrent(editForm);
- break;
- }
- case EventID.EVENT_EDIT_SAVE:
- {
- phoneRecords.updatePhone((PhoneItem)args[0]);
- list.refresh(phoneRecords.getPhones());
- setCurrent(list);
- break;
- }
- case EventID.EVENT_DELETE:
- {
- phoneRecords.deletePhone(((PhoneItem)args[0]).getId());
- list.refresh(phoneRecords.getPhones());
- setCurrent(list);
- break;
- }
- case EventID.EVENT_ABOUT:
- {
- setCurrent(about);
- break;
- }
- case EventID.EVENT_FIND:
- {
- setCurrent(new PhoneFind("dd",this));
- break;
- }
- case EventID.EVENT_VIEW_FINDOK:
- {
- findForm.refresh(phoneRecords.getContinutePhones());
- setCurrent(findForm);
- break;
- }
- default:
- break;
- }
- }
-
- }