/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */

/** \page main Getting started
 * This document presents how to build a project use minime component, event, resourcemanager, ... .
 * minime is responsible for ui implement, view change,event dispatch and Resource management.
 * 
 * The progrom is divided in following sections:
 * - \ref com.fmc.view Application Views
 * - \ref com.fmc.midlet Application midlet
 * - \ref minime Contains resources classes, and UI facilites.
 * - \ref minime.core Defines the application running enviroment.
 * - \ref minime.ui Provides a set of features for implementation of user interfaces.
 * 
 * \package com.fmc.midlet
 * This package is responsible for starting up midlet.And more, it contains constants of images index 
 * and text index used for application.
 * 
 * \page Summarize
 * How to create a simple application use minime:
 *   - create a midlet class which inherits Minilet:\n
 *      public class SimpleMidlet extends Minilet\n 
 *      {\n 
 *          public SimpleMidlet()\n 
 *          {\n 
 *              ...\n 
 *          }\n
 *          \n
 *          ...\n 
 *      }\n 
 *   - create a view class,this view has softbar,but not has title:
 *      public class MainView extends BaseView\n
 *      {\n
 *          public MainView()\n
 *          {\n
 *              super(null, true, backgroundImage);\n
 *              ...\n
 *          }\n
 *          ...\n
 *      }\n
 *   - Inport ResourceManager into application, because texts are built into a text binary file, 
 *     images are built into image binary file, so it gets resource based on resource id, it is 
 *     introduced in ResourceManager document.\n
 *     Lets initialize ResourceManager in TestMidlet constructure method, like this:\n
 *      public SimpleMidlet()\n
 *      {\n
 *          ...\n
 *          // set resource files\n
 *          String[] rscFileNames = {"/rsc/text.${text.rsc.file}.bin", "/rsc/image.${img.rsc.file}.bin"};\n
 *          try\n
 *          {\n
 *              ResourceManager.createResourceManger(rscFileNames);\n
 *              // set defualt resource store\n
 *              ResourceManager.setDefaultResourceStore(ResourceManager.STRING_STORE, rscFileNames[0]);\n
 *              ResourceManager.setDefaultResourceStore(ResourceManager.IMAGE_STORE, rscFileNames[1]);\n
 *          }\n
 *          catch(IOException e){}\n
 *      }\n
 *     when preprocess resource, resource ids are built into a java file, e.g. ImageRscId stores the image
 *     resource ids. So we can get resource using resource id like this:\n
 *      public MainView() extends BaseView\n
 *      {\n
 *          super(null, true, ImageRscId.IM_BACKGROUND);\n
 *          ...\n
 *      }\n
 *   - Add components into view. We can add lots of components into view to enrich it.For example, add a
 *     Label and set softbar in MainView like this:\n
 *      public MainView()\n
 *      {\n
 *          ...\n
 *          // initialize the label\n
 *          infoLabel = new Label(TextRscId.TX_WELCOME, Portability.BLACK);\n
 *      }\n
 *      public void activate()\n
 *      {\n
 *          super.activate();\n
 *          ...\n
 *          // add the label into view\n
 *          addToView(infoLabel);\n
 *          // set softbar buttons' label\n
 *          setSoftbar(ResourceManager.NONE_RESOURCE, TextRscId.TX_CHOOSE, TextRscId.TX_EXIT);\n
 *      }\n
 *      public void deactivate()\n
 *      {\n
 *          ...\n   
 *          // remove the label from the view\n
 *          removeFromView(infoLabel);\n
 *          super.deactivate();\n
 *      }\n
 *      public Rectangle layout()\n
 *      {\n
 *          Rectangle da = super.layout();\n
 *          ...\n
 *          infoLabel.setPosition(da.x, da.y + 74, da.w/2, 0);\n
 *          ...\n
 *      }\n
 *     Actually, when the view is begin to display on screen, it will call activate method to load 
 *     some dynamic resources, when leave screen, it will call deactivate method to remove resources.
 *     Method layout is used to calculate the view display area and set components' position, most of
 *     time component can calculate its size by itself, but some times it needs to calculate component's
 *     size in layout method, if want to learn detail please read minime ui document. 
 *  - About how to change views,it relates to Runtime class,Runtime class has three methods about views
 *    change. 
 *      -# pushView(view) is push the view into Runtime stack and display this view on screen.
 *      -# popView() is pop a view from stack and take the view at the top of stack as the current view to
 *         display, if no view in stack will quit the midlet. 
 *      -# gotoView(view) is just display the view and 
 *         doesn't push the view into stack.
 *    For example, we have another DisplayView, we change the displayed view from mainView to displayView
 *    like this, in mainView:\n
 *      public boolean onEvent(Event evt)\n
 *      {\n
 *          ...\n
 *          Runtime.getInstance().pushView(new DisplayView());\n
 *          ...\n
 *      }\n
 *    Now the view displayed on screen is displayView.If want to back to mainView, in displayView just do
 *    like this:\n
 *      public class DisplayView extends BaseView\n
 *      {\n
 *          ...\n
 *          public boolean onEvent(Event evt)\n
 *          {\n
 *              ...\n
 *              Runtime.getInstance().popView();\n
 *              ...\n
 *          }\n
 *      }\n
 *    Now the view displayed on screen is mainView.
 *  - Another characteristic in minime is Event handling mechanism, it's something like windows message
 *    mechanism.The Event class has many event type, an object can fire some type event,EventDispatcher will
 *    add this event into queue and call EventListener to handle this event.Maybe there are many eventListener
 *    in EventDispather, EventDispatcher will call those event listeners to handle event in loop until some 
 *    one handled it.If one eventListener can't handle the event onEvent(event) method will return false, 
 *    otherwise some one can handle the event it, onEvent will return true and other listeners won't be 
 *    called anymore. So if some class implements EventListener interface it will listen events.
 *    Let's introduce a simple example, Because the base View in minime has implemented EventListener 
 *    interface, so it can listen events, mainView is the child class of View, so it also can listen events.
 *    The MainView listens softbar event, when press one softkey, softbar will fire a SB_EVENT type event.
 *    In MainView, onEvent method will catch this event and handle it:\n
 *      public boolean onEvent(Event evt)\n
 *      {\n
 *          if(evt.type == Event.SB_EVENT)\n
 *          {\n
 *              if (evt.paramB == TextRscId.TX_BACK)\n
 *              {\n
 *                  ...\n
 *                  return true;\n
 *              }\n
 *          }\n
 *          return false;\n
 *      }\n
 *    In this example, it catches the softbar event and if event paramB is TextRscId.TX_DOWNLOAD, it will 
 *    handle the event and return true, otherwise return false and next EventListener will check the event.
 *
 */
package com.fmc.midlet;

import java.io.IOException;

import javax.microedition.lcdui.Display;

import minime.core.Minilet;
import minime.core.ResourceManager;
import minime.core.Runtime;

import com.fmc.view.SimpleMenuView;
import com.fmc.view.RadioButtonView;
import com.fmc.view.CheckBoxView;

/**
 * This is the main class, it inherit Minilet to to control the MIDlet and to be
 * able to retrieve properties from the application descriptor and notify and
 * request state changes. The methods of this class allow the application
 * management software to create, start, pause, and destroy a MIDlet.
 * 
 * @author yishu.chou
 * 
 */
public class SimpleMidlet extends Minilet
{
    public static SimpleMidlet inst;

    public SimpleMidlet()
    {
        inst = this;

        /**
         * Creates a new object of ResourceManager with the resource files, and
         * set these files as default, so it can directly use some methods like
         * ResourceManager.getString(txtId)..., otherwise it can only use
         * something like ResourceManager.getString("/rsc/text.${txt.rsc.file}.bin", txtId)
         * to get resource.
         */
        String[] rscFileNames = { "/rsc/text.${txt.rsc.file}.bin", "/rsc/image.${img.rsc.file}.bin" };
        try
        {
            ResourceManager.createResourceManger(rscFileNames);
            // set defualt resource store
            ResourceManager.setDefaultResourceStore(ResourceManager.STRING_STORE, rscFileNames[0]);
            ResourceManager.setDefaultResourceStore(ResourceManager.IMAGE_STORE, rscFileNames[1]);
        }
        catch (IOException e)
        {
            quitMidlet();
        }

        /**
         * Creates a main view object, push it into Runtime stack and make it as
         * the current view to display on screen.
         */
        Runtime.getInstance().pushView(new SimpleMenuView());
//        Runtime.getInstance().pushView(new CheckBoxView());
        //Runtime.getInstance().pushView(new RadioButtonView());
    }

    /**
     * Quits the application
     * 
     */
    public void quitMidlet()
    {
        destroyApp(false);
        notifyDestroyed();
    }

    /**
     * returns the Display object by this instance of midlet
     * 
     * @return the Display object by this instance of midlet
     */
    public Display getMidletDisplay()
    {
        return Display.getDisplay(this);
    }

}
