A P P E N D I X  A

Sample Application Source Code

This appendix provides the source code for the four files that comprise the sample ConverterMIDlet application:


ConverterMIDlet Source Code

CODE EXAMPLE A-1 ConverterMIDlet.java
package myconverter;
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
 
public class ConverterMIDlet extends MIDlet {
  static  ConverterMIDlet instance;
  private Converter       mainScreen;
 
  private static String storedDataStr = "FromToCurr";
  private RecordStore   storedData;
  
  /**Construct the midlet*/
  public ConverterMIDlet() {
    this.instance = this;
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**Main method*/
  public void startApp() {
    int fridx = 0;
    int toidx = 1;
    try {
      storedData = RecordStore.openRecordStore(storedDataStr, true);
      if (storedData.getNumRecords() > 0) {
        byte[] data = storedData.getRecord(1);
        fridx = (int)data[0];
        toidx = (int)data[1];
      }
    } catch (RecordStoreException e) {
    }
    mainScreen = new Converter(fridx, toidx);
    Display.getDisplay(this).setCurrent(mainScreen);
  }
 
  /**Handle pausing the MIDlet*/
  public void pauseApp() {
  }
 
  /**Handle destroying the MIDlet*/
  public void destroyApp(boolean unconditional) {
    try {
      byte[] data = new byte[2];
      data[0] = (byte)mainScreen.settings.frList.getSelectedIndex();
      data[1] = (byte)mainScreen.settings.toList.getSelectedIndex();
      if (storedData.getNumRecords() > 0)
        storedData.setRecord(1, data, 0, 2);
      else
        storedData.addRecord(data, 0, 2);
    } catch (Exception e) {
    }
  }
 
  /**Quit the MIDlet*/
  public static void quitApp() {
    instance.destroyApp(true);
    instance.notifyDestroyed();
    instance = null;
  }
  public static void setScreen(Displayable d) {
    Display.getDisplay(instance).setCurrent(d);
  }
 
  private void jbInit() throws Exception {
  }
}


Converter Source Code

CODE EXAMPLE A-2 Converter.java
package converter;
 
import javax.microedition.lcdui.*;
 
public class Converter extends Form {
  static Converter  instance = null;
  TextField  frTextField = new TextField("", "", 1, 1);
  StringItem toStringItem = new StringItem("", "");
 
  Settings  settings  = null;
 
  /**Construct the displayable*/
  public Converter(int initFr, int initTo) {
    super("Converter");
    // set up this Displayable to listen to command events
    setCommandListener(new CommandListener()  {
      public void commandAction(Command c, Displayable d) {
        this_commandPerformed(c, d);
      }
    });
    setItemStateListener(new ItemStateListener() {
      public void itemStateChanged(Item item) {
        int fridx = settings.frList.getSelectedIndex();
        int toidx = settings.toList.getSelectedIndex();
        convert(fridx, toidx);
      }
    });
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    instance = this;
    settings = new Settings(initFr, initTo);
    changeFrom(initFr);
    changeTo(initTo);
  }
 
  /**Component initialization*/
  private void jbInit() throws Exception  {
    toStringItem.setLabel("(to)");
    frTextField.setLabel("(from)");
    frTextField.setConstraints(TextField.NUMERIC);
    frTextField.setMaxSize(10);
    // add the Exit command
    addCommand(new Command("Exit", Command.EXIT, 1));
    addCommand(new Command("Settings", Command.SCREEN, 1));
    this.append(frTextField);
    this.append(toStringItem);
  }
 
  /**Handle command events*/
  public void this_commandPerformed(Command c, Displayable d) {
    if (c.getCommandType() == Command.EXIT) {
      // exit the MIDlet
      ConverterMIDlet.quitApp();
    } else {
      ConverterMIDlet.setScreen(settings);
    }
  }
  public static void goBack(int fridx, int toidx) {
    instance.convert(fridx, toidx);
    ConverterMIDlet.setScreen(instance);
  }
 
  void convert(int fridx, int toidx) {
    String frvstr = frTextField.getString();
    int toval = 0;
    if (frvstr.length() > 0) {
      toval = Currencies.convert(
Integer.parseInt(frvstr), fridx, toidx);
    }
    toStringItem.setText(String.valueOf(toval));
  }
  static void changeFrom(int newidx) {
    instance.frTextField.setLabel(Currencies.currencies[newidx]);
  }
  static void changeTo(int newidx) {
    instance.toStringItem.setLabel(Currencies.currencies[newidx]);
  }
}


Currencies Source Code

CODE EXAMPLE A-3 Currencies.java
package converter;
 
import javax.microedition.lcdui.*;
 
public class Currencies extends List {
  static String[] currencies = {
    new String("US $"),      // US dollar
    new String("JP \u00a5"), // yen
    new String("Euro")
  };
  static int[][] ratesTop = {
    {  100, 13315,   112},
    {  100,   100,   100},
    {  100, 11830,   100}
  };
  static int[][] ratesBot = {
    {  100,   100,   100},
    {13315,   100, 11830},
    {  112,   100,   100}
  };
  static int  TYPE_FROM  = 0,
              TYPE_TO   = 1;
  private int type;
 
  /**Construct the displayable*/
  public Currencies(String name, int type, int initidx) {
    super(name + "Currencies", List.EXCLUSIVE);
    // set up this Displayable to listen to command events
    setCommandListener(new CommandListener()  {
      public void commandAction(Command c, Displayable d) {
        this_commandPerformed(c, d);
      }
    });
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    this.type = type;
    setSelectedIndex(initidx, true);
  }
 
  /**Component initialization*/
  private void jbInit() throws Exception  {
    for (int i = 0; i < currencies.length; i++) {
      this.append(currencies[i], null);
    }
    //addCommand(new Command("Back", Command.BACK, 2));
    addCommand(new Command("Save", Command.OK,   1));
  }
 
  /** Handle command events*/
  public void this_commandPerformed(Command c, Displayable d) {
    // add your command event handling here
    if (c.getCommandType() == Command.OK) {
      // save the settings and update the mainScreen
      if (type == TYPE_FROM)
        Converter.changeFrom(getSelectedIndex());
      else
        Converter.changeTo(getSelectedIndex());
    }
    // always goes back to Settings Screen
    Settings.goBack();
  }
 
  public static int convert(int frval, int fridx, int toidx) {
    return (frval * ratesTop[fridx][toidx]) / ratesBot[fridx][toidx];
  }
}


Settings Source Code

CODE EXAMPLE A-4 Settings.java
package converter;
 
import javax.microedition.lcdui.*;
 
public class Settings extends List {
  private static Settings instance = null;
  Currencies frList = null;
  Currencies toList = null;
 
  /**Construct the displayable*/
  public Settings(int initFr, int initTo) {
    super("Settings", List.IMPLICIT);
    // set up this Displayable to listen to command events
    setCommandListener(new CommandListener()  {
      public void commandAction(Command c, Displayable d) {
        this_commandPerformed(c, d);
      }
    });
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    instance = this;
    frList = new Currencies("From", Currencies.TYPE_FROM, initFr);
    toList = new Currencies("To",   Currencies.TYPE_TO,   initTo);
  }
 
  /**Component initialization*/
  private void jbInit() throws Exception  {
    append("1. From Currencies", null);
    append("2. To Currencies", null);
    addCommand(new Command("Back", Command.BACK, 2));
  }
 
  /** Handle command events*/
  public void this_commandPerformed(Command c, Displayable d) {
    // add your command event handling here
    if (c.getCommandType() == Command.BACK) {
      Converter.goBack(frList.getSelectedIndex(), toList.getSelectedIndex());
    } else {
        if (getSelectedIndex() == 0)
            ConverterMIDlet.setScreen(frList);
        else
            ConverterMIDlet.setScreen(toList);
    }
  }
 
  public static void goBack() {
    ConverterMIDlet.setScreen(instance);
  }
}