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

📄 historyrecordlist.java

📁 本程序用JavaME语言描述了一个运行在手机上的电子书系统
💻 JAVA
字号:
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
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.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;


public class HistoryRecordList extends List implements CommandListener {			//历史记录列表
	
	final static int MAX_HR = 20;														//最大条目数
	
	//命令集合
	final static Command CMD_back = new Command("返回", Command.BACK, 2);
	final static Command CMD_choose = new Command("选择", Command.SCREEN, 1);
	final static Command CMD_del = new Command("删除", Command.SCREEN, 3);
	final static Command CMD_delAll = new Command("全部删除", Command.SCREEN, 3);
	
	private HistoryRecord[] hrList = new HistoryRecord[MAX_HR];
	//主程序reference
	TxtReader bookReader = null;
	RecordStore rs = null;
	
	private Image ErrorIcon = null;
	private Image HistoryIcon = null;
	
	public HistoryRecordList(TxtReader tr){
		super("历史纪录", List.IMPLICIT);
		
		this.addCommand(CMD_back);
		this.addCommand(CMD_choose);
		this.addCommand(CMD_del);
		this.addCommand(CMD_delAll);
		this.setSelectCommand(CMD_choose);
		this.setCommandListener(this);
		
		bookReader = tr;
		try{
			rs = RecordStore.openRecordStore("ReadingHistory", true);
			ErrorIcon = Image.createImage("/icon/Error.png");
			HistoryIcon = Image.createImage("/icon/History.png");
		}catch (Exception e){
			e.printStackTrace();
		}
		update();
	}
	
	public void write(String fileName, String path){									//写入一条记录
		boolean isSame = false;
		int num = 0;
		for (int i=0; i<MAX_HR; ++i)
			if (hrList[i]==null){
				num = i;
				break;
			}
		HistoryRecord[] hra = new HistoryRecord[num];
		for (int i=0; i<num; ++i){
			try{
				hra[i] = HistoryRecord.deserialization(rs.getRecord(hrList[i].getID()));
			}catch (Exception e){e.printStackTrace();}
			if (hra[i].getPath().equals(path)&&hra[i].getTxtName().equals(fileName)){
				isSame = true;
				break;
			}
		}
		if (!isSame){		
			boolean isFull = true;
			for (int i = 0; i < MAX_HR; ++i)
				if (null == hrList[i]) {
					isFull = false;
					break;
				}
			try {
				if (isFull){
					del(MAX_HR-1);
				}
				int id = rs.getNextRecordID();
				HistoryRecord hr = new HistoryRecord(fileName, id, path);
				byte[] data = hr.serialization();
				rs.addRecord(data, 0, data.length);
			} catch (Exception e) {
			}
			update();
		}
	}

	
	public void update(){																//刷新一条记录
		try{
			this.deleteAll();
			RecordEnumeration re = rs.enumerateRecords(null, null, false);
			for (int i=0; i<re.numRecords(); ++i){
				hrList[i] = HistoryRecord.deserialization(re.nextRecord());
				this.append(hrList[i].getTxtName(), HistoryIcon);
			}
			for (int i=re.numRecords(); i<MAX_HR; ++i){
				hrList[i] = null;
			}
			re.destroy();
			
		}catch (Exception e){}
	}
	
	public void addFile(String fileName, String path){
		write(fileName, path);
	}
	
	private void back(){																//返回主界面
		Display.getDisplay(bookReader).setCurrent(bookReader.getDisplayable(TxtReader.MAINMANU));
	}
	private class chooseThread extends Thread{										//辅助打开文件的线程类
		HistoryRecordList hrl;
		int pos;
		
		public chooseThread(HistoryRecordList l, int pos){
			hrl = l;
			this.pos = pos;
		}

		public void run() {
	        ReadingBoard RB = (ReadingBoard)bookReader.getDisplayable(TxtReader.READINGBORD);
	        if(RB.selectFile(hrList[pos].getPath(), true))
	        	Display.getDisplay(bookReader).setCurrent(RB);
	        else
	        	Display.getDisplay(bookReader).setCurrent(hrl);
		}
	}
	
	private void choose(){																//选择某条记录
		int select = this.getSelectedIndex();
		if (-1!=select){
			new chooseThread(this, select).start();
		}else{
			Display.getDisplay(bookReader).setCurrent(new Alert("错误", "请选择历史记录!", ErrorIcon, AlertType.ERROR));
		}
	}
	
	private void del(int select){														//删除一条记录
		if (-1!=select){
			try{
				rs.deleteRecord(hrList[select].getID());
				update();
			}catch (Exception e){}
		}else{
			Display.getDisplay(bookReader).setCurrent(new Alert("错误", "请选择历史记录!", ErrorIcon, AlertType.ERROR));
		}
	}
	
	private void delAll(){																//删除全部记录
		int num = this.size();
		for (int i=0; i<num; ++i){
			del(0);
		}
	}
			
	public void commandAction(Command c, Displayable d) {								//命令响应
		if (c==CMD_back){
			back();
		} else if (c==CMD_choose){
			choose();
		} else if (c==CMD_del){
			int select = this.getSelectedIndex();
			del(select);
		} else if (c==CMD_delAll){
			delAll();
		}
	}

}

⌨️ 快捷键说明

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