001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.lcdui;
028:
029: import java.util.Calendar;
030: import java.util.Date;
031: import java.util.TimeZone;
032:
033: import com.sun.midp.log.Logging;
034: import com.sun.midp.log.LogChannels;
035:
036: /**
037: * Look and feel implementation of <code>DateField</code> based on
038: * platform widget.
039: */
040: class DateFieldLFImpl extends ItemLFImpl implements DateFieldLF {
041:
042: /**
043: * Creates <code>DateFieldLF</code> for the passed in
044: * <code>DateField</code> object.
045: *
046: * @param dateField the <code>DateField</code> object associated with
047: this view
048: */
049: DateFieldLFImpl(DateField dateField) {
050: super (dateField);
051:
052: df = dateField;
053: }
054:
055: /**
056: * Notifies L&F of a date change in the corresponding
057: * <code>DateField</code>.
058: *
059: * @param date the new <code>Date</code> set in the
060: * <code>DateField</code>
061: */
062: public void lSetDate(java.util.Date date) {
063: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
064: Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI,
065: "(mdSetDate: id=" + nativeId + ")");
066: }
067:
068: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
069: if (date != null) {
070: setDate0(nativeId, date.getTime());
071: } else {
072: setDate0(nativeId, 0l);
073: }
074: }
075: }
076:
077: /**
078: * Setting date/time of this widget.
079: *
080: * @param nativeId native resource id of this <code>Item</code>
081: * @param date in seconds
082: */
083: private native void setDate0(int nativeId, long date);
084:
085: /**
086: * Notifies L&F of a new input mode set in the corresponding
087: * <code>DateField</code>.
088: *
089: * @param mode the new input mode set in the <code>DateField</code>.
090: */
091: public void lSetInputMode(int mode) {
092:
093: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
094: Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI,
095: "(mdSetInputMode: id=" + nativeId + ")");
096: }
097:
098: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
099: setInputMode0(nativeId, mode);
100: lRequestInvalidate(true, true);
101: }
102: }
103:
104: /**
105: * Set mode - time and or date.
106: *
107: * @param nativeId native resource id of this <code>Item</code>
108: * @param mode the new input mode set in the <code>DateField</code>.
109: */
110: private native void setInputMode0(int nativeId, int mode);
111:
112: /**
113: * Overrides <code>ItemLFImpl</code> to sync with
114: * native resource before hiding it
115: * Called by the system to hide this <code>Item</code>'s
116: * native resource
117: */
118: void lHideNativeResource() {
119:
120: if (!dateSynced) {
121: // sync java peer with native data before hiding
122: df.setDateImpl(lGetDate());
123: dateSynced = true;
124: }
125:
126: super .lHideNativeResource();
127: }
128:
129: /**
130: * Called by event delivery to notify an <code>ItemLF</code> in current
131: * <code>FormLF</code> of a change in its peer state.
132: *
133: * @param hint <code>1</code> if date,
134: * <code>2</code> if hour,
135: * <code>3</code> if minute changed
136: *
137: * @return always <code>true</code> to notify
138: * <code>ItemStateListener</code>
139: */
140: boolean uCallPeerStateChanged(int hint) {
141: // Any hint means user has changed some aspect of the date in native
142: // since native peer changed, java peer is no longer up to date.
143: dateSynced = false;
144:
145: // if the datefield is not initialized yet, initialize now.
146: if (!df.initialized) {
147: synchronized (Display.LCDUILock) {
148: lGetDate();
149: }
150: }
151: // Set flag so native resource will be queried for latest value
152: // Tell Form that ItemStateListener should be notified
153: return true;
154: }
155:
156: /**
157: * Gets the date currently set on the date field widget.
158: * This method is called by <code>Date</code> only if <code>Date</code>
159: * is initialized.
160: *
161: * @return the date this widget is currently set to
162: */
163: public Date lGetDate() {
164:
165: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
166: Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI,
167: "(mdGetDate: id=" + nativeId + ")");
168: }
169:
170: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
171:
172: long ld = getDate0(nativeId);
173:
174: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
175: Logging.report(Logging.INFORMATION,
176: LogChannels.LC_HIGHUI,
177: "(mdGetDate: returned long==" + ld + ")");
178: }
179: if (ld > 0) {
180: Date nd = new Date(ld);
181: df.setDateImpl(nd);
182: dateSynced = true;
183: return nd;
184: }
185: } else if (dateSynced && df.currentDate != null) {
186: return df.currentDate.getTime();
187: }
188:
189: return null;
190: }
191:
192: // *****************************************************
193: // Package private methods
194: // *****************************************************
195:
196: /**
197: * Determine if this <code>Item</code> should have a newline after it.
198: *
199: * @return <code>true</code> if it should have a newline after
200: */
201: boolean equateNLA() {
202: if (super .equateNLA()) {
203: return true;
204: }
205:
206: return ((df.layout & Item.LAYOUT_2) != Item.LAYOUT_2);
207: }
208:
209: /**
210: * Determine if this <code>Item</code> should have a newline before it.
211: *
212: * @return <code>true</code> if it should have a newline before
213: */
214: boolean equateNLB() {
215: if (super .equateNLB()) {
216: return true;
217: }
218:
219: return ((df.layout & Item.LAYOUT_2) != Item.LAYOUT_2);
220: }
221:
222: /**
223: * Gets the current value set on the native datefield.
224: *
225: * @param nativeId native resource id of this <code>Item</code>
226: *
227: * @return time in millis since 1970
228: */
229: private native long getDate0(int nativeId);
230:
231: /**
232: * Create native resource for current <code>DateField</code>.
233: * Override function in <code>ItemLFImpl</code>.
234: *
235: * @param ownerId Owner screen's native resource id
236: */
237: void createNativeResource(int ownerId) {
238: nativeId = createNativeResource0(ownerId, df.label, df.layout,
239: (df.getDate() != null ? df.currentDate.getTime()
240: .getTime() : 0), df.mode, df.currentDate
241: .getTimeZone().getID());
242:
243: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
244: Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI,
245: "(createNativeResource: id=" + nativeId + ")");
246: }
247: }
248:
249: /**
250: * KNI function that create native resource for current
251: * <code>DateField</code>.
252: *
253: * @param ownerId Owner screen's native resource id
254: * (<code>MidpDisplayable *</code>)
255: * @param label label sting of this <code>Item</code>
256: * @param layout layout directive associated with
257: * this <code>DateField</code>.
258: * @param datetime date in millis since Jan 1'st 1970
259: * @param displayMode date and or time mode
260: * @param timeZone label of timezone
261: *
262: * @return native resource id (<code>MidpItem *</code>) of this
263: * <code>Item</code>
264: */
265: private native int createNativeResource0(int ownerId, String label,
266: int layout, long datetime, int displayMode, String timeZone);
267:
268: /**
269: * <code>DateField</code> associated with this view.
270: */
271: private DateField df;
272:
273: /**
274: * Flag indicating if date value stored in java is up to date with native.
275: * When visible, the date is stored in the native widget.
276: * When hiding (and destroying) the native resource, we need to cache the
277: * value in the java peer for later use.
278: * dateSynced flag is a dirty bit used to check if a sync is needed
279: * when hiding.
280: */
281: private boolean dateSynced = true;
282: }
|