01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.components.table.renderer;
06:
07: import com.opensymphony.webwork.components.table.WebTable;
08:
09: import java.text.DecimalFormat;
10:
11: /**
12: * @author $author$
13: * @version $Revision: 1282 $
14: */
15: public class NumericCellRenderer extends AbstractCellRenderer {
16:
17: DecimalFormat _formater = new DecimalFormat();
18:
19: /**
20: * this is the format string that DecimalFormat would use.
21: *
22: * @see DecimalFormat
23: */
24: String _formatString = null;
25:
26: /**
27: * if set the is the color to use if Number is negative.
28: */
29: String _negativeColor = null;
30:
31: /**
32: * if set this is the color to render if number is positive
33: */
34: String _positiveColor = null;
35:
36: public NumericCellRenderer() {
37: super ();
38: }
39:
40: public String getCellValue(WebTable table, Object data, int row,
41: int col) {
42: StringBuffer retVal = new StringBuffer(128);
43:
44: if (data == null) {
45: return "";
46: }
47:
48: if (data instanceof Number) {
49: double cellValue = ((Number) data).doubleValue();
50:
51: if (cellValue >= 0) {
52: processNumber(retVal, _positiveColor, cellValue);
53: } else {
54: processNumber(retVal, _negativeColor, cellValue);
55: }
56:
57: return retVal.toString();
58: }
59:
60: return data.toString();
61: }
62:
63: public void setFormatString(String format) {
64: _formatString = format;
65: _formater.applyPattern(_formatString);
66: }
67:
68: public void setNegativeColor(String color) {
69: _negativeColor = color;
70: }
71:
72: public void setPositiveColor(String color) {
73: _positiveColor = color;
74: }
75:
76: protected void processNumber(StringBuffer buf, String color,
77: double cellValue) {
78: if (color != null) {
79: buf.append(" <font color='").append(color).append("'>");
80: }
81:
82: buf.append(_formater.format(cellValue));
83:
84: if (color != null) {
85: buf.append("</font>");
86: }
87: }
88: }
|