[J2ME] ResourceList
来源:互联网 发布:youtube软件 编辑:程序博客网 时间:2024/06/03 20:42
package midview;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
public class ResourceList extends List implements CommandListener{
final static String DATHHEAD = "file:///";
final static String ROOT = "SDCard/";
final char SLASH = '/';
final Image upIcon = null;
final Image folderIcon = null;
final Image fileIcon = null;
private MIDlet midlet = null;
private String currFile = null;
private Command openCommand = new Command("Open", Command.SCREEN, 1);
private Command exitCommand = new Command("Exit", Command.EXIT, 2);
private Runnable listRunable = new Runnable(){
public void run() {
try {
listFiles(currFile);
} catch (IOException e) {
e.printStackTrace();
}
}
};
private Runnable openFileRunable = new Runnable(){
public void run() {
try {
openFile(currFile);
} catch (Exception e) {
e.printStackTrace();
}
}
};
public ResourceList(MIDlet midlet, String title, int listType) {
super(title, listType);
this.midlet = midlet;
this.setCommandListener(this);
this.addCommand(openCommand);
this.addCommand(exitCommand);
try {
listFiles(ROOT);
} catch (IOException e) {
e.printStackTrace();
}
}
private void listFiles(String dir) throws IOException{
Enumeration enums;
FileConnection fc = null;
this.deleteAll();
if (dir==null || dir.equals(ROOT)) {
enums = FileSystemRegistry.listRoots();
this.currFile = ROOT;
} else {
append("..", upIcon);
fc = (FileConnection) Connector.open(DATHHEAD + dir);
enums = fc.list();
}
while (enums.hasMoreElements()) {
String file = (String) enums.nextElement();
if (file.charAt(file.length() - 1) == SLASH) {
// Folder
append(file, folderIcon);
} else {
// File
append(file, fileIcon);
}
}
if (fc != null) {
fc.close();
}
}
public void commandAction(Command command, Displayable display) {
if(command==openCommand){
int index = this.getSelectedIndex();
String oldDir = this.currFile;
try {
String file = this.getString(index);
if (file.charAt(file.length() - 1) == SLASH ) { //dir
currFile += file;
new Thread(this.listRunable).start();
}else if(file.equals("..")){
currFile += file;
new Thread(this.listRunable).start();
}else{ //file, open
}
} catch (Exception e) {
this.currFile = oldDir;
e.printStackTrace();
}
}else if(command==exitCommand){
openFile(null);
}
}
/**
* open file
*/
private void openFile(String filename){
filename = "/test.gif";
Displayable d = (Displayable)getRegApp(filename);
Display.getDisplay(this.midlet).setCurrent(d);
((IOpen)d).open(filename);
}
/**
* get associated class
* @param ext
* @return
*/
private IOpen getRegApp(String ext){
return new ImageViewer();
}
}
- [J2ME] ResourceList
- j2me
- j2me
- J2ME
- J2ME
- j2me
- J2ME
- j2me
- j2me
- j2me
- J2ME
- j2me
- j2me
- j2me
- j2me
- J2ME
- J2ME
- j2me
- FFPLAY的原理(四)
- SWT(JFace)体验之多线程问题
- 解决document.formname.submit()对象不支持此属性或方法的办法
- FFPLAY的原理(五)
- FFPLAY的原理(六)
- [J2ME] ResourceList
- 如何将阿拉伯数字写成英文数字
- C#将DateTable表数据导出到Excel中
- Labview8.6安装步骤
- IT运维可以更美的!博客内容已经迁移到 HTTP://BLOG.VSHARING.COM/HENRYFLYFLY 欢迎光临指导!!
- FFPLAY的原理(七)
- 大学生活
- WebGIS的主要发展趋势及前沿应用
- 经济活动分析报告