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 BackGround backGround;
private UserDataRecord userRecord;
private String title;
private TextEdit textEdit_name;
private TextEdit textEdit_passwd;
private TextEdit textEdit_passwd_re;
private int textEdit_name_x, textEdit_name_y;
private int textEdit_passwd_x, textEdit_passwd_y;
private int textEdit_passwd_re_x, textEdit_passwd_re_y;
private int currentlySelectedIndex = 0;
private String username, passwd, passwd_re;
long caretBlinkDelay = 500L;
long lastCaretBlink = 0;
private String object_name, editor;
private boolean cursorBlinkOn1, cursorBlinkOn2, cursorBlinkOn3;
private int width, 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);
backGround = new BackGround(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) {
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; }
draw(); redraw();
}
public void draw() {
backGround.drawBackGround(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(0xFFFFFF);
graphics.fillRect(0, 0, width, height);
}
public void checkTimeStamp() {
long currentTime = System.currentTimeMillis();
if (lastCaretBlink + caretBlinkDelay < currentTime) {
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() {
while (true) {
checkTimeStamp(); redraw();
try { synchronized(this) { 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.equals("") && !passwd.equals("") && !passwd_re.equals("")) {
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_TOKEN_SCREEN, args);
}
} break;
}
case KeyID.KEY_EDIT:
case Canvas.KEY_NUM0: case Canvas.KEY_NUM1: case Canvas.KEY_NUM2: case Canvas.KEY_NUM3:
case Canvas.KEY_NUM4: case Canvas.KEY_NUM5: case Canvas.KEY_NUM6: case Canvas.KEY_NUM7:
case Canvas.KEY_NUM8: case Canvas.KEY_NUM9: {
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 Canvas.UP: case Canvas.LEFT: {
currentlySelectedIndex--; if (currentlySelectedIndex < 0) currentlySelectedIndex = 0; else redraw(); break;
}
case Canvas.DOWN: case Canvas.RIGHT: {
currentlySelectedIndex++; if (currentlySelectedIndex > 2) currentlySelectedIndex = 2; else redraw(); break;
}
}
}
}