⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileexplorer.java

📁 最强手机阅读器Anyview3.0版的界面代码
💻 JAVA
字号:
package com.ismyway.anyview.win;

import java.util.Calendar;
import java.util.Enumeration;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Image;

import com.ismyway.anyview.others.Configure;
import com.ismyway.anyview.others.Operation;
import com.ismyway.anyview.others.OperationsQueue;
import com.ismyway.anyview.others.Reader;
import com.ismyway.anyview.others.Settings;
import com.ismyway.fairyui.Callback;
import com.ismyway.fairyui.Component;
import com.ismyway.fairyui.Confirm;
import com.ismyway.fairyui.MenuItem;
import com.ismyway.fairyui.Panel;
import com.ismyway.fairyui.PopMenu;
import com.ismyway.fairyui.Row;
import com.ismyway.fairyui.StringItem;
import com.ismyway.util.ArrayList;
import com.ismyway.util.FileSystemReader;
import com.ismyway.util.Res;
import com.ismyway.util.Utility;

public class FileExplorer extends Panel {
	private int timer = 0;
	private OperationsQueue queue;
	private String currentPath = null;
	private String copyofCurrentPath = null;
	private Row files = new Row();
	private Image folder, html, image, txt, umd, pdb, unknow;
	private ArrayList currentFiles = new ArrayList();

	MenuItem utf8 = new MenuItem(Res.get("UTF-8"));
	MenuItem unicode = new MenuItem(Res.get("UNICODE"));

	MenuItem openas;

	public FileExplorer(Callback callback, Panel from) {
		super(callback, from);
		add(files);
		addCommand(new Command(Res.get("Close"), Command.EXIT, 1));
		MenuItem[][] submenu = { { unicode, utf8 } };
		openas = new MenuItem(Res.get("Open As"), submenu[0]);

		MenuItem[] mainmenu = { Welcome.miOpen, openas, Welcome.miClose };
		PopMenu menu = new PopMenu(this, mainmenu);

		addCommand(new Command(Res.get("Menu"), Command.OK, 1), menu);
		setRecycle(Configure.listRecycle);

		queue = OperationsQueue.getInstance();

		try {
			folder = Image.createImage("/res/folder.png");
			html = Image.createImage("/res/html.png");
			image = Image.createImage("/res/image.png");
			txt = Image.createImage("/res/txt.png");
			umd = Image.createImage("/res/umd.png");
			pdb = Image.createImage("/res/pdb.png");
			unknow = Image.createImage("/res/unknow.png");
		} catch (Exception e) {
			showAlert(e.toString());
		}

		if ("".equals(Configure.lastOpenDir) || "/".equals(Configure.lastOpenDir)) {
			currentPath = null;
		} else {
			currentPath = Configure.lastOpenDir;
		}

		if (null == currentPath) {
			openRootDir();
		} else {
			try {
				openCurrentDir();
			} catch (Exception e) {
				openRootDir();
			}
		}
	}

	public void commandAction(Command cmd, Component c) {
		if (c instanceof StringItem) {
			queue.enqueueOperation(new Operation(this, Operation.OPEN_OP));
			return;
		}

		String func = cmd.getLabel();
		if (func.equals(Res.get("Close"))) {
			mainCanvas.closePopup();
		}
	}

	public void callback(Component component, Command cmd, Object[] parameters) {
		String func = cmd.getLabel();
		if (func.equals(Res.get("Close"))) {
			mainCanvas.closePopup();
		} else if (func.equals(Res.get("Delete"))) {
			StringItem file = (StringItem) getCurrentPointer();
			if (null == file || file.getText().equals("..")) {
				return;
			}
			String path = file.getText();
			String filename = copyofCurrentPath + path;
			mainCanvas.showPopup(new Confirm(this, Res.get("Delete") + filename), Settings.ANIMATE_DOWN);
		} else if (func.equals(Res.get("Open"))) {
			openSelected((byte) 0);
		} else if (func.equals(Res.get("UTF-8"))) {
			openSelected(Reader.UTF8);
		} else if (func.equals(Res.get("UNICODE"))) {
			openSelected(Reader.UNICODE);
		}
	}

	public void openRootDir() {
		setBusymode(true);
		setTitle(Res.get("Phone Root"));
		currentPath = null;
		copyofCurrentPath = null;
		files.clear();
		openas.setEnable(false);
		try {
			String[] roots = FileSystemReader.listRoots();
			for (int i = 0; i < roots.length; i++) {
				files.add(new StringItem(roots[i], folder));
			}
			validate();
			repaint();
		} catch (Exception e) {
			showAlert(e.toString());
		}
		setBusymode(false);
	}

	public void openCurrentDir() throws Exception {
		setBusymode(true);
		setTitle(currentPath);
		copyofCurrentPath = currentPath;
		files.clear();
		openas.setEnable(true);

		currentFiles.clear();
		files.add(new StringItem("..", folder));

		//System.out.println(currentPath);

		Enumeration em = FileSystemReader.list(copyofCurrentPath, null, FileSystemReader.DIR_FILES, Configure.showHiddenFile);

		while (em.hasMoreElements()) {
			String file = (String) em.nextElement();
			listAll(file);
		}

		validate();
		setBusymode(false);
		repaint();
	}

	private void listAll(String file) {
		String suffix = file.toLowerCase();
		if (file.startsWith(copyofCurrentPath)) {
			file = file.substring(copyofCurrentPath.length());
		}

		String appendix = "";
		if (suffix.endsWith("/")) {
			files.add(new StringItem(file, folder));
		} else if (suffix.endsWith(".txt")) {
			currentFiles.add(currentPath + appendix + file);
			files.add(new StringItem(file, txt));
		} else if (suffix.endsWith(".jpg") || suffix.endsWith(".jpeg") || suffix.endsWith(".png")
				|| suffix.endsWith(".gif")) {
			currentFiles.add(currentPath + appendix + file);
			files.add(new StringItem(file, image));
		} else if (suffix.endsWith(".htm") || suffix.endsWith(".html")) {
			currentFiles.add(currentPath + appendix + file);
			files.add(new StringItem(file, html));
		} else if (suffix.endsWith(".umd")) {
			currentFiles.add(currentPath + appendix + file);
			files.add(new StringItem(file, umd));
		} else if (suffix.endsWith(".pdb")) {
			currentFiles.add(currentPath + appendix + file);
			files.add(new StringItem(file, pdb));
		} else {
			files.add(new StringItem(file, unknow));
		}
	}

	public void openSelected(byte encode) {
		if (getCurrentPointer() instanceof StringItem) {
			StringItem file = (StringItem) getCurrentPointer();
			String path = file.getText();

			if (path.endsWith("/") || path.toLowerCase().endsWith(".zip")) {
				if (null == currentPath) {
					currentPath = path;
				} else {
					currentPath += path;
				}
				try {
					openCurrentDir();
				} catch (Exception e) {
					openRootDir();
				}
			} else if (path.equals("..")) {
				openUpDirectory();
			} else {
				/*if (!copyofCurrentPath.toLowerCase().endsWith(".zip")) {
				 Configure.lastOpenDir = copyofCurrentPath;
				 }*/
				Configure.lastOpenDir = copyofCurrentPath;
				String filename = copyofCurrentPath + path;
				openFile(filename, encode);
			}
		}
	}

	public void openFile(String filename, byte encode) {
		String suffix = Utility.getSuffix(filename);

		if (null == suffix) {
			return;
		}

		if (null != Reader.instance) {
			Reader.instance.close();
		}

		if (encode != 0) { //不为自动检测时,删除记录
			return;
		}

		if (suffix.equals(".txt")) {
		} else if (suffix.equals(".png")) {
			mainCanvas.closePopup();
			mainCanvas.setCurrent(new ImageReader(currentFiles, filename, encode));
		} else if (suffix.equals(".jpg") || suffix.equals(".jpeg")) {
			mainCanvas.closePopup();
			mainCanvas.setCurrent(new ImageReader(currentFiles, filename, encode));
		} else if (suffix.equals(".gif")) {
			mainCanvas.closePopup();
			mainCanvas.setCurrent(new ImageReader(currentFiles, filename, encode));
		} else if (suffix.equals(".htm") || suffix.equals(".html")) {
		} else if (suffix.equals(".umd")) {
			
		} else if (suffix.equals(".pdb")) {
			
		} else {

		}
	}

	public void openUpDirectory() {
		String currentRootPath = null;

		currentRootPath = copyofCurrentPath;
		if (currentRootPath.startsWith("file://")) {
			currentRootPath = currentRootPath.substring(7);
		}
		currentRootPath = currentRootPath.substring(0, currentRootPath.length() - 1);

		int start = currentRootPath.lastIndexOf('/');
		if (start > 0) {
			currentRootPath = currentRootPath.substring(0, start + 1);
		} else {
			currentRootPath = "";
		}

		if (null == currentRootPath || "".equals(currentRootPath)) {
			openRootDir();
			return;
		}

		currentPath = currentRootPath;
		try {
			openCurrentDir();
		} catch (Exception e) {
			openRootDir();
		}
	}

	public void deleteCurrent() {

	}

	public boolean isAnimated() {
		super.isAnimated();
		return true;
	}

	public boolean clock() {
		boolean repaint = super.clock();
		if (repaint) {
			return repaint;
		}

		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR);
		int min = calendar.get(Calendar.MINUTE);
		int sec = calendar.get(Calendar.SECOND);
		int pm = calendar.get(Calendar.AM_PM);
		hour = (pm == 1) ? hour + 12 : hour;

		StringBuffer sb = new StringBuffer();
		sb.append((hour < 10) ? "0" : "");
		sb.append(hour).append(":");
		sb.append((min < 10) ? "0" : "").append(min);
		sb.append(":").append((sec < 10) ? "0" : "").append(sec);

		String str = null;
		if (rightCmd != null) {
			if (timer++ < 50) {
				str = rightCmd.getLabel();
			} else {
				str = sb.toString();
			}
		} else {
			str = sb.toString();
		}

		if (rightString.equals(sb.toString())) {
			return false;
		}

		rightString = str;
		if (timer > 100) {
			timer = 0;
		}
		return true;
	}

	public boolean keyReleased(int key) {
		if (key == Settings.VKEY_NUM0) {
			if (null != copyofCurrentPath) {
				openUpDirectory();
				return true;
			}
		} else if (key == Settings.VKEY_NUM0) {
			if (null != copyofCurrentPath) {
				openUpDirectory();
				return true;
			}
		}
		return super.keyReleased(key);
	}

	public boolean vkeyReturn() {
		if (null != copyofCurrentPath) {
			openUpDirectory();
			return true;
		}
		return false;
	}

	public boolean vkeyDelete() {
		return false;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -