📄 filebrowser.java
字号:
*/
void showCurrDir() {
Enumeration e;
FileConnection currDir = null;
List browser;
try {
if (MEGA_ROOT.equals(currDirName)) {
e = FileSystemRegistry.listRoots();
browser = new List(currDirName, List.IMPLICIT);
} else {
currDir = (FileConnection)Connector.open("file://localhost/" + currDirName);
e = currDir.list();
browser = new List(currDirName, List.IMPLICIT);
// not root - draw UP_DIRECTORY
browser.append(UP_DIRECTORY, dirIcon);
}
while (e.hasMoreElements()) {
String fileName = (String)e.nextElement();
if (fileName.charAt(fileName.length() - 1) == SEP) {
// This is directory
browser.append(fileName, dirIcon);
} else {
// this is regular file
browser.append(fileName, fileIcon);
}
}
browser.setSelectCommand(view);
//Do not allow creating files/directories beside root
if (!MEGA_ROOT.equals(currDirName)) {
browser.addCommand(prop);
browser.addCommand(creat);
browser.addCommand(delete);
}
browser.addCommand(exit);
browser.setCommandListener(this);
if (currDir != null) {
currDir.close();
}
Display.getDisplay(this).setCurrent(browser);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
void traverseDirectory(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
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;
}
showCurrDir();
}
void showFile(String fileName) {
try {
FileConnection fc =
(FileConnection)Connector.open("file://localhost/" + currDirName + fileName);
if (!fc.exists()) {
throw new IOException("File does not exists");
}
InputStream fis = fc.openInputStream();
byte[] b = new byte[1024];
int length = fis.read(b, 0, 1024);
fis.close();
fc.close();
TextBox viewer =
new TextBox("View File: " + fileName, null, 1024,
TextField.ANY | TextField.UNEDITABLE);
viewer.addCommand(back);
viewer.addCommand(exit);
viewer.setCommandListener(this);
if (length > 0) {
viewer.setString(new String(b, 0, length));
}
Display.getDisplay(this).setCurrent(viewer);
} catch (Exception e) {
Alert alert =
new Alert("Error!",
"Can not access file " + fileName + " in directory " + currDirName +
"\nException: " + e.getMessage(), null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
}
}
void deleteFile(String fileName) {
try {
FileConnection fc = (FileConnection)Connector.open("file:///" + currDirName + fileName);
fc.delete();
} catch (Exception e) {
Alert alert =
new Alert("Error!",
"Can not access/delete file " + fileName + " in directory " + currDirName +
"\nException: " + e.getMessage(), null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
}
}
void showProperties(String fileName) {
try {
if (fileName.equals(UP_DIRECTORY)) {
return;
}
FileConnection fc =
(FileConnection)Connector.open("file://localhost/" + currDirName + fileName);
if (!fc.exists()) {
throw new IOException("File does not exists");
}
Form props = new Form("Properties: " + fileName);
ChoiceGroup attrs = new ChoiceGroup("Attributes:", Choice.MULTIPLE, attrList, null);
attrs.setSelectedFlags(new boolean[] { fc.canRead(), fc.canWrite(), fc.isHidden() });
props.append(new StringItem("Location:", currDirName));
props.append(new StringItem("Type: ", fc.isDirectory() ? "Directory" : "Regular File"));
props.append(new StringItem("Modified:", myDate(fc.lastModified())));
props.append(attrs);
props.addCommand(back);
props.addCommand(exit);
props.setCommandListener(this);
fc.close();
Display.getDisplay(this).setCurrent(props);
} catch (Exception e) {
Alert alert =
new Alert("Error!",
"Can not access file " + fileName + " in directory " + currDirName +
"\nException: " + e.getMessage(), null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
}
}
void createFile() {
Form creator = new Form("New File");
nameInput = new TextField("Enter Name", null, 256, TextField.ANY);
typeInput = new ChoiceGroup("Enter File Type", Choice.EXCLUSIVE, typeList, iconList);
creator.append(nameInput);
creator.append(typeInput);
creator.addCommand(creatOK);
creator.addCommand(back);
creator.addCommand(exit);
creator.setCommandListener(this);
Display.getDisplay(this).setCurrent(creator);
}
void createFile(String newName, boolean isDirectory) {
try {
FileConnection fc = (FileConnection)Connector.open("file:///" + currDirName + newName);
if (isDirectory) {
fc.mkdir();
} else {
fc.create();
}
showCurrDir();
} catch (Exception e) {
String s = "Can not create file '" + newName + "'";
if ((e.getMessage() != null) && (e.getMessage().length() > 0)) {
s += ("\n" + e);
}
Alert alert = new Alert("Error!", s, null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
// Restore the commands that were removed in commandAction()
Display.getDisplay(this).getCurrent().addCommand(creatOK);
Display.getDisplay(this).getCurrent().addCommand(back);
}
}
private String myDate(long time) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(time));
StringBuffer sb = new StringBuffer();
sb.append(cal.get(Calendar.HOUR_OF_DAY));
sb.append(':');
sb.append(cal.get(Calendar.MINUTE));
sb.append(':');
sb.append(cal.get(Calendar.SECOND));
sb.append(',');
sb.append(' ');
sb.append(cal.get(Calendar.DAY_OF_MONTH));
sb.append(' ');
sb.append(monthList[cal.get(Calendar.MONTH)]);
sb.append(' ');
sb.append(cal.get(Calendar.YEAR));
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -