📄 fileoperation.java
字号:
package com.cuit.lui;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
import com.cuit.lui.CommandListener;
public class FileOperation implements CommandListener, ItemCommandListener,
javax.microedition.lcdui.CommandListener {
private Lfm lfm;
private FileConnection fc;
private String pointerString;
private List fileList;
private Vector roots = new Vector();
private Panel firstPanel;
private PopUp rootLeftPopup;
private Shortcut operation;
private ChoiceGroup att;
private String currentFileURL;
private List propertyList;
private boolean isCopied = false;
private boolean isCut = false;
private String copyorcutFileURL;
private String copyorcutFilename;
private Form form;
private TextField renameField, newFolderField, newFileField;
private TextBox inputBox;
public static int status = 0;
public final static int STATUS_ROOT = 1;
public final static int STATUS_FILE = STATUS_ROOT + 1;
public final static int STATUS_TEXT = STATUS_FILE + 1;
public final static int STATUS_IMAGE = STATUS_TEXT + 1;
public final static int STATUS_PROPERTY = STATUS_IMAGE + 1;
private Command cmd_ok = new Command("ok", Command.OK, 1);
private Command cmd_enter = new Command("enter", Command.OK, 1);
private Command cmd_back = new Command("back", Command.BACK, 1);
private Command cmd_cancel = new Command("cancel", Command.CANCEL, 1);
private Command cmd_quit = new Command("quit", Command.EXIT, 1);
private Command cmd_newFile = new Command("newFile", Command.OK, 1);
private Command cmd_newFolder = new Command("newFolder", Command.OK, 1);
private Command cmd_copy = new Command("copy", Command.OK, 1);
private Command cmd_cut = new Command("cut", Command.OK, 1);
private Command cmd_paste = new Command("paste", Command.OK, 1);
private Command cmd_rename = new Command("rename", Command.OK, 1);
private Command cmd_delete = new Command("delete", Command.OK, 1);
private Command cmd_property = new Command("property", Command.OK, 1);
private Command cmd_apply = new Command("apply", Command.OK, 1);
public FileOperation(Lfm lfm) {
initialPanel();
initialLeftPopup();
this.lfm = lfm;
}
private void initialPanel() {
firstPanel = new Panel();
firstPanel.setTitle("Root...");
getRoot();
status = STATUS_ROOT;
}
private void initialShortcuts() {
operation = new Shortcut("fileOperation");
Row newFile = new Row("new file", 60);
Row newFolder = new Row("new Folder", 60);
Row copy = new Row("copy", 60);
Row cut = new Row("cut", 60);
Row paste = new Row("paste", 60);
Row rename = new Row("rename", 60);
Row delete = new Row("delete", 60);
Row property = new Row("property", 60);
newFile.addCommand(cmd_newFile);
newFolder.addCommand(cmd_newFolder);
copy.addCommand(cmd_copy);
paste.addCommand(cmd_paste);
cut.addCommand(cmd_cut);
rename.addCommand(cmd_rename);
delete.addCommand(cmd_delete);
property.addCommand(cmd_property);
newFile.setCommandListener(this);
newFolder.setCommandListener(this);
copy.setCommandListener(this);
paste.setCommandListener(this);
cut.setCommandListener(this);
rename.setCommandListener(this);
delete.setCommandListener(this);
property.setCommandListener(this);
operation.add(newFile);
operation.add(newFolder);
operation.add(copy);
operation.add(cut);
operation.add(paste);
operation.add(rename);
operation.add(delete);
operation.add(property);
if (fileList != null) {
fileList.pin(operation);
}
}
private void initialRename() {
form = new Form("new name:");
renameField = new TextField("", "input new name here", 20,
TextField.ANY);
renameField.addCommand(cmd_ok);
renameField.addCommand(cmd_cancel);
renameField.setItemCommandListener(this);
form.append(renameField);
}
private void initialNewFolder() {
form = new Form("new name:");
newFolderField = new TextField("", "input new name here", 20,
TextField.ANY);
newFolderField.addCommand(cmd_ok);
newFolderField.addCommand(cmd_cancel);
newFolderField.setItemCommandListener(this);
form.append(newFolderField);
}
private void initialNewFile() {
form = new Form("new name:");
newFileField = new TextField("", "input new name here", 20,
TextField.ANY);
newFileField.addCommand(cmd_ok);
newFileField.addCommand(cmd_cancel);
newFileField.setItemCommandListener(this);
form.append(newFileField);
}
private void initialInputBox(String name) {
inputBox = new TextBox(name, "", 1000, TextField.ANY);
inputBox.addCommand(cmd_ok);
inputBox.addCommand(cmd_cancel);
inputBox.setCommandListener(this);
}
public void createNewFolder(final String s, final boolean isReadOnly,
final boolean isHidden) {
new Thread(new Runnable() {
public void run() {
try {
fc = (FileConnection) Connector.open(fc.getURL() + s,
Connector.READ_WRITE);
fc.mkdir();
if (isReadOnly)
fc.setWritable(false);
else
fc.setWritable(true);
if (isHidden)
fc.setHidden(true);
else {
fc.setHidden(false);
}
fc.setFileConnection("..");
showCurrentDir();
} catch (IOException e) {
System.out.println("make new folder error!");
e.printStackTrace();
}
}
}).start();
}
public void makeFile(final String s, final String r,
final boolean isReadOnly, final boolean isHidden) {
new Thread(new Runnable() {
public void run() {
try {
String originalURL = fc.getURL();
fc = (FileConnection) Connector.open(originalURL + s,
Connector.READ_WRITE);
fc.create();
OutputStream fout = fc.openOutputStream();
OutputStreamWriter rout = new OutputStreamWriter(fout);
rout.write(r);
rout.close();
fout.close();
if (isReadOnly)
fc.setWritable(false);
else
fc.setWritable(true);
if (isHidden)
fc.setHidden(true);
else {
fc.setHidden(false);
}
fc = (FileConnection) Connector.open(originalURL);
showCurrentDir();
} catch (IOException e) {
System.out.println("make file error!");
e.printStackTrace();
}
}
}) {
}.start();
}
private void getRoot() {
Enumeration rootEnum = FileSystemRegistry.listRoots();
Row rootRow = null;
while (rootEnum.hasMoreElements()) {
String s = (String) rootEnum.nextElement();
rootRow = new Row(s, 150);
roots.addElement(s);
firstPanel.add(rootRow);
}
}
public void appear() {
Lfm.lscreen.setCurrentPanel(firstPanel);
initialFileList();
showFileList();
}
private void initialLeftPopup() {
rootLeftPopup = new PopUp("menu");
Row enter = new Row("enter", 60);
Row cancel = new Row("cancel", 60);
Row quit = new Row("quit", 60);
rootLeftPopup.add(enter);
rootLeftPopup.add(cancel);
rootLeftPopup.add(quit);
enter.addCommand(cmd_enter);
cancel.addCommand(cmd_cancel);
quit.addCommand(cmd_quit);
enter.setCommandListener(this);
cancel.setCommandListener(this);
quit.setCommandListener(this);
rootLeftPopup.setBorder(true);
firstPanel.addLeftPopUp(rootLeftPopup);
}
private void showCurrentDir() {
if (fileList != null) {
fileList.removeAll();
} else {
fileList = new List();
}
new Thread(new Runnable() {
public void run() {
try {
if (fc == null) {
System.out.println("fileconnection can't be null");
return;
}
Enumeration el = fc.list("*", true);
while (el.hasMoreElements()) {
String content = (String) el.nextElement();
System.out.println("file: " + content);
fileList.append(null, content);
}
fileList.setTitle(fc.getURL());
} catch (Exception e) {
System.out.println("show current directory error!");
e.printStackTrace();
}
}
}).start();
Lfm.lscreen.repaint();
}
private void determineRootFileConnection(final String s) {
System.out.println("s: " + s);
new Thread(new Runnable() {
public void run() {
if (status == STATUS_ROOT) {
try {
fc = (FileConnection) Connector.open("file:///" + s,
Connector.READ_WRITE);
System.out.println("fc: " + fc.getPath());
} catch (Exception e) {
System.out
.println("determineRootFileConnection error!");
e.printStackTrace();
}
}
}
}).start();
}
private void initialFileList() {
fileList = new List();
int i = firstPanel.getRowPointer();
if (i < 0)
i = 0;
String s = ((Row) (firstPanel.rows.elementAt(i))).getText();
pointerString = s;
fileList.setTitle(pointerString);
fileList.addLeftCommand(cmd_enter);
fileList.addRightCommand(cmd_back);
fileList.setCommandListener(this);
}
private void showFileList() {
if (fc == null) {
determineRootFileConnection(pointerString);
} else {
showCurrentDir();
}
}
private void enter(String s) {
if (isDirectory(s)) {
enterDirectory(s);
showCurrentDir();
} else {
execute(s);
}
}
private void execute(String s) {
if (s.endsWith("txt")) {
viewText(s);
} else if (s.endsWith("png") || s.endsWith("PNG")) {
viewImage(fileList.getSelectedString());
}else{
Lfm.lscreen.showAlert(null, "unsupported MIME type!");
}
}
private void viewImage(final String s) {
String originalURL = fc.getURL();
ImageViewer imageViewer=null;
try {
fc.setFileConnection(s);
imageViewer = new ImageViewer(fc);
imageViewer.setTitle(s);
fc = (FileConnection) Connector.open(originalURL);
} catch (Exception e) {
e.printStackTrace();
}
if(imageViewer!=null){
imageViewer.addRightCommand(cmd_back);
imageViewer.setCommandListener(this);
}
status=STATUS_IMAGE;
Lfm.lscreen.setCurrentPanel(imageViewer);
}
private void viewText(final String s) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -