 * MobileFileBrowser.java
 * MobileFileBrowser.java *
 * * Created on 2006年10月18日, 下午3:34
 * Created on 2006年10月18日, 下午3:34 */
 */
 package hello;
package hello;
 import java.io.IOException;
import java.io.IOException; import java.util.Enumeration;
import java.util.Enumeration; import javax.microedition.io.*;
import javax.microedition.io.*; import javax.microedition.io.file.*;
import javax.microedition.io.file.*; import javax.microedition.midlet.*;
import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
import javax.microedition.lcdui.*;

 /** *//**
/** *//** *
 * * @author  冯东
 * @author  冯东 * @version
 * @version */
 */ public class MobileFileBrowser extends MIDlet implements CommandListener
public class MobileFileBrowser extends MIDlet implements CommandListener

 {
{ public String currentDir = "";//当前路径
    public String currentDir = "";//当前路径
 private Display dspFileBrowse;//显示管理对象
    private Display dspFileBrowse;//显示管理对象 
     private Command exitCommand;//退出按钮
    private Command exitCommand;//退出按钮 private Command openCommand;//打开文件或文件夹按钮
    private Command openCommand;//打开文件或文件夹按钮 
     private List fileList;//显示文件夹内容的列表框
    private List fileList;//显示文件夹内容的列表框 
     private String fathorFolder = "..";
    private String fathorFolder = ".."; 
     //构造函数
    //构造函数 public MobileFileBrowser()
    public MobileFileBrowser()
 
     {
{ this.dspFileBrowse = Display.getDisplay(this);
        this.dspFileBrowse = Display.getDisplay(this); 
         this.fileList = new List("手机手机文件浏览器", List.IMPLICIT);
        this.fileList = new List("手机手机文件浏览器", List.IMPLICIT); 
         this.exitCommand = new Command("退出", Command.EXIT, 1);
        this.exitCommand = new Command("退出", Command.EXIT, 1); this.openCommand = new Command("打开", Command.OK, 1);
        this.openCommand = new Command("打开", Command.OK, 1); 
         //将按钮添加到窗体中
        //将按钮添加到窗体中 this.fileList.addCommand(this.openCommand);
        this.fileList.addCommand(this.openCommand); this.fileList.addCommand(this.exitCommand);
        this.fileList.addCommand(this.exitCommand); this.fileList.setCommandListener(this);
        this.fileList.setCommandListener(this);
 this.dspFileBrowse.setCurrent(this.fileList);
        this.dspFileBrowse.setCurrent(this.fileList);  }
    } 
     public void startApp()
    public void startApp()
 
     {
{ //获取系统的根目录
        //获取系统的根目录 this.getRoots();
        this.getRoots(); }
    } 
     public void pauseApp()
    public void pauseApp()
 
     {
{ }
    } 
     public void destroyApp(boolean unconditional)
    public void destroyApp(boolean unconditional)
 
     {
{ this.notifyDestroyed();
        this.notifyDestroyed(); }
    }
 public void commandAction(Command command, Displayable displayable)
    public void commandAction(Command command, Displayable displayable)
 
     {
{ if(command == this.openCommand)//判断是否是打开命令按钮
        if(command == this.openCommand)//判断是否是打开命令按钮
 
         {
{ //判断选择的是子目录/父目录还是文件并返回对应的路径字符串
            //判断选择的是子目录/父目录还是文件并返回对应的路径字符串 this.judgeDir();
            this.judgeDir(); }
        } if(command == this.exitCommand)
        if(command == this.exitCommand)
 
         {
{ this.destroyApp(true);
            this.destroyApp(true); }
        } }
    } 
     //判断选择的目录
    //判断选择的目录 private void judgeDir()
    private void judgeDir()
 
     {
{ String currSelect = getCurrentSelect();//获取当前选择的项
        String currSelect = getCurrentSelect();//获取当前选择的项 //获取父目录路径
        //获取父目录路径 if(currSelect == "..")//返回上一级目录
        if(currSelect == "..")//返回上一级目录
 
         {
{ int loopTime = this.currentDir.length() - 2;
            int loopTime = this.currentDir.length() - 2; char[] charTemp = this.currentDir.toCharArray();
            char[] charTemp = this.currentDir.toCharArray(); char folder ='/';
            char folder ='/'; while(loopTime > 0)
            while(loopTime > 0)
 
             {
{ if(charTemp[loopTime] == folder)
                if(charTemp[loopTime] == folder)
 
                 {
{ break;
                    break; }
                } loopTime --;
                loopTime --; }
            } if(loopTime == 0)
            if(loopTime == 0)
 
             {
{ this.getRoots();
                this.getRoots(); }
            } else
            else
 
             {
{ this.currentDir = this.currentDir.substring(0, loopTime+1);
                this.currentDir = this.currentDir.substring(0, loopTime+1); this.getFolders(this.currentDir);
                this.getFolders(this.currentDir); }
            } return;
            return; }
        } //判断是否是目录
        //判断是否是目录 int strLen = currSelect.length();//获取字符串长度
        int strLen = currSelect.length();//获取字符串长度 
         if(lastStr.equals("/"))//相同则表明选中的是路径
        if(lastStr.equals("/"))//相同则表明选中的是路径
 
         {
{ this.currentDir += currSelect;
            this.currentDir += currSelect; this.getFolders(this.currentDir);
            this.getFolders(this.currentDir); return;
            return; }
        } //选中的是文件不做任何操作
        //选中的是文件不做任何操作 return;
        return; }
    } 
     //获取根目录
    //获取根目录 private void getRoots()
    private void getRoots()
 
     {
{ this.currentDir = "";//设置当前文件夹
        this.currentDir = "";//设置当前文件夹 Enumeration enumRoot = FileSystemRegistry.listRoots();//返回根目录组成的序列
        Enumeration enumRoot = FileSystemRegistry.listRoots();//返回根目录组成的序列 this.fileList.deleteAll();//清空以前的
        this.fileList.deleteAll();//清空以前的 while(enumRoot.hasMoreElements())
        while(enumRoot.hasMoreElements())
 
         {
{ this.fileList.append((String)enumRoot.nextElement(), null);
            this.fileList.append((String)enumRoot.nextElement(), null); }
        } }
    } 
     //获取当前选择项
    //获取当前选择项 private String getCurrentSelect()
    private String getCurrentSelect()
 
     {
{ int a = this.fileList.getSelectedIndex();//获取当前索引数值
        int a = this.fileList.getSelectedIndex();//获取当前索引数值 String result = this.fileList.getString(a);//获取当前选项的字符串
        String result = this.fileList.getString(a);//获取当前选项的字符串 return result;
        return result; }
    } 
     //由传递的路径字符串获取文件夹的内容
    //由传递的路径字符串获取文件夹的内容 private void getFolders(String dir)
    private void getFolders(String dir)
 
     {
{ //        System.out.println(dir);
//        System.out.println(dir); try
        try
 
         {
{ FileConnection fc = (FileConnection) Connector.open("file://localhost/" + dir);//创建对应的连接对象
            FileConnection fc = (FileConnection) Connector.open("file://localhost/" + dir);//创建对应的连接对象 Enumeration enumFolders = fc.list();//调用list函数获取文件夹内容序列
            Enumeration enumFolders = fc.list();//调用list函数获取文件夹内容序列 this.fileList.deleteAll();//清空以存在的项
            this.fileList.deleteAll();//清空以存在的项 this.fileList.append(this.fathorFolder, null);//添家父目录..
            this.fileList.append(this.fathorFolder, null);//添家父目录.. while(enumFolders.hasMoreElements())//遍历序列并将内容添加到列表中
            while(enumFolders.hasMoreElements())//遍历序列并将内容添加到列表中
 
             {
{ String listinfo = (String)enumFolders.nextElement();
                String listinfo = (String)enumFolders.nextElement(); this.fileList.append(listinfo, null);
                this.fileList.append(listinfo, null); }
            } } catch (IOException ex)
        } catch (IOException ex)
 
         {
{ ex.printStackTrace();
            ex.printStackTrace(); }
        } }
    } }
}