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.*;
/**
*
* @author hadeslee
*/
public class FileBrowser extends List implements CommandListener {
/**
* Command fired on file selection.
*/
public static final Command SELECT_FILE_COMMAND = new Command("选择", Command.OK, 1);
public static final Command BACK_COMMAND = new Command("返回", Command.BACK, 2);
private String currDirName;
private String currFile;
private Image dirIcon;
private Image fileIcon;
/* special string denotes upper directory */
private static final String UP_DIRECTORY = "..";
/* special string that denotes upper directory accessible by this browser.
* this virtual directory contains all roots.
*/
private static final String MEGA_ROOT = "/";
/* separator string as defined by FC specification */
private static final String SEP_STR = "/";
/* separator character as defined by FC specification */
private static final char SEP = '/';
private Display display;
private String selectedURL;
private String filter = null;
private String title;
Screen backScreen;
/**
* Creates a new instance of FileBrowser for given <code>Display</code> object.
* @param display non null display object.
*/
public FileBrowser(Display dis,Screen back) {
super("选择文件", IMPLICIT);
currDirName = MEGA_ROOT;
display = dis;
backScreen = back;
setSelectCommand(SELECT_FILE_COMMAND);
setSelectCommand(BACK_COMMAND);
setCommandListener(this);
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;
}
}
/**
* 显示当前的文件夹
*/
public void showDir() {
new Thread(new Runnable() {
public void run() {
try {
showCurrDir();
} catch (SecurityException e) {
Alert alert = new Alert("错误", "您没有权限访问此文件或文件夹!", 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.
* @param c a <code>Command</code> object identifying the command. This is either
* one of the applications have been added to <code>Displayable</code> with <code>addCommand(Command)</code>
* or is the implicit <code>SELECT_COMMAND</code> of List.
* @param d the <code>Displayable</code> on which this event has occurred
*/
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 (c.equals(BACK_COMMAND)) {
display.setCurrent(backScreen);
}
}
/**
* 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;
}
private void doDismiss() {
selectedURL = "file:///" + currDirName + currFile;
System.out.println(selectedURL);
((EditForm)backScreen).siPath.setText(selectedURL);
display.setCurrent(backScreen);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SMSclient.rar_smsclient
共18个文件
class:10个
java:3个
jad:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 152 浏览量
2022-09-24
14:08:05
上传
评论
收藏 24KB RAR 举报
温馨提示
J2ME 浏览文件功能,使用 JSR75.本来要做短信发送功能的。但是还没做出来
资源推荐
资源详情
资源评论
收起资源包目录
SMSclient.rar (18个子文件)
SMSclient
bin
SMSclient.jar 6KB
SMSclient.jad 246B
MANIFEST.MF 200B
res
tmplib
tmpclasses
EditForm.class 3KB
FileBrowser$1.class 1KB
FileBrowser.class 6KB
FileBrowser$2.class 820B
ClientMIDlet.class 1KB
src
EditForm.java 1KB
ClientMIDlet.java 527B
FileBrowser.java 7KB
classes
EditForm.class 3KB
FileBrowser$1.class 1KB
FileBrowser.class 6KB
FileBrowser$2.class 866B
ClientMIDlet.class 1KB
SMSclient.pro 394B
lib
project.properties 324B
共 18 条
- 1
资源评论
小贝德罗
- 粉丝: 71
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功