MoME Guide

Sergio Morozov


A short & concise guide.

Overview

mome – is the main package of MoMELib library. Is intended to simplify implementation of commands processing in J2ME MIDlets. It introduces commands execution thread. A thread for processing command events. This thread is separate from AMS (Application Management Software) callback thread. In this case there is no need to worry about deadlocks with AMS and timing of callbacks implementations (deadlocks with other part of application are of course still possible). It is also possible to push user commands for processing programmaticaly. Command events are processed serially. A command events queue is maintained, so commands can be activated while another command is processed.

This package abstracts a term “command”. Command here means any object not necessarily LCDUI Command (including it of course). This simplifies definition and usage of commands. Recall commands are tested based on identity not equality. It is possible to push command – source association for processing in commands execution thread. Source in this case can be any object not necessarily LCDUI component on which this command occurred (including it of course). Because of this, it is even possible to push any complementary argument to command.

Note Like with any J2ME library it is recommended to use obfuscator to minimize memory usage.

Installation

Installation of MoME is like installation of any other j2me library. Just make classes contained in jar file accessible for class loaders and make sure they are in jar file of your project. To do this follow instruction of your IDE. In a case of pure WTK 2.2 just copy jar file to your project lib directory.

Usage

The use of this package is pretty simple. Developer should subclass MoXMIDlet and override method MoXMIDlet.xCommandAction( Object , Object ). The commands processing should be putted here. Then as usual define needed Commands and instantiate needed components. Set subclass of MoxMidlet as CommandListener or/and ItemCommandListener or ItemStateListener of above components. Thats all. Method MoXMIDlet.xCommandAction( Object , Object ) will be called serially for each command event in thread separate from AMS callback thread. For example:

          
  //Define Commands.
    ... 
  private static final Command TEST = new Command("Run", "Run Tests",
  Command.SCREEN, 32); 
    ...
  
  // Initiate components.
    ...
  Form mainForm = new Form("AppName");
    ...
             
  // Set this as CommandListener of component.
    ...
  mainForm.setCommandListener( this);
    ...
          
  // Override XCommandListener xCommandAction( Object cmd, Object src) method.
  public void xCommandAction(Object cmd, Object src)
  { 
    ...
    if (cmd == TEST) 
    {
      this.clearResult(this.suite.countTestCases());
      this.suite.run(this.getTestResult()); 
    } 
    ...
  }
    ...

Note For ItemStateChanged events command will be MoXMIDlet.ITEM_STATE_CHANGED and source will be item which state has changed.

It is also possible to process commands in AMS callback thread too. To do this just override action method of respective CommandListener, put commands processing code to be executed in AMS callback thread first and then call super method. For example:

  ...
  public void commandAction(Command cmd, Displayable src)
  {
    ...
    if (cmd == STOP) this.stopThread();
    else if (cmd == START) this.startThread();
    else super.commandAction( cmd, src);
  }
  ...

It is also possible to push command events (command - source association) to commands execution thread. Just call MoXMIDlet.pushCommand( Object cmd, Object src) method. Remember command can be any object and is tested based on identity, so it is easy to define user command like:

 
  private static final Object FIRE = new Object();

Source can also be any object. It needs not be only Displayable or Item on which command has occurred. It can be even complementary argument to command. For example:

in component:

  ...
  protected void keyPressed(int key)
  {
    switch (this.getGameAction(key))
    {
      case Canvas.FIRE:
        this.owner.pushCommand( FIRE, myBook);
        break;
      ...
    }
    ...
  }

in MoXMIDlet

  ...
  public void xCommandAction(Object cmd, Object src)
  { 
    if (cmd == FIRE) ((Book) src).burnIt();
    else
    ...
  }
  ...

Sergio Morozov. 2007 SourceForge.net Logo