📄 searchscreen.java
字号:
package org.zblog.zenghelper.screen;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import org.zblog.zenghelper.util.Navigator;
import org.zblog.zenghelper.dbtool.WordGroup;
import org.zblog.zenghelper.dbtool.EnWord;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Choice;
import java.util.Vector;
import org.zblog.zenghelper.dbtool.CnWord;
import org.zblog.zenghelper.dbtool.NumGenerator;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.Item;
import org.zblog.zenghelper.screen.sub.DelWordScreen;
/**
* <br><strong>Z英语学习助手-英汉查询屏幕</strong><br>
* <br>该屏幕包括:
* <br>1.一个TextField(用于输入英或汉关键字)
* <br>2.一个choiceGroup(用于列表查询结果,主要是为了再次对查询结果进行搜索)
* <br>3.一个Back按钮用于返回主菜单
* <br>4.其它Command:查询,详细信息,解释查询
* @author <a href="mailto:zcw@zblog.org">朱传伟</a><br>
* <a href="http://www.zblog.org">www.zblog.org</a></p>
* @version <strong>ZEnHelper</strong> Ver 1.0
*/
public class SearchScreen extends Form implements CommandListener,ItemStateListener{
private static SearchScreen instance;
private TextField text=null;
public ChoiceGroup cgp=null;
private Command back=null;
private Command search=null;
private Command info=null;
private Command summary=null;
private Command delete=null;
//当前查询的类型:"英汉查询"或"汉英查询"
public boolean isEn=true;
//当前查询关键字在RMS中rsId
public int current =-1;
public Vector currentIds=null;
private SearchScreen(){
super("英汉查询");
text=new TextField(null,"",100,TextField.ANY);
cgp=new ChoiceGroup("查询结果",Choice.EXCLUSIVE);
append(text);
append(cgp);
back=new Command("返回",Command.BACK,1);
search=new Command("查询",Command.ITEM,1);
summary=new Command("查询解释",Command.ITEM,2);
info=new Command("详细信息",Command.ITEM,3);
addCommand(search);
addCommand(back);
//addCommand(summary);
//addCommand(info);
setCommandListener(this);
setItemStateListener(this);
}
/**
* 清除该对象的各种属性,将对象还原为初始状态,因为该类的对象采用static方式创建,所以
* 有时需要将对象的属性还原为初始状态.
*/
public void clear(){
text.setString(null);
for(int n=cgp.size()-1;n>=0;n--)
cgp.delete(n);
current=-1;
currentIds=null;
removeCommand(delete);
removeCommand(summary);
removeCommand(info);
}
/**
* 该方法是工厂方法,用于创建SearchScreen对象
* @return SearchScreen
*/
public synchronized static SearchScreen getInstance(){
if(instance==null)
instance=new SearchScreen();
else
instance.clear();
return instance;
}
/**
* CommandListener回调方法,用于捕获该类定义的几的Command
* @param command Command
* @param displayable Displayable
*/
public void commandAction(Command command, Displayable displayable) {
String lb=command.getLabel();
if(lb.equals("返回")){
//clear();
Navigator.current=Navigator.Main_Screen;
Navigator.show();
}
else if(lb.equals("查询")){
search();
}
else if(lb.equals("详细信息")){
showInfo();
}
else if(lb.equals("查询解释")){
searchSummary();
}
else if(lb.equals("删除单词")){
Navigator.show(new DelWordScreen("单词",this));
}
else if(lb.equals("删除解释")){
Navigator.show(new DelWordScreen("解释",this));
}
}
/**
* 对于"查询"的操作方法,查询完毕后,将查询的类型和rsId分别保存到isEn和current属性中,
* 如果没有找到待查询的关键字,则将代表rsId的属性current置为-1.
*/
private void search(){
String word=text.getString();
if(word!=null&&!word.equals("")){
word=word.trim();
WordGroup wg=new WordGroup(word);
isEn=wg.isen;
current=wg.searchWord(word);
}
else{
current=-1;
}
showResult();
}
/**
* 针对"查询解释"的操作:<br>
* 查询解释,主要是根据用户选择的解释,把解释作为查询"关键字"进行逆向查询.
*/
private void searchSummary(){
if(current!=-1&¤tIds.size()>0){
int i=cgp.getSelectedIndex();
current=Integer.parseInt((String)currentIds.elementAt(i));
isEn=!isEn;
showResult();
}
}
/**
* 显示查询结果方法:<br>
* 本方法一般是在执行完查询后调用,以显示查询结果.
*/
private void showResult(){
//首先清除以前的查询结果
for(int n=cgp.size()-1;n>=0;n--)
cgp.delete(n);
//显示查询结果
removeCommand(delete);
removeCommand(summary);
removeCommand(info);
if(current!=-1){
//设置删除Command
if(isEn)
delete=new Command("删除单词",Command.ITEM,4);
else
delete=new Command("删除解释",Command.ITEM,4);
addCommand(delete);
//添加查询解释Command
addCommand(summary);
//显示查询结果
String str=null;
if(isEn){
//添加查看详细信息Command
addCommand(info);
EnWord ew=new EnWord(current);
text.setString(ew.word);
Vector cns=ew.cnRSId;
currentIds=cns;
Vector adjs=ew.cnAdj;
CnWord tcw=null;
for(int i=0;i<cns.size();i++){
tcw=new CnWord(Integer.parseInt((String)cns.elementAt(i)));
str=adjs.elementAt(i)+"."+tcw.word;
cgp.append(str,null);
}
}
else{
CnWord cw=new CnWord(current);
text.setString(cw.word);
Vector ens=cw.enRSId;
currentIds=ens;
EnWord tew=null;
for(int i=0;i<ens.size();i++){
tew=new EnWord(Integer.parseInt((String)ens.elementAt(i)));
cgp.append(tew.word,null);
}
}
}
}
/**
* 显示单词的详细信息方法,该方法只对"英汉查询"时有效.主要是显示该单词的详细信息.
* 包括单词,英标,是否生词,中文解释.用户可以在该页面中对单词的所有属性进行修改,
* 然后提交修改后的单词.
*/
private void showInfo(){
if(isEn&¤t!=-1){
EnWord tew=new EnWord(current);
AddWordScreen aws=AddWordScreen.getInstance();
aws.enWord=tew.word;
aws.rsId=tew.rsId;
aws.groupId=NumGenerator.getInstance().getWordNum(tew.word,true);
aws.senId=tew.senId;
if(tew.senId==-1)aws.sen = false;
else aws.sen=true;
String[]ybs=tew.ybs;
if(ybs!=null&&ybs.length>0){
for(int i=0;i<ybs.length;i++){
aws.ybs.addElement(ybs[i]);
}
}
Vector tcns=tew.cnRSId;
Vector tadjs=tew.cnAdj;
CnWord tcw=null;
if(tcns!=null&&tcns.size()>0){
for(int i=0;i<tcns.size();i++){
tcw=new CnWord(Integer.parseInt((String)tcns.elementAt(i)));
aws.cnWords.addElement(tadjs.elementAt(i)+"."+tcw.word);
}
}
Navigator.show(aws);
}
}
/**
* 侦探text中关键字的改变,如果有改变则,清除查询结果
* @param item Item
*/
public void itemStateChanged(Item item) {
if(item==text){
for(int n=cgp.size()-1;n>=0;n--){
cgp.delete(n);
}
removeCommand(delete);
removeCommand(summary);
removeCommand(info);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -