package com.marinilli.b2.c5;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
 * Chapter 5 - A Sample MIDlet
 *
 *
 * @author Mauro Marinilli
 * @version 1.0
 */

public class AMIDLet extends MIDlet implements CommandListener{
  private List mainList;
  private MIDLicenseManager licenseManager;

  /**
   * Constructor
   */
  public AMIDLet() {
    licenseManager = new MIDLicenseManager(this);
  }

  /**
   * Invoked when the MIDlet is paused
   */
  protected void pauseApp() {
  }

  /**
   * for cleaning up resources
   */
  protected void destroyApp(boolean parm1) throws javax.microedition.midlet.MIDletStateChangeException {
    licenseManager = null;
    mainList = null;
  }

  /**
   * To launch the MIDlet
   */
  protected void startApp() throws javax.microedition.midlet.MIDletStateChangeException {
    mainList = new List("Select a Number",
                            List.IMPLICIT,
                            new String[] {"Option 1","Option 2","Option 3","Register"},
                            null);

    Display.getDisplay(this).setCurrent(mainList);
    mainList.setCommandListener(this);
  }

  /**
   * handles user commands
   */
  public void commandAction(Command c, Displayable d) {
    int pos = mainList.getSelectedIndex();
    if (pos==3){
      licenseManager.register();
    }

    try {
      show("Hello!", "OK?");
    }
    catch(Exception err) {
      show("Error", "general exception");
    }
  }

  /**
   * utility method to show modal alerts
   */
  private void show(String title, String txt){
    Alert a = new Alert(title);
    a.setString(txt);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(this).setCurrent(a);
  }

}