📄 datefield.java
字号:
//#condition polish.usePolishGui
/*
* Copyright (c) 2004-2005 Robert Virkus / Enough Software
*
* This file is part of J2ME Polish.
*
* J2ME Polish is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* J2ME Polish is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with J2ME Polish; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Commercial licenses are also available, please
* refer to the accompanying LICENSE.txt or visit
* http://www.j2mepolish.org for details.
*/
package de.enough.polish.ui;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
//#if polish.blackberry
//# import net.rim.device.api.ui.Field;
//# import net.rim.device.api.ui.FieldChangeListener;
//# import net.rim.device.api.ui.UiApplication;
//# import net.rim.device.api.ui.XYRect;
//# import de.enough.polish.blackberry.ui.PolishDateField;
//#endif
import de.enough.polish.util.BitMapFontViewer;
import de.enough.polish.util.Locale;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* A <code>DateField</code> is an editable component for presenting
* date and time (calendar)
* information that may be placed into a <code>Form</code>. Value for
* this field can be
* initially set or left unset. If value is not set then the UI for the field
* shows this clearly. The field value for "not initialized
* state" is not valid
* value and <code>getDate()</code> for this state returns <code>null</code>.
* <p>
* Instance of a <code>DateField</code> can be configured to accept
* date or time information
* or both of them. This input mode configuration is done by
* <code>DATE</code>, <code>TIME</code> or
* <code>DATE_TIME</code> static fields of this
* class. <code>DATE</code> input mode allows to set only
* date information and <code>TIME</code> only time information
* (hours, minutes). <code>DATE_TIME</code>
* allows to set both clock time and date values.
* <p>
* In <code>TIME</code> input mode the date components of
* <code>Date</code> object
* must be set to the "zero epoch" value of January 1, 1970.
* <p>
* Calendar calculations in this field are based on default locale and defined
* time zone. Because of the calculations and different input modes date object
* may not contain same millisecond value when set to this field and get back
* from this field.
* <HR>
*
* @author Robert Virkus, robert@enough.de
* @since MIDP 1.0
*/
public class DateField extends StringItem
//#if polish.DateField.useDirectInput == true || polish.Bugs.dateFieldBroken || polish.blackberry
//#define tmp.directInput
//# implements ItemCommandListener
//#else
implements CommandListener
//#endif
//#if polish.blackberry
//# , FieldChangeListener
//#endif
{
/**
* Input mode for date information (day, month, year). With this mode this
* <code>DateField</code> presents and allows only to modify date
* value. The time
* information of date object is ignored.
*
* <P>Value <code>1</code> is assigned to <code>DATE</code>.</P>
*/
public static final int DATE = 1;
/**
* Input mode for time information (hours and minutes). With this mode this
* <code>DateField</code> presents and allows only to modify
* time. The date components
* should be set to the "zero epoch" value of January 1, 1970 and
* should not be accessed.
*
* <P>Value <code>2</code> is assigned to <code>TIME</code>.</P>
*/
public static final int TIME = 2;
/**
* Input mode for date (day, month, year) and time (minutes, hours)
* information. With this mode this <code>DateField</code>
* presents and allows to modify
* both time and date information.
*
* <P>Value <code>3</code> is assigned to <code>DATE_TIME</code>.</P>
*/
public static final int DATE_TIME = 3;
private Date date;
private int inputMode;
private TimeZone timeZone;
private boolean showCaret;
private int originalWidth;
private int originalHeight;
private long lastCaretSwitch;
private Calendar calendar;
//#if tmp.directInput
//# private static final int[] DAYS_IN_MONTHS = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//# private int editIndex;
//# private int textComplementColor;
//# private int currentField;
//# private int currentFieldStartIndex;
//# private ItemCommandListener additionalItemCommandListener;
//#if polish.css.font-bitmap
//# private int caretWidth;
//# private int caretX;
//# private int fontHeight;
//# private BitMapFontViewer caretViewer;
//#endif
//#else
private javax.microedition.lcdui.DateField midpDateField;
private javax.microedition.lcdui.Form form;
//#endif
//#if polish.blackberry
//# private PolishDateField blackberryDateField;
//#endif
/**
* Creates a <code>DateField</code> object with the specified
* label and mode. This call
* is identical to <code>DateField(label, mode, null)</code>.
*
* @param label item label
* @param mode the input mode, one of DATE, TIME or DATE_TIME
* @throws IllegalArgumentException if the input mode's value is invalid
*/
public DateField( String label, int mode)
{
this( label, mode, null, null );
}
/**
* Creates a <code>DateField</code> object with the specified
* label and mode. This call
* is identical to <code>DateField(label, mode, null)</code>.
*
* @param label item label
* @param mode the input mode, one of DATE, TIME or DATE_TIME
* @param style the CSS style for this item
* @throws IllegalArgumentException if the input mode's value is invalid
*/
public DateField( String label, int mode, Style style)
{
this( label, mode, null, style );
}
/**
* Creates a date field in which calendar calculations are based
* on specific
* <code>TimeZone</code> object and the default calendaring system for the
* current locale.
* The value of the <code>DateField</code> is initially in the
* "uninitialized" state.
* If <code>timeZone</code> is <code>null</code>, the system's
* default time zone is used.
*
* @param label item label
* @param mode the input mode, one of DATE, TIME or DATE_TIME
* @param timeZone a specific time zone, or null for the default time zone
* @throws IllegalArgumentException if the input mode's value is invalid
*/
public DateField( String label, int mode, TimeZone timeZone)
{
this( label, mode, timeZone, null );
}
/**
* Creates a date field in which calendar calculations are based
* on specific
* <code>TimeZone</code> object and the default calendaring system for the
* current locale.
* The value of the <code>DateField</code> is initially in the
* "uninitialized" state.
* If <code>timeZone</code> is <code>null</code>, the system's
* default time zone is used.
*
* @param label item label
* @param mode the input mode, one of DATE, TIME or DATE_TIME
* @param timeZone a specific time zone, or null for the default time zone
* @param style the CSS style for this item
* @throws IllegalArgumentException if the input mode's value is invalid
*/
public DateField( String label, int mode, TimeZone timeZone, Style style)
{
super( label, null, INTERACTIVE, style );
this.inputMode = mode;
if (timeZone != null) {
this.timeZone = timeZone;
} else {
this.timeZone = TimeZone.getDefault();
}
setDate( null );
//#if tmp.directInput
//#ifdef polish.i18n.useDynamicTranslations
//# String clearLabel = "Clear";
//# if ( clearLabel != TextField.CLEAR_CMD.getLabel()) {
//# TextField.CLEAR_CMD = new Command( "Clear", Command.ITEM, 2 );
//# }
//#endif
//# addCommand( TextField.CLEAR_CMD );
//# this.itemCommandListener = this;
//#endif
//#if polish.blackberry
//#
//# this.blackberryDateField = new PolishDateField(
//# System.currentTimeMillis(),
//# PolishDateField.getDateFormat( mode, this.timeZone ),
//# PolishDateField.EDITABLE );
//# this.blackberryDateField.setChangeListener( this );
//# this._bbField = this.blackberryDateField;
//#endif
}
/**
* Returns date value of this field. Returned value is
* <code>null</code> if field value is not initialized.
* The date object is constructed according the rules of
* locale specific calendaring system and defined time zone.
*
* In <code>TIME</code> mode field the date components are set to
* the "zero
* epoch" value of January 1, 1970. If a date object that presents time
* beyond one day from this "zero epoch" then this field
* is in "not initialized" state and this method returns <code>null</code>.
*
* In <code>DATE</code> mode field the time component of the calendar is set
* to zero when
* constructing the date object.
*
* @return date object representing time or date depending on input mode
* @see #setDate(java.util.Date)
*/
public Date getDate()
{
return this.date;
}
/**
* Sets a new value for this field. <code>null</code> can be
* passed to set the field
* state to "not initialized" state. The input mode of
* this field defines
* what components of passed <code>Date</code> object is used.<p>
*
* In <code>TIME</code> input mode the date components must be set
* to the "zero
* epoch" value of January 1, 1970. If a date object that presents time
* beyond one day then this field is in "not initialized" state.
* In <code>TIME</code> input mode the date component of
* <code>Date</code> object is ignored and time
* component is used to precision of minutes.<p>
*
* In <code>DATE</code> input mode the time component of
* <code>Date</code> object is ignored.<p>
*
* In <code>DATE_TIME</code> input mode the date and time
* component of <code>Date</code> are used but
* only to precision of minutes.
*
* @param date new value for this field
* @see #getDate()
*/
public void setDate( Date date)
{
//#if polish.blackberry
//# if (this.blackberryDateField != null && date != this.date ) {
//# Object bbLock = UiApplication.getEventLock();
//# synchronized (bbLock) {
//# if (date != null) {
//# this.blackberryDateField.setDate( date.getTime() );
//# } else {
//# this.blackberryDateField.setDate( System.currentTimeMillis() );
//# }
//# }
//# }
//#endif
if ( date != null && this.inputMode == TIME) {
// check if the year-month-day is set to zero (so that the date starts at 1970-01-01)
// 1 day =
// 1000 // == 1 sec
// * 60 // == 1 min
// * 60 // == 1 hour
// * 24 // == 1 day
// = 86.400.000 == 0x5265C00
if ( date.getTime() > 86400000 ) {
if (this.calendar == null) {
this.calendar = Calendar.getInstance();
this.calendar.setTimeZone(this.timeZone);
}
this.calendar.setTime(date);
long timeOnly = this.calendar.get( Calendar.MILLISECOND )
+ this.calendar.get( Calendar.SECOND ) * 1000
+ this.calendar.get( Calendar.MINUTE ) * 60 * 1000
+ this.calendar.get( Calendar.HOUR ) * 60 * 60 * 1000;
date.setTime(timeOnly);
//System.out.print("Adjusting date from " + this.calendar + " to " );
//this.calendar.setTime(date);
//System.out.println( this.calendar );
}
}
this.date = date;
//#if ! tmp.directInput
if (this.midpDateField != null) {
this.midpDateField.setDate( date );
}
//#endif
// set date:
if (date == null ) {
if (this.inputMode == DATE) {
//#if polish.DateFormatEmptyText:defined
this.text = "MM-DD-YYYY";
//#elif polish.DateFormat == us || polish.DateFormat == mdy
//# this.text = "MM-DD-YYYY";
//#elif polish.DateFormat == de
//# this.text = "TT.MM.JJJJ";
//#elif polish.DateFormat == fr
//# this.text = "JJ/MM/AAAA";
//#elif polish.DateFormat == dmy
//# this.text = "DD-MM-YYYY";
//#else
//# // default to ymd
//# this.text = "YYYY-MM-DD";
//#endif
} else if (this.inputMode == TIME) {
this.text = "hh:mm";
} else {
//#if polish.DateFormatEmptyText:defined
this.text = "MM-DD-YYYY hh:mm";
//#elif polish.DateFormat == us || polish.DateFormat == mdy
//# this.text = "MM-DD-YYYY hh:mm";
//#elif polish.DateFormat == de
//# this.text = "TT.MM.JJJJ hh:mm";
//#elif polish.DateFormat == fr
//# this.text = "JJ/MM/AAAA hh:mm";
//#elif polish.DateFormat == dmy
//# this.text = "DD-MM-YYYY hh:mm";
//#else
//# // default to ymd
//# this.text = "YYYY-MM-DD hh:mm";
//#endif
}
} else {
if (this.calendar == null) {
this.calendar = Calendar.getInstance();
this.calendar.setTimeZone(this.timeZone);
}
this.calendar.setTime(date);
StringBuffer buffer = new StringBuffer(10);
if ((this.inputMode == DATE) || (this.inputMode == DATE_TIME)) {
int year = this.calendar.get(Calendar.YEAR);
int month = this.calendar.get( Calendar.MONTH );
int day = this.calendar.get( Calendar.DAY_OF_MONTH );
//#if polish.DateFormat == us
//# if (month < 9) {
//# buffer.append('0');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -