
        
import java.io.IOException;
import java.util.TimerTask;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Form;
import javax.microedition.rms.RecordStore;
import javax.microedition.lcdui.ItemStateListener;


public class BTL1 extends MIDlet implements ItemStateListener,CommandListener
{

    Display display;
    TextField tf;
    Ticker ticker;
    Form frmMain;
    ChoiceGroup choice1;
    List mlist;
    TextBox mtextbox;
    int i;
    String Str;
    Command cmdExit, cmdCancel, cmdOpen,cmdSave,cmdClear, cmdInformation, cmdRename, cmdBack, cmdSlideshow;
  //  String subjects[] = {"Firefox.png", "yahoo.png", "Recycle.png", "Shodon01.jpg", "images.bmp", "anhnhe.gif"};
    Image img[];
    RecordStore rs = null;
    static final String REC_STORE = "db_nhieuanh";
    public BTL1(){
       String [] stringElements = {"Firefox.png", "yahoo.png", "Recycle.png", "Shodon01.jpg", "anhnhe.gif"};
Image [] imageElements = {loadImage("/Firefox.png"),loadImage("/yahoo.png"),loadImage("/Recycle.png"),loadImage("/Shodon01.jpg"),loadImage("/anhnhe.gif")};
   mlist = new List("View Image", List.IMPLICIT,stringElements,imageElements);
  
 
        cmdExit = new Command("Exit", Command.EXIT, 6);
        cmdOpen = new Command("Open", Command.SCREEN, 1);
        cmdCancel = new Command("Cancel", Command.CANCEL, 5);
        cmdInformation = new Command("Information", Command.SCREEN, 3);
        cmdRename = new Command("Rename", Command.SCREEN, 2);
        cmdSlideshow = new Command("Slideshow", Command.SCREEN, 4);
        mlist.addCommand(cmdExit);
        mlist.addCommand(cmdOpen);
        mlist.addCommand(cmdCancel);
        mlist.addCommand(cmdInformation);
        mlist.addCommand(cmdSlideshow);
        mlist.addCommand(cmdRename);
        mlist.setCommandListener(this);

    }
    public void startApp() {
        Display.getDisplay(this).setCurrent(mlist);
          
    }
    Image loadImage(String name){
     Image image = null;
     try{
         image = Image.createImage(name);
     }
     catch(IOException ex){
         System.out.println(ex);
     
     }
    return image;
    }
    
public void itemStateChanged(Item item)
{
	
}
    public void information() {
 int index = mlist.getSelectedIndex();
System.out.println("Gia tri cua index = " + index);
String currentValue = mlist.getString(index);
Alert alert = new Alert("Image Select :", currentValue, null,null);
cmdBack = new Command("Back", Command.SCREEN, 1);
alert.addCommand(cmdBack);
 display = Display.getDisplay(this);
 display.setCurrent(alert);
alert.setTimeout(Alert.FOREVER);
 alert.setCommandListener(this);
    }

    
    public void Slideshow() {
        display = Display.getDisplay(this);
         frmMain = new Form("");
        int t;
      
        int index = mlist.getSelectedIndex();
      //  while(index<6){
        for(t=0;t<30;t+=5)
        {
            for(index=0;index<6;index++){
                
                
                
                
                if(index == 0)
                {
            try
    {
      // Read the appropriate image based on color support
                
      Image  im = Image.createImage((display.isColor()) ?
                                   "/Firefox.png":"/Firefox.png");

      frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));
      
      display.setCurrent(frmMain);
            }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load  .png file");
    }
            
        }
            else if (index ==1)
        {
                 
            try
    {
      // Read the appropriate image based on color support
                
      Image im = Image.createImage((display.isColor()) ?
                                   "/yahoo.png":"/yahoo.png");
       frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));

       display.setCurrent(frmMain);
        
    }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load yahoo.png file");
    } 
        }
                
                 else if (index ==2)
        {
                 
            try
    {
                
      Image im = Image.createImage((display.isColor()) ?
                                   "/Recycle.png":"/Recycle.png");
       frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));
 
       display.setCurrent(frmMain);
    }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load  .png file");
    }
            
        }
            
            else if (index ==3)
        {
                 
            try
    {
                
      Image im = Image.createImage((display.isColor()) ?
                                   "/Shodon01.jpg":"/Shodon01.jpg");
       frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));

       display.setCurrent(frmMain);
     
    }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load  Shodon01.jpg file");
    }
            
        
        }
          else if (index ==4)
        {
                 
            try
    {
                
      Image im = Image.createImage((display.isColor()) ?
                                   "/anhnhe.gif":"/anhnhe.gif");
       frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));
     
       display.setCurrent(frmMain);
            }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load  anhnhe.gif file");
    }
            
        
        }
           
            }
        }
        
    }
        
      
 //   }
    public void Open(){
        display = Display.getDisplay(this);
         frmMain = new Form("");
       //  ticker = new Ticker("xem anh");
         cmdBack = new Command("Back", Command.SCREEN,1);
         int index = mlist.getSelectedIndex();
        if(index == 0){
            try
    {
      // Read the appropriate image based on color support
                
      Image im = Image.createImage((display.isColor()) ?
                                   "/Firefox.png":"/Firefox.png");

      frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));

      display.setCurrent(frmMain);
    }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load  Firefox.png file");
    }
            
        }
        else if (index ==1)
        {
                 
            try
    {
      // Read the appropriate image based on color support
                
      Image im = Image.createImage((display.isColor()) ?
                                   "/yahoo.png":"/yahoo.png");

      frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));

      display.setCurrent(frmMain);
    }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load yahoo.png file");
    }
            
        
        }
            
          else if (index ==2)
        {
                 
            try
    {
      // Read the appropriate image based on color support
                
      Image im = Image.createImage((display.isColor()) ?
                                   "/Recycle.png":"/Recycle.png");

      frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));

      display.setCurrent(frmMain);
    }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load  Recycle.png file");
    }
            
        
        }
            
            else if (index ==3)
        {
                 
            try
    {
      // Read the appropriate image based on color support
                
      Image im = Image.createImage((display.isColor()) ?
                                   "/Shodon01.jpg":"/Shodon01.jpg");

      frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));

      display.setCurrent(frmMain);
    }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load  Shodon01.jpg file");
    }
            
        
        }
          else if (index ==4)
        {
                 
            try
    {
      // Read the appropriate image based on color support
                
      Image im = Image.createImage((display.isColor()) ?
                                   "/anhnhe.gif":"/anhnhe.gif");

      frmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER, null));

      display.setCurrent(frmMain);
    }
    catch (java.io.IOException e)
    {
      System.err.println("Can't load  anhnhe.gif file");
    }
            
        
        }
            
           
            frmMain.addCommand(cmdBack);
            frmMain.setTicker(ticker);
            frmMain.setItemStateListener(this);
            frmMain.setCommandListener(this);
           

    }
    public void Save(){
    //    mlist.textfield.setText(textfield.getText());
         display = Display.getDisplay(this);
   int index = mlist.getSelectedIndex();
   Image I = mlist.getImage(index);
  // String currentValue = mlist.getString(index);
   String St = mtextbox.getString();
   mlist.set(index, St,I);
   
    display.setCurrent(mlist);
    }
    public void Cancel() {
    }
    public void Clear()
            
    {   
         display = Display.getDisplay(this);
         //int index = mlist.getSelectedIndex();
        //String currentValue = mlist.getString(index);
       // mtextbox.getCaretPosition();
        int abc = mtextbox.getCaretPosition();
        
                mtextbox.delete(abc,1);
        
    }
      
    public void Rename()
 {      display = Display.getDisplay(this);
       int index = mlist.getSelectedIndex();
       String currentValue = mlist.getString(index);
        
       mtextbox = new TextBox("New name ",currentValue ,20,TextField.ANY);
       mtextbox.setString(currentValue);
       
       cmdSave = new Command("Save",Command.SCREEN,1);
      cmdClear = new Command("Clear",Command.BACK,1);
       
      mtextbox.addCommand(cmdSave);
      mtextbox.addCommand(cmdClear);
      mtextbox.setCommandListener(this);
     display.setCurrent(mtextbox);    
   

  

      

    }

   
    
 /*   public void openRecStore()
{
  try
  {
    
   rs = RecordStore.openRecordStore(REC_STORE, true );
  }
  catch (Exception e)
  {
    db(e.toString());
 }
}
   private void db(String str)
{
  System.err.println("Msg: " + str);
}
public void writeRecord(String str)
{
  byte[] rec = str.getBytes();

  try
  {
    rs.addRecord(rec, 0, rec.length);
  }
  catch (Exception e)
  {
    db(e.toString());
  }
}
*/
    public void pauseApp() {
    }

    public void destroyApp(boolean dk) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == cmdExit) {
            
            destroyApp(false);
            notifyDestroyed();
            
        }
        else if(c==cmdClear){
           Clear(); 
        } 
        else if (c == cmdInformation) {
             
            information();
           
           
           
            
        } 
        else if (c == cmdRename) {
            
            Rename();
            
        }
        else if (c== cmdBack)
        {
           display.setCurrent(mlist);
        }
        
        else if(c== cmdSave)
        {
          Save();  
        }
        
        else if (c == cmdOpen) {
            
                Open();
                
        }
        
        else if (c == cmdCancel) {

        }
        
        else if (c == cmdSlideshow) {
            Slideshow();
            
        }
    }
}   

