FileDocCategorySizeDatePackage
MIDletSelector.javaAPI DocphoneME MR2 API (J2ME)7230Wed May 02 18:00:06 BST 2007com.sun.midp.appmanager

MIDletSelector

public final class MIDletSelector extends Object implements CommandListener
Selector provides a simple user interface to select MIDlets to run. It extracts the list of MIDlets from the attributes in the descriptor file and presents them to the user using the MIDlet-<n> name and icon if any. When the user selects a MIDlet an instance of the class indicated by MIDlet-<n> classname is created.

Fields Summary
private List
mlist
The List of all the MIDlets.
private RunningMIDletSuiteInfo
suiteInfo
Information needed to display a list of MIDlets.
private Display
display
The Display.
private Displayable
parentDisplayable
The parent's display able.
ApplicationManager
manager
Parent app manager.
private int
mcount
Number of midlets in minfo.
private MIDletInfo[]
minfo
MIDlet information, class, name, icon; one per MIDlet.
private Command
backCmd
the Command object to exit back to the MIDlet Suite Manager
private Command
launchCmd
the Command object for "Launch".
private int
selectedMidlet
Index of the selected MIDlet, starts at -1 for non-selected.
Constructors Summary
MIDletSelector(RunningMIDletSuiteInfo theSuiteInfo, Display theDisplay, Displayable theParentDisplayable, ApplicationManager theManager)
Create and initialize a new Selector MIDlet. The Display is retrieved and the list of MIDlets read from the descriptor file.

param
theSuiteInfo information needed to display a list of MIDlets
param
theDisplay the Display
param
theParentDisplayable the parent's displayable
param
theManager the parent application manager


                                                       
       
                    
                       

        MIDletSuiteStorage mss;

        suiteInfo = theSuiteInfo;
        display = theDisplay;
        parentDisplayable = theParentDisplayable;
        manager = theManager;
        mcount = 0;
        minfo = new MIDletInfo[20];

        mss = MIDletSuiteStorage.getMIDletSuiteStorage();

        readMIDletInfo(mss);
        setupList(mss);

        mlist.addCommand(launchCmd);
        mlist.addCommand(backCmd);

        mlist.setCommandListener(this); // Listen for the selection

        display.setCurrent(mlist);
    
Methods Summary
private voidaddMIDlet(MIDletInfo info)
Add a MIDlet to the list.

param
info MIDlet information to add to MIDlet

        if (mcount >= minfo.length) {
            MIDletInfo[] n = new MIDletInfo[mcount+4];
            System.arraycopy(minfo, 0, n, 0, mcount);
            minfo = n;
        }

        minfo[mcount++] = info;
    
public voidcommandAction(Command c, Displayable s)
Respond to a command issued on any Screen. The commands on list is Select and About. Select triggers the creation of the MIDlet of the same name. About puts up the copyright notice.

param
c command activated by the user
param
s the Displayable the command was on.

        if ((s == mlist && c == List.SELECT_COMMAND) || (c == launchCmd)) {
            synchronized (this) {
                if (selectedMidlet != -1) {
                    // the previous selected MIDlet is being launched
                    return;
                }

                selectedMidlet = mlist.getSelectedIndex();
            }

            manager.launchSuite(suiteInfo, minfo[selectedMidlet].classname);
            display.setCurrent(parentDisplayable);
            return;
        }

        if (c == backCmd) {
            display.setCurrent(parentDisplayable);
            return;
        }
    
private voidreadMIDletInfo(MIDletSuiteStorage mss)
Read in and create a MIDletInfo for each MIDlet-<n>

param
mss the midlet suite storage

        try {
            MIDletSuite midletSuite =
                mss.getMIDletSuite(suiteInfo.suiteId, false);

            if (midletSuite == null) {
                return;
            }

            try {
                for (int n = 1; n < 100; n++) {
                    String nth = "MIDlet-"+ n;
                    String attr = midletSuite.getProperty(nth);
                    if (attr == null || attr.length() == 0)
                        break;

                    addMIDlet(new MIDletInfo(attr));
                }
            } finally {
                midletSuite.close();
            }
        } catch (Throwable t) {
            throw t;
        }
    
private voidsetupList(MIDletSuiteStorage mss)
Read the set of MIDlet names, icons and classes Fill in the list.

param
mss the midlet suite storage

        if (mlist == null) {
            mlist = new List(Resource.getString
                             (ResourceConstants.AMS_SELECTOR_SEL_TO_LAUNCH),
                             Choice.IMPLICIT);

            // Add each midlet
            for (int i = 0; i < mcount; i++) {
                Image icon = null;
                if (minfo[i].icon != null) {
                    icon = RunningMIDletSuiteInfo.getIcon(suiteInfo.suiteId,
                        minfo[i].icon, mss);
                }

                mlist.append(minfo[i].name, icon);
            }
        }