/*
 *CommandDemo program to show how to use a CommandListener to handle user interaction
 *written by K. Banerjee
 *j2melbs.com
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class CommandDemo extends MIDlet implements CommandListener {
	//A class must implement the interface CommandListener to handle commands
	
	private Form form;
	Command exitCmd, helpCmd, okCmd;

	public CommandDemo() {
		
		form = new Form("Command Demo");	//Construct the Form

		// create some commands and add them to this form
		exitCmd = new Command("EXIT", Command.EXIT, 1);
		helpCmd = new Command("HELP", Command.HELP, 2);
		okCmd = new Command("OK", Command.OK, 2);
		form.addCommand(exitCmd);
		form.addCommand(helpCmd);
		form.addCommand(okCmd);

		// set itself as the command listener
		form.setCommandListener(this);
	}
	
	//A class implementing CommandListener must define its commandAction() function
	//to handle commands
	public void commandAction(Command com, Displayable dis) {

		if(exitCmd.equals(com)) {
			
			notifyDestroyed();	//exits application
		}
		else if(helpCmd.equals(com)) {
			
			form.append("You clicked on HELP command");
		}
		else if(okCmd.equals(com)) {
			
			form.append("You clicked on OK command");
		}
		/*
		 *NOTE here we are not using the displayable object passed to the function because
		 *we know there is only one diaplayable object, the form, in this application
		 *If the same CommandListener listenes to more than one displayable objects for 
		 *possible interaction with a user through commands, this argument needs to be used
		 **/	
	}

	public void startApp() {
		Display display = Display.getDisplay(this);
		display.setCurrent(form);
		/*
		 *NOTE that exitCmd with higher priority is displayed separately
		 *whereas helpCmd and okCmd are clubbed together if there are only two positions
		 *to show commands on the screen.
		 */
	}

	public void pauseApp() {
	}

	public void destroyApp(boolean unconditional) {
	}
}