import java.io.*;
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.lcdui.ImageItem;
/**
* @author RK
*/
public class ContactBook extends MIDlet implements CommandListener
{
public RecordStore recStore=null;
public RecordEnumeration recEnum;
public Display disp;
public Alert alert;
public Command startCmd;
public Command storeCmd;
public Command exitCmd;
public Command helpCmd;
public Command getRecCmd,backCmd,okCmd;
public Form form,cForm,dataForm;
public StringItem stringItem;
public Image errorImg;
public Image okImg,userImg;
public Image wcImg;
public TextField name=null;
public TextField number=null;
int recFlag=0;
public void startApp()
{
disp=Display.getDisplay(this);
form=new Form("WelCome");
cForm=new Form("Input Contacts Details");
dataForm=new Form("Contacts.");
name=new TextField("Name","",29,TextField.ANY);
number=new TextField("Mobile No:","",20,TextField.NUMERIC);
//name.setMaxSize(20);
//number.setMaxSize(10);
startCmd=new Command("Add New",Command.OK,1);
storeCmd=new Command("Save",Command.OK,1);
backCmd=new Command("Back",Command.EXIT,0);
getRecCmd=new Command("Show Contacts",Command.OK,1);
helpCmd=new Command("Help",Command.OK,1);
okCmd=new Command("Back",Command.OK,1);
exitCmd=new Command("Exit",Command.EXIT,0) ;
try
{
wcImg=Image.createImage("/Images/Address-Book.png");
ImageItem w= new ImageItem("ContactBook",wcImg,ImageItem.LAYOUT_CENTER|ImageItem.LAYOUT_VCENTER, "Contacts");//LAYOUT_NEWLINE_BEFORE
form.append(w);
}catch(IOException e){}
form.addCommand(startCmd);
form.addCommand(getRecCmd);
form.addCommand(helpCmd);
dataForm.addCommand(okCmd);
cForm.append(name);
cForm.append(number);
cForm.addCommand(storeCmd);
cForm.addCommand(backCmd);
dataForm.addCommand(okCmd);
form.addCommand(exitCmd);
form.setCommandListener(this);
cForm.setCommandListener(this);
dataForm.setCommandListener(this);
try
{
errorImg = Image.createImage("/Images/Alert.png");
okImg=Image.createImage("/Images/Ok.png");
} catch (IOException ex)
{
alert=new Alert("Error.","Error while creating Images Store",errorImg,AlertType.ERROR);
alert.setTimeout(2000);
disp.setCurrent(alert);
}
disp.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable d)
{
if(c==startCmd)
{
//CREATING RECORDSTORE
try
{
alert=new Alert("","Contact form Opened successfully",okImg,AlertType.INFO);
alert.setTimeout(2000);
disp.setCurrent(alert);
disp.setCurrent(cForm);
recStore = RecordStore.openRecordStore("MyContacts", true);
recStore.closeRecordStore();
recFlag=1;
}catch (Exception ex)
{
alert=new Alert("Error.","Error WHile opening Record Store",errorImg,AlertType.ERROR);
alert.setTimeout(2000);
disp.setCurrent(alert);
}
}
else if(c==storeCmd)
{
try
{
recStore = RecordStore.openRecordStore("MyContacts", false);
String cname=name.getString();
String cnum=number.getString();
if(cname.equals("")||cnum.equals(""))
{
throw new IOException();
}
cname="Name : "+cname;
cnum="Mob No. : "+cnum;
name.setString("");
number.setString("");
byte[] byteOutputData = cname.getBytes();
recStore.addRecord(byteOutputData, 0,byteOutputData.length);
byteOutputData = cnum.getBytes();
recStore.addRecord(byteOutputData, 0,byteOutputData.length);
byteOutputData=null;
recStore.closeRecordStore();
alert=new Alert("Congratulation","Contact Saved Successfully.",okImg,AlertType.INFO);
alert.setTimeout(2000);
disp.setCurrent(alert);
}catch (Exception ex)
{
alert=new Alert("Opsss !!! ."," Empty Contact Name or Mob No.!!",errorImg,AlertType.ERROR);
alert.setTimeout(1500);
disp.setCurrent(alert);
}
}
else if(c==getRecCmd)
{
try
{
String data="";
byte[] byteInputData = new byte[1];
int length = 0;
recStore = RecordStore.openRecordStore("MyContacts",false);
for (int x = 1; x <= recStore.getNumRecords(); x++)
{
if (recStore.getRecordSize(x) > byteInputData.length)
{
byteInputData = new byte[recStore.getRecordSize(x)];
}
length = recStore.getRecord(x, byteInputData, 0);
data=data+(new String(byteInputData,0,length))+"\n";
}
recStore.closeRecordStore();
dataForm.deleteAll();
dataForm.append(new StringItem(null,data));
disp.setCurrent(null);
disp.setCurrent(dataForm);
}
catch (Exception error)
{
alert = new Alert("Error Reading", error.toString(),null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
disp.setCurrent(alert);
}
} else if(c==backCmd)
{
disp.setCurrent(form);
}
else if(c==helpCmd)
{
alert = new Alert("ContactStore","rajkirpal.er@live.com",null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
disp.setCurrent(alert);
} else if(c==okCmd)
{
disp.setCurrent(form);
}
else if(c==exitCmd)
{
alert = new Alert("ContactStore","Thank You",null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
disp.setCurrent(alert);
destroyApp(true);
notifyDestroyed();
}
}
}
No comments:
Post a Comment