/** To change this template, choose Tools | Templates
* and open the template in the editor.*/packagecom.hadeslee.test;importjava.io.IOException;importjava.util.Enumeration;importjavax.microedition.io.Connector;importjavax.microedition.io.file.FileConnection;importjavax.microedition.io.file.FileSystemRegistry;importjavax.microedition.lcdui.Alert;importjavax.microedition.lcdui.AlertType;importjavax.microedition.lcdui.Command;importjavax.microedition.lcdui.CommandListener;importjavax.microedition.lcdui.Display;importjavax.microedition.lcdui.Displayable;importjavax.microedition.lcdui.Image;importjavax.microedition.lcdui.List;/***
*@authorhadeslee*/publicclassFileBrowserextendsListimplementsCommandListener {/*** Command fired on file selection.*/publicstaticfinalCommand SELECT_FILE_COMMAND=newCommand("选择", Command.OK,1);privateString currDirName;privateString currFile;privateImage dirIcon;privateImage fileIcon;privateCommandListener commandListener;/*special string denotes upper directory*/privatestaticfinalString UP_DIRECTORY="..";/*special string that denotes upper directory accessible by this browser.
* this virtual directory contains all roots.*/privatestaticfinalString MEGA_ROOT="/";/*separator string as defined by FC specification*/privatestaticfinalString SEP_STR="/";/*separator character as defined by FC specification*/privatestaticfinalcharSEP='/';privateDisplay display;privateString selectedURL;privateString filter=null;privateString title;/*** Creates a new instance of FileBrowser for given Display
object.
*@paramdisplay non null display object.*/publicFileBrowser(Display display) {super("文件浏览器", IMPLICIT);
currDirName=MEGA_ROOT;this.display=display;super.setCommandListener(this);
setSelectCommand(SELECT_FILE_COMMAND);try{
dirIcon=Image.createImage(this.getClass().getResourceAsStream("dir.png"));
}catch(IOException e) {
dirIcon=null;
}try{
fileIcon=Image.createImage(this.getClass().getResourceAsStream("file.png"));
}catch(IOException e) {
fileIcon=null;
}
showDir();
}/*** 显示当前的文件夹*/privatevoidshowDir() {newThread(newRunnable() {publicvoidrun() {try{
showCurrDir();
}catch(SecurityException e) {
Alert alert=newAlert("错误","您没有权限访问此文件或文件夹!",null, AlertType.ERROR);
alert.setTimeout(2000);
display.setCurrent(alert, FileBrowser.this);
}catch(Exception e) {
e.printStackTrace();
}
}
}).start();
}/*** Indicates that a command event has occurred on Displayable d.
*@paramc a Command
object identifying the command. This is either
* one of the applications have been added to Displayable
with addCommand(Command)
* or is the implicit SELECT_COMMAND
of List.
*@paramd the Displayable
on which this event has occurred*/publicvoidcommandAction(Command c, Displayable d) {if(c.equals(SELECT_FILE_COMMAND)) {
List curr=(List) d;
currFile=curr.getString(curr.getSelectedIndex());newThread(newRunnable() {publicvoidrun() {if(currFile.endsWith(SEP_STR)||currFile.equals(UP_DIRECTORY)) {
openDir(currFile);
}else{//switch To NextdoDismiss();
}
}
}).start();
}else{if(commandListener!=null) {
commandListener.commandAction(c, d);
}
}
}/*** Sets component's title.
*@paramtitle component's title.*/publicvoidsetTitle(String title) {this.title=title;super.setTitle(title);
}/*** Show file list in the current directory .*/privatevoidshowCurrDir() {if(title==null) {super.setTitle(currDirName);
}
Enumeration e=null;
FileConnection currDir=null;
deleteAll();if(MEGA_ROOT.equals(currDirName)) {
append(UP_DIRECTORY, dirIcon);
e=FileSystemRegistry.listRoots();
}else{try{
currDir=(FileConnection) Connector.open("file:///"+currDirName);
e=currDir.list();
}catch(IOException ioe) {
}
append(UP_DIRECTORY, dirIcon);
}if(e==null) {try{
currDir.close();
}catch(IOException ioe) {
ioe.printStackTrace();
}return;
}while(e.hasMoreElements()) {
String fileName=(String) e.nextElement();if(fileName.charAt(fileName.length()-1)==SEP) {//This is directoryappend(fileName, dirIcon);
}else{//this is regular fileif(filter==null||fileName.indexOf(filter)>-1) {
append(fileName, fileIcon);
}
}
}if(currDir!=null) {try{
currDir.close();
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
}privatevoidopenDir(String fileName) {/*In case of directory just change the current directory
* and show it*/if(currDirName.equals(MEGA_ROOT)) {if(fileName.equals(UP_DIRECTORY)) {//can not go up from MEGA_ROOTreturn;
}
currDirName=fileName;
}elseif(fileName.equals(UP_DIRECTORY)) {//Go up one directory//TODO use setFileConnection when implementedinti=currDirName.lastIndexOf(SEP, currDirName.length()-2);if(i!=-1) {
currDirName=currDirName.substring(0, i+1);
}else{
currDirName=MEGA_ROOT;
}
}else{
currDirName=currDirName+fileName;
}
showDir();
}/*** Returns selected file as a FileConnection
object.
*@returnnon null FileConection
object*/publicFileConnection getSelectedFile()throwsIOException {
FileConnection fileConnection=(FileConnection) Connector.open(selectedURL);returnfileConnection;
}/*** Returns selected FileURL
object.
*@returnnon null FileURL
object*/publicString getSelectedFileURL() {returnselectedURL;
}/*** Sets the file filter.
*@paramfilter file filter String object*/publicvoidsetFilter(String filter) {this.filter=filter;
}/*** Returns command listener.
*@returnnon null CommandListener
object*/protectedCommandListener getCommandListener() {returncommandListener;
}/*** Sets command listener to this component.
*@paramcommandListener CommandListener
to be used*/publicvoidsetCommandListener(CommandListener commandListener) {this.commandListener=commandListener;
}privatevoiddoDismiss() {
selectedURL="file:///"+currDirName+currFile;
CommandListener listener=getCommandListener();if(listener!=null) {
listener.commandAction(SELECT_FILE_COMMAND,this);
}
}
}