MidpStub.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.midlet.MIDlet;
  2. import javax.microedition.lcdui.*;
  3. public class MidpStub extends MIDlet implements CommandListener
  4. {
  5.     private ExampleCanvas iCanvas;
  6.     private final static Command iOkCommand = new Command("Ok", Command.OK, 1);
  7.     private final static Command iExitCommand = new Command ("Exit", Command.EXIT, 1);
  8.     private List        iSelList;
  9.     private Display     iDisplay;
  10.     private Alert       iError;
  11.     private boolean firstTime;
  12.     
  13.     public MidpStub()
  14.     {    
  15.         String[] EXAMPLE_STRINGS = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
  16.         iSelList = new List("Select Example", ChoiceGroup.IMPLICIT, EXAMPLE_STRINGS, (Image[])null);
  17.         iSelList.addCommand(iOkCommand);
  18.         iSelList.addCommand(iExitCommand);
  19.         
  20.         iDisplay = Display.getDisplay(this);
  21.         iError = new Alert("M3G Examples", 
  22.                            "",
  23.                            null,
  24.                            AlertType.ERROR);
  25.         iError.setTimeout(Alert.FOREVER);
  26.         firstTime = true;
  27.     }
  28.     protected void startApp()
  29.     {
  30.         if (firstTime)
  31.         {
  32.             
  33.             iSelList.setCommandListener(this);
  34.             iDisplay.setCurrent(iSelList);
  35.             firstTime = false;
  36.         }
  37.     }
  38.     public void commandAction(Command aCommand, Displayable aDisp)
  39.     {
  40.         
  41.         if (aCommand == iOkCommand && aDisp == iSelList)
  42.         {
  43.             int selIndex = iSelList.getSelectedIndex();
  44.             
  45.             try 
  46.             {
  47.                 Class exampleClass = Class.forName("Example" + iSelList.getString(selIndex));
  48.                 ExampleBase example = (ExampleBase)exampleClass.newInstance();
  49.                 example.setPlatformServices(new MidpPlatformServices());
  50.                 if (iCanvas != null) iCanvas.stopAndWait();
  51.                 iCanvas = new ExampleCanvas(example);               
  52.                 iCanvas.setCommandListener(this);
  53.                 iCanvas.addCommand(iExitCommand);
  54.                 iDisplay.setCurrent(iCanvas);
  55.             }
  56.             catch (ClassNotFoundException notFound)
  57.             {
  58.                 Displayable temp = iDisplay.getCurrent();
  59.                 iError.setString("Example" + (selIndex+1) + " not found");
  60.                 if (iCanvas != null) iCanvas.stopAndWait();
  61.                 iDisplay.setCurrent(iError, temp);
  62.             }
  63.             catch (Exception e)
  64.             {
  65.                 Displayable temp = iDisplay.getCurrent();
  66.                 iError.setString("Unknown error (" + e.getMessage() +")");
  67.                 if (iCanvas != null) iCanvas.stopAndWait();
  68.                 iDisplay.setCurrent(iError, temp);              
  69.             }
  70.         }
  71.         else if (aCommand == iExitCommand && aDisp == iCanvas)
  72.         {   
  73.             iDisplay.setCurrent(iSelList);
  74.         }
  75.         else if (aCommand == iExitCommand && aDisp == iSelList)
  76.         {
  77.             if (iCanvas != null) iCanvas.stopAndWait();
  78.             notifyDestroyed();
  79.         }   
  80.     }
  81.     protected void pauseApp()
  82.     {
  83.         // Here we should stop the constant redrawing, but why bother =)
  84.     }
  85.     protected void destroyApp(boolean aUnconditional)
  86.     {
  87.         if (iCanvas != null) iCanvas.stopAndWait();
  88.     }
  89.     public static class MidpPlatformServices implements PlatformServices {
  90.         public Object loadImage(String name) {
  91.             try
  92.             {
  93.                 return Image.createImage(getResourceDir() + name);
  94.             }
  95.             catch (Exception e)
  96.             {
  97.                 e.printStackTrace();
  98.                 return null;
  99.             }
  100.         }
  101.         public String getResourceDir() {
  102.             return "/";
  103.         }
  104.     }
  105.     
  106. }