?? textcomponent.java
字號:
package com.jmobilecore.ui.core;
import java.util.TimerTask;
import java.util.Timer;
/**
* The <code>TextComponent</code> class is the superclass of
* any component that allows the editing of some text.
* <p/>
* The <code>TextComponent</code> class defines a set of methods
* that supports text editing using phone keypad.
*
* @author Greg Gridin
*/
abstract public class TextComponent extends Component {
/**
* Constraints for the document
*/
protected int constraint = C_NO_CHARS;
/**
* Numeric characters
*/
protected static final String NUMERIC_CHARS = "0123456789";
/**
* Uppercase characters
*/
protected static final String UC_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* Lowercase characters
*/
protected static final String LC_CHARS = "abcdefghijklmnopqrstuvwxyz";
/**
* Email special characters
*/
protected static final String EMAIL_SPEC_CHARS = "~_.@";
/**
* Space character
*/
protected static final String SPACE_CHARS = " ";
/**
* All special character
*/
protected static final String ALL_SPEC_CHARS = " ~`!@#$%^&*()_-+=\\|'\".,/?<>:;[]";
/**
* Do not allows any chars
*/
public static final int C_NO_CHARS = 0x00;
/**
* Allows numbers in the document
* @see #NUMERIC_CHARS
*/
public static final int C_NUMERIC = 0x01;
/**
* Allows lowercase letters in the document
* @see #LC_CHARS
*/
public static final int C_LOWER_CASE = 0x02;
/**
* Allows uppercase letters in the document
* @see #UC_CHARS
*/
public static final int C_UPPER_CASE = 0x04;
/**
* Allows email special letters in the document
* @see #EMAIL_SPEC_CHARS
*/
public static final int C_EMAIL_SPEC_CHARS = 0x08;
/**
* Allows spaces in the document
* @see #SPACE_CHARS
*/
public static final int C_SPACE = 0x10;
/**
* Allows all special characters in the document
* @see #ALL_SPEC_CHARS
*/
public static final int C_ALL_SPEC_CHARS = 0x80;
/**
* Allows all alphabet characters and space in the document
*/
public static final int C_ALPHA = C_LOWER_CASE | C_UPPER_CASE | C_SPACE;
/**
* Allows alphanumeric characters in the document
*/
public static final int C_ALPHA_NUMERIC = C_ALPHA | C_NUMERIC;
/**
* Allows all email characters in the document
*/
public static final int C_EMAIL =
C_ALPHA_NUMERIC | C_EMAIL_SPEC_CHARS ^ C_SPACE;
/**
* Allows any characters in the document
*/
public static final int C_ANY = 0xFF;
/**
* The array of allowed characted
*/
protected char[] validChars = null;
/**
* Indicated if text component is in insert mode
*/
volatile protected boolean insertMode = true;
/**
* Index into <code>charOptionCounter</code> for the last character used.
*/
volatile protected int charOptionCounter = 0;
/**
* The characters, mapped to the number keys
*/
final static protected char[][] CHAR_OPTIONS = {
{'0', '+'},
{'1', '.', '-', '\'', '+', '!', '\"', '*', '#', '$', '%', '(', ')', '@', '&', '_',
'~', '`', '^', '=', '\\', '|', ',', '/', '?', '<', '>', ':', ';', '[', ']'},
{'A', 'B', 'C', 'a', 'b', 'c', '2'},
{'D', 'E', 'F', 'd', 'e', 'f', '3'},
{'G', 'H', 'I', 'g', 'h', 'i', '4'},
{'J', 'K', 'L', 'j', 'k', 'l', '5'},
{'M', 'N', 'O', 'm', 'n', 'o', '6'},
{'P', 'Q', 'R', 'S', 'p', 'q', 'r', 's', '7'},
{'T', 'U', 'V', 't', 'u', 'v', '8'},
{'W', 'X', 'Y', 'Z', 'w', 'x', 'y', 'z', '9'},
{' '}
};
/**
* Phone keys used for data input.
*/
final static protected int[] KEY_OPTIONS = {
PlatformCanvas.KEY_NUM0, PlatformCanvas.KEY_NUM1, PlatformCanvas.KEY_NUM2, PlatformCanvas.KEY_NUM3,
PlatformCanvas.KEY_NUM4, PlatformCanvas.KEY_NUM5, PlatformCanvas.KEY_NUM6, PlatformCanvas.KEY_NUM7,
PlatformCanvas.KEY_NUM8, PlatformCanvas.KEY_NUM9, PlatformCanvas.KEY_POUND
};
protected int lastKeyCode = 0;
/**
* Creates a new <code>TextComponent</code> instance.
* @param constr Constraints for this document
*/
public TextComponent(int constr) {
this.constraint = constr;
focusable = true;
int vc = 0;
if ((constr & C_NUMERIC) > 0) {
vc += NUMERIC_CHARS.length();
}
if ((constr & C_LOWER_CASE) > 0) {
vc += UC_CHARS.length();
}
if ((constr & C_UPPER_CASE) > 0) {
vc += LC_CHARS.length();
}
if ((constr & C_ALL_SPEC_CHARS) > 0) {
vc += ALL_SPEC_CHARS.length();
} else {
if ((constr & C_EMAIL_SPEC_CHARS) > 0) {
vc += EMAIL_SPEC_CHARS.length();
}
if ((constr & C_SPACE) > 0) {
vc += SPACE_CHARS.length();
}
}
validChars = new char[vc];
int i, len;
vc = 0;
if ((constr & C_NUMERIC) > 0) {
len = NUMERIC_CHARS.length();
for (i = 0; i < len; i++)
validChars[vc++] = NUMERIC_CHARS.charAt(i);
}
if ((constr & C_LOWER_CASE) > 0) {
len = LC_CHARS.length();
for (i = 0; i < len; i++)
validChars[vc++] = LC_CHARS.charAt(i);
}
if ((constr & C_UPPER_CASE) > 0) {
len = UC_CHARS.length();
for (i = 0; i < len; i++)
validChars[vc++] = UC_CHARS.charAt(i);
}
if ((constr & C_ALL_SPEC_CHARS) > 0) {
len = ALL_SPEC_CHARS.length();
for (i = 0; i < len; i++)
validChars[vc++] = ALL_SPEC_CHARS.charAt(i);
} else {
if ((constr & C_EMAIL_SPEC_CHARS) > 0) {
len = EMAIL_SPEC_CHARS.length();
for (i = 0; i < len; i++)
validChars[vc++] = EMAIL_SPEC_CHARS.charAt(i);
}
if ((constr & C_SPACE) > 0) {
len = SPACE_CHARS.length();
for (i = 0; i < len; i++)
validChars[vc++] = SPACE_CHARS.charAt(i);
}
}
}
/**
* Adds valid characters for the component.
* @param str string of valid characters
*/
public void addValidChars(String str) {
int strlen = str.length();
char[] newValidChars = new char[validChars.length + strlen];
int i;
for (i = 0; i < validChars.length; i++) {
newValidChars[i] = validChars[i];
}
for (int t = 0; t < strlen; t++) {
newValidChars[i + t] = str.charAt(t);
}
validChars = newValidChars;
}
protected boolean preProcessKeyCode(int keyCode) {
if (keyCode != lastKeyCode) {
insertMode = true;
charOptionCounter = 0;
return true;
}
return false;
}
protected char processKeyCode(int keyCode) {
final int keyIndex = getKeyOption(keyCode);
if (keyIndex == -1) {
insertMode = true;
charOptionCounter = 0;
return (char) keyCode;
}
int rc = charOptionCounter;
final int len = CHAR_OPTIONS[keyIndex].length;
if (rc >= len) rc %= len;
char ch, rslt = 0;
for (int p = 0; p < len; p++) {
ch = CHAR_OPTIONS[keyIndex][rc];
if (isCharValid(ch)) {
if (rslt == 0) {
charOptionCounter = rc + 1;
rslt = ch;
} else {
insertMode = false;
return rslt;
}
}
rc = (rc + 1) % len;
}
insertMode = true;
charOptionCounter = 0;
return rslt;
}
/**
* Checks if the character is allows symbol
* @param ch test character
* @return <code>true</code> if the characted is allwed symbol
* for the component, <code>false</code> otherwise
*/
protected boolean isCharValid(char ch) {
int vcl = validChars.length;
for (int i = 0; i < vcl; i++) {
if (validChars[i] == ch) return true;
}
return false;
}
protected int getKeyOption(int keyCode) {
int l = KEY_OPTIONS.length;
for (int keyIndex = 0; keyIndex < l; keyIndex++) {
if (KEY_OPTIONS[keyIndex] == keyCode) return keyIndex;
}
return -1;
}
/**
* The gap between the screen border and the textfield frame
*/
final static public int BORDER_WIDTH = 1;
/**
* The gap between the textfield frame and textfield text
*/
final static public int BORDER_GAP = 1;
/**
* Gets the position of the text insertion caret for this text component.
*
* @return the position of the text insertion caret
*/
public int getCaretPosition() {
return getComposer().getCaretPosition();
}
/**
* Sets the position of the text insertion caret for this text component.
* The caret position is constrained to be at or before the current selection end and non-negative.
* @param caretPosition the position of the text insertion caret
* @return <code>true</code> if setting of caret position is possible <code>false</code>
*/
public boolean setCaretPosition(int caretPosition) {
return getComposer().setCaretPosition(caretPosition);
}
/**
* The flag that determines whether or not this text component is editable.
* If the flag is set to true, this text component becomes user editable.
* If the flag is set to false, the user cannot change the text of this text component.
*/
public boolean editable = true;
/**
* Gets current input composer
*/
abstract protected AbstractComposer getComposer();
public static final byte CURSOR_TYPE_SYMBOL = 1;
public static final byte CURSOR_TYPE_LINE = 2;
protected void resetCursorHandler() {
insertMode = true;
charOptionCounter = 0;
}
/**
* Blinking rate for the curson in milliseconds
*/
protected static final int CURSOR_DELAY = 1000;
/**
* The timer is used to determine if key input time is elapsed
*/
protected Timer cHandler = null;
/**
* Cancels the timer
*/
synchronized protected void resetCursor()
{
if (cHandler != null)
{
cHandler.cancel();
cHandler = null;
}
}
/**
* Initializes new timer
*/
protected void initCursor()
{
if (!insertMode)
{
cHandler = new Timer();
cHandler.schedule(new CursorHandler(this), CURSOR_DELAY);
}
}
/**
* The timer task, when elapsed cursor moves to next position
*/
class CursorHandler extends TimerTask
{
TextComponent parentField;
public CursorHandler(TextComponent tf)
{
parentField = tf;
}
public void run()
{
resetCursorHandler();
getComposer().caretRight();
parentScreen.repaint();
resetCursor();
}
}
/**
* Default destructor. Helps VM to perform garbage collection
*/
public void destructor() {
validChars = null;
resetCursor();
}
} // class TextComponent
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -