1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.ui.swing.components;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.text.Format;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
/**
* A cell renderer for dealing with formatted content. Subclasses can specify
* formats or colors or styles for specific values or locations in the table by
* overridding getFormatForContext(), getForegroundForContext() and/or
* getBackgroundForContext().
*
* @author michael@mpowers.net
* @version $Revision: 904 $ $Date: 2006-02-18 18:19:05 -0500 (Sat, 18 Feb 2006)
* $
*/
public class FormattedCellRenderer extends DefaultTableCellRenderer {
protected Format currentFormat, defaultFormat;
protected Color defaultForeground, defaultBackground;
protected Font defaultFont;
/**
* Default constructor with no specified format.
*/
public FormattedCellRenderer() {
this((Format) null);
}
/**
* Constructor specifying a format for renderered content.
*/
public FormattedCellRenderer(Format aFormat) {
currentFormat = null;
defaultFormat = aFormat;
defaultForeground = super.getForeground();
defaultBackground = super.getForeground();
}
/**
* Returns the format currently in use to format cell content.
*
* @return The Format that is currently being used.
*/
public Format getFormat() {
return defaultFormat;
}
/**
* Sets the format to be used to format cell content.
*/
public void setFormat(Format aFormat) {
defaultFormat = aFormat;
}
/**
* Overrides to retain the default foreground color, much the same as the
* DefaultCellRenderer does. We have to do this because DefaultCellRenderer's
* ivars are private.
*/
public void setForeground(Color c) {
super.setForeground(c);
defaultForeground = c;
}
/**
* Overrides to retain the default background color, much the same as the
* DefaultCellRenderer does. We have to do this because DefaultCellRenderer's
* ivars are private.
*/
public void setBackground(Color c) {
super.setBackground(c);
defaultBackground = c;
}
/**
* Overrides to retain the default font, much the same as the
* DefaultCellRenderer does. We have to do this because DefaultCellRenderer's
* ivars are private.
*/
public void setFont(Font f) {
super.setFont(f);
defaultFont = f;
}
/**
* Overridden to format the value with the appropriate Format. If the value
* cannot be formatted with the Format, the superclass method is called.
*
* @param value An Object to be formatted.
*/
protected void setValue(Object value) {
if (currentFormat != null) {
try {
// if ( ( value instanceof Number ) && ( value.toString().indexOf( "E" ) != -1 ) )
// {
// System.out.println( "FormattedCellRenderer.setValue: format = '" + currentFormat.getClass() + "'" );
// System.out.println( "FormattedCellRenderer.setValue: value = '" + value + "'" );
// System.out.println( "FormattedCellRenderer.setValue: double value = '" + ((Number)value).doubleValue() + "'" );
// System.out.println( "FormattedCellRenderer.setValue: float value = '" + ((Number)value).floatValue() + "'" );
// System.out.println( "FormattedCellRenderer.setValue: converted = '" + currentFormat.format( value ) + "'" );
// }
// WORKAROUND: This works around what may be a rounding bug in DecimalFormat.
// (PR 256/297)
currentFormat.format(ZERO);
// DEBUG: code to test for weird one/zero problem (PR 256/297)
String result = currentFormat.format(value);
/*
* above workaround seems to be working if ( result.equals( "1" ) ) {
* System.out.println(
* "FormattedCellRenderer.setValue: Could be the ONE/ZERO problem!" );
* System.out.println( "FormattedCellRenderer.setValue: format = '" +
* currentFormat.getClass() + "'" ); System.out.println(
* "FormattedCellRenderer.setValue: original value = '" + value + "'" );
* System.out.println( "FormattedCellRenderer.setValue: result = '" + result +
* "'" ); }
*/
setText(result);
// setText( currentFormat.format( value ) );
return;
} catch (IllegalArgumentException exc) {
// fall back on superclass implementation
}
}
super.setValue(value);
}
// FIXME: remove this when possible
private static Double ZERO = new Double(0.0);
/**
* Overridden to call context delegate methods.
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Format format;
// allow for context-sensitve formatting
format = getFormatForContext(table, value, isSelected, hasFocus, row, column);
if (format != null) {
currentFormat = format;
} else {
currentFormat = defaultFormat;
}
Color color;
// allow for context-sensitve foreground color
color = getForegroundForContext(table, value, isSelected, hasFocus, row, column);
if (color != null) {
super.setForeground(color);
} else {
super.setForeground(defaultForeground);
}
// allow for context-sensitve background color
color = getBackgroundForContext(table, value, isSelected, hasFocus, row, column);
if (color != null) {
super.setBackground(color);
} else {
super.setBackground(defaultBackground);
}
// have to call this here because super defaults to table's font
Component result = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// NOTE: DefaultTableCellRenderer returns itself.
// allow for context-sensitve font
Font font = getFontForContext(table, value, isSelected, hasFocus, row, column);
if (font != null) {
result.setFont(font);
} else {
result.setFont(defaultFont);
}
return result;
}
/**
* Override this method to provide a specific format for the specific cell to be
* rendered by this component. Any format returned by this method will take
* precedence of the format specified by setFormat(). <br>
* <br>
* This default implementation returns null.
*
* @return A Format for this cell, or null to rely on the the format specified
* by setFormat().
*/
public Format getFormatForContext(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
return null;
}
/**
* Override this method to provide a foreground color for the renderer. Because
* the table specifies colors for selected cells, these colors will only be used
* when renderering unselected cells. <br>
* <br>
* This default implementation returns null.
*
* @return A Color for the foreground of the cell, or null to rely on the
* table's default color scheme.
*/
public Color getForegroundForContext(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
return null;
}
/**
* Override this method to provide a background color for the renderer. Because
* the table specifies colors for selected cells, these colors will only be used
* when renderering unselected cells. <br>
* <br>
* This default implementation returns null.
*
* @return A Color for the background of the cell, or null to rely on the
* table's default color scheme.
*/
public Color getBackgroundForContext(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
return null;
}
/**
* Override this method to provide a font for the renderer.<br>
* <br>
* This default implementation returns null.
*
* @return A Font for the cell, or null to rely on the table's default font.
*/
public Font getFontForContext(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
return null;
}
}
|