1 5 package com.opensymphony.webwork.components.table.renderer; 6 7 import com.opensymphony.webwork.components.table.WebTable; 8 9 import java.text.DecimalFormat ; 10 11 12 16 public class NumericCellRenderer extends AbstractCellRenderer { 17 19 DecimalFormat _formater = new DecimalFormat (); 20 21 26 String _formatString = null; 27 28 31 String _negativeColor = null; 32 33 36 String _positiveColor = null; 37 38 40 public NumericCellRenderer() { 41 super(); 42 } 43 44 46 public String getCellValue(WebTable table, Object data, int row, int col) { 47 StringBuffer retVal = new StringBuffer (128); 48 49 if (data == null) { 50 return ""; 51 } 52 53 if (data instanceof Number ) { 54 double cellValue = ((Number ) data).doubleValue(); 55 56 if (cellValue >= 0) { 57 processNumber(retVal, _positiveColor, cellValue); 58 } else { 59 processNumber(retVal, _negativeColor, cellValue); 60 } 61 62 return retVal.toString(); 63 } 64 65 return data.toString(); 66 } 67 68 public void setFormatString(String format) { 69 _formatString = format; 70 _formater.applyPattern(_formatString); 71 } 72 73 public void setNegativeColor(String color) { 74 _negativeColor = color; 75 } 76 77 public void setPositiveColor(String color) { 78 _positiveColor = color; 79 } 80 81 protected void processNumber(StringBuffer buf, String color, double cellValue) { 82 if (color != null) { 83 buf.append(" <font color='").append(color).append("'>"); 84 } 85 86 buf.append(_formater.format(cellValue)); 87 88 if (color != null) { 89 buf.append("</font>"); 90 } 91 } 92 } 93 | Popular Tags |