📄 noterecordlist.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 NoteRecordList extends List implements CommandListener { //标注列表
final public static int MAXNUM = 100; //最大条目数
private TxtReader bookReader = null; //主程序reference
private String txtName = null;
private int[] idList = new int[MAXNUM]; //选项ID列表
private RecordStore rs;
private TextBox viewBox = null;
private Image ErrorIcon = null;
private Image NoteIcon = null;
//是否处于viewBox显示状态
private boolean isViewMode = false;
//命令集合
private static Command CMD_back = new Command("返回", Command.BACK, 1);
private static Command CMD_view = new Command("阅读", Command.SCREEN, 1);
private static Command CMD_del = new Command("删除", Command.SCREEN, 3);
private static Command CMD_delAll = new Command("全部删除", Command.SCREEN, 4);
private static Command CMD_goTo = new Command("进入文章", Command.SCREEN, 2);
private static Command CMD_save = new Command("保存", Command.SCREEN, 2);
//唯一的构造方法
public NoteRecordList(TxtReader midlet, String txtName){
//List初始化
super(txtName+" - 札记列表", List.IMPLICIT);
this.setSelectCommand(CMD_view);
//主程序Reference
bookReader = midlet;
//打开/创建 RecordStore
try{
rs = RecordStore.openRecordStore(txtName+"NRL", true);
ErrorIcon = Image.createImage("/icon/Error.png");
NoteIcon = Image.createImage("/icon/Note.png");
}catch (Exception e){
e.printStackTrace();
}
//文本名称有用D
this.txtName = txtName;
//添加Command 设置响应器
this.addCommand(CMD_back);
this.addCommand(CMD_view);
this.addCommand(CMD_del);
this.addCommand(CMD_delAll);
this.addCommand(CMD_goTo);
this.setCommandListener(this);
//初始化idList 以及选项
for (int i=0; i<MAXNUM; ++i)
idList[i] = -1;
update();
//viewBox的初始化
viewBox = new TextBox(this.txtName, "", 128, TextField.ANY);
viewBox.addCommand(CMD_back);
viewBox.addCommand(CMD_save);
viewBox.setCommandListener(this);
}
public String getTxtName(){
return this.txtName;
}
//返回ReadingBoard
private void back(){
if (isViewMode){
viewBox.setString("");
Display.getDisplay(bookReader).setCurrent(this);
isViewMode = false;
}else{
Display.getDisplay(bookReader).setCurrent(bookReader.getDisplayable(TxtReader.READINGBORD));
}
}
//察看和修改
private void view(){
int pos = this.getSelectedIndex();
if (-1!=pos){
try{
NoteRecord nr = NoteRecord.deserialization(rs.getRecord(idList[pos]));
String text = nr.getNote();
viewBox.setString(text);
Display.getDisplay(bookReader).setCurrent(viewBox);
isViewMode = true;
}catch (Exception e){
e.printStackTrace();
}
}else{
Display.getDisplay(bookReader).setCurrent(new Alert("错误", "请选择注释!", ErrorIcon, AlertType.ERROR));
}
}
//删除条目()
private void del(){
int pos = this.getSelectedIndex();
try{
if (-1!=pos){
rs.deleteRecord(idList[pos]);
update();
}else{
Display.getDisplay(bookReader).setCurrent(new Alert("错误", "请选择注释!", ErrorIcon, AlertType.ERROR));
}
}catch (Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
//删除全部条目
private void delAll(){
try{
for (int i=0; i!=MAXNUM && idList[i]!=-1; ++i){
rs.deleteRecord(idList[i]);
idList[i] = -1;
}
}
catch (Exception e){
e.printStackTrace();
}
update();
}
//转到标注在ReadingBoard中的位置
private void goTo(){
int pos = this.getSelectedIndex();
if (pos!=-1){
try{
NoteRecord nr = NoteRecord.deserialization(rs.getRecord(idList[pos]));
ReadingBoard rb = (ReadingBoard)bookReader.getDisplayable(TxtReader.READINGBORD);
Display.getDisplay(bookReader).setCurrent(rb);
rb.goByMark(nr.getLine());
}catch (Exception e){
e.printStackTrace();
}
}
}
//显示列表
public void showList(){
Display.getDisplay(bookReader).setCurrent(this);
}
//添加NoteRecord,失败返回false
public boolean addRecord(NoteRecord nr){
int num = 0;
for (num=0; num<MAXNUM; ++num)
if (idList[num]==-1)
break;
if (num==MAXNUM){
Display.getDisplay(bookReader).setCurrent(new Alert("错误", "注释记录已满!", ErrorIcon, AlertType.ERROR));
return false;
}
if (nr!=null){
try{
byte[] data = nr.serialization();
int pos = 0;
for (int i=0;idList[i]!=-1; ++i){
++pos;
}
idList[pos] = rs.getNextRecordID();
this.append(nr.getNote(), NoteIcon);
rs.addRecord(data, 0, data.length);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
return false;
}
//在ViewMode下,保存修改
private void save(){
String str = viewBox.getString();
if (str.equals("")){
Display.getDisplay(bookReader).setCurrent(new Alert("错误", "注释不能为空!", ErrorIcon, AlertType.ERROR));
}else{
try{
int pos = this.getSelectedIndex();
NoteRecord nr = NoteRecord.deserialization(rs.getRecord(idList[pos]));
nr.setNote(str);
rs.deleteRecord(idList[pos]);
nr.setID(rs.getNextRecordID());
byte[] data = nr.serialization();
rs.addRecord(data, 0, data.length);
update();
back();
}catch (Exception e){
e.printStackTrace();
}
}
}
//命令响应
public void commandAction(Command c, Displayable d) {
if (c==CMD_back){
back();
}else if (c==CMD_view || c==NoteRecordList.SELECT_COMMAND){
view();
}else if (c==CMD_del){
del();
}else if (c==CMD_delAll){
delAll();
}else if (c==CMD_goTo){
goTo();
}else if (c==CMD_save){
save();
}
}
//更新idList和List选项
private void update(){
this.deleteAll();
NoteRecord nr = null;
try{
RecordEnumeration re = rs.enumerateRecords(null, null, false);
for (int i=0; i<re.numRecords(); ++i){
idList[i] = re.nextRecordId();
nr = NoteRecord.deserialization(rs.getRecord(idList[i]));
this.append(nr.getNote(), NoteIcon);
}
for (int i=re.numRecords(); i<MAXNUM; ++i){
idList[i] = -1;
}
re.destroy();
}catch (Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
//返回全部NoteRecord
public NoteRecord[] getAll(){
try{
int num = 0;
for (int i=0; i<MAXNUM; ++i)
if (idList[i]==-1){
num = i;
break;
}
NoteRecord[] list = new NoteRecord[num];
for (int i=0; i<num; ++i)
list[i] = NoteRecord.deserialization(rs.getRecord(idList[i]));
return list;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -