001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.appmanager;
027:
028: import javax.microedition.lcdui.*;
029:
030: import com.sun.midp.i18n.Resource;
031: import com.sun.midp.i18n.ResourceConstants;
032:
033: import com.sun.midp.main.MIDletSuiteLoader;
034:
035: import com.sun.midp.midlet.MIDletSuite;
036:
037: import com.sun.midp.midletsuite.*;
038:
039: import com.sun.midp.log.Logging;
040: import com.sun.midp.log.LogChannels;
041:
042: /**
043: * Selector provides a simple user interface to select MIDlets to run.
044: * It extracts the list of MIDlets from the attributes in the
045: * descriptor file and presents them to the user using the MIDlet-<n>
046: * name and icon if any. When the user selects a MIDlet an instance
047: * of the class indicated by MIDlet-<n> classname is created.
048: */
049: final class MIDletSelector implements CommandListener {
050: /**
051: * The List of all the MIDlets.
052: */
053: private List mlist;
054: /**
055: * Information needed to display a list of MIDlets.
056: */
057: private RunningMIDletSuiteInfo suiteInfo;
058: /**
059: * The Display.
060: */
061: private Display display;
062: /**
063: * The parent's display able.
064: */
065: private Displayable parentDisplayable;
066: /**
067: * Parent app manager.
068: */
069: ApplicationManager manager;
070: /**
071: * Number of midlets in minfo.
072: */
073: private int mcount;
074: /**
075: * MIDlet information, class, name, icon; one per MIDlet.
076: */
077: private MIDletInfo[] minfo;
078: /**
079: * the Command object to exit back to the MIDlet Suite Manager
080: */
081: private Command backCmd = new Command(Resource
082: .getString(ResourceConstants.BACK), Command.BACK, 2);
083: /**
084: * the Command object for "Launch".
085: */
086: private Command launchCmd = new Command(Resource
087: .getString(ResourceConstants.LAUNCH), Command.ITEM, 1);
088: /**
089: * Index of the selected MIDlet, starts at -1 for non-selected.
090: */
091: private int selectedMidlet = -1;
092:
093: /**
094: * Create and initialize a new Selector MIDlet.
095: * The Display is retrieved and the list of MIDlets read
096: * from the descriptor file.
097: *
098: * @param theSuiteInfo information needed to display a list of MIDlets
099: * @param theDisplay the Display
100: * @param theParentDisplayable the parent's displayable
101: * @param theManager the parent application manager
102: */
103: MIDletSelector(RunningMIDletSuiteInfo theSuiteInfo,
104: Display theDisplay, Displayable theParentDisplayable,
105: ApplicationManager theManager) throws Throwable {
106:
107: MIDletSuiteStorage mss;
108:
109: suiteInfo = theSuiteInfo;
110: display = theDisplay;
111: parentDisplayable = theParentDisplayable;
112: manager = theManager;
113: mcount = 0;
114: minfo = new MIDletInfo[20];
115:
116: mss = MIDletSuiteStorage.getMIDletSuiteStorage();
117:
118: readMIDletInfo(mss);
119: setupList(mss);
120:
121: mlist.addCommand(launchCmd);
122: mlist.addCommand(backCmd);
123:
124: mlist.setCommandListener(this ); // Listen for the selection
125:
126: display.setCurrent(mlist);
127: }
128:
129: /**
130: * Respond to a command issued on any Screen.
131: * The commands on list is Select and About.
132: * Select triggers the creation of the MIDlet of the same name.
133: * About puts up the copyright notice.
134: *
135: * @param c command activated by the user
136: * @param s the Displayable the command was on.
137: */
138: public void commandAction(Command c, Displayable s) {
139: if ((s == mlist && c == List.SELECT_COMMAND)
140: || (c == launchCmd)) {
141: synchronized (this ) {
142: if (selectedMidlet != -1) {
143: // the previous selected MIDlet is being launched
144: return;
145: }
146:
147: selectedMidlet = mlist.getSelectedIndex();
148: }
149:
150: manager.launchSuite(suiteInfo,
151: minfo[selectedMidlet].classname);
152: display.setCurrent(parentDisplayable);
153: return;
154: }
155:
156: if (c == backCmd) {
157: display.setCurrent(parentDisplayable);
158: return;
159: }
160: }
161:
162: /**
163: * Read the set of MIDlet names, icons and classes
164: * Fill in the list.
165: *
166: * @param mss the midlet suite storage
167: */
168: private void setupList(MIDletSuiteStorage mss) {
169: if (mlist == null) {
170: mlist = new List(
171: Resource
172: .getString(ResourceConstants.AMS_SELECTOR_SEL_TO_LAUNCH),
173: Choice.IMPLICIT);
174:
175: // Add each midlet
176: for (int i = 0; i < mcount; i++) {
177: Image icon = null;
178: if (minfo[i].icon != null) {
179: icon = RunningMIDletSuiteInfo.getIcon(
180: suiteInfo.suiteId, minfo[i].icon, mss);
181: }
182:
183: mlist.append(minfo[i].name, icon);
184: }
185: }
186: }
187:
188: /**
189: * Read in and create a MIDletInfo for each MIDlet-<n>
190: *
191: * @param mss the midlet suite storage
192: */
193: private void readMIDletInfo(MIDletSuiteStorage mss)
194: throws Throwable {
195: try {
196: MIDletSuite midletSuite = mss.getMIDletSuite(
197: suiteInfo.suiteId, false);
198:
199: if (midletSuite == null) {
200: return;
201: }
202:
203: try {
204: for (int n = 1; n < 100; n++) {
205: String nth = "MIDlet-" + n;
206: String attr = midletSuite.getProperty(nth);
207: if (attr == null || attr.length() == 0)
208: break;
209:
210: addMIDlet(new MIDletInfo(attr));
211: }
212: } finally {
213: midletSuite.close();
214: }
215: } catch (Throwable t) {
216: throw t;
217: }
218: }
219:
220: /**
221: * Add a MIDlet to the list.
222: * @param info MIDlet information to add to MIDlet
223: */
224: private void addMIDlet(MIDletInfo info) {
225: if (mcount >= minfo.length) {
226: MIDletInfo[] n = new MIDletInfo[mcount + 4];
227: System.arraycopy(minfo, 0, n, 0, mcount);
228: minfo = n;
229: }
230:
231: minfo[mcount++] = info;
232: }
233: }
|