import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordListener;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotFoundException;
public class RecordMonitorMIDlet extends MIDlet implements CommandListener, RecordListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command deleteCommand= new Command("Delete", Command.SCREEN, 1);
private Command changeCommand = new Command("Edit", Command.SCREEN, 1);
private Display display;
RecordStore mainRS = null;
RecordStore backupRS = null;
public RecordMonitorMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
TextBox aTextBox = new TextBox("Main", null, 256, TextField.ANY);
byte[] nameEmail = null;
boolean existingOrNot = false;
boolean OK = true;
existingOrNot = existing("aRS1");
if (existingOrNot) {
try {
mainRS = RecordStore.openRecordStore("aRS1", false);
mainRS.addRecordListener(this);
} catch (Exception e) {
OK = false;
} finally {
if (OK) {
aTextBox.setString("Ok");
} else {
aTextBox.setString("Failed");
}
}
} else {
try {
mainRS = RecordStore.openRecordStore("aRS1", true);
mainRS.addRecordListener(this);
} catch (Exception e) {
OK = false;
} finally {
if (OK) {
aTextBox.setString("Ok");
} else {
aTextBox.setString("Falied");
}
}
}
if (OK)
try {
backupRS = RecordStore.openRecordStore("aRS2", true);
} catch (Exception e) {
}
if (OK)
try {
nameEmail = "JIDCA=login@yourname.net".getBytes();
mainRS.addRecord(nameEmail, 0, nameEmail.length);
aTextBox.setString("Added");
} catch (Exception e) {
aTextBox.setString("Add Falied");
}
String result = new String(nameEmail);
int position = result.indexOf('=');
result = "Name:" + result.substring(0, position) + "\n" + "E-mail?"
+ result.substring(position + 1, result.length());
aTextBox.setString(result);
try {
int recordID = mainRS.getNextRecordID() - 1;
byte[] newNameEmail = "Logis=login@yourname.net".getBytes();
mainRS.setRecord(recordID, newNameEmail, 0, newNameEmail.length);
recordID = mainRS.getNextRecordID() - 1;
mainRS.deleteRecord(recordID);
} catch (Exception e) {
}
aTextBox.addCommand(exitCommand);
aTextBox.addCommand(deleteCommand);
aTextBox.addCommand(changeCommand);
aTextBox.setCommandListener(this);
display.setCurrent(aTextBox);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
try {
if (mainRS != null) {
mainRS.closeRecordStore();
}
} catch (Exception e) {
}
try {
if (backupRS != null) {
backupRS.closeRecordStore();
}
} catch (Exception e) {
}
}
public boolean existing(String recordStoreName) {
boolean existingOrNot = false;
RecordStore mainRS = null;
if (recordStoreName.length() > 32)
return false;
try {
mainRS = RecordStore.openRecordStore(recordStoreName, false);
} catch (RecordStoreNotFoundException e) {
existingOrNot = false;
} catch (Exception e) {
} finally {
try {
mainRS.closeRecordStore();
} catch (Exception e) {
}
}
return existingOrNot;
}
public void commandAction(Command c, Displayable s) {
if (c == deleteCommand) {
try {
int recordID = mainRS.getNextRecordID() - 1;
mainRS.deleteRecord(recordID);
} catch (Exception e) {
}
} else if (c == changeCommand) {
try {
int recordID = mainRS.getNextRecordID() - 1;
byte[] newNameEmail = "Logis=login@yourname.net".getBytes();
mainRS.setRecord(recordID, newNameEmail, 0, newNameEmail.length);
} catch (Exception e) {
}
} else {
destroyApp(false);
notifyDestroyed();
}
}
public void recordAdded(RecordStore recordStore, int recordID) {
if (recordStore == mainRS) {
try {
byte[] data = recordStore.getRecord(recordID);
int recID = backupRS.addRecord(data, 0, data.length);
} catch (Exception e) {
}
}
}
public void recordChanged(RecordStore recordStore, int recordID) {
if (recordStore == mainRS) {
try {
byte[] data = recordStore.getRecord(recordID);
backupRS.setRecord(recordID, data, 0, data.length);
} catch (Exception e) {
}
}
}
public void recordDeleted(RecordStore recordStore, int recordID) {
if (recordStore == mainRS) {
try {
backupRS.deleteRecord(recordID);
} catch (Exception e) {
}
}
}
}