September 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

BREW & J2ME: Let's Be Friends!

  • December 4, 2003
  • By Radu Braniste
  • Send Email »
  • More Articles »

Wrapping Up

Let's write an application from scratch, using the presented technology.

The framework comes encapsulated in cppapp and brewJ2ME. An application is a class—be it Midlet in file MyApp.h. These are the steps to be followed:

  1. Include "MyApp.h" in cppapp.h.
  2. Create a Midlet class inheriting from IMidlet.
  3. Define type APPLICATION_TYPE as Midlet.

And here is the resulting skeleton:

class Midlet : public IMidlet
{
};
typedef Midlet APPLICATION_TYPE ;

Our simple application will deal with two lists, each one having four items—{"1","2","3","4"} and {"A","B","C","D"}. Pressing "1," for example, takes us to the correspondent "A," pressing "C" switches to "3," and so on. This logic is embedded in the event handlers: my14ListUsage and myADListUsage.

class Midlet : public IMidlet
{
typedef ListImpl<Midlet> List;
private:
  bool my14ListUsage( List* l)
  {
    int pos = l->getSelectedIndex();
    return (pos == INDEX_OUT_OF_BOUNDS) ?
            false : BuildADList(pos), true;
  }
  bool myADListUsage( List* l) 
  {
    int pos = l->getSelectedIndex();
    return  (pos == INDEX_OUT_OF_BOUNDS)  ?
             false : Build14List(pos), true;
  }
  List * Init14List()
  {
    List* l = List::getList("List14", this);
    if (!l) return 0;
    l->setCbk(my14ListUsage);
    WString a[] = {"1","2","3","4"};
    l->append(a,SIZE_OF(a));
    return l;
  }
  List * InitADList()
  {
    List* l = List::getList("ListAD", this);
    if (!l) return 0;
    l->setCbk(myADListUsage);
    WString a[] = {"A","B","C","D"};
    l->append(a,SIZE_OF(a));
    return l;
  }
  void BuildADList(int ix )
  {
    if (!lAD_)
    {
      lAD_ =InitADList();
    }
    BuildList(lAD_, ix);
  }
  void Build14List(int ix )
  {
    if (!l14_)
    {
      l14_ = Init14List();
    }
    BuildList(l14_, ix);
  }
  void BuildList(List* l , int ix)
  {
    if (!l || (l->size()-1)<ix) return;
    l->setSelection(ix);
    getDisplayable()->setCurrent(l);
  }
public:
  virtual bool onStart() 
  {
    l14_ = 0;
    lAD_ = 0;
    BuildADList(2);
    return true;
  }
private:
  List* l14_;
  List* lAD_;
};

What's really interesting, other than the striking simplicity of code, is the clarity of event handling.

Where Do We Go from Here?

This first attempt didn't cover important subjects such as multi-control screens, handling events other than EVT_COMMAND, and controlling the registered resources. The next installment will discuss all these topics as well as mechanisms to extend the framework.

Accompanying Source Code

Source code: cppapp.zip - 98 kb

References

  1. BREW and J2METM—A Complete Wireless Solution for Operators Committed to Java—Qualcomm http://www.qualcomm.com/brew/images/about/pdf/brew_j2me.pdf
  2. Design Patterns: Elements of Reusable Object-Oriented Software—ErichGamma, RichardHelm, RalphJohnson, and John Vlissides, Addison-Wesley, 1994
  3. String in BREW Revisited—a BrewString Generalizationhttp://www.developer.com/ws/brew/article.php/2229051
  4. Emulating C++ Exception Handling—Gregory Colvin, C/C++ Users Journal, December 1994
  5. For Brew Developers, There Are New Kids in Town: IThread & IRscPool, Part 2http://www.developer.com/ws/brew/article.php/3105131

About the Author

Radu Braniste is Director of Technology at Epicad. He can be contacted at rbraniste@epicad.com

# # #





Page 4 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel