001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: DateTableCellRenderer.java,v $ 023 Revision 1.7 2004/05/12 19:13:58 markl 024 comment block updates 025 026 Revision 1.6 2004/03/18 07:04:09 markl 027 use "--" to represent unknown value, not "???" 028 029 Revision 1.5 2004/03/16 06:43:39 markl 030 LocaleManager method change 031 032 Revision 1.4 2004/01/22 23:55:31 markl 033 documentation corrections 034 035 Revision 1.3 2003/01/19 09:50:53 markl 036 Javadoc & comment header updates. 037 038 Revision 1.2 2002/08/11 10:09:55 markl 039 Removed deprecated call. 040 041 Revision 1.1 2002/08/11 09:51:59 markl 042 Readded to package. 043 044 Revision 1.2 2001/09/26 07:05:36 markl 045 Version fixes, email address fixes. 046 047 Revision 1.1 2000/08/07 08:24:54 markl 048 Import into CVS. 049 ---------------------------------------------------------------------------- 050*/ 051 052package kiwi.ui; 053 054import javax.swing.*; 055import javax.swing.table.*; 056import java.util.*; 057 058import kiwi.text.*; 059import kiwi.util.*; 060 061/** A table cell renderer for displaying dates and/or times, formatted 062 * according to the rules of the current locale. 063 * 064 * @author Mark Lindner 065 * 066 * @see kiwi.text.FormatConstants 067 * @see kiwi.util.LocaleManager 068 */ 069 070public class DateTableCellRenderer extends DefaultTableCellRenderer 071 { 072 /** A string representation of the "unknown value"; a value that is either 073 * of the wrong type or for which there is no available format. 074 */ 075 076 public static final String VALUE_UNKNOWN = "--"; 077 078 private LocaleManager lm = LocaleManager.getDefault(); 079 private int type; 080 081 /** Construct a new <code>DateTableCellRenderer</code> for dates. 082 */ 083 084 public DateTableCellRenderer() 085 { 086 this(FormatConstants.DATE_FORMAT); 087 } 088 089 /** Construct a new <code>DateTableCellRenderer</code> of the specified 090 * type. 091 * 092 * @param type The fromatting type to be used by this field; one of the 093 * constants <code>DATE_FORMAT</code>, <code>TIME_FORMAT</code>, or 094 * <code>DATE_TIME_FORMAT</code>, defined in 095 * <code>kiwi.text.FormatConstants</code>. 096 */ 097 098 public DateTableCellRenderer(int type) 099 { 100 this.type = type; 101 } 102 103 /** Set the formatting type. 104 * 105 * @param type The data type to be rendered by this cell renderer. See the 106 * constructor for more information. 107 */ 108 109 public void setType(int type) 110 { 111 this.type = type; 112 } 113 114 /** Get the formatting type. 115 * 116 * @return The data type being rendered by this cell renderer. 117 */ 118 119 public int getType() 120 { 121 return(type); 122 } 123 124 /** Set the value to be displayed by this cell renderer. It is 125 * assumed that the object passed in is an instance of 126 * <code>Date</code>, <code>Calendar</code>, or 127 * <code>DateHolder</code>; if any other type of object is passed 128 * in, or if the rendering type is not recognized, the 129 * <code>VALUE_UNKNOWN</code> string will be rendered in the cell. 130 * 131 * @param value The value to render (must be a <code>Double</code>). 132 */ 133 134 protected void setValue(Object value) 135 { 136 Date d = null; 137 138 if(value instanceof Date) 139 d = (Date)value; 140 else if(value instanceof Calendar) 141 d = ((Calendar)value).getTime(); 142 else if(value instanceof DateHolder) 143 d = ((DateHolder)value).getValue(); 144 145 String s = VALUE_UNKNOWN; 146 if(d != null) 147 { 148 switch(type) 149 { 150 case FormatConstants.DATE_FORMAT: 151 s = lm.formatDate(d); 152 break; 153 154 case FormatConstants.TIME_FORMAT: 155 s = lm.formatTime(d); 156 break; 157 158 case FormatConstants.DATE_TIME_FORMAT: 159 s = lm.formatDateTime(d); 160 break; 161 } 162 } 163 164 setText(s); 165 } 166 167 } 168 169/* end of source file */