1.WordOrganizerMIDlet.java
package team12.wordOrganizer;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Alert;
import javax.microedition.rms.RecordStore;
import javax.microedition.midlet.MIDletStateChangeException;
public class WordOrganizerMIDlet extends MIDlet {
private RecordStore rs;
private BrowseList browseList;
private AddForm addForm;
private SearchForm searchForm;
private ChooseList chooseList;
private ViewWord viewWord;
private Detail detail;
private DetailView detailView;
public WordOrganizerMIDlet() {
// TODO Auto-generated constructor stub
}
public void destroyApp(boolean arg0) {
// TODO Auto-generated method stub
try
{
rs.closeRecordStore();
}
catch(Exception e)
{}
}
public void pauseApp() {
}
public void startApp() {
try{
rs=RecordStore.openRecordStore("WS",true);
}catch(Exception e)
{}
chooseList=new ChooseList(this);
Display.getDisplay(this).setCurrent(chooseList);
}
public void goToWordList(String[] words)
{
ViewWord wordList=new ViewWord(this,words);
Display.getDisplay(this).setCurrent(wordList);
}
public void goToBrowseWordList()
{
browseList=new BrowseList(this);
Display.getDisplay(this).setCurrent(browseList);
}
public void goToBrowseWordList(Alert a)
{
browseList=new BrowseList(this);
Display.getDisplay(this).setCurrent(a,browseList);
}
public void goToAddForm()
{
addForm=new AddForm(this);
Display.getDisplay(this).setCurrent(addForm);
}
public void goToSearchForm()
{
searchForm=new SearchForm(this);
Display.getDisplay(this).setCurrent(searchForm);
}
public void goToChooseList()
{
chooseList = new ChooseList(this);
Display.getDisplay(this).setCurrent(chooseList);
}
public void goToChooseList(Alert a)
{
chooseList=new ChooseList(this);
Display.getDisplay(this).setCurrent(a,chooseList);
}
public void goToDetail(String result)
{
detail=new Detail(this,result);
Display.getDisplay(this).setCurrent(detail);
}
public void goToDetailView(String result, String[] wordList)
{
detailView=new DetailView(this,result,wordList);
Display.getDisplay(this).setCurrent(detailView);
}
public void exitApp(){
destroyApp(false);
notifyDestroyed();
}
public RecordStore getRS(){
return rs;
}
}
2.AddForm.java
package team12.wordOrganizer;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.*;
import javax.microedition.lcdui.Ticker;
public class AddForm extends Form implements CommandListener
{
private RecordStore rs;
private Ticker ticker;
private Command cmdBack,cmdStore;
private WordOrganizerMIDlet midlet;
private TextField alphabet,word,description;
private ChoiceGroup wordtype;
String[] choicetype={"Noun","Adjective","Verb"};
public AddForm(WordOrganizerMIDlet midlet) {
super("Add Word,Please Fill the form.");
ticker = new Ticker("Personal Word Organizer");
this.midlet=midlet;
rs = midlet.getRS();
alphabet=new TextField("Alphabet: ","",1,TextField.ANY);
word=new TextField("Word: ","",18,TextField.ANY);
description=new TextField("Description","",30,TextField.ANY);
wordtype=new ChoiceGroup("Word Type :", Choice.EXCLUSIVE,choicetype,null);
cmdBack=new Command("Back",Command.STOP,1);
cmdStore=new Command("Store",Command.SCREEN,1);
append(alphabet);
append(word);
append(description);
append(wordtype);
setTicker(ticker);
addCommand(cmdBack);
addCommand(cmdStore);
setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1)
{
if(c==cmdBack)
midlet.goToChooseList();
else if(c==cmdStore)
{
int rid;
byte[] data;
try
{
WS ws=new WS(alphabet.getString(),word.getString(),description.getString(),wordtype.getString(wordtype.getSelectedIndex()));
data=ws.toBytes();
rid=rs.addRecord(data,0, data.length);
System.out.println("Record: is added");
Alert a=new Alert("Message","The new word is successfully added!",null,null);
a.setTimeout(3000);
midlet.goToChooseList(a);
}
catch(Exception e)
{
System.out.println("Error in adding WS record:"+e);
Alert a=new Alert("Message","The new word is not successfully added!",null,null);
a.setTimeout(3000);
midlet.goToChooseList(a);
}
}
}
}
3.BrowseList.java
package team12.wordOrganizer;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Alert;
import javax.microedition.rms.*;
import javax.microedition.lcdui.Ticker;
public class BrowseList extends List implements CommandListener {
private Ticker ticker;
private WordOrganizerMIDlet midlet;
private RecordStore rs;
private Command cmdBack;
static String[] options={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V",
"R","S","T","U","V","W","X","Y","Z"};
public BrowseList(WordOrganizerMIDlet midlet) {
super("Browse Word,Please Choose one:",List.IMPLICIT,options,null);
cmdBack = new Command("Back", Command.EXIT, 1);
this.midlet=midlet;
rs = midlet.getRS();
ticker = new Ticker("Personal Word Organizer");
addCommand(cmdBack);
setTicker(ticker);
setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1) {
if(c==cmdBack)
{
midlet.goToChooseList();
}
else if(c==List.SELECT_COMMAND)
{
RecordEnumeration re = null;
WS data = null;
try
{
RecordFilter rf = new WSFilterByAlphabet(this.getString(this.getSelectedIndex()));
WSComparator rc = new WSComparator();
re = rs.enumerateRecords(rf,rc,false);
if(re.numRecords()> 0)
{
String[] wordList = new String[re.numRecords()];
for(int i=0;i
re.hasNextElement();
int id = re.nextRecordId();
data = new WS(rs.getRecord(id));
wordList[i] = data.getWord();
}
midlet.goToWordList(wordList);
}
else
{
System.out.println("no words exists");
Alert a=new Alert("Message","No words exists!",null,null);
a.setTimeout(3000);
midlet.goToBrowseWordList(a);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
4.ChooseList.java
package team12.wordOrganizer;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Ticker;
public class ChooseList extends List implements CommandListener {
private Ticker ticker;
private Command cmdExit;
private WordOrganizerMIDlet midlet;
static String[] options={"Browse Word","Add Word","Search Word"};
public ChooseList(WordOrganizerMIDlet midlet) {
super("Please choose one:",List.IMPLICIT,options,null);
cmdExit = new Command("Exit", Command.EXIT, 1);
ticker = new Ticker("Personal Word Organizer");
this.midlet=midlet;
addCommand(cmdExit);
setTicker(ticker);
setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1) {
if(c==List.SELECT_COMMAND)
{
if(this.getString(this.getSelectedIndex()).equals("Browse Word"))
midlet.goToBrowseWordList();
else if(this.getString(this.getSelectedIndex()).equals("Add Word"))
midlet.goToAddForm();
else if(this.getString(this.getSelectedIndex()).equals("Search Word"))
midlet.goToSearchForm();
}
else if(c==cmdExit){
midlet.exitApp();
}
}
}
5.SearchForm.java
package team12.wordOrganizer;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Form;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.lcdui.Ticker;
public class SearchForm extends Form implements CommandListener {
private WordOrganizerMIDlet midlet;
private Command cmdBack, cmdSearch;
private TextField word;
private RecordStore rs;
private String result=null;
private Ticker ticker;
public SearchForm(WordOrganizerMIDlet midlet)
{
super("Search Form,Please enter word:");
this.midlet = midlet;
rs = midlet.getRS();
ticker = new Ticker("Personal Word Organizer");
cmdBack = new Command("Back", Command.STOP, 1);
cmdSearch = new Command("Search", Command.SCREEN, 1);
word=new TextField("Word:","",18,TextField.ANY);
append(word);
setTicker(ticker);
addCommand(cmdSearch);
addCommand(cmdBack);
setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1) {
if (c == cmdBack)
{
midlet.goToChooseList();
}
if(c==cmdSearch)
{
RecordEnumeration re=null;
WS data1;
try{
RecordFilter rf=new WSFilterByWord(word.getString());
re=rs.enumerateRecords(rf, null, false);
if(re.hasNextElement())
{
int id=re.nextRecordId();
data1=new WS(rs.getRecord(id));
System.out.println("Record"+id+"is"+data1.getWord());
result="Word:"+data1.getWord()+"\n"+
"Type:"+data1.getType()+"\n"+
"Description:"+data1.getDescription();
midlet.goToDetail(result);
}
else
{
System.out.println("This word is not in recordStore!");
result="The word is not in your store";
midlet.goToDetail(result);
}
}
catch(Exception e)
{
}
}
}
}
6.ViewWord.java
package team12.wordOrganizer;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.*;
import javax.microedition.lcdui.Ticker;
public class ViewWord extends List implements CommandListener {
private Ticker ticker;
private WordOrganizerMIDlet midlet;
private RecordStore rs;
private Command cmdBack;
private String[] wordList;
public ViewWord(WordOrganizerMIDlet midlet, String[] wordList)
{
super("View Word,Please Choose one:",List.IMPLICIT,wordList,null);
cmdBack = new Command("Back", Command.BACK, 1);
ticker = new Ticker("Personal Word Organizer");
this.midlet=midlet;
rs = midlet.getRS();
this.wordList = wordList;
addCommand(cmdBack);
setTicker(ticker);
setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1)
{
if(c==cmdBack)
{
midlet.goToBrowseWordList();
}
if(c==List.SELECT_COMMAND)
{
RecordEnumeration re=null;
WS data;
String result;
try{
RecordFilter rf=new WSFilterByWord(this.getString(this.getSelectedIndex()));
re=rs.enumerateRecords(rf, null, false);
if(re.hasNextElement())
{
int id=re.nextRecordId();
data=new WS(rs.getRecord(id));
System.out.println("Record"+id+"is"+data.getWord());
result="Word:"+data.getWord()+"\n"+
"Type:"+data.getType()+"\n"+
"Description:"+data.getDescription();
midlet.goToDetailView(result,this.wordList);
}
else
{
System.out.println("This word is not in recordStore!");
result="The word is not in your store";
midlet.goToDetailView(result,this.wordList);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Detail.java
package team12.wordOrganizer;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.Ticker;
public class Detail extends TextBox implements CommandListener
{
private WordOrganizerMIDlet midlet;
private Command cmdBack;
private Ticker ticker;
public Detail(WordOrganizerMIDlet midlet, String text) {
super("Detail View", text, 100, 0);
this.midlet=midlet;
ticker = new Ticker("Personal Word Organizer");
cmdBack=new Command("Back",Command.BACK,1);
addCommand(cmdBack);
setTicker(ticker);
setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1) {
if(c==cmdBack)
{
midlet.goToSearchForm();
}
}
}
7.DetailView.java
package team12.wordOrganizer;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.Ticker;
public class DetailView extends TextBox implements CommandListener
{
private String[] wordList;
private WordOrganizerMIDlet midlet;
private Command cmdBack;
private Ticker ticker;
public DetailView(WordOrganizerMIDlet midlet, String text, String[] wordList) {
super("Detail View", text, 100, 0);
this.midlet=midlet;
this.wordList = wordList;
ticker = new Ticker("Personal Word Organizer");
cmdBack=new Command("Back",Command.BACK,1);
addCommand(cmdBack);
setTicker(ticker);
setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1) {
if(c==cmdBack)
{
midlet.goToWordList(this.wordList);
}
}
}
8.WS.java
package team12.wordOrganizer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class WS {
private String alphabet;
private String word;
private String description;
private String type;
public WS(String alphabet, String word, String description, String type) {
this.alphabet = alphabet;
this.word = word;
this.description = description;
this.type = type;
}
public String getAlphabet() {
return alphabet;
}
public String getWord() {
return word;
}
public String getDescription() {
return description;
}
public String getType() {
return type;
}
public byte[] toBytes(){
byte data[]=null;
try{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
dos.writeUTF(alphabet);
dos.writeUTF(word);
dos.writeUTF(description);
dos.writeUTF(type);
data=baos.toByteArray();
baos.close();
dos.close();
}
catch(Exception e)
{
}
return data;
}
public WS(byte[] rec)
{
try{
ByteArrayInputStream bs=new ByteArrayInputStream(rec);
DataInputStream dis=new DataInputStream(bs);
this.alphabet=dis.readUTF();
this.word=dis.readUTF();
this.description=dis.readUTF();
this.type=dis.readUTF();
bs.close();
dis.close();
}
catch(Exception e){}
}
}
9.WSComparator.java
package team12.wordOrganizer;
import javax.microedition.rms.RecordComparator;
public class WSComparator implements RecordComparator
{
public int compare(byte[] rec1, byte[] rec2) {
WS r1=new WS(rec1);
WS r2=new WS(rec2);
if(r1.getWord().compareTo(r2.getWord())==1)
return RecordComparator.PRECEDES;
if(r1.getWord().compareTo(r2.getWord())==0)
return RecordComparator.EQUIVALENT;
return RecordComparator.FOLLOWS;
}
}
10.WSFilterByAlphabet.java
package team12.wordOrganizer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.*;
public class WSFilterByAlphabet implements RecordFilter
{
/*Filter by Alphabet*/
private String alphabet;
public WSFilterByAlphabet(String alphabet)
{
this.alphabet = alphabet;
}
public boolean matches(byte[] rec)
{
WS ws= new WS(rec);
if(ws.getAlphabet().equals(this.alphabet))
return true;
else
return false;
}
}
11.WSFilterByWord.java
package team12.wordOrganizer;
import javax.microedition.rms.RecordFilter;
public class WSFilterByWord implements RecordFilter {
private String word;
public boolean matches(byte[] rec) {
WS ws=new WS(rec);
if(ws.getWord().equalsIgnoreCase(this.word))
return true;
else
return false;
}
public WSFilterByWord(String word) {
this.word = word;
}
}
No comments:
Post a Comment