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: NumericTableCellRenderer.java,v $ 023 Revision 1.9 2004/05/12 18:54:05 markl 024 comment block updates 025 026 Revision 1.8 2004/03/18 07:04:09 markl 027 use "--" to represent unknown value, not "???" 028 029 Revision 1.7 2004/03/16 06:43:39 markl 030 LocaleManager method change 031 032 Revision 1.6 2003/01/19 09:50:53 markl 033 Javadoc & comment header updates. 034 035 Revision 1.5 2001/10/25 20:22:16 markl 036 Handle Long values, default on invalid formatter types. 037 038 Revision 1.4 2001/03/20 00:54:53 markl 039 Fixed deprecated calls. 040 041 Revision 1.3 2001/03/12 09:27:59 markl 042 Source code and Javadoc cleanup. 043 044 Revision 1.2 1999/08/01 06:32:38 markl 045 Typo fix. 046 047 Revision 1.1 1999/07/12 08:51:09 markl 048 Initial revision 049 ---------------------------------------------------------------------------- 050*/ 051 052package kiwi.ui; 053 054import javax.swing.*; 055import javax.swing.table.*; 056 057import kiwi.text.*; 058import kiwi.util.*; 059 060/** A table cell renderer for displaying numeric fields, including integer, 061 * decimal, percentage, and currency amounts, formatted according to the 062 * rules of the current locale. 063 * 064 * @author Mark Lindner 065 * 066 * @see kiwi.text.FormatConstants 067 * @see kiwi.util.LocaleManager 068 * @see kiwi.ui.NumericField 069 * @see kiwi.ui.NumericCellEditor 070 * @see kiwi.db.DomainObjectFieldAdapter 071 */ 072 073public class NumericTableCellRenderer extends DefaultTableCellRenderer 074 { 075 /** A string representation of the "unknown value"; a value that is either 076 * of the wrong type or for which there is no available format. 077 */ 078 public static final String VALUE_UNKNOWN = "--"; 079 080 private LocaleManager lm = LocaleManager.getDefault(); 081 private boolean grouping; 082 private int decimals, type; 083 084 /** Construct a new <code>NumericTableCellRenderer</code> of the specified 085 * type. 086 * 087 * @param type The data type to be rendered by this field; one of the 088 * constants <code>CURRENCY_FORMAT</code>, <code>DECIMAL_FORMAT</code>, 089 * <code>INTEGER_FORMAT</code> or <code>PERCENTAGE_FORMAT</code>, defined in 090 * <code>kiwi.text.FormatConstants</code>. 091 */ 092 093 public NumericTableCellRenderer(int type) 094 { 095 this(type, 2, true); 096 } 097 098 /** Construct a new <code>NumericTableCellRenderer</code> of the specified 099 * type, number of decimal places displayed, and grouping flag. 100 * 101 * @param type The data type to be rendered by this field; one of the 102 * constants <code>CURRENCY_FORMAT</code>, <code>DECIMAL_FORMAT</code>, 103 * <code>INTEGER_FORMAT</code< or <code>PERCENTAGE_FORMAT</code>, defined in 104 * <code>kiwi.text.FormatConstants</code>. 105 * @param decimals The number of decimal places to be displayed (for 106 * non-integer values only). 107 * @param grouping A flag specifying whether grouping should be turned on. 108 */ 109 110 public NumericTableCellRenderer(int type, int decimals, boolean grouping) 111 { 112 this.type = type; 113 this.decimals = decimals; 114 this.grouping = grouping; 115 116 setHorizontalAlignment(SwingConstants.RIGHT); 117 } 118 119 /** Set the formatting type. 120 * 121 * @param type The data type to be rendered by this cell renderer. See the 122 * constructor for more information. 123 */ 124 125 public void setType(int type) 126 { 127 this.type = type; 128 } 129 130 /** Get the formatting type. 131 * 132 * @return The data type being rendered by this cell renderer. 133 */ 134 135 public int getType() 136 { 137 return(type); 138 } 139 140 /** Set the number of decimal places to display for non-integer values. 141 * 142 * @param decimals The number of decimal places. 143 * @exception java.lang.IllegalArgumentException If <code>decimals</code> 144 * is less than 0. 145 */ 146 147 public void setDecimals(int decimals) throws IllegalArgumentException 148 { 149 if(decimals < 0) 150 throw(new IllegalArgumentException("decimals must be >= 0")); 151 152 this.decimals = decimals; 153 } 154 155 /** Get the number of decimal places being displayed by this cell renderer. 156 * 157 * @return The number of decimal places. 158 */ 159 160 public int getDecimals() 161 { 162 return(decimals); 163 } 164 165 /** Enable or disable grouping for this cell renderer. 166 * 167 * @param grouping A flag that specifies whether grouping should be turned on 168 * or off. 169 */ 170 171 public void setGrouping(boolean grouping) 172 { 173 this.grouping = grouping; 174 } 175 176 /** Determine whether this cell renderer is performing grouping. 177 * 178 * @return <code>true</code> if grouping is turned on and <code>false</code> 179 * otherwise. 180 */ 181 182 public boolean isGrouping() 183 { 184 return(grouping); 185 } 186 187 /** Set the value to be displayed by this cell renderer. It is assumed that 188 * the object passed in is a <code>Double</code> or <code>Long</code> 189 * instance; if any other type of object is passed in, the 190 * <code>VALUE_UNKNOWN</code> string will be rendered in the cell. 191 * 192 * @param value The value to render (must be a <code>Double</code> or 193 * <code>Long</code>). 194 */ 195 196 protected void setValue(Object value) 197 { 198 double val = 0.0; 199 String s = VALUE_UNKNOWN; 200 201 if(value.getClass() == Double.class) 202 { 203 val = ((Double)value).doubleValue(); 204 } 205 else if(value.getClass() == Long.class) 206 { 207 val = (double)((Long)value).longValue(); 208 } 209 else 210 { 211 setText(VALUE_UNKNOWN); 212 return; 213 } 214 215 switch(type) 216 { 217 case FormatConstants.CURRENCY_FORMAT: 218 s = lm.formatCurrency(val, decimals, grouping); 219 break; 220 221 case FormatConstants.INTEGER_FORMAT: 222 s = lm.formatInteger((long)val, grouping); 223 break; 224 225 case FormatConstants.PERCENTAGE_FORMAT: 226 s = lm.formatPercentage(val, decimals, grouping); 227 break; 228 229 case FormatConstants.DECIMAL_FORMAT: 230 default: 231 s = lm.formatDecimal(val, decimals, grouping); 232 break; 233 } 234 235 setText(s); 236 } 237 238 } 239 240/* end of source file */