/*
 * Teapot5MIDlet for Java 3D-enabled mobile phones supporting M3G (JSR-184) API.
 * Created by Mobilefish.com (http://www.mobilefish.com)
 * 
 * File: Teapot5MIDlet.java 
 * Needs: Teapot5Canvas.java
 * Resources: teapot5_box.m3g
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * If you have any questions, please contact contact@mobilefish.com
 */

package com.mobilefish.m3g;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.mobilefish.m3g.Teapot5Canvas;

public class Teapot5MIDlet extends MIDlet implements CommandListener {

    // Holds the unique display
    private Display display = null;
    // Holds the canvas
    private Teapot5Canvas canvas = null;
    // Create exit command
    private Command exitCommand = new Command("Exit", Command.ITEM, 1);
    
    /**
     * Teapot5MIDlet - default constructor.
     */
    public Teapot5MIDlet() {
        super();
        // Create the display
        display = Display.getDisplay(this);
        // Create the canvas 
        canvas = new Teapot5Canvas(this);
        // Set the listener to be the MIDlet
        canvas.setCommandListener(this);
        // Add exit command to the canvas
        canvas.addCommand(exitCommand);
    }

    /**
     * startApp()
     */
    protected void startApp() throws MIDletStateChangeException {
        try {
            // Start canvas
            canvas.start();
            // Set canvas to the display
            display.setCurrent(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * pauseApp()
     */
    protected void pauseApp() {
    }

   /**
     * destroyApp()
     */
    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException {
        notifyDestroyed();
    }

    /**
     * Handle commands.
     */
    public void commandAction(Command cmd, Displayable disp) {
        if (cmd == exitCommand) {
            try {
                destroyApp(false);
                notifyDestroyed();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
}
