BTree | |||||
Home | BTree Guide | BTree Performance | Sample Applications | BTree Roadmap | Other Products |
This documentation is supplied as an example of the functionality available with the full distribution. This describes the sample application class supplied with the read-only Java implementation of the BTree for the J2ME platform. The class declaration is as follows -
import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import com.virtualmachinery.btreemero.*; import com.virtualmachinery.btreero.interfaces.*; import com.virtualmachinery.btree.exceptions.*; import com.virtualmachinery.btree.basicinterfaces.*; /** * |
The details above outline the variables required to run the application. We will refer to these later in the document. The classes Command, Display, Form, TextField and TextBox are described in more detail in the J2ME Wireless toolkit documentation.
As BtreeRODemoMIDlet extends the Abstract MIDlet class it must implement two methods - startApp() and pauseApp(). These methods start and suspend the application. The startApp() method code for BTreeRODemoMIDlet follows -
public void startApp() throws MIDletStateChangeException { display = Display.getDisplay(this); // Open the BTree //openBTree("http://localhost:80/HTTPTree",false); boolean ok = openBTree("socket://localhost:5000/SocketTree",false); //Using Socket if ok { // Create all the menus and forms resultField = new TextField("Value:", "", MAX_DATA_CHARS, TextField.ANY); menu = new List("BTree Demo", Choice.IMPLICIT); menu.append("View record", null); menu.append("Show all", null); menu.append("Show selected", null); menu.addCommand(EXIT_COMMAND); menu.addCommand(ABOUT_COMMAND); menu.setCommandListener(new BTreeDemoROCommandListener()); keylistAll = new List("All Entries", Choice.IMPLICIT); keylistAll.addCommand(BACK_COMMAND); keylistAll.addCommand(SHOW_COMMAND); keylistAll.setCommandListener(new BTreeDemoROCommandListener()); keylistSome = new List("Selected Entries", Choice.IMPLICIT); keylistSome.addCommand(BACK_COMMAND); keylistSome.addCommand(SHOW_COMMAND); keylistSome.setCommandListener(new BTreeDemoROCommandListener()); viewForm = new Form("View"); viewForm.append(resultField); viewForm.addCommand(BACK_COMMAND); viewForm.setCommandListener(new BTreeDemoROCommandListener()); lookupKeyBox = new TextBox("Enter a Key to look up:","", MAX_INDEX_CHARS, TextField.ANY); lookupKeyBox.addCommand(SHOW_COMMAND); lookupKeyBox.addCommand(BACK_COMMAND); lookupKeyBox.setCommandListener(new BTreeDemoROCommandListener()); startKeyBox = new TextBox("Enter start string:","", MAX_INDEX_CHARS, TextField.ANY); startKeyBox.addCommand(SHOW_COMMAND); startKeyBox.addCommand(BACK_COMMAND); startKeyBox.setCommandListener(new BTreeDemoROCommandListener()); mainMenu(); } } |
This method first gets the devices display and assigns it to the display attribute of the class. It then calls the openBtree method and attempts to connect to the pre-existing BTree located at the URL "http://localhost:80/HTTPTree". If you have located the BTree elsewhere you will need to change this URL. The code can also be amended to use a BTree via the Socket Connection. You can find details of how to use the Socket Connection here. The method then builds the Menu, Forms, Textfields and TextBoxes required by the application. This construction process is more fully described in the J2ME Wireless Toolkit documentation
The next method we want to look at is the openBTree method -
private boolean openBTree(String resource, boolean newTree) { boolean ok = false; try { btree = new BTreeMEROInMem(resource+".DAT",resource+".IND",512,512); ok = true; } catch(BTreeFileException bfe) { error("Could not open BTree - Check Connection ("+bfe.toString()+")"); } catch(BTreeException be) {error("BTree Exception - Check Connection ("+be.toString()+")");} catch(Exception e) {error("General Exception opening BTree - Check Connection ("+e.toString()+")");} finally { return ok; } } |
This method attempts to open an instance of BTreeMEROInMem using the resource address supplied
When the initialisation is complete the mainMenu() method is called -
private void mainMenu() { display.setCurrent(menu); currentMenu = "Main"; } |
This loads the initial menu onto the Display. This menu has been assigned a CommandListener (in this case a BTreeROCommandListener instance assigned to the menu in the startApp() method). When the menu is loaded it's BTreeROCommandListener waits for commands from the device and processes them in it's CommandAction() method -
public void commandAction(Command c, Displayable d) { String type = c.getLabel(); if (type.equals("Main")) { mainMenu(); } else if (type.equals("Back")) { if (currentMenu.equals("ViewFormAll")) { display.setCurrent(keylistAll); currentMenu = "KeyListAll"; } else { if (currentMenu.equals("ViewFormSome")) { display.setCurrent(keylistSome); currentMenu = "KeyListSome"; } else { mainMenu();} } } else if (type.equals("Show")) { if (currentMenu.equals("ViewEntry")) { viewForm();} else { if ((currentMenu.equals("KeyListAll")) || (currentMenu.equals("KeyListSome"))) { List temp = (List) display.getCurrent(); viewForm(temp.getString(temp.getSelectedIndex()),currentMenu.substring(7)); } else { if (currentMenu.equals("StartKey")) {showListSome(startKeyBox.getString());} } } } else if (type.equals("Exit")) { try { destroyApp(true); } catch (MIDletStateChangeException msce) { mainMenu(); } catch (Exception e) { System.out.println("UNKNOWN ERROR DESTROYING APP "+e.toString()); } } else if (type.equals("About")) { about(); } else { List shown = (List) display.getCurrent(); if (currentMenu.equals("Main")) { switch (shown.getSelectedIndex()) { case 0: viewEntry(); break; case 1: showListAll(); break; case 2: startEntry(); break; default: break; } } } return; } |
This method filters the commands as they come in and decides which of the other methods in the class are to be run. As we are on the Main menu we end up in the last 'else' block of the code. This calls the functions to View a record in the BTree (viewEntry()), show all the records in the BTree (showListAll()) or show selected records from the BTree (showListSome()). As an example we will follow what happens when we view a record in the BTree. When we choose the 'View Record' option on the menu and press 'select' we end up in the last 'else' block of the commandAction method.This causes the viewEntry() method to be called -
private void viewEntry() { lookupKeyBox.setString(""); display.setCurrent(lookupKeyBox); currentMenu = "ViewEntry"; } |
This initialises and displays the TextBox used to accept the text of the key to be looked up from the user. If you look at the original creation of the lookupKeyBox in the startApp() method you'll see that two commands (the SHOW_COMMAND and the BACK_COMMAND) have been added. The TextBox also has it's own BTreeROCommandListener instance which processes any command key strokes - again using the commandAction method. In this case the first part of the method is used and if the 'Back' command is detected the main menu is displayed (by calling mainMenu()). If the 'Show' command is detected the viewForm() method is called -
private void viewForm() { if (lookupKeyBox.getString().length()!=0) { try { resultField.setString(btree.get(lookupKeyBox.getString())); display.setCurrent(viewForm); currentMenu = "ViewForm"; } catch (BTreeException be) {System.out.println(be.toString());} } } |
This performs the lookup on the BTree and displays the result in the resultField textField which has been associated with this Form. The only option on this screen is to press the 'Back' key and this will again be processed by the commandAction method and will pass control back to the main menu.
All of the other functions perform in a simiar manner and you should be able to trace how they work by following the flow of control in the commandAction method.
You can change the source of the data by changing the resource string used to call openBTree() in the startApp() method. You can also change the max number of characters that can be accepted/displayed in the key and value fields by changing the static variables MAX_INDEX_CHARS and MAX_DATA_CHARS
You can use this demo class as a starting block for other applications using the BTreeMERO.