DateTimeRenderer.java
上传用户:haobig99
上传日期:2022-06-15
资源大小:369k
文件大小:5k
- /*
- * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation. Sun designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Sun in the LICENSE file that accompanied this code.
- *
- * This code 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
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
- */
- package com.sun.lwuit.spinner;
- import com.sun.lwuit.Component;
- import com.sun.lwuit.List;
- import com.sun.lwuit.list.DefaultListCellRenderer;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * A renderer that can represent values for Date and time, time is represented as an integer
- * for seconds since midnight. This is formatted accordingly by the renderer
- *
- * @author Shai Almog
- */
- class DateTimeRenderer extends DefaultListCellRenderer {
- private boolean date;
- private int type;
- private char separatorChar;
- private boolean twentyFourHours;
- private boolean showSeconds;
- private DateTimeRenderer() {
- super(false);
- }
- boolean isShowSeconds() {
- return showSeconds;
- }
- /**
- * Construct a time renderer
- *
- * @param twentyFourHours show the value as 24 hour values or AM/PM
- * @param showSeconds show the value of the seconds as well or hide it
- */
- public static DateTimeRenderer createTimeRenderer(boolean twentyFourHours, boolean showSeconds) {
- DateTimeRenderer d = new DateTimeRenderer();
- d.twentyFourHours = twentyFourHours;
- d.showSeconds = showSeconds;
- return d;
- }
- /**
- * Constructs a date renderer
- *
- * @param separatorChar char separating the entries within the renderer such as /, - etc.
- * @param format the date, one of the constant values in this class
- */
- public static DateTimeRenderer createDateRenderer(char separatorChar, int format) {
- DateTimeRenderer d = new DateTimeRenderer();
- d.date = true;
- d.separatorChar = separatorChar;
- d.type = format;
- return d;
- }
- private String twoDigits(int i) {
- if(i < 10) {
- return "0" + i;
- }
- return "" + i;
- }
- /**
- * @inheritDoc
- */
- public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
- if(date) {
- Calendar c = Calendar.getInstance();
- c.setTime((Date)value);
- int day = c.get(Calendar.DAY_OF_MONTH);
- int month = c.get(Calendar.MONTH) + 1;
- int year = c.get(Calendar.YEAR);
- switch(type) {
- case Spinner.DATE_FORMAT_DD_MM_YYYY:
- value = twoDigits(day) + separatorChar + twoDigits(month) + separatorChar + year;
- break;
- case Spinner.DATE_FORMAT_MM_DD_YYYY:
- value = twoDigits(month) + separatorChar + twoDigits(day) + separatorChar + year;
- break;
- case Spinner.DATE_FORMAT_DD_MM_YY:
- value = twoDigits(day) + separatorChar + twoDigits(month) + separatorChar + (year % 100);
- break;
- case Spinner.DATE_FORMAT_MM_DD_YY:
- value = twoDigits(month) + separatorChar + twoDigits(day) + separatorChar + (year % 100);
- break;
- }
- } else {
- int v = ((Integer)value).intValue();
- int seconds = v % 60;
- v /= 60;
- int minutes = v % 60;
- v /= 60;
- int hours;
- if(twentyFourHours) {
- hours = v % 24;
- } else {
- hours = v % 12;
- }
- if(showSeconds) {
- value = twoDigits(hours) + ":" + twoDigits(minutes) + ":" + twoDigits(seconds);
- } else {
- value = twoDigits(hours) + ":" + twoDigits(minutes);
- }
- }
- return super.getListCellRendererComponent(list, value, index, isSelected);
- }
- /**
- * @return the twentyFourHours
- */
- public boolean isTwentyFourHours() {
- return twentyFourHours;
- }
- }