📄 timefield.java
字号:
package com.jmobilecore.ui;
import com.jmobilecore.ui.core.TextField;
import com.jmobilecore.ui.core.TextComponent;
import java.util.Date;
/**
* This class displays formatted textfield for entering time information
* Format of the time is specified by user.
* Blocks of digits are separated by divider.
*
* @author Igor Shevlyakov - initial implementation
* @author Greg Gridin - redesign
*/
public class TimeField extends TextField {
// internal constants and variables
protected static final String AM = "AM";
protected static final String PM = "PM";
protected int sec, min, hour;
protected boolean pm;
/**
* Time format, example: 19:03:24.
*/
public static final byte HH_mm_ss = 1;
/**
* Time format, example: 09:00.
*/
public static final byte HH_mm = 2;
/**
* Time format, example: 09:07:32PM.
*/
public static final byte hh_mm_ssX = 3;
/**
* Time format, example: 11:07AM.
*/
public static final byte hh_mmX = 4;
/**
* The current TimeField formatting style.
*/
protected byte STYLE;
/**
* The divider for date formatting
*/
public char divider = ':';
/**
* The composer for AM/PM field
*/
class AMPMComposer extends TextBlockComposer {
public AMPMComposer() {
super(STATIC_FOCUSABLE, AM);
}
public boolean addChar(char ch, boolean insertMode) {
if (ch == '2') {
setText(AM);
}
if (ch == '7') {
setText(PM);
}
return true;
}
};
/**
* Constructs a new <code>TimeField</code> field.
*/
public TimeField() {
this(HH_mm_ss, ':', null);
}
/**
* Constructs a new <code>TimeField</code> field of specified format
*
* @param style the time format
*/
public TimeField(byte style) {
this(style, ':', null);
}
/**
* Constructs a new <code>TimeField</code> field of specified format
*
* @param style the time format
* @param divider the divider for the time field
*/
public TimeField(byte style, char divider) {
this(style, divider, null);
}
/**
* Constructs a new <code>TimeField</code> object with the specified value.
*
* @param time the date value
* @see #setStyle
*/
public TimeField(byte style, char divider, Date time) {
super(-1, TextComponent.C_NUMERIC);
setStyle(style, divider, time);
}
/**
* Initialize time field composer
*
* @return <code>CustomFieldComposer</code> for time field
*/
protected CustomFieldComposer initComposer() {
TextBlockComposer[] blocks = null;
if (STYLE == HH_mm) {
blocks = new TextBlockComposer[2];
} else if (STYLE == hh_mmX || STYLE == HH_mm_ss) {
blocks = new TextBlockComposer[3];
} else {
blocks = new TextBlockComposer[4];
}
if (STYLE == hh_mm_ssX || STYLE == hh_mmX) {
blocks[0] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 2, ' ', 1, 12);
} else {
blocks[0] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 2, ' ', 0, 23);
}
blocks[1] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 2, ' ', 0, 59);
if (STYLE == HH_mm_ss || STYLE == hh_mm_ssX) {
blocks[2] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 2, ' ', 0, 59);
}
if (STYLE == hh_mm_ssX || STYLE == hh_mmX) {
blocks[blocks.length - 1] = new AMPMComposer();
}
return new CustomFieldComposer(blocks) {
public void setText(String text) {
}
};
}
protected char[] getFormattedText(boolean calcCursorOffset) {
TextBlockComposer blocks[] = ((CustomFieldComposer) composer).textBlocks;
DigitalBlockComposer hourComposer = (DigitalBlockComposer) blocks[0];
DigitalBlockComposer minComposer = (DigitalBlockComposer) blocks[1];
DigitalBlockComposer secComposer = null;
AMPMComposer ampmComposer = null;
if (STYLE == HH_mm_ss || STYLE == hh_mm_ssX) {
secComposer = (DigitalBlockComposer) blocks[2];
}
if (STYLE == hh_mmX || STYLE == hh_mm_ssX) {
ampmComposer = (AMPMComposer) blocks[blocks.length - 1];
}
StringBuffer buffer = new StringBuffer(10);
buffer.append(hourComposer.getText());
buffer.append(divider);
buffer.append(minComposer.getText());
if (STYLE == HH_mm_ss || STYLE == hh_mm_ssX) {
buffer.append(divider);
buffer.append(secComposer.getText());
}
if (STYLE == hh_mm_ssX || STYLE == hh_mmX) {
buffer.append(' ');
buffer.append(ampmComposer.getText());
}
char[] formattedText = buffer.toString().toCharArray();
if (calcCursorOffset) {
calculatedCaretPosition = composer.getCaretPosition() + ((CustomFieldComposer) composer).getCurrentBlock();
calculatedCursorOffset = getCursorOffset(formattedText, calculatedCaretPosition);
}
currentLength = formattedText.length;
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;
}
/**
* Sets <code>sec, min, hour, pm</code> variables
* If the field contains invalid value, the method sets
* variable <code>hour</code> to <code>-1</code>
*/
protected boolean getBlocks() {
TextBlockComposer blocks[] = ((CustomFieldComposer) composer).textBlocks;
DigitalBlockComposer hourComposer = (DigitalBlockComposer) blocks[0];
DigitalBlockComposer minComposer = (DigitalBlockComposer) blocks[1];
DigitalBlockComposer secComposer = null;
AMPMComposer ampmComposer = null;
if (STYLE == HH_mm_ss || STYLE == hh_mm_ssX) {
secComposer = (DigitalBlockComposer) blocks[2];
}
if (STYLE == hh_mmX || STYLE == hh_mm_ssX) {
ampmComposer = (AMPMComposer) blocks[blocks.length - 1];
}
sec = min = hour = 0;
pm = false;
try {
if (secComposer != null) {
sec = (int) secComposer.getValue();
}
min = (int) minComposer.getValue();
hour = (int) hourComposer.getValue();
if (ampmComposer != null) {
pm = PM.equals(ampmComposer.getText());
}
} catch (Exception e) {
hour = -1;
return false;
}
return true;
}
/**
* Returns the time value that is presented by this component.
* Date portion of returned <code>Date</code> value is set to 0 (year, month, day)
*/
public Date getTime() {
getBlocks();
if (hour < 0) return null;
if (pm) {
hour = hour + 12;
}
return new Date(((((hour * 60L) + min) * 60L) + sec) * 1000L);
}
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 newTime) {
if (newStyle < 1 || newStyle > 4) return;
Date time = newTime;
if (time==null) time = getTime();
STYLE = newStyle;
this.divider = divider;
if (composer != null) {
composer.destructor();
}
composer = initComposer();
setTime(time);
}
public byte getStyle() {
return STYLE;
}
/**
* Sets time that is presented by this time component to be the specified value.
* Date portion of <code>time</code> argument is ignored
*
* @param time the date value
*/
public void setTime(Date time) {
if (time == null) {
((CustomFieldComposer) composer).clear();
} else {
TextBlockComposer blocks[] = ((CustomFieldComposer) composer).textBlocks;
DigitalBlockComposer hourComposer = (DigitalBlockComposer) blocks[0];
DigitalBlockComposer minComposer = (DigitalBlockComposer) blocks[1];
DigitalBlockComposer secComposer = null;
AMPMComposer ampmComposer = null;
if (STYLE == HH_mm_ss || STYLE == hh_mm_ssX) {
secComposer = (DigitalBlockComposer) blocks[2];
}
if (STYLE == hh_mmX || STYLE == hh_mm_ssX) {
ampmComposer = (AMPMComposer) blocks[blocks.length - 1];
}
if (time == null) {
((CustomFieldComposer) composer).clear();
if (ampmComposer != null) {
ampmComposer.setText(AM);
}
} else {
int temp = (int) ((time.getTime() / 1000L) % (24L * 60L * 60L));
if (secComposer != null) {
int sec = temp % 60;
String secStr = ((sec < 10) ? "0" : "") + String.valueOf(sec);
secComposer.setText(secStr);
}
int min = (temp /= 60) % 60;
String minStr = ((min < 10) ? "0" : "") + String.valueOf(min);
minComposer.setText(minStr);
temp /= 60;
if (ampmComposer != null) {
String ampm = AM;
if ((temp == 0) || (temp > 12)) {
ampm = PM;
if ((temp -= 12) < 0) {
temp = 12;
}
}
ampmComposer.setText(ampm);
}
String hourStr = ((temp < 10) ? "0" : "") + String.valueOf(temp);
hourComposer.setText(hourStr);
}
}
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 #setTime(Date time) method instead.
*
* @deprecated
*/
public void setText(String text) {
}
/**
* Tests time field value for correctness
*
* @return <code>true</code> if the time field is valid
* <code>false</code> otherwise
*/
public boolean isValid() {
if (!((CustomFieldComposer) composer).isComplete()) return false;
getBlocks();
return hour >= 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -