public void commandAction(Command c, Displayable d) {
if (c.equals(SELECT_FILE_COMMAND)) {
List curr = (List) d;
currFile = curr.getString(curr.getSelectedIndex());
new Thread(new Runnable() {
public void run() {
if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) {
openDir(currFile);
} else {
//switch To Next
doDismiss();
}
}
}).start();
} else {
if (commandListener != null) {
commandListener.commandAction(c, d);
}
}
}
/**
* Sets component's title.
* @param title component's title.
*/
public void setTitle(String title) {
this.title = title;
super.setTitle(title);
}
/**
* Show file list in the current directory .
*/
private void showCurrDir() {
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 directory
append(fileName, dirIcon);
} else {
// this is regular file
if (filter == null || fileName.indexOf(filter) > -1) {
append(fileName, fileIcon);
}
}
}
if (currDir != null) {
try {
currDir.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
private void openDir(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_ROOT
return;
}
currDirName = fileName;
} else if (fileName.equals(UP_DIRECTORY)) {
// Go up one directory
// TODO use setFileConnection when implemented
int i = 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 <code>FileConnection</code> object.
* @return non null <code>FileConection</code> object
*/
public FileConnection getSelectedFile() throws IOException {
FileConnection fileConnection = (FileConnection) Connector.open(selectedURL);
return fileConnection;
}
/**
* Returns selected <code>FileURL</code> object.
* @return non null <code>FileURL</code> object
*/
public String getSelectedFileURL() {
return selectedURL;
}
/**
* Sets the file filter.
* @param filter file filter String object
*/
public void setFilter(String filter) {
this.filter = filter;
}
/**
* Returns command listener.
* @return non null <code>CommandListener</code> object
*/
protected CommandListener getCommandListener() {
return commandListener;
}
/**
* Sets command listener to this component.
* @param commandListener <code>CommandListener</code> to be used
*/
public void setCommandListener(CommandListener commandListener) {
this.commandListener = commandListener;
}
private void doDismiss() {
selectedURL = "file:///" + currDirName + currFile;
CommandListener listener = getCommandListener();
if (listener != null) {
listener.commandAction(SELECT_FILE_COMMAND, this);
}
}
}
这个类可以用做浏览手机文件之用,也可以用做得到得选的文件之用,如果想要在选择了文件以后做什么事情的话,可以调用它的setCommandListener方法。并且处理SELECT_FILE_COMMAND.
经过试验,我发现所有的只读文件夹对于FileConnection来说,就是私有的,是不能访问到的,所以当我们要访问的时候,最好是把那些只读的文件夹改为可读写。
发现一件N76的文件的好玩的事情。在N76里面,你访问的时候,总共有四个根文件夹,分别是:手机存储/ C:/ 存储卡/ E:/可是我发现手机存储和C是一样的,存储卡和E也是一样的,也就是说可以用file:///手机存储/来访问也可以用file:///C:/来访问。
责任编辑:小草