?? datefield.java
字號:
package com.jmobilecore.ui;
import com.jmobilecore.ui.core.TextField;
import com.jmobilecore.ui.core.TextComponent;
import java.util.Date;
import java.util.Calendar;
/**
* This class displays formatted textfield for entering date information
* Format of the date is specified by user.
* Blocks of digits are separated by divider.
*
* @author Igor Shevlyakov - initial implementation
* @author Greg Gridin - redesign
*/
public class DateField extends TextField {
// internal constants and variables
static final protected int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
protected int day, month, year;
/**
* Date format, example: 2004:03:26.
*/
public static final byte YYYY_MM_DD = 1;
/**
* Date format, example: 26/03/2004.
*/
public static final byte DD_MM_YYYY = 2;
/**
* Date format, example: 03/26/2004.
*/
public static final byte MM_DD_YYYY = 3;
/**
* The current DateField formatting style.
*/
protected byte STYLE;
/**
* The divider for date formatting
*/
public char divider = '/';
/**
* Constructs a new <code>DateField</code> field.
*/
public DateField() {
this(MM_DD_YYYY, '/', null);
}
/**
* Constructs a new <code>DateField</code> object of specified format.
*
* @param style the date format
*/
public DateField(byte style) {
this(style, '/', null);
}
/**
* Constructs a new <code>DateField</code> object of specified format.
*
* @param style the date format
* @param divider the divider for the date field
*/
public DateField(byte style, char divider) {
this(style, divider, null);
}
/**
* Constructs a new <code>DateField</code> object of specified format
* with the specified value.
*
* @param style the date format
* @param divider the divider for the date field
* @param date the date value
* @see #setStyle
*/
public DateField(byte style, char divider, Date date) {
super(-1, TextComponent.C_NUMERIC);
setStyle(style, divider, date);
}
/**
* Initialize date field composer
*
* @return <code>CustomFieldComposer</code> for date field
*/
protected CustomFieldComposer initComposer() {
DigitalBlockComposer[] textBlocks = new DigitalBlockComposer[3];
textBlocks[0] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 4, ' ', 1970, 2038);
textBlocks[1] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 2, ' ', 1, 12);
textBlocks[2] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 2, ' ', 1, 31);
return new CustomFieldComposer(textBlocks) {
public void setText(String text) {
}
public boolean caretLeft() {
if (textBlocks[curBlock].caretLeft()) {
if (textBlocks[curBlock].getCaretPosition() >= 0) {
return true;
}
}
boolean isComplete = textBlocks[curBlock].isComplete();
if (!isComplete) return true;
return setCaretPosition(getCaretPosition()-1);
}
public boolean caretRight() {
if (textBlocks[curBlock].caretRight()) {
if (textBlocks[curBlock].getCaretPosition() < textBlocks[curBlock].getMaxSize()) {
return true;
}
}
boolean isComplete = textBlocks[curBlock].isComplete();
if (!isComplete) return true;
return setCaretPosition(getCaretPosition());
}
public int getCaretPosition() {
if (STYLE==YYYY_MM_DD) {
if (curBlock==0) return textBlocks[0].getCaretPosition();
if (curBlock==1) return textBlocks[1].getCaretPosition()+4;
if (curBlock==2) return textBlocks[2].getCaretPosition()+6;
}
if (STYLE==DD_MM_YYYY) {
if (curBlock==0) return textBlocks[0].getCaretPosition()+4;
if (curBlock==1) return textBlocks[1].getCaretPosition()+2;
if (curBlock==2) return textBlocks[2].getCaretPosition();
}
if (STYLE==MM_DD_YYYY) {
if (curBlock==0) return textBlocks[0].getCaretPosition()+4;
if (curBlock==1) return textBlocks[1].getCaretPosition();
if (curBlock==2) return textBlocks[2].getCaretPosition()+2;
}
return 0;
}
public boolean setCaretPosition(int caretPosition) {
if (caretPosition<0 || caretPosition>8) {
return false;
}
if (STYLE==YYYY_MM_DD) {
if (caretPosition<4) { textBlocks[curBlock=0].setCaretPosition(caretPosition); }
else if (caretPosition<6) { textBlocks[curBlock=1].setCaretPosition(caretPosition-4); }
else textBlocks[curBlock=2].setCaretPosition(caretPosition-6);
return true;
}
if (STYLE==DD_MM_YYYY) {
if (caretPosition<2) textBlocks[curBlock=2].setCaretPosition(caretPosition);
else if (caretPosition<4) textBlocks[curBlock=1].setCaretPosition(caretPosition-2);
else textBlocks[curBlock=0].setCaretPosition(caretPosition-4);
return true;
}
if (STYLE==MM_DD_YYYY) {
if (caretPosition<2) textBlocks[curBlock=1].setCaretPosition(caretPosition);
else if (caretPosition<4) textBlocks[curBlock=2].setCaretPosition(caretPosition-2);
else textBlocks[curBlock=0].setCaretPosition(caretPosition-4);
return true;
}
return false;
}
};
}
/**
* Sets <code>day, month, year</code> variables
* If the field contains invalid value, the method sets
* variable <code>year</code> to <code>-1</code>
*/
protected void getBlocks() {
day = month = year = 0;
try {
DigitalBlockComposer blocks[] = (DigitalBlockComposer[])((CustomFieldComposer) composer).textBlocks;
year = (int) blocks[0].getValue();
month = (int) blocks[1].getValue();
day = (int) blocks[2].getValue();
} catch (Exception e) {
year = -1;
return;
}
}
protected char[] getFormattedText(boolean calcCursorOffset) {
TextBlockComposer blocks[] = ((CustomFieldComposer) composer).textBlocks;
currentLength = 10;
char formattedText[] = new char[currentLength];
int curBlock = ((CustomFieldComposer)composer).getCurrentBlock();
int fmtCaretPos = 0;
char [] yearText = blocks[0].getChars();
char [] monthText = blocks[1].getChars();
char [] dayText = blocks[2].getChars();
int yearPos = blocks[0].getCaretPosition();
int monthPos = blocks[1].getCaretPosition();
int dayPos = blocks[2].getCaretPosition();
if (STYLE==YYYY_MM_DD) {
System.arraycopy(yearText, 0, formattedText, 0, 4);
formattedText[4] = divider;
System.arraycopy(monthText, 0, formattedText, 5, 2);
formattedText[7] = divider;
System.arraycopy(dayText, 0, formattedText, 8, 2);
if (curBlock == 0) {
fmtCaretPos = yearPos;
}
if (curBlock == 1) {
fmtCaretPos = 5 + monthPos;
}
if (curBlock == 2) {
fmtCaretPos = 8 + dayPos;
}
}
if (STYLE==DD_MM_YYYY) {
System.arraycopy(dayText, 0, formattedText, 0, 2);
formattedText[2] = divider;
System.arraycopy(monthText, 0, formattedText, 3, 2);
formattedText[5] = divider;
System.arraycopy(yearText, 0, formattedText, 6, 4);
if (curBlock == 0) {
fmtCaretPos = 6 + yearPos;
}
if (curBlock == 1) {
fmtCaretPos = 3 + monthPos;
}
if (curBlock == 2) {
fmtCaretPos = dayPos;
}
}
if (STYLE==MM_DD_YYYY) {
System.arraycopy(monthText, 0, formattedText, 0, 2);
formattedText[2] = divider;
System.arraycopy(dayText, 0, formattedText, 3, 2);
formattedText[5] = divider;
System.arraycopy(yearText, 0, formattedText, 6, 4);
if (curBlock == 0) {
fmtCaretPos = 6 + yearPos;
}
if (curBlock == 1) {
fmtCaretPos = monthPos;
}
if (curBlock == 2) {
fmtCaretPos = 3 + dayPos;
}
}
if (calcCursorOffset) {
calculatedCaretPosition = fmtCaretPos;
calculatedCursorOffset = getCursorOffset(formattedText, calculatedCaretPosition);
}
return formattedText;
}
/**
* Get formatted representation of <code>Ipv4Field</code> value
* or <code>null</code> if <code>IPv4Field</code> object value is set to null or incomplete
*
* @return the <code>String</code> representing bank card number
*/
public String getFormattedText() {
if (isValid()) {
return String.valueOf(getFormattedText(false));
}
return null;
}
public void setStyle(byte newStyle) {
setStyle(newStyle, '/', null);
}
public void setStyle(byte newStyle, char divider) {
setStyle(newStyle, divider, null);
}
public void setStyle(byte newStyle, char divider, Date newDate) {
if (newStyle < 1 || newStyle > 3) return;
Date date = newDate;
if (date==null) date = getDate();
STYLE = newStyle;
this.divider = divider;
if (composer != null) {
composer.destructor();
}
composer = initComposer();
setDate(date);
}
public byte getStyle() {
return STYLE;
}
/**
* Returns the date value that is presented by this component.
* Time portion of returned <code>Date</code> value is set to 0 (hours,
* minutes, seconds)
*
* @return the <code>Date</code> representing the specified date
*/
public Date getDate() {
if (!isValid()) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, day);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* Sets date that is presented by this date component to be the
* specified value.
* Time portion of <code>date</code> argument is ignored
*
* @param date the date value
*/
public void setDate(Date date) {
if (date == null) {
((CustomFieldComposer) composer).clear();
} else {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH) + 1;
year = cal.get(Calendar.YEAR);
cal = null;
String dayStr = ((day < 10) ? "0" : "") + String.valueOf(day);
String monthStr = ((month < 10) ? "0" : "") + String.valueOf(month);
String yearStr = String.valueOf(year);
DigitalBlockComposer blocks[] = (DigitalBlockComposer[])((CustomFieldComposer) composer).textBlocks;
blocks[0].setText(yearStr);
blocks[1].setText(monthStr);
blocks[2].setText(dayStr);
}
setCaretPosition(0);
}
/**
* This method should not be used for the class and intentionally set to deprecated
* Use #getFormattedText() method instead.
*
* @deprecated
*/
public String getText() {
return null;
}
/**
* This method should not be used for the class and intentionally set to
* deprecated
* Use #setDate(Date date) method instead.
*
* @deprecated
*/
public void setText(String text) {
}
/**
* Tests date field value for correctness
*
* @return <code>true</code> if the date field is valid
* <code>false</code> otherwise
*/
public boolean isValid() {
if (!((CustomFieldComposer) composer).isComplete()) return false;
getBlocks();
if (year <= 0) return false;
days[1] = ((year % 4) == 0) ? 29 : 28;
return day <= days[month - 1];
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -