/**
* RMSDemo.Java
* @author j2meSalsa.com
* This class file shows you RMS Manipulations.
*/
package com.jme.rms;
// Midlet class's package
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
// Midlet's User interface API
import javax.microedition.lcdui.*;
public class RMSDemo extends MIDlet implements CommandListener {
private Display display;
// Commands
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command backCommand = new Command("Back", Command.EXIT, 1);
private Command addCommand = new Command("Add Record", Command.EXIT, 1);
// Record Store Obj's
private RecordStore rs;
private static final String rmsName = "salsaRMS";
// Items for data capture and display
private Form addForm;
private TextField tfAddRecord;
private Form showAllRecordsForm;
private List enumList;
// List for main menu
private List mainList;
private final String[] mainListArray = {
"Add Record",
"Show Records by Rec ID",
"Show Records by Enumeration"
};
private final Image[] imageArray = null;
/**
* Midlet constructor is called and the objects are instantiated.
* Midlet enters paused state
* */
public RMSDemo() {
// Get MIDlets display handle
display = Display.getDisplay(this);
// Render Main List
mainList = new List("Choose", Choice.IMPLICIT, mainListArray, imageArray);
mainList.addCommand(exitCommand);
mainList.setCommandListener(this);
// Create Record Store
try {
rs = RecordStore.openRecordStore(rmsName, true );
}catch( RecordStoreException e ){
}
}
/**
* startApp() method is called to change the paused state to active state.
*/
protected void startApp() {
display.setCurrent(mainList);
}
/**
* The MIDlet frees as much resources as it can.
*/
protected void pauseApp() {}
/**
* Midlet cleans up resources before the application exits.
*
*/
protected void destroyApp(boolean bool) {}
/**
* Form to capture data to be inserted into Record Store.
*
*/
private void showAddRecordsForm() {
addForm = new Form("Add Record");
tfAddRecord = new TextField("Insert Text", "", 15, TextField.ANY);
addForm.append(tfAddRecord);
addForm.addCommand(addCommand);
addForm.addCommand(backCommand);
addForm.setCommandListener(this);
display.setCurrent(addForm);
}
/**
* Form to display all records from Record Store by parsing via Record ID's.
*
*/
private void showAllRecordsForm() {
showAllRecordsForm = new Form("All Records");
int recordStoreSize=0;
int numRecords=0;
int totalRecords=0;
try{
recordStoreSize = rs.getSize();
numRecords = rs.getNumRecords();
totalRecords = rs.getNextRecordID() - 1;
}catch(Exception e){}
showAllRecordsForm.append("Record Store Size: " + recordStoreSize + " bytes"
+ "\nNumber of Records: " + numRecords
+ "\n...Contents...");
for( int id = 1; id <= totalRecords; id++ ){
try {
byte[] data = new byte[rs.getRecordSize(id)];
data = rs.getRecord(id);
showAllRecordsForm.append("Record #" + id + ": " + new String(data) + "\n");
} catch( Exception e ){
e.printStackTrace();
}
}
showAllRecordsForm.addCommand(backCommand);
showAllRecordsForm.setCommandListener(this);
display.setCurrent(showAllRecordsForm);
}
/**
* Form to display all records from Record Store by parsing via Enumeration.
*
*/
private void showEnumRecordsForm() {
enumList = new List("Enumeration Demo", Choice.IMPLICIT);
try {
// Create Enumeration
RecordEnumeration recEnum = rs.enumerateRecords( null, null, false );
// Traverse the Enumeration to get get records.
while( recEnum.hasNextElement() ){
byte[] data = recEnum.nextRecord();
enumList.append(new String(data), null);
}
// Always destroy Enumeration
recEnum.destroy();
} catch (Exception e) {
e.printStackTrace();
}
enumList.addCommand(backCommand);
enumList.setCommandListener(this);
display.setCurrent(enumList);
}
/**
* Good ol' MIDlet event handling.
*
*/
public void commandAction(Command cmd, Displayable disp) {
if (disp.equals(mainList)) {
// In the main list
if (cmd == List.SELECT_COMMAND) {
if (disp.equals(mainList)) {
switch (((List)disp).getSelectedIndex()) {
case 0:
showAddRecordsForm();
break;
case 1:
showAllRecordsForm();
break;
case 2:
showEnumRecordsForm();
break;
}
}
}
}
if (cmd == exitCommand) {
destroyApp(false);
notifyDestroyed(); // Midlet notifies the AMS that it has done its work.
}
if ((cmd == backCommand) && (disp == addForm || disp == showAllRecordsForm || disp == enumList)) {
display.setCurrent(mainList);
}
if (cmd == addCommand) {
byte[] data = tfAddRecord.getString().getBytes();
try {
int recordNo = rs.addRecord( data, 0, data.length);
display.setCurrent(new Alert("Insertion Status", "Record inserted at Record ID: " + recordNo, null, AlertType.CONFIRMATION) );
}
catch(Exception e){}
}
}
}
This page is a part of
a frames based web site. If you have landed on this page from a search engine
click here to view the complete page.