/*
* Created on 2004-6-20
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.north.phonebook.ui;
import com.north.phonebook.model.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import com.north.phonebook.PhoneBookMIDlet;
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class UIController implements DialogListener
{
private Display display;
private Alert alert;
private PhoneBookMIDlet phoneBookMIDlet;
private Model model;
private WaitCanvas waitCanvas;
private IndexFunctionUI indexFunctionUI;
private WelcomeUI infomationUI;
private NewPhoneUI newPhoneUI;
private ListPhoneUI listPhoneUI;
private SearchPhoneUI searchPhoneUI;
private DetailInfoUI detailInfoUI;
private ConfirmationDialog dialog;
public static class EventID
{
private EventID()
{
}
public static final byte EVENT_NEW_RECORD_SELECTED = 1;
public static final byte EVENT_SAVE_RECORD_SELECTED = 2;
public static final byte EVENT_NEWPHONE_BACK_MAINUI = 3;
public static final byte EVENT_LISTPHONE_BACK_MAINUI = 4;
public static final byte EVENT_SEARCHUI_BACK_MAINNUI = 5;
public static final byte EVENT_CLEAR_RECORD_YES = 6;
public static final byte EVENT_CLEAR_RECORD_NO = 7;
public static final byte EVENT_DELETE_RECORD = 8;
public static final byte EVENT_DELETE_RECORD_YES = 9;
public static final byte EVENT_DELETE_RECORD_NO = 10;
public static final byte EVENT_DISPLAY_INFOMATION = 11;
public static final byte EVENT_DETAIL_BACK_LIST = 12;
public static final byte EVENT_SEARCH_RECORD = 13;
public static final byte EVENT_SEARCH_RECORD_ANYWAY = 14;
public static final byte ADD_NEW_RECORD = 100;
public static final byte SEARCH_RECORD = 101;
public static final byte CLEAR_RECORD = 102;
public static final byte LIST_RECORD = 103;
public static final byte HELP = 104;
}
public UIController(PhoneBookMIDlet pbm)
{
this.phoneBookMIDlet = pbm;
}
public void init(Model model)
{
this.display = Display.getDisplay(phoneBookMIDlet);
this.model = model;
waitCanvas = new WaitCanvas("", true);
indexFunctionUI = new IndexFunctionUI(this);
infomationUI = new WelcomeUI();
newPhoneUI = new NewPhoneUI(this);
listPhoneUI = new ListPhoneUI(this);
searchPhoneUI = new SearchPhoneUI(this);
detailInfoUI = new DetailInfoUI(this);
dialog = new ConfirmationDialog(display, null);
dialog.setDialogListener(this);
displayWelcome();
}
public void setCurrent(Displayable disp)
{
display.setCurrent(disp);
}
public void setCurrent(Alert alert, Displayable disp)
{
display.setCurrent(alert, disp);
}
public void displayWelcome()
{
setCurrent(infomationUI);
try
{
Thread.sleep(2000);
} catch (InterruptedException e)
{
}
setCurrent(indexFunctionUI);
}
public void showAlert(String message, Displayable next, AlertType type)
{
alert = new Alert(Title.alertTitle, message, null, type);
alert.setTimeout(1500);
setCurrent(alert, next);
}
public void handleEvent(int eventID)
{
switch (eventID)
{
case EventID.ADD_NEW_RECORD:
{
newPhoneUI.clear();
display.setCurrent(newPhoneUI);
break;
}
case EventID.CLEAR_RECORD:
{
dialog.setMessage(Title.delete_phonebook);
dialog.display(EventID.CLEAR_RECORD);
break;
}
case EventID.EVENT_CLEAR_RECORD_YES:
{
try
{
model.clearAllRecord();
display.setCurrent(indexFunctionUI);
} catch (ApplicationException e)
{
e.printStackTrace();
}
break;
}
case EventID.LIST_RECORD:
{
new Thread()
{
public void run()
{
try
{
waitCanvas.setMMessage(Title.gettingList);
display.setCurrent(waitCanvas);
Index[] index = model.listRecord();
listPhoneUI.init(index);
display.setCurrent(listPhoneUI);
} catch (ApplicationException e)
{
e.printStackTrace();
}
}
}.start();
break;
}
case EventID.SEARCH_RECORD:
{
searchPhoneUI.init();
display.setCurrent(searchPhoneUI);
break;
}
case EventID.EVENT_NEWPHONE_BACK_MAINUI:
{
display.setCurrent(indexFunctionUI);
break;
}
case EventID.EVENT_LISTPHONE_BACK_MAINUI:
{
display.setCurrent(indexFunctionUI);
break;
}
case EventID.EVENT_SEARCHUI_BACK_MAINNUI:
{
display.setCurrent(indexFunctionUI);
break;
}
case EventID.EVENT_DETAIL_BACK_LIST:
{
display.setCurrent(listPhoneUI);
break;
}
default:
break;
}
}
public void handleEvent(int eventID, Object[] obj)
{
switch (eventID)
{
case EventID.EVENT_SAVE_RECORD_SELECTED:
{
try
{
Account account = (Account) obj[0];
if (model.isRecordExist(account.getUserName()))
{
showAlert(Title.record_exist, indexFunctionUI,
AlertType.WARNING);
} else
{
model.addRecord(account);
showAlert(Title.record_added, indexFunctionUI,
AlertType.CONFIRMATION);
}
} catch (ApplicationException e)
{
e.printStackTrace();
}
break;
}
case EventID.EVENT_DELETE_RECORD:
{
String userName = (String) obj[0];
dialog.setMessage(Title.DELETE_RECORD + userName + "?");
dialog.display(EventID.EVENT_DELETE_RECORD, obj);
break;
}
case EventID.EVENT_DELETE_RECORD_YES:
{
try
{
String userName = (String) obj[0];
model.deleteRecord(userName);
int i = ((Integer)obj[1]).intValue();
listPhoneUI.delete(i);
//listPhoneUI.subLastSize();
} catch (ApplicationException e)
{
e.printStackTrace();