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

/**
 * The minime core package defines the interactions between the application 
 * and the environment in which the application runs.
 * 
 */
package minime.core;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * This is the main class.it inherit MIDlet 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 Fabrice Armisen
 */

public class Minilet extends MIDlet implements CommandListener
{

    private EventDispatcher eventDispatcher = null;
    private boolean startedOnce = false;
    public static Minilet inst;

    public Minilet()
    {
        minime.core.Runtime.bootstrap(this);
        eventDispatcher = EventDispatcher.getInstance();
        Displayable d = new CanvasDisplayable() ;
        minime.core.Runtime.getInstance().setDisplayable(d);
        inst = this;
        d.startGameThread() ;
    }

    //
    // from MIDlet
    //
    /**
     * Start the application.
     * 
     * @exception MIDletStateChangeException
     *                Description of the Exception
     */
    public void startApp() throws MIDletStateChangeException
    {
        eventDispatcher.fire(Event.createEvent(Event.APP_EVENT, startedOnce ? Event.APP_RESUME : Event.APP_START, 0));
        startedOnce = true;
    }

    public void setCurrentDisplayable(Displayable displayable)
    {
    }

    public void setDefaultDisplayable()
    {
    }

    /**
     * stop and throw out the garbage.
     * 
     * @param unconditional
     *            Description of the Parameter
     * @exception MIDletStateChangeException
     *                Description of the Exception
     */
    public void destroyApp(boolean unconditional)
    {
        eventDispatcher.fire(Event.createEvent(Event.APP_EVENT, Event.APP_DESTROY, 0));
    }

    /**
     * Pause the application.
     */
    public void pauseApp()
    {
        eventDispatcher.fire(Event.createEvent(Event.APP_EVENT, Event.APP_PAUSE, 0));
    }

    //
    // from CommandListener
    //
    public void commandAction(Command c, javax.microedition.lcdui.Displayable d)
    {

    }
}
