View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.microemu.app.launcher;
21  
22  import java.util.Vector;
23  
24  import javax.microedition.lcdui.Command;
25  import javax.microedition.lcdui.CommandListener;
26  import javax.microedition.lcdui.Display;
27  import javax.microedition.lcdui.Displayable;
28  import javax.microedition.lcdui.List;
29  import javax.microedition.midlet.MIDlet;
30  
31  import org.microemu.MIDletEntry;
32  import org.microemu.app.CommonInterface;
33  
34  public class Launcher extends MIDlet implements CommandListener {
35  
36  	private static final Command CMD_LAUNCH = new Command("Start", Command.ITEM, 0);;
37  
38  	private static final String NOMIDLETS = "[no midlets]";
39  
40  	private CommonInterface common;
41  
42  	private List menuList;
43  
44  	private static String midletSuiteName = null;
45  
46  	private static Vector midletEntries = new Vector();
47  
48  	private MIDlet currentMIDlet = null;
49  
50  	public Launcher(CommonInterface common) {
51  		this.common = common;
52  	}
53  
54  	public String getSuiteName() {
55  		return midletSuiteName;
56  	}
57  
58  	public static void setSuiteName(String midletSuiteName) {
59  		Launcher.midletSuiteName = midletSuiteName;
60  	}
61  
62  	public static void addMIDletEntry(MIDletEntry entry) {
63  		midletEntries.addElement(entry);
64  	}
65  
66  	public static void removeMIDletEntries() {
67  		midletEntries.removeAllElements();
68  	}
69  
70  	public MIDletEntry getSelectedMidletEntry() {
71  		if (menuList != null) {
72  			int idx = menuList.getSelectedIndex();
73  			if (!menuList.getString(idx).equals(NOMIDLETS)) {
74  				return (MIDletEntry) midletEntries.elementAt(idx);
75  			}
76  		}
77  
78  		return null;
79  	}
80  
81  	public MIDlet getCurrentMIDlet() {
82  		return currentMIDlet;
83  	}
84  
85  	public void setCurrentMIDlet(MIDlet midlet) {
86  		currentMIDlet = midlet;
87  	}
88  
89  	public void destroyApp(boolean unconditional) {
90  	}
91  
92  	public void pauseApp() {
93  	}
94  
95  	public void startApp() {
96  		menuList = new List("Launcher", List.IMPLICIT);
97  		menuList.addCommand(CMD_LAUNCH);
98  		menuList.setCommandListener(this);
99  
100 		if (midletEntries.size() == 0) {
101 			menuList.append(NOMIDLETS, null);
102 		} else {
103 			for (int i = 0; i < midletEntries.size(); i++) {
104 				menuList.append(((MIDletEntry) midletEntries.elementAt(i)).getName(), null);
105 			}
106 		}
107 
108 		Display.getDisplay(this).setCurrent(menuList);
109 	}
110 
111 	public void commandAction(Command c, Displayable d) {
112 		if (d == menuList) {
113 			if (c == List.SELECT_COMMAND || c == CMD_LAUNCH) {
114 				MIDletEntry entry = getSelectedMidletEntry();
115 				if (entry != null) {
116 					common.initMIDlet(true);
117 				}
118 			}
119 		}
120 	}
121 
122 }