?? demomenu.java
字號:
/*==============================================================================
FILE: DemoMenu.java
GENERAL DESCRIPTION:
MIDlet to display menu of demonstration MIDlets.
Copyright (c) 2002 Qualcomm Inc. All rights reserved.
==============================================================================*/
package com.qualcomm.demo.menu;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import java.lang.*;
import com.sun.midp.midlet.Scheduler;
import com.qualcomm.midp.Main;
import javax.microedition.io.Connector;
import com.sun.midp.io.j2me.storage.RandomAccessStream;
import com.qualcomm.qjaeservices.file.FileService;
import java.util.*;
/**
* DemoMenu MIDlet.
*/
public class DemoMenu extends MIDlet implements CommandListener
{
// Copyright text to display when "About" softkey pressed
private final String aboutText =
"Copyright 2002 Qualcomm Inc.\n" +
"All Rights Reserved.\n\n" +
"Memory Size: " + Runtime.getRuntime().totalMemory();
// MIDlet to instantiate when Intro is chosen
private final String INTRO_MIDLET = "com.qualcomm.demo.menu.DemoIntro";
// MIDlet to instantiate when TCKMode is chosen
private final String TCK_MODE_MIDLET = "com.qualcomm.demo.misc.TCKMode";
// MIDlet to instantiate when External Midlets is chosen
private final String MIDLET_BROWSER = "com.qualcomm.demo.menu.MidletBrowser";
// Display for this MIDlet
private Display display;
// Fixed softkeys for "main menu" screen
private final Command externalCommand =
new Command("External MIDlets", Command.SCREEN, 101);
private final Command managerCommand =
new Command("AMS Manager", Command.SCREEN, 102);
private final Command introCommand =
new Command("Java Introduction", Command.SCREEN, 103);
private final Command tckModeCommand =
new Command("TCK Mode", Command.SCREEN, 104);
private final Command debugCommand =
new Command("Debug", Command.SCREEN, 105);
private final Command aboutCommand =
new Command("About", Command.HELP, 106);
private final Command exitCommand =
new Command("Exit", Command.EXIT, 107);
// debug screen
private final Command backCommand =
new Command("Back", Command.BACK, 1);
private final Command startGCCommand =
new Command("Start Garbage Collection", Command.SCREEN, 101);
private final Command stopGCCommand =
new Command("Stop Garbage Collection", Command.SCREEN, 102);
// Softkeys to select a menu (assigned in startApp)
private Command[] menuCmds;
// The current menu
private MenuData menu;
// the array to hold the midlet in the toplevel folder
private MidletInfo topLevelMidlets[];
// "Main menu" screen
private List menuScreen;
private List debugScreen ;
// "About" screen
private final Alert aboutScreen =
new Alert("About DemoMenu", aboutText, null, null);
private String TOP_LEVEL_DIR_NAME = "/toplevel";
// the following function reads the midlet information from the topLevel
// folder and displays the menu.
private void loadMidletsInTopLevel()
{
String[] jadFiles =
FileService.getFilesWithExtension(TOP_LEVEL_DIR_NAME, "jad");
String[] jadData = new String[jadFiles.length];
int[] jadMidletCount = new int[jadFiles.length];
String absDirPath;
int i, count = 0;
absDirPath = FileService.getRoot() + TOP_LEVEL_DIR_NAME;
// Loop through all JAD files
for (i = 0; i < jadFiles.length; i++)
{
RandomAccessStream stream = new RandomAccessStream();
byte[] data = null;
// Load JAD file contents
try
{
stream.connect(absDirPath + "/" + jadFiles[i],
Connector.READ);
data = new byte[stream.getSizeOf()];
stream.readBytes(data, 0, data.length);
stream.disconnect();
}
catch (java.io.IOException e)
{
}
if (data != null)
{
// Convert any Windows-style "\r\n" to plain "\n"
// (This just improves the appearance when the
// data is displayed.)
int len = data.length;
int n;
for (n = 0; n < (len - 1); n++)
{
if ((data[n] == '\r') && (data[n+1] == '\n'))
{
len--;
System.arraycopy(data, n + 1, data, n, len - n);
}
}
// Store JAD file contents as String
jadData[i] = new String(data, 0, len);
// Count MIDlets in this JAD file
int m;
for (m = 1; jadData[i].indexOf("MIDlet-" + m + ":") >= 0; m++);
jadMidletCount[i] = (m - 1);
// Update total MIDlet count
count += jadMidletCount[i];
}
}
// Create new midlets array
topLevelMidlets = new MidletInfo[count];
// Fill midlets array
int item = 0;
for (i = 0; i < jadFiles.length; i++)
{
int midlet;
for (midlet = 1; midlet <= jadMidletCount[i]; midlet++)
{
topLevelMidlets[item++] =
new MidletInfo(absDirPath, jadFiles[i],
jadData[i], midlet);
}
}
}
// Sets up and displays the given menu
private void setupMenu(int menuIndex)
{
int i;
// Get menu data associated with menuIndex
menu = MenuData.getMenu(menuIndex);
// Create new List screen to display menu on
menuScreen = new List(menu.getName(), List.IMPLICIT);
// Configure softkeys
for (i = 0; i < menuCmds.length; i++)
{
menuScreen.addCommand(menuCmds[i]);
}
menuScreen.addCommand(externalCommand);
menuScreen.addCommand(managerCommand);
menuScreen.addCommand(introCommand);
menuScreen.addCommand(aboutCommand);
menuScreen.addCommand(tckModeCommand);
menuScreen.addCommand(debugCommand);
menuScreen.addCommand(exitCommand);
menuScreen.setCommandListener(this);
loadMidletsInTopLevel();
for (i = 0; i < topLevelMidlets.length; i++)
{
menuScreen.append(topLevelMidlets[i].name,
topLevelMidlets[i].icon);
}
// Append menu items
for (i = 0; i < menu.getNumItems(); i++)
{
menuScreen.append(menu.getItemName(i), menu.getItemIcon(i));
}
// Display menu
display.setCurrent(menuScreen);
}
// Schedules a midlet
private void scheduleMidlet(String name, String classname,
boolean keepInBackground)
{
// Try to instantiate new MIDlet
try
{
Scheduler.getScheduler().scheduleMIDlet(
(MIDlet)(Class.forName(classname).newInstance()));
}
catch (Exception e)
{
// Create an error string
String errorString =
"Could not start " + name + ".\n\n" +
"Error instantiating " + classname + "\n" +
e.toString();
// Create an "error" screen containing error message
Alert errorScreen = new Alert("Error", errorString, null, null);
// Go to "error" screen
display.setCurrent(errorScreen, menuScreen);
return;
}
if (keepInBackground)
{
// Put current MIDlet in background, allowing new one to run.
display.setCurrent(null);
}
else
{
// Destroy this MIDlet, allowing new midlet to be scheduled instead
destroyApp(true);
notifyDestroyed();
}
}
/**
* Handles commands.
*/
public void commandAction(Command c, Displayable s)
{
if (s == menuScreen)
{
if (c == externalCommand)
{
// Register this MIDlet as starting app to go back to when external
// midlet browser exits, and external midlet browser as starting app
// to go to when we exit right now.
// Note: The external midlet browser registers new apps and exits to
// start them, so we cannot simply schedule the external midlet browser
// from this midlet, but must register it as the starting app.
Main.registerStartApp(getClass().getName(), null);
Main.registerStartApp(MIDLET_BROWSER, null);
// Exit this app to go to Manager
destroyApp(true);
notifyDestroyed();
}
else if (c == managerCommand)
{
// Register this MIDlet as starting app to go back to when Manager
// exits, and Manager as starting app to go to when we exit right now.
Main.registerStartApp(getClass().getName(), null);
Main.registerStartApp(null, null);
// Exit this app to go to Manager
destroyApp(true);
notifyDestroyed();
}
else if (c == introCommand)
{
// Instantiate Intro MIDlet in place of Menu MIDlet
scheduleMidlet("Introduction", INTRO_MIDLET, false);
}
else if (c == aboutCommand)
{
// Go to "about" screen
display.setCurrent(aboutScreen, menuScreen);
}
else if (c == exitCommand)
{
// Exit this app
destroyApp(true);
notifyDestroyed();
}
else if (c == tckModeCommand)
{
// Register this MIDlet as starting app to
Main.registerStartApp(getClass().getName(), null);
// Instantiate TCKMode Midlet in place of Menu Midlet
scheduleMidlet("TCKMode", TCK_MODE_MIDLET, false);
}
else if (c == debugCommand)
{
display.setCurrent(debugScreen);
}
else if (c == List.SELECT_COMMAND)
{
// Get selected menu item
int i = menuScreen.getSelectedIndex();
// TBD - If we need to free up more memory for midlets, set the
// keepInBackground parameter to false instead of true.
// Register this MIDlet as starting app to
Main.registerStartApp(getClass().getName(), null);
// if (i < menu.getNumItems())
if (i < topLevelMidlets.length)
{
MidletInfo selectedMidlet;
// selectedMidlet = topLevelMidlets[i - menu.getNumItems()];
selectedMidlet = topLevelMidlets[i];
Main.registerStartApp(selectedMidlet.classname,
selectedMidlet.dir + "/" +
selectedMidlet.jadFile);
// Exit this app
destroyApp(true);
notifyDestroyed();
}
else
{
// Set properties for selected menu item
int n;
for (n = 0;
n < menu.getItemNumProperties(i - topLevelMidlets.length);
n++)
{
// TBD - This works for now, but when we add the MIDP
// security features we'll need to figure out how to
// get this MIDlet
// properly authorized to do this.
Scheduler.getScheduler().getMIDletSuite().addProperty
(
menu.getItemPropertyKey(i - topLevelMidlets.length, n),
menu.getItemPropertyValue(i - topLevelMidlets.length , n)
);
}
// Schedule selected MIDlet
scheduleMidlet(menu.getItemName(i - topLevelMidlets.length),
menu.getItemClassname(i - topLevelMidlets.length),
false);
}
}
else
{
// Find which menu was selected
int i;
for (i = 0; i < menuCmds.length; i++)
{
if (c == menuCmds[i])
{
// Go to selected menu
setupMenu(i);
break;
}
}
}
}
else if (s == debugScreen)
{
if (c == startGCCommand)
{
startGarbageCollection();
}
else if (c == stopGCCommand)
{
stopGarbageCollection();
}
else if (c == backCommand)
{
display.setCurrent(menuScreen);
}
}
}
/**
* Starts the MIDlet.
*/
public void startApp()
{
// Save display
display = Display.getDisplay(this);
// Set up menu softkeys
menuCmds = new Command[MenuData.getNumMenus()];
int i;
for (i = 0; i < menuCmds.length; i++)
{
menuCmds[i] = new Command(MenuData.getMenu(i).getName(),
Command.SCREEN, i);
}
// Go to main menu screen with first menu
setupMenu(0);
// set up debug menu
debugScreen = new List("Debug", List.IMPLICIT);
debugScreen.addCommand(backCommand);
debugScreen.addCommand(startGCCommand);
debugScreen.addCommand(stopGCCommand);
debugScreen.setCommandListener(this);
}
/**
* Prepares the MIDlet to be paused (and possibly later started again).
*/
public void pauseApp()
{
// Nothing needs to be cleaned up for this MIDlet
}
/**
* Prepares the MIDlet to be destroyed.
*/
public void destroyApp(boolean unconditional)
{
// Nothing needs to be cleaned up for this MIDlet
}
private native int startGarbageCollection();
private native int stopGarbageCollection();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -