?? addaccount.java
字號:
/*
* AddAccount.java
*
* Created on 2007年3月11日, 下午2:15
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package net.bccn.account.ui;
import java.util.Date;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import net.bccn.account.model.Account;
import net.bccn.account.util.Util;
/**
*
* @author hadeslee
*/
public class AddAccount extends Form implements ItemStateListener,CommandListener{
private static AddAccount af;
private ChoiceGroup type,detail;//類型和細節
private TextField money,remark;//金額,備注
private DateField date;//日期
private Command ok,back,reset;//OK,返回,重置的按鈕
String[] output=new String[]{"日常用品","化妝品","交通","服裝",
"娛樂","書籍報紙","其它"};
String[] input=new String[]{"工資","獎金","外快","其它"};
private boolean isModify;//是否是在修改
private Account temp;//臨時的變量
private static Image[] outputImage=new Image[7],inputImage=new Image[4];
/**
* Creates a new instance of AddAccount
*/
private AddAccount() {
super("添加賬目");
initWindow();
}
private void initWindow(){
type=new ChoiceGroup("賬目類型",Choice.EXCLUSIVE,
new String[]{"支出","收入"},null);
type.setSelectedIndex(0,true);
Image[] outs=ViewAccountForm.getInstance().getOutput();
Image[] ins=ViewAccountForm.getInstance().getInput();
for(int i=0;i<outputImage.length;i++){
outputImage[i]=outs[i+1];
}
for(int i=0;i<inputImage.length;i++){
inputImage[i]=ins[i+1];
}
detail=new ChoiceGroup("詳細",Choice.POPUP,
new String[]{"日常用品","化妝品","交通","服裝",
"娛樂","書籍報紙","其它"},outputImage);
money=new TextField("金額",null,8,TextField.DECIMAL);
remark=new TextField("備注",null,20,TextField.ANY);
date=new DateField("日期",DateField.DATE);
date.setDate(new Date());
ok=new Command("確定",Command.OK,1);
back=new Command("返回",Command.BACK,0);
reset=new Command("重置",Command.CANCEL,2);
this.append(type);
this.append(detail);
this.append(money);
this.append(date);
this.append(remark);
this.addCommand(ok);
this.addCommand(back);
this.addCommand(reset);
this.setCommandListener(this);
this.setItemStateListener(this);
}
public static AddAccount getInstance(){
if(af==null){
af=new AddAccount();
}
af.setTitle("添加記錄");
af.isModify=false;
af.addCommand(af.ok);
af.addCommand(af.reset);
af.reset();
return af;
}
//重載一個方法,第一個參數是想傳進來的賬目,第二個參數表示修改還是查看
public static AddAccount getInstance(Account modi,boolean b){
if(af==null){
af=new AddAccount();
}
af.isModify=b;
af.temp=modi;
if(b){
af.setTitle("修改賬目");
}else{
af.setTitle("查看賬目");
}
af.type.setSelectedIndex((modi.getType().equals("支出")?0:1),true);
if(af.type.isSelected(0)){//此時表示支出
af.detail.deleteAll();
for(int i=0;i<af.output.length;i++){
af.detail.insert(i,af.output[i],outputImage[i]);
if(af.output[i].equals(modi.getDetail())){
af.detail.setSelectedIndex(i,true);
}
}
}else{
af.detail.deleteAll();
for(int i=0;i<af.input.length;i++){
af.detail.insert(i,af.input[i],inputImage[i]);
if(af.input[i].equals(modi.getDetail())){
af.detail.setSelectedIndex(i,true);
}
}
}
af.money.setString(Float.toString(modi.getMoney()));
af.date.setDate(modi.getDate());
af.remark.setString(modi.getRemark());
if(b){
af.addCommand(af.ok);
af.addCommand(af.reset);
}else{
af.removeCommand(af.ok);
af.removeCommand(af.reset);
}
return af;
}
//根據當前的輸入得出一個Account對象
private Account getAccount(){
Account ac=new Account();
ac.setType(type.getString(type.getSelectedIndex()));
ac.setDetail(detail.getString(detail.getSelectedIndex()));
ac.setMoney(Float.parseFloat(money.getString()));
ac.setDate(date.getDate());
if(remark.getString()!=null){
ac.setRemark(remark.getString());
}
if(isModify){
ac.setID(temp.getID());
}
return ac;
}
public void commandAction(Command command, Displayable displayable) {
if(command==back){
if(this.getTitle().equals("添加賬目"))
Util.changeTo(Util.MAIN_FORM);
else
Util.changeTo(Util.VIEW_ACCOUNT);
}else if(command==ok){
//如果金額里面沒有輸入金額,則不往下執行
if(money.getString()==null||money.getString().equals("")||
Float.parseFloat(money.getString())<=0){
Util.showINFO(AlertType.ERROR,"請輸入金額!!\n金額數值不能小于等于0!!");
return;
}
if(isModify){
Account aw=getAccount();
boolean b=Util.updateRecord(aw);
if(b){
Util.changeTo(Util.VIEW_ACCOUNT_FORM);
Util.showINFO(AlertType.INFO,"記錄修改成功!!請返回!!",ViewAccount.getInstance());
ViewAccount.updateList(aw);
}else{
Util.showINFO(AlertType.ERROR,"記錄修改失敗!!");
}
}else{
boolean b=Util.saveRecord(this.getAccount());
if(b){
reset();
Util.showINFO(AlertType.INFO,"記錄添加成功!!");
}else{
Util.showINFO(AlertType.ERROR,"記錄添加失敗!!");
}
}
}else if(command==reset){
reset();
}
}
//重置
private void reset(){
this.setTitle("添加賬目");
type.setSelectedIndex(0,true);
detail.deleteAll();
for(int i=0;i<output.length;i++){
detail.insert(i,output[i],outputImage[i]);
}
detail.setSelectedIndex(0,true);
money.setString(null);
date.setDate(new Date());
remark.setString(null);
}
public void itemStateChanged(Item item){
if(item==type){
int index=type.getSelectedIndex();
if(index==0){
detail.deleteAll();
for(int i=0;i<output.length;i++){
detail.insert(i,output[i],outputImage[i]);
}
}else{
detail.deleteAll();
for(int i=0;i<input.length;i++){
detail.insert(i,input[i],inputImage[i]);
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -