
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * @author Dominik Schmidt

Copyright (C) 2006 Media Informatics Group (www.mimuc.de), 
University of Munich, Contact person: Enrico Rukzio
(Enrico.Rukzio@ifi.lmu.de)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


 */
public class CommandTest extends MIDlet implements CommandListener {

	private Display display;

	private Form[] forms;

	private List menu;

	public CommandTest() {
		super();
		display = Display.getDisplay(this);
		init();
	}

	public void commandAction(Command c, Displayable d) {
		if (c == List.SELECT_COMMAND) {
			List list = (List) d;
			int i = list.getSelectedIndex();

			if (i == forms.length) {
				notifyDestroyed();
			} else {
				display.setCurrent(forms[list.getSelectedIndex()]);
			}
		} else {
			display.setCurrent(menu);
		}

	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	private void init() {

		menu = new List("Main menu", List.IMPLICIT);
		menu.setCommandListener(this);

		int formCount = 26;
		forms = new Form[formCount];
		for (int i = 0; i < forms.length; i++) {
			forms[i] = new Form("Form" + i);

			forms[i].append("Select any command to return to main menu");
			forms[i].setCommandListener(this);

			menu.append("Form" + i, null);
		}

		int l = 5;
		Command okCommandLowPrio = new Command("OK", Command.OK, l);
		Command cancelCommandLowPrio = new Command("Cancel", Command.CANCEL, l);
		Command backCommandLowPrio = new Command("Back", Command.BACK, l);
		Command helpCommandLowPrio = new Command("Help", Command.HELP, l);
		Command screen1CommandLowPrio = new Command("Screen1", Command.SCREEN,
				l);
		Command screen2CommandLowPrio = new Command("Screen2", Command.SCREEN,
				l);
		Command itemCommandLowPrio = new Command("Item", Command.ITEM, l);
		Command exitCommandLowPrio = new Command("Exit", Command.EXIT, l);

		int h = 10;
		Command okCommandHighPrio = new Command("OK", Command.OK, h);
		Command cancelCommandHighPrio = new Command("Cancel", Command.CANCEL, h);
		Command backCommandHighPrio = new Command("Back", Command.BACK, h);
		Command helpCommandHighPrio = new Command("Help", Command.HELP, h);
		Command screen1CommandHighPrio = new Command("Screen1", Command.SCREEN,
				h);
		Command screen2CommandHighPrio = new Command("Screen2", Command.SCREEN,
				h);
		Command itemCommandHighPrio = new Command("Item", Command.ITEM, h);
		Command exitCommandHighPrio = new Command("Exit", Command.EXIT, h);

		int i = 0;
		// List (main menu)
		//		Siemens S65:
		//			-- (Häkchen)->Select Select
		//		Nokia 6230:
		//		J2ME Polish:
		//			Select [arrow symbol]->Select --

		forms[i].append("Cancel [h], OK [h]");
		forms[i].addCommand(cancelCommandHighPrio);
		forms[i].addCommand(okCommandHighPrio);
		//		Siemens S65:
		//		  	OK C Cancel
		//		Nokia 6230:
		//			-- OK Cancel
		//		J2ME Polish:
		//			OK -- Cancel

		i++;

		forms[i].append("Cancel [l], OK [h]");
		forms[i].addCommand(cancelCommandLowPrio);
		forms[i].addCommand(okCommandHighPrio);
		//		Siemens S65:
		//		  	OK C Cancel
		//		Nokia 6230:
		//			-- OK Cancel
		//		J2ME Polish:
		//			OK -- Cancel

		i++;

		forms[i].append("OK [h], Cancel [h]");
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(cancelCommandHighPrio);
		//		Siemens S65:
		//		  	OK C Cancel
		//		Nokia 6230:
		//			-- OK Cancel
		//		J2ME Polish:
		//			OK -- Cancel

		i++;

		forms[i].append("Back [h], OK [h]");
		forms[i].addCommand(backCommandHighPrio);
		forms[i].addCommand(okCommandHighPrio);
		//		Siemens S65:
		//		  OK B Back (rote Taste geht)
		//		Nokia 6230:
		//			-- OK Back
		//		J2ME Polish:
		//			OK -- Back (rote Taste geht)

		i++;

		forms[i].append("OK [h], Back [h]");
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(backCommandHighPrio);
		//		Siemens S65:
		//		  	OK B Back (rote Taste geht)
		//		Nokia 6230:
		//			-- OK Back
		//		J2ME Polish:
		//			OK -- Back (rote Taste geht)

		i++;

		forms[i].append("OK [h], Cancel [h], Help [h]");
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(cancelCommandHighPrio);
		forms[i].addCommand(helpCommandHighPrio);
		//		  Siemens S65:
		//		 	 OK C Options
		//		  			- Cancel
		//		  			- Help
		//		  Nokia 6230:
		//			Option. OK Cancel
		//			- Help
		//		  J2ME Polish:
		//			Options -- Cancel
		//			- OK
		//			- Help

		i++;

		forms[i].append("OK [l], Cancel [l], Help [h]");
		forms[i].addCommand(okCommandLowPrio);
		forms[i].addCommand(cancelCommandLowPrio);
		forms[i].addCommand(helpCommandHighPrio);
		//		  Siemens S65:
		//		 	OK C Options
		//		  			- Cancel
		//		  			- Help
		//		  Nokia 6230:
		//			Option. OK Cancel
		//			- Help
		//		  J2ME Polish:
		//			Options -- Cancel
		//			- OK
		//			- Help

		i++;

		forms[i]
				.append("Screen1 [h], Screen2 [h], Help [h], OK [h], Cancel [h]");
		forms[i].addCommand(screen1CommandHighPrio);
		forms[i].addCommand(screen2CommandHighPrio);
		forms[i].addCommand(helpCommandHighPrio);
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(cancelCommandHighPrio);
		//		 Siemens S65:
		//		 Screen2 S Options
		//		  				- Screen1
		//		  				- Cancel
		//		  				- OK
		//		  				- Help
		//		
		//		  Nokia 6230:
		//			Option. OK Cancel
		//			- Screen1
		//			- Screen2
		//			- Help
		//		  J2ME Polish:
		//			Options -- Cancel
		//			- Screen1
		//			- Screen2
		//			- OK
		//			- Help

		i++;

		forms[i]
				.append("Screen2 [h], Screen1 [h], Help [h], OK [h], Cancel [h], Back [h]");
		forms[i].addCommand(screen2CommandHighPrio);
		forms[i].addCommand(screen1CommandHighPrio);
		forms[i].addCommand(helpCommandHighPrio);
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(cancelCommandHighPrio);
		forms[i].addCommand(backCommandHighPrio);
		//		 Siemens S65:
		//		 Screen1 S Options
		//		  				- Screen2
		//						- Back
		//		  				- Cancel
		//		  				- OK
		//		  				- Help
		//		 
		//		  Nokia 6230:
		//			Option. OK Cancel
		//			- Screen2
		//			- Screen1
		//			- Help
		//			- Back
		//		  J2ME Polish:
		//			Options -- Cancel
		//			- Screen2
		//			- Screen1
		//			- Help
		//			- OK
		//			- Back

		i++;

		forms[i]
				.append("Screen1 [l], Screen2 [l], Help [h], OK [h], Cancel [h]");
		forms[i].addCommand(screen1CommandLowPrio);
		forms[i].addCommand(screen2CommandLowPrio);
		forms[i].addCommand(helpCommandHighPrio);
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(cancelCommandHighPrio);
		//		 Siemens S65:
		//		 Screen2 S Options
		//		  				- Screen1
		//		  				- Cancel
		//		  				- OK
		//		  				- Help
		//		  Nokia 6230:
		//			Option. OK Cancel
		//			- Screen1
		//			- Screen2
		//			- Help
		//		  J2ME Polish:
		//			Options -- Cancel
		//			- Screen1
		//			- Screen2
		//			- Help
		//			- OK

		i++;

		forms[i]
				.append("Screen1 [h], Screen2 [l], Help [h], OK [h], Cancel [h]");
		forms[i].addCommand(screen1CommandHighPrio);
		forms[i].addCommand(screen2CommandLowPrio);
		forms[i].addCommand(helpCommandHighPrio);
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(cancelCommandHighPrio);
		//		 Siemens S65:
		//		 Screen1 S Options
		//		  				- Screen2
		//		  				- Cancel
		//		  				- OK
		//	
		//		  Nokia 6230:
		//			Option. OK Cancel
		//			- Screen2
		//			- Screen1
		//			- Help
		//		  J2ME Polish:
		//			Options -- Cancel
		//			- Screen1
		//			- Screen2
		//			- Help
		//			- OK

		i++;

		forms[i].append("Screen1 [h], Item [h],  OK [h], Cancel [h]");
		forms[i].addCommand(screen1CommandHighPrio);
		forms[i].addCommand(itemCommandHighPrio);
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(cancelCommandHighPrio);
		//		  Siemens S65:
		//		  Cancel S Options
		//		  			- Screen1
		//					- OK
		//					- Item
		//		  Nokia 6230:
		//			Option. OK Cancel
		//			- Item
		//			- Screen1
		//		  J2ME Polish:
		//			Options -- Cancel
		//			- Screen1
		//			- Item
		//			- OK

		i++;

		forms[i].append("Screen1 [h], Item [l],  OK [h], Cancel [h]");
		forms[i].addCommand(screen1CommandHighPrio);
		forms[i].addCommand(itemCommandLowPrio);
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(cancelCommandHighPrio);
		//		  Siemens S65:
		//		  Screen1 I Options
		//		  			- Item
		//					- Cancel
		//					- OK
		//		  Nokia 6230:
		//			Option. OK Cancel
		//			- Item
		//			- Screen1
		//		  J2ME Polish:
		//			Options -- Cancel
		//			- Screen1
		//			- Item
		//			- OK

		i++;

		forms[i].append("Cancel [h], Item [h]");
		forms[i].addCommand(cancelCommandHighPrio);
		forms[i].addCommand(itemCommandHighPrio);
		//		  Siemens S65:
		//			Item C Cancel
		//		  Nokia 6230:
		//			-- Item Cancel
		//		  J2ME Polish:
		//			Item -- Cancel

		i++;

		forms[i].append("Screen1 [h]");
		forms[i].addCommand(screen1CommandHighPrio);
		//		  Siemens S65:
		//			-- S Screen1
		//		  Nokia 6230:
		//			-- Screen1 --
		//		  J2ME Polish:
		//			Screen1 -- --

		i++;

		forms[i].append("Screen1 [h], Screen2 [h]");
		forms[i].addCommand(screen1CommandHighPrio);
		forms[i].addCommand(screen2CommandHighPrio);
		//		  Siemens S65:
		//			Screen2 S Screen1
		//		  Nokia 6230:
		//			Option. Screen1 --
		//			- Screen2
		//		  J2ME Polish:
		//			Options -- --
		//			- Screen1
		//			- Screen2

		i++;

		forms[i].append("OK [h], Exit [h]");
		forms[i].addCommand(okCommandHighPrio);
		forms[i].addCommand(exitCommandHighPrio);
		//		  Siemens S65:
		//			Exit O OK
		//		  Nokia 6230:
		//			-- OK Exit
		//		  J2ME Polish:
		//			Options -- --
		//			- OK
		//			- Exit

		i++;

		forms[i].append("Cancel [h], Exit [h]");
		forms[i].addCommand(cancelCommandHighPrio);
		forms[i].addCommand(exitCommandHighPrio);
		//		  Siemens S65:
		//			Exit C Cancel
		//		  Nokia 6230:
		//			Option. -- Cancel
		//			- Exit
		//		  J2ME Polish:
		//			Exit -- Cancel

		i++;

		forms[i].append("Exit [h]");
		forms[i].addCommand(exitCommandHighPrio);
		//		  Siemens S65:
		//			-- E Exit
		//		  Nokia 6230:
		//			-- -- Exit
		//		  J2ME Polish:
		//			Exit -- --

		i++;

		forms[i].append("Cancel [h]");
		forms[i].addCommand(cancelCommandHighPrio);
		//		  Siemens S65:
		//			-- C Cancel
		//		  Nokia 6230:
		//			-- -- Cancel
		//		  J2ME Polish:
		//			-- -- Cancel

		i++;

		forms[i].append("Back [h]");
		forms[i].addCommand(backCommandHighPrio);
		//		  Siemens S65:
		//			-- B Back (rote Taste geht)
		//		  Nokia 6230:
		//			-- -- Back
		//		  J2ME Polish:
		//			-- -- Back (rote Taste geht)

		i++;

		forms[i].append("Help [h]");
		forms[i].addCommand(helpCommandHighPrio);
		//		  Siemens S65:
		//			-- H Help
		//		  Nokia 6230:
		//			-- Help --
		//		  J2ME Polish:
		//			Help -- --

		i++;

		forms[i].append("Screen1 [h]");
		forms[i].addCommand(screen1CommandHighPrio);
		//		  Siemens S65:
		//			-- S Screen1
		//		  Nokia 6230:
		//			-- Screen1 --
		//	  	J2ME Polish:
		//			Screen1 -- --

		i++;

		forms[i].append("Screen1 [h], Screen2 [h], OK [h]");
		forms[i].addCommand(screen1CommandHighPrio);
		forms[i].addCommand(screen2CommandHighPrio);
		forms[i].addCommand(okCommandHighPrio);
		//		  Siemens S65:
		//			Screen2 S Options
		//						- Screen1
		//						- OK
		//		  Nokia 6230:
		//			Option. OK --
		//			- Screen1
		//			- Screen2
		//		  J2ME Polish:
		//			Options -- --
		//			- Screen1
		//			- Screen2
		//			- OK

		i++;

		forms[i].append("Item [h], Back [h]");
		forms[i].addCommand(itemCommandHighPrio);
		forms[i].addCommand(backCommandHighPrio);
		//		  Siemens S65:
		//			Item B Back (rote Taste geht)
		//		  Nokia 6230:
		//			-- Item Back
		//		  J2ME Polish:
		//			Item -- Back (rote Taste geht)

		i++;

		forms[i].append("Back [h], Exit [h], Cancel [h]");
		forms[i].addCommand(backCommandHighPrio);
		forms[i].addCommand(exitCommandHighPrio);
		forms[i].addCommand(cancelCommandHighPrio);
		//		  Siemens S65:
		//			Cancel B Options
		//					 - Back
		//					 - Exit
		//		  Nokia 6230:
		//			Option. -- Cancel
		//			- Back
		//			- Exit
		//		  J2ME Polish:
		//			Options -- Back
		//			- Exit
		//			- Cancel

		menu.append("Exit", null);

		// - Nokia 6230: Prioritäten gelten nur innerhalb von Command-Klassen
		// - Siemens S65: Prioritäten können auch über Command-Klassen hinweg
		//   die Positionierung beeinflussen (bei OK und Cancel bspsweise aber
		// 	 nicht) - Zusammenhang mit Prioritätswert?
		// - Reihenfolge des Hinzufügens ist nur bei gleicher Priorität relevant
		// - Nokia 6230 belegt rechten Button nur mit Cancel, Exit oder Back
		// - Nokia 6230 legt Cancel, Exit und Back nicht in die Mitte
		// - Siemens S65: Die mittlere Taste entspricht immer der rechten oder
		//   falls ein Menü vorhanden ist dem ersten Eintrag, sie ist mit dem
		//   ersten Buchstaben beschriftet
		// - Siemens S65: Ist ein Back-Command definiert, so wird auch die rote
		//   Auflegentaste damit belegt
		// - Siemens S65: Priorität der Command-Plätze: Mitte (und damit auch
		//   rechts), links, rechts
		// - Siemens S65: Priorität der Commands: Screen, Back, Cancel, Exit,
		//   OK, Item, Help
		// - Nokia 6230: Priorität der Command-Plätze: Mitte, links, rechts nur
		//   für Ausgewählte
		// - Nokia 6230: Priorität der Commands: OK, Item, Screen, Help, Cancel,
		//   Back, Exit
		// - J2ME-Polish belegt rechten Button nur mit Cancel oder Back
		// - J2ME-Polish: mittlere Taste kann nicht explizit belegt werden
		// - J2ME-Polish: Priorität der Command-Plätze: na, da nur links frei
		//   belegt wird
		// - J2ME-Polish: Priorität der Commands: Screen, Item, OK, Help (?),
		//   Back, Exit, Cancel

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {

		display.setCurrent(menu);

	}

}