📄 showcontactinfosreen.java
字号:
import java.io.ByteArrayOutputStream;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.*;
import com.sun.perseus.model.Set;
public class showContactInfoSreen extends Form implements CommandListener, ItemCommandListener {
private final Command editArrayCommand=new Command("修改",Command.OK,1);
private final Command editBooleanCommand=new Command("修改",Command.OK,1);
private final Command commitCommand=new Command("提交",Command.OK,2);
private final Command backCommand=new Command("后退",Command.BACK,1);
private final Command showVCard=new Command("显示vCard",Command.SCREEN,5);
private final Command showVCalender=new Command("显示vCalender",Command.SCREEN,5);
private final Command addField=new Command("添加项目",Command.SCREEN,2);
private final Command removeField=new Command("删除项目",Command.SCREEN,3);
private final Command addCatCommand=new Command("添加到分组",Command.SCREEN,3);
private final Command removeCatCommand=new Command("从分组中删除",Command.SCREEN,3);
private final ContactListDemo midlet;
private final showPersonList caller;
private final PIMList list;
private final PIMItem item;
private final Hashtable fieldTable=new Hashtable();/////////////
public showContactInfoSreen(ContactListDemo midlet,showPersonList caller,PIMList list,PIMItem item) {
super("联系人信息");
this.midlet=midlet;
this.caller=caller;
this.item=item;
this.list=list;
addCommand(backCommand);
addCommand(commitCommand);
setCommandListener(this);
}
private boolean isClassField(int field)
{
return item instanceof Contact&&field==Contact.CLASS||
item instanceof Event&&field==Event.CLASS||
item instanceof ToDo&&field==ToDo.CLASS;
}
private void populateForm() throws PIMException
{
deleteAll();
fieldTable.clear();
int []fields=item.getPIMList().getSupportedFields();
boolean allFieldsUsed=true;
for(int i=0;i<fields.length;i++)
{
int field=fields[i];
if(isClassField(field)){//排除CLASS字段
continue;
}
if(item.countValues(field)==0){//判断字段是否在电话本中
allFieldsUsed=false;
continue;
}
int dataType=item.getPIMList().getFieldDataType(field);
String label=item.getPIMList().getFieldLabel(field);
Item formItem=null;
switch (dataType) {
case PIMItem.STRING:{
String sValue=item.getString(field, 0);
if(sValue==null){
sValue="";
}
int style=TextField.ANY;
switch (field) {
case Contact.EMAIL:
style=TextField.EMAILADDR;
break;
case Contact.TEL:
style=TextField.PHONENUMBER;
break;
case Contact.URL:
style=TextField.URL;
break;
}
try {
formItem=new TextField(label,sValue,128,style);
} catch (Exception e) {
formItem=new TextField(label,sValue,128,TextField.ANY);
}
break;
}
case PIMItem.BOOLEAN:
formItem=new StringItem(label,item.getBoolean(field, 0)?"yes":"no");
formItem.setDefaultCommand(editBooleanCommand);
break;
case PIMItem.STRING_ARRAY:
String a[]=item.getStringArray(field, 0);
if(a!=null){
formItem=new StringItem(label,joinStringArray(a));/////
formItem.setDefaultCommand(editArrayCommand);
}
break;
case PIMItem.DATE:
long time=item.getDate(field, 0);
int style= DateField.DATE_TIME;
switch (field) { /////
case Contact.BIRTHDAY:
style=DateField.DATE;
break;
}
formItem=new DateField(label,style);
((DateField)formItem).setDate(new Date(time));
break;
case PIMItem.INT:
formItem=new TextField(label,String.valueOf(item.getInt(field, 0)),64,TextField.DECIMAL);
break;
case PIMItem.BINARY:
byte []data=item.getBinary(field, 0);
if(data!=null){
formItem=new StringItem(label,data.length+"byte");
}
break;
}
if(formItem!=null){
append(formItem);
fieldTable.put(formItem, new Integer(field));
formItem.addCommand(removeField);
formItem.setItemCommandListener(this);
}
}
//添加VCard的命令提示
addCommand(showVCard);
if(!allFieldsUsed){
addCommand(addField);
}else {
removeCommand(addField);
}
//分组信息
String cats[]=item.getCategories();
if(cats.length>0){
addCommand(removeCatCommand);
StringItem si=new StringItem("分组名称",joinStringArray(cats));
this.insert(0, si);
}else{
addCommand(addCatCommand);
}
}
private String joinStringArray(String a[]){
StringBuffer sb=new StringBuffer();
for(int i=0;i<a.length;i++){
if(a[i]!=null&&a[i].length()>0){
if(sb.length()>0){
sb.append(",");
}
sb.append(a[i]);
}
}
return sb.toString();
}
public void commandAction(Command c, Displayable d) {
if(c==backCommand){
try {
getUserData();
} catch (Exception e) {
}
try {
caller.populateList();
} catch (Exception e) {
}
Display.getDisplay(midlet).setCurrent(caller);
}
else if(c==commitCommand){
commit();
}
else if(c==showVCard){
showItem("VCARD/2.1");
}
else if(c==showVCalender){
showItem("VCALENDAR/1.0");
}
else if(c==addField){
addField();
}
else if(c==addCatCommand){
Display.getDisplay(midlet).setCurrent(new SetCatagoryList(midlet,this,list,item));
}
}
private void addField() {
int allFields[]=item.getPIMList().getSupportedFields();
final Vector unusedField=new Vector();
for(int i =0;i<allFields.length;i++){
if(item.countValues(allFields[i])==0&&!isClassField(allFields[i])){
unusedField.addElement(new Integer(allFields[i]));
}
}
final List fieldList=new List("选择要添加的项目",List.IMPLICIT);
for(Enumeration e=unusedField.elements();e.hasMoreElements();){
int field=((Integer)e.nextElement()).intValue();
fieldList.append(item.getPIMList().getFieldLabel(field), null);
}
fieldList.addCommand(new Command("取消",Command.CANCEL,1));
fieldList.setSelectCommand(new Command("添加",Command.OK,1));
fieldList.setCommandListener(new CommandListener(){
public void commandAction(Command c, Displayable d) {
if(c.getCommandType()==Command.OK){
try {
int index=fieldList.getSelectedIndex();
int field=((Integer)unusedField.elementAt(index)).intValue();
addField(field);
} catch (Exception e) {}
try {
getUserData();
populateForm();
} catch (Exception e) {}
}
Display.getDisplay(midlet).setCurrent(showContactInfoSreen.this);
}
});
Display.getDisplay(midlet).setCurrent(fieldList);
}
private void addField(int field) {
switch (item.getPIMList().getFieldDataType(field)) {
case PIMItem.STRING:
item.addString(field, PIMItem.ATTR_NONE, "");
break;
case PIMItem.STRING_ARRAY:{
int supportedElements[]=item.getPIMList().getSupportedArrayElements(field);
int arraySize=0;
for(int i=0;i<supportedElements.length;i++){
arraySize=Math.max(arraySize, supportedElements[i]+1);
}
String a[]=new String[arraySize];
for(int i=0;i<a.length;i++){
a[i]="";
}
item.addStringArray(field, PIMItem.ATTR_NONE, a);
break;
}
case PIMItem.BINARY:
item.addBinary(field, PIMItem.ATTR_NONE, new byte[16], 0, 16);
break;
case PIMItem.BOOLEAN:
item.addBoolean(field, PIMItem.ATTR_NONE, false);
break;
case PIMItem.DATE:
item.addDate(field, PIMItem.ATTR_NONE, new Date().getTime());
break;
case PIMItem.INT:
item.addInt(field, PIMItem.ATTR_NONE, 0);
break;
}
}
private void showItem(String format) {
try {
getUserData();
populateForm();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
PIM.getInstance().toSerialFormat(item, baos, "UTF-8", format);//////
String s=new String(baos.toByteArray(),"UTF-8");
Alert a=new Alert(format,s,null,AlertType.INFO);
a.setTimeout(Alert.FOREVER);
Display.getDisplay(midlet).setCurrent(a,this);
} catch (Exception e) {
e.printStackTrace();
}
}
private void commit() {
try {
getUserData();
item.commit();
populateForm();
} catch (Exception e) {
}
}
private void getUserData() throws NumberFormatException{
int itemCount=size();
for(int i=0;i<itemCount;i++){
Item formItem=get(i);
int field=((Integer)fieldTable.get(formItem)).intValue();
if(item.countValues(field)<1){
continue;
}
int dataType=item.getPIMList().getFieldDataType(field);
switch (dataType) {
case PIMItem.STRING:{
String s=((TextField)formItem).getString();
try {
item.setString(field, 0, PIMItem.ATTR_NONE, s);
} catch (Exception e) {
}
break;
}
case PIMItem.DATE:{
long time=((DateField)formItem).getDate().getTime();
try {
item.setDate(field, 0, PIMItem.ATTR_NONE, time);
} catch (Exception e) {
}
break;
}
case PIMItem.INT:{
String s=((TextField)formItem).getString();
int j=Integer.parseInt(s);
item.setInt (field, 0, PIMItem.ATTR_NONE, j);
break;
}
}
}
}
public void commandAction(Command c, final Item formItem) {
final int field=((Integer)fieldTable.get(formItem)).intValue();
if(c==editBooleanCommand){
boolean newValue=!item.getBoolean(field, 0);
item.setBoolean(field, 0, PIMItem.ATTR_NONE, newValue);
((StringItem)formItem).setText(newValue?"yes":"no");
}
else if(c==editArrayCommand){
String label=item.getPIMList().getFieldLabel(field);
final String a[]=item.getStringArray(field, 0);
final TextField textFields[]=new TextField[a.length];
for(int i=0;i<a.length;i++){
String elementLabel=item.getPIMList().getArrayElementLabel(field, i);
textFields[i]=new TextField(elementLabel,a[i],128,TextField.ANY);
}
Form form=new Form(label,textFields);
final Command okcommand=new Command("OK",Command.OK,1);/////为什么要final?
final Command cancelcommand=new Command("取消",Command.CANCEL,1);
form.addCommand(okcommand);
form.addCommand(cancelcommand);
form.setCommandListener(new CommandListener(){
public void commandAction(Command c, Displayable d) {
if(c==okcommand){
for(int i=0;i<textFields.length;i++){
a[i]=textFields[i].getString();
}
item.setStringArray(field, 0, item.getAttributes(field, 0), a);
((StringItem)formItem).setText(joinStringArray(a));
}
Display.getDisplay(midlet).setCurrent(showContactInfoSreen.this);
}
} );
Display.getDisplay(midlet).setCurrent(form);
}
else if(c==removeField){
try {
item.removeValue(field, 0);
} catch (Exception e) {
}
try {
populateForm();
} catch (Exception e) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -