UserDataRecord.Java
- package com.token.util;
- import Java.util.Vector;
- import Javax.microedition.rms.RecordComparator;
- import Javax.microedition.rms.RecordEnumeration;
- import Javax.microedition.rms.RecordStore;
- import Javax.microedition.rms.RecordStoreException;
- import com.token.model.*;
- public class UserDataRecord {
- private static final String RECORDSTORE_NAME="USER_DB";
- private static RecordStore info;
- public UserDataRecord(){
- }
- //打開RecordStore,沒有則創建
- public void openDataBase() {
- try {
- info = RecordStore.openRecordStore(RECORDSTORE_NAME, true);
- }catch (RecordStoreException ex) {
- info =null;
- }
- }
- //關閉RecordStore
- public void closeDataBase() {
- if (info!= null) {
- try {
- info.closeRecordStore();
- info=null;
- } catch (RecordStoreException ex) {}
- }
- }
- //增加記錄
- public int db_addRecord(UserDataItem item) {
- try {
- this.openDataBase();
- byte[] data=item.getBytes();
- int id=info.getNextRecordID();
- info.addRecord(data,0,data.length);
- this.closeDataBase();
- return id;
- } catch (RecordStoreException ex) { }
- return -1;
- }
- //更新記錄
- public void db_updateRecord(UserDataItem item){
- try {
- this.openDataBase();
- byte[] data=item.getBytes();
- info.setRecord(item.getId(),data,0,data.length);
- this.closeDataBase();
- } catch (RecordStoreException ex) { }
- }
- //訪問一條記錄
- public UserDataItem db_getRecord(int id){
- UserDataItem item=null;
- try {
- this.openDataBase();
- item = new UserDataItem(id,info.getRecord(id));
- this.closeDataBase();
- } catch (RecordStoreException ex) { }
- return item;
- }
- //刪除一條記錄
- public void db_deleteRecord(int id){
- try {
- this.openDataBase();
- info.deleteRecord(id);
- this.closeDataBase();
- } catch (RecordStoreException ex) {}
- }
- //刪除所有記錄
- public void db_deleteAllRecord(){
- try {
- RecordStore.deleteRecordStore(RECORDSTORE_NAME);
- } catch (RecordStoreException ex) {}
- }
- //訪問所有記錄
- public Vector db_getRecords(){
- Vector items=new Vector(10,3);
- this.openDataBase();//打開RecordStore
- RecordEnumeration enum1=null;
- int ind=0;
- try{
- UserDataItem item=null;
- enum1=info.enumerateRecords(null,new InnerComparator(),false);
- while(enum1.hASPreviousElement()){
- ind=enum1.previousRecordId();
- item=new UserDataItem(ind,info.getRecord(ind));
- items.addElement(item);
- }
- }catch(Exception ex){ex.printStackTrace();}
- finally{
- try{
- enum1.destroy();
- }catch(Exception e){}
- this.closeDataBase();//關閉RecordStore
- }//end finally
- return items;
- }
- //一個簡單的比較器
- private class InnerComparator implements RecordComparator{
- public int compare(byte[] rec1, byte[] rec2){
- if(rec1.length>rec2.length)
- return FOLLOWS;
- else if(rec1.length<rec2.length)
- return PRECEDES;
- else
- return EQUIVALENT;
- }
- }
- }
視圖(vIEw)
WelcomeScreen.Java
- package com.token.vIEw;
- import Javax.microedition.lcdui.*;
- import Javax.microedition.lcdui.game.GameCanvas;
- import com.token.util.UIController;
- import com.token.util.StringDealMethod;
- import com.token.vIEw.components.*;
- public class WelcomeScreen extends GameCanvas {
- private UIController controller;
- private Graphics graphics;
- private Font ft;
- private String info;
- private String info_wrap[];
- private int width;
- private int height;
- private Menu menu;
- private Head head;
- private BackGroud backGroud;
- public WelcomeScreen(UIController control) {
- // TODO Auto-generated constructor stub
- super(false);
- controller=control;
- setFullScreenMode(true);
- graphics = getGraphics();
- width = getWidth();
- height = getHeight();
- menu = new Menu(this);
- head = new Head(this);
- backGroud = new BackGroud(this);
- }
- public void show() {
- // TODO Auto-generated method stub
- //clearScreen();
- backGroud.drawBackGroud(this, graphics);
- head.drawHead(this,graphics,"");
- menu.drawMenu(this, graphics,"下一步","退出");
- drawBody();
- }
- public void drawBody()
- {
- ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
- info = "歡迎使用!\n"
- +"1 MVC測試;\n"
- +"2 自動換行測試,繪制可自動識別換行的字符串。\n";
- info_wrap = StringDealMethod.format(info, width-10, ft);
- graphics.setColor(Color.text);
- graphics.setFont(ft);
- for(int i=0; i<info_wrap.length; i++)
- {
- graphics.drawString(info_wrap[i], 5, i * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);
- }
- }
- public void clearScreen()
- {
- graphics.setColor(0xff,0xff,0xff);
- graphics.fillRect(0, 0, width, height);
- }
- protected void keyPressed(int keycode)
- {
- switch(keycode)
- {
- case KeyID.SOFT_RIGHT:
- {
- controller.handleEvent(UIController.EventID.EVENT_EXIT,null);
- break;
- }
- case KeyID.SOFT_LEFT:
- {
- String editor = "regist_name";
- Object [] args = {"registScreen",editor, null,null,null};
- controller.handleEvent(UIController.EventID.EVENT_NEXT_USER_REGIST_SCREEN,args);
- break;
- }
- default:;
- }
- }
- }
UserRegist.Java
- package com.token.vIEw;
- import Javax.microedition.lcdui.Font;
- import Javax.microedition.lcdui.Graphics;
- import Javax.microedition.lcdui.game.GameCanvas;
- import com.token.model.*;
- import com.token.util.*;
- import com.token.vIEw.components.*;
- public class UserRegist extends GameCanvas implements Runnable {
- private UIController controller;
- private Graphics graphics;
- private Font ft;
- private Menu menu;
- private Head head;
- private BackGroud backGroud;
- private UserDataRecord userRecord;
- private String title;
- private TextEdit textEdit_name;
- private TextEdit textEdit_passwd;
- private TextEdit textEdit_passwd_re;
- private int textEdit_name_x;
- private int textEdit_name_y;
- private int textEdit_passwd_x;
- private int textEdit_passwd_y;
- private int textEdit_passwd_re_x;
- private int textEdit_passwd_re_y;
- private int currentlySelectedIndex = 0;
- private String username;
- private String passwd;
- private String passwd_re;
- long caretBlinkDelay = 500L;
- long lastCaretBlink = 0;
- private String object_name;
- private String editor;
- private boolean cursorBlinkOn1;
- private boolean cursorBlinkOn2;
- private boolean cursorBlinkOn3;
- private int width;
- private int height;
- public UserRegist(UIController control)
- {
- super(false);
- this.controller=control;
- this.title = "用戶注冊";
- setFullScreenMode(true);
- graphics = getGraphics();
- width = getWidth();
- height = getHeight();
- menu = new Menu(this);
- head = new Head(this);
- backGroud = new BackGroud(this);
- userRecord = new UserDataRecord();
- textEdit_name = new TextEdit(this);
- textEdit_passwd = new TextEdit(this);
- textEdit_passwd_re = new TextEdit(this);
- }
- public void show(Object[] args) {
- // TODO Auto-generated method stub
- setFullScreenMode(true);
- object_name = ((String)args[0]!=null)?(String)args[0]:"";
- editor = ((String)args[1]!=null)?(String)args[1]:"";
- username = ((String)args[2]!=null)?(String)args[2]:"";
- passwd = ((String)args[3]!=null)?(String)args[3]:"";
- passwd_re = ((String)args[4]!=null)?(String)args[4]:"";
- if(editor.equals("regist_name"))
- {
- cursorBlinkOn1 = true;
- cursorBlinkOn2 = false;
- cursorBlinkOn3 = false;
- currentlySelectedIndex =0;
- }
- else if(editor.equals("regist_passwd"))
- {
- cursorBlinkOn1 = false;
- cursorBlinkOn2 = true;
- cursorBlinkOn3 = false;
- currentlySelectedIndex =1;
- }
- else if(editor.equals("regist_passwd_re"))
- {
- cursorBlinkOn1 = false;
- cursorBlinkOn2 = false;
- cursorBlinkOn3 = true;
- currentlySelectedIndex =2;
- }
- //System.out.println(object_name);
- //System.out.println(editor);
- draw();
- redraw();
- }
- public void draw()
- {
- //clearScreen();
- backGroud.drawBackGroud(this, graphics);
- head.drawHead(this,graphics,this.title);
- menu.drawMenu(this,graphics,"下一步","退出");
- drawBody();
- }
- private void redraw()
- {
- switch(currentlySelectedIndex)
- {
- case 0:
- {
- cursorBlinkOn2 = false;
- cursorBlinkOn3 = false;
- editor = "regist_name";
- break;
- }
- case 1:
- {
- cursorBlinkOn1 = false;
- cursorBlinkOn3 = false;
- editor = "regist_passwd";
- break;
- }
- case 2:
- {
- cursorBlinkOn1 = false;
- cursorBlinkOn2 = false;
- editor = "regist_passwd_re";
- break;
- }
- default:;
- }
- textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);
- textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);
- textEdit_passwd.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);
- textEdit_name.flushGraphics();
- }
- public void drawBody()
- {
- int margin =5;
- ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
- String info = "用戶名:\n";
- String info_wrap1[] = StringDealMethod.format(info, width-10, ft);
- graphics.setFont(ft);
- graphics.setColor(Color.text);
- for(int i=0; i<info_wrap1.length; i++)
- {
- graphics.drawString(info_wrap1[i],5, (i) * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);
- }
- textEdit_name_x = 5;
- textEdit_name_y = info_wrap1.length * ft.getHeight()+40;
- textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);
- info = "用戶密碼:\n";
- String info_wrap2[] = StringDealMethod.format(info, width-10, ft);
- graphics.setFont(ft);
- graphics.setColor(Color.text);
- for(int i=0; i<info_wrap2.length; i++)
- {
- graphics.drawString(info_wrap2[i],5, (i+info_wrap1.length) * ft.getHeight()+textEdit_name.height+margin+40, Graphics.TOP|Graphics.LEFT);
- }
- textEdit_passwd_x = 5;
- textEdit_passwd_y = (info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+margin+40;
- textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);
- info = "密碼確認:\n";
- String info_wrap3[] = StringDealMethod.format(info, width-10, ft);
- graphics.setFont(ft);
- graphics.setColor(Color.text);
- for(int i=0; i<info_wrap3.length; i++)
- {
- graphics.drawString(info_wrap3[i],5, (i+info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40, Graphics.TOP|Graphics.LEFT);
- }
- textEdit_passwd_re_x = 5;
- textEdit_passwd_re_y = (info_wrap1.length+info_wrap2.length+info_wrap3.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40;
- textEdit_passwd_re.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);
- }
- public void clearScreen()
- {
- graphics.setColor(0xff,0xff,0xff);
- graphics.fillRect(0, 0, width, height);
- }
- public void checkTimeStamp()
- {
- long currentTime = System.currentTimeMillis();
- //System.out.println("1");
- if(lastCaretBlink + caretBlinkDelay < currentTime)
- {
- //System.out.println("2");
- if(editor.equals("regist_name"))
- {
- cursorBlinkOn1 =! cursorBlinkOn1;
- cursorBlinkOn2 = false;
- cursorBlinkOn3 = false;
- }
- else if(editor.equals("regist_passwd"))
- {
- cursorBlinkOn1 = false;
- cursorBlinkOn2 =! cursorBlinkOn2;
- cursorBlinkOn3 = false;
- }
- else if(editor.equals("regist_passwd_re"))
- {
- cursorBlinkOn1 = false;
- cursorBlinkOn2 = false;
- cursorBlinkOn3 =! cursorBlinkOn3;
- }
- lastCaretBlink = currentTime;
- }
- }
- public void run()
- {
- //System.out.println("run");
- while(true)
- {
- checkTimeStamp();
- redraw();
- try
- {
- synchronized(this)
- {
- //System.out.println("3");
- wait(50L);
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- protected void keyPressed(int keyCode)
- {
- switch(keyCode)
- {
- case KeyID.SOFT_RIGHT:
- {
- controller.handleEvent(UIController.EventID.EVENT_EXIT,null);
- break;
- }
- case KeyID.SOFT_LEFT:
- {
- if(username!="" && passwd!=""&&passwd_re!="")
- {
- if(passwd.equals(passwd_re))
- {
- userRecord.db_deleteAllRecord();
- if(userRecord.db_getRecord(1)==null)
- {
- UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes());
- userRecord.db_addRecord(userItem);
- userItem = null;
- System.gc();
- }
- String update = "start";
- Object [] args = {"activeScreen", null, update};
- controller.handleEvent(UIController.EventID.EVENT_NEXT_ACTIVE_SCREEN,args);
- }
- }
- break;
- }
- case KeyID.KEY_EDIT:
- case KEY_NUM0:
- case KEY_NUM1:
- case KEY_NUM2:
- case KEY_NUM3:
- case KEY_NUM4:
- case KEY_NUM5:
- case KEY_NUM6:
- case KEY_NUM7:
- case KEY_NUM8:
- case KEY_NUM9:
- {
- //System.out.println(editor);
- Object[] args = {object_name,editor,username,passwd,passwd_re};
- controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT,args);
- break;
- }
- default:;
- }
- keyCode = getGameAction(keyCode);
- switch(keyCode)
- {
- case UP:
- case LEFT:
- {
- currentlySelectedIndex--;
- if(currentlySelectedIndex<0)
- {
- currentlySelectedIndex=0;
- }
- else
- {
- redraw();
- }
- break;
- }
- case DOWN:
- case RIGHT:
- {
- currentlySelectedIndex++;
- if(currentlySelectedIndex>2)
- {
- currentlySelectedIndex=2;
- }
- else
- {
- redraw();
- }
- break;
- }
- }
- }
- }
*TextEdit是利用GameCanvas繪制的自定義文本編輯框。後續文章將給出具體實現。