📄 bookmarklist.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.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
public class BookMarkList extends List implements CommandListener { //书签列表类
public final static int MAX_BM = 10; //列表最大书签数
private static TxtReader bookReader = null; //主程序reference
//命令集合
private static Command CMD_back = new Command("返回",Command.BACK,1);
private static Command CMD_enter = new Command("进入书签",Command.SCREEN,0);
private static Command CMD_delete = new Command("删除",Command.SCREEN,2);
private static Command CMD_edit = new Command("编辑", Command.SCREEN, 2);
private static Command CMD_ok = new Command("确定", Command.OK, 1);
private static Command CMD_delAll = new Command("删除所有", Command.SCREEN, 3);
private static TextBox editBox = null;
private static RecordStore rs = null;
private boolean isEditMode = false;
private int[] idList = new int[MAX_BM]; //列表选项对应书签的ID号
private RecordStore default_rs = null;
private String txtName = null;
private Image ErrorIcon = null;
private Image BookMarkIcon = null;
public BookMarkList(String fileName, TxtReader tr){
super(fileName+" - 书签列表", List.IMPLICIT);
this.txtName = fileName;
bookReader = tr;
addCommand(CMD_back);
addCommand(CMD_enter);
addCommand(CMD_delete);
addCommand(CMD_edit);
addCommand(CMD_delAll);
setCommandListener(this);
try{
rs = RecordStore.openRecordStore(fileName, true);
ErrorIcon = Image.createImage("/icon/Error.png");
BookMarkIcon = Image.createImage("/icon/BookMark.png");
}catch (Exception e){ //不应如此简单处理
e.printStackTrace();
}
update();
}
public void update(){ //刷新书签列表
this.deleteAll();
try{
RecordEnumeration re = rs.enumerateRecords(null, null, false);
BookMark bm;
for (int i=0; i<re.numRecords(); ++i){
bm = BookMark.deserialization(re.nextRecord());
this.append(bm.getCento(), BookMarkIcon);
idList[i] = bm.getID();
}
for (int i=re.numRecords(); i<MAX_BM; ++i){
idList[i] = -1;
}
re.destroy();
}catch (Exception e){
e.printStackTrace();
}
}
public int whenEnter(){
BookMark bm = null;
try{
default_rs = RecordStore.openRecordStore(txtName.concat("fgfdg"),
true);
RecordEnumeration re = default_rs.enumerateRecords(null, null, false);
bm = BookMark.deserialization(re.nextRecord());
re.destroy();
default_rs.deleteRecord(bm.getID());
default_rs.closeRecordStore();
}catch (Exception e){
return 0;
}
return bm.getLine();
}
public void whenQuit(int line){
try{
default_rs = RecordStore.openRecordStore(txtName.concat("fgfdg"),
true);
RecordEnumeration re = default_rs.enumerateRecords(null, null, false);
for (int i=0; i<re.numRecords(); ++i){
default_rs.deleteRecord(re.nextRecordId());
}
re.destroy();
BookMark bm = new BookMark();
bm.setCento("");
bm.setID(default_rs.getNextRecordID());
bm.setLine(line);
byte[] data = bm.serialization();
default_rs.addRecord(data, 0, data.length);
default_rs.closeRecordStore();
//RecordStore.deleteRecordStore(txtName.concat("fgfdg"));
}catch (Exception e){
e.printStackTrace();
}
}
private void back(){ //返回上级界面
if (isEditMode){
isEditMode = false;
Display.getDisplay(bookReader).setCurrent(this);
editBox = null;
}else{
Display.getDisplay(bookReader).setCurrent(bookReader.getDisplayable(TxtReader.READINGBORD));
}
}
private void enter(){ //跳转到文本
int select = this.getSelectedIndex();
if (-1!=select){
try{
BookMark bm = BookMark.deserialization(rs.getRecord(idList[select]));
ReadingBoard rb = (ReadingBoard)bookReader.getDisplayable(TxtReader.READINGBORD);
Display.getDisplay(bookReader).setCurrent(rb);
rb.goByMark(bm.getLine());
}catch (Exception e){
e.printStackTrace();
}
}
}
private void delete(){ //删除一条书签
int select = this.getSelectedIndex();
if (-1!=select){
try{
int id = idList[select];
rs.deleteRecord(id);
}catch (Exception e){
e.printStackTrace();
}
update();
}
else{
Display.getDisplay(bookReader).setCurrent(new Alert("错误", "请选择文件!", ErrorIcon, AlertType.ERROR));
}
}
private void delAll(){
int i=0;
this.deleteAll();
while (i!=BookMarkList.MAX_BM && idList[i]!=-1){
try{
rs.deleteRecord(idList[i]);
}catch (Exception e){
e.printStackTrace();
}
idList[i] = -1;
++i;
}
}
public void add(int line, String fs){ //添加一条书签
boolean isFull = true;
for (int i=0; i<MAX_BM; ++i)
if (idList[i]==-1){
isFull = false;
break;
}
if (!isFull){
try{
BookMark bm = new BookMark(fs, line, rs.getNextRecordID());
byte[] data = bm.serialization();
rs.addRecord(data, 0, data.length);
}catch (Exception e){
e.printStackTrace();
}
}
else{
Display.getDisplay(bookReader).setCurrent(new Alert("错误", "书签列表已满!", ErrorIcon, AlertType.ERROR));
}
update();
}
private void edit(){ //编辑书签标题
int pos = this.getSelectedIndex();
if (-1!=pos){
try{
isEditMode = true;
editBox = new TextBox("editBox", this.getString(pos), 40, TextField.ANY);
editBox.addCommand(CMD_back);
editBox.addCommand(CMD_ok);
editBox.setCommandListener(this);
Display.getDisplay(bookReader).setCurrent(editBox);
}catch (Exception e){
e.printStackTrace();
}
}else{
Display.getDisplay(bookReader).setCurrent(new Alert("错误", "请选择文件!", ErrorIcon, AlertType.ERROR));
}
}
private void ok(){ //编辑成功
try{
String s = editBox.getString();
if (s==null || s.equals("")){
Display.getDisplay(bookReader).setCurrent(new Alert("错误", "内容不能为空", ErrorIcon, AlertType.ERROR));
}else{
int select = this.getSelectedIndex();
int id = idList[select];
BookMark bm = BookMark.deserialization(rs.getRecord(id));
rs.deleteRecord(id);
bm.setCento(s);
bm.setID(rs.getNextRecordID());
byte[] data = bm.serialization();
rs.addRecord(data, 0, data.length);
update();
Display.getDisplay(bookReader).setCurrent(this);
}
}catch (Exception e){
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable d) { //命令响应
if (c==CMD_back){
back();
}else if (c==CMD_enter || c==List.SELECT_COMMAND){
enter();
}else if (c==CMD_delete){
delete();
}else if (c==CMD_edit){
edit();
}else if (c==CMD_ok){
ok();
}else if (c==CMD_delAll){
delAll();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -