www.pudn.com > phonebk.rar > PhoneBookMidlet.java


/* 
 * PhoneBookMidlet.java 
 *主程序  
 * Created on 2006年3月17日, 上午10:11 
 */ 
 
package com.j2medev.sample.phonebook; 
 
import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
import javax.microedition.rms.*; 
import java.io.*; 
 
/** 
 * 
 * @author  Admin 
 * @version 
 */ 
public class PhoneBookMidlet extends MIDlet implements Controller,CommandListener{ 
    private Display display; 
    private Model model; 
    private AccoutRead accoutread; 
     
    private AddEntryUI addEntryUI; 
    private DetailUI detailUI; 
    private EditEntryUI editEntryUI; 
    private ListEntryUI listEntryUI; 
    private SearchUI searchUI; 
    private WelcomeUI welcomeUI; 
    private LoginUI loginUI; 
    private LoginSet loginset; 
    private PassetUI passetui; 
    private List mainlist = new List("主菜单",List.IMPLICIT, new String[] {"电话本列表","添加记录","查找记录","登录设置","密码设置","帮助"}, null); 
    private Command exitCommand = new Command("退出",Command.EXIT, 1); 
    public PhoneBookMidlet(){ 
        super(); 
        display = Display.getDisplay(this); 
        model = new Model(); 
         
        detailUI = new DetailUI(this); 
        editEntryUI = new EditEntryUI(this); 
        listEntryUI = new ListEntryUI(this); 
        addEntryUI = new AddEntryUI(this); 
        searchUI = new SearchUI(this); 
        loginUI = new LoginUI(this); 
        loginset = new LoginSet(this); 
        passetui = new PassetUI(this); 
        //welcomeUI = new WelcomeUI(this, listEntryUI); 
         
         
    } 
   
    public void startApp() { 
         welcomeUI = new WelcomeUI(this, listEntryUI); 
         accoutread = new AccoutRead(this); 
         mainlist.addCommand(exitCommand); 
         mainlist.setCommandListener(this); 
         accoutread.start();   //启动线程 
       // Entry[] all = model.getAll(); 
        //listEntryUI.load(all); 
try{  
       model.deletAllRecord();   //用于删除所有record时用************ 
        refresh(null, 0); 
        handleCommand(Controller.SHOW_LIST_ENTRY, null); 
     }catch(RecordStoreException e){ 
         e.printStackTrace(); 
     }catch(IOException ex){ 
         ex.printStackTrace(); 
     } 
 
 
        //在这儿加可以启动线程读取accout中的登录信息; 
       // display.setCurrent(welcomeUI,listEntryUI); 
    // display.setCurrent(welcomeUI); 
    } 
     
    public void pauseApp() { 
    } 
     
    public void destroyApp(boolean unconditional) { 
    } 
    public void handleCommand(int command,Object[] arg) { 
        switch(command){ 
            //进行密码设置 
            case Controller.DO_PASSSET_ENTRY: 
                try{ 
                 boolean up2 = model.userandpass1((String)arg[0],(String)arg[1]);                 
                if(up2){ 
                     //System.out.println((String)arg[4]); 
                      model.saveloginset((String)arg[2],(String)arg[3],(String)arg[4]);  
                       
                     show("重新设置完成",mainlist);                     
                    //保存设置 
                }else{ 
                    show("旧密码或用户名错误,不能重新进行设置",passetui); 
                   // 提示密码不正确不能进行重新设置 
                } 
                }catch(IOException e){ 
                        e.printStackTrace(); 
                    } 
                break; 
             
            //显示密码设置页面 
            case Controller.SHOW_PASSSET_ENTRY: 
                show(passetui); 
                break; 
            //显示错误信息 
            case Controller.SHOW_ERR_ENTRY: 
                show("用户名和密码为空,不能进行设置", loginset); 
                break; 
            //显示登录 
            case Controller.SHOW_LOGINSET_ENTRY: 
                //System.out.println("kfdkdkldskldskljdfjkldfs"); 
                show(loginset); 
                break; 
            //登录设置判断 
            case Controller.DO_LOGINSET_ENTRY: 
                boolean up1 = model.userandpass((String)arg[0],(String)arg[1]); 
                if(up1){ 
                    try{ 
                         
                    model.saveloginset((String)arg[0],(String)arg[1],(String)arg[2]); 
                     
                     show("登录设置成功",mainlist); 
                    }catch(IOException e){ 
                        e.printStackTrace(); 
                    } 
                    //保存设置 
                }else{ 
                    show("密码或用户名错误,不能进行设置",loginset); 
                   // 提示密码不正确不能进行设置 
                } 
                break; 
            //显示主菜单 
            case Controller.SHOW_LIST_BACK: 
                show(mainlist); 
                break; 
            //用户名密码判断正确是否,决定能否进入。 
            case Controller.DO_ENTRY_ENTRY: 
                boolean up = model.userandpass((String)arg[0],(String)arg[1]); 
                //System.out.println((String)arg[1]); 
                entrysystem(up); 
                 
                break; 
             
            case Controller.SHOW_LIST_ENTRY: 
                try{ 
                refresh(null,0); 
                show(listEntryUI); 
                }catch(RecordStoreException e){ 
                e.printStackTrace(); 
                }catch(IOException ex){ 
                    ex.printStackTrace(); 
                } 
                break; 
                 
            case Controller.SHOW_LIST1_ENTRY: 
                try{ 
                refresh((String)arg[0], 1); 
                }catch(RecordStoreException e){ 
                    e.printStackTrace(); 
                    break; 
                }catch(IOException ex){ 
                    ex.printStackTrace(); 
                    break; 
                } 
              
                show(listEntryUI); 
                 
                break; 
            case Controller.SHOW_ADD_ENTRY: 
                addEntryUI.clear(); 
                show(addEntryUI); 
                break; 
                //显示联系人的详细信息 
            case Controller.SHOW_DETALL_ENTRY: 
                detailUI.load(model.getEntry((String)arg[0])); 
               //  
                show(detailUI); 
                break; 
              //显示编辑   
            case Controller.SHOW_EDIT_ENTRY: 
                editEntryUI.load(model.getEntry((String)arg[0])); 
                show(editEntryUI); 
                break; 
           //添加联系人      
            case Controller.DO_ADD_ENTRY: 
                try{ 
                model.addEntry((Entry)arg[0]); 
                refresh(null, 0); 
                show("联系人添加完成",listEntryUI); 
                }catch(RecordStoreException e){ 
                    e.printStackTrace(); 
                    break; 
                }catch(IOException ex){ 
                    ex.printStackTrace(); 
                    break; 
                } 
                break; 
                //删除联系人 
            case Controller.DO_DELETE_ENTRY: 
                try{ 
                model.deletRecord((String)arg[0]); 
                refresh(null, 0); 
                show("删除联系人完成",listEntryUI); 
                }catch(RecordStoreException e){ 
                    e.printStackTrace(); 
                    break; 
                }catch(IOException ex){ 
                    ex.printStackTrace(); 
                    break; 
                } 
                break; 
                //更新联系人信息,先删除,再添加。 
            case Controller.DO_UPDATE_ENTRY: 
                try{                     
                model.deletRecord((String)arg[0]); 
                //System.out.println((String)arg[0]); 
                model.addEntry((Entry)arg[1]); 
                refresh(null, 0); 
                show("更新联系信息完成",listEntryUI); 
                }catch(RecordStoreException e){ 
                    e.printStackTrace(); 
                    break; 
                }catch(IOException ex){ 
                    ex.printStackTrace(); 
                    break; 
                } 
                break; 
                                 
            case Controller.APP_EXIT: 
                destroyApp(false); 
                notifyDestroyed(); 
                break; 
                 
            case Controller.SHOW_SEARCH_ENTRY: 
                show(searchUI); 
                break; 
        } 
    } 
    public void show(Displayable d){ 
        display.setCurrent(d); 
       // System.out.println("执行到这儿了"); 
    } 
    private void refresh(String name,int flag) throws RecordStoreException ,IOException{ 
        Index[] all = model.getAll(name,flag); 
        //System.out.println("执行到这儿了"); 
        listEntryUI.load(all); 
        
    } 
    public void show(String mesg,Displayable d){ 
        Alert alert = new Alert("系统提示信息", mesg, null, AlertType.CONFIRMATION); 
        alert.setTimeout(Alert.FOREVER); 
        display.setCurrent(alert, d); 
    } 
    //public void setCurrent(Displayable displayable){ 
    //    display.setCurrent(displayable); 
   // } 
    public boolean getinfor(){ 
         
        return model.getinfor(); 
    } 
    //根据accout中的信息判断是否自动登录 
    public void setCurrent(boolean infor){ 
        if(infor){ 
             show(mainlist); 
        }else{ 
       //show(mainlist); 
           show(loginUI); 
        //display.setCurrent(loginUI); 
        } 
    } 
     
    private void entrysystem(boolean up){ 
        if(up){            
            show(mainlist); 
        }else{ 
            show("用户名或密码错误", loginUI); 
        } 
    } 
     
    
     
     
    public void commandAction(Command cmd,Displayable d){ 
        if(cmd == exitCommand){ 
            handleCommand(Controller.APP_EXIT,null); 
        } 
        if(cmd == mainlist.SELECT_COMMAND){ 
          int index = mainlist.getSelectedIndex(); 
          switch(index){ 
              case 0: 
               handleCommand(Controller.SHOW_LIST_ENTRY,null); 
               break; 
              case 1: 
                  handleCommand(Controller.SHOW_ADD_ENTRY,null); 
                  break; 
              case 2: 
                  handleCommand(Controller.SHOW_SEARCH_ENTRY,null); 
                  break; 
              case 3: 
                  handleCommand(Controller.SHOW_LOGINSET_ENTRY,null); //登录设置 
                  break; 
              case 4: 
                 handleCommand(Controller.SHOW_PASSSET_ENTRY,null); 
                 break; 
          } 
        } 
    } 
}