1 package com.calipso.reportgenerator.userinterface; 2 3 import com.calipso.reportgenerator.common.ReportResult; 4 import com.calipso.reportgenerator.common.QueryMetric; 5 import com.calipso.reportgenerator.common.ReportMetricSpec; 6 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 7 import javax.swing.table.DefaultTableCellRenderer ; 8 import javax.swing.*; 9 import java.awt.*; 10 import java.text.NumberFormat ; 11 import java.text.DecimalFormat ; 12 13 16 public class DataTableCellRenderer extends DefaultTableCellRenderer { 17 private ColorConditionManager conditionManager; 18 private ReportResult reportResult; 19 private Color defaultColor; 20 21 27 public DataTableCellRenderer(ColorConditionManager conditionManager, ReportResult reportResult, Color background) { 28 this.conditionManager = conditionManager; 29 this.reportResult = reportResult; 30 this.defaultColor = background; 31 } 32 33 43 public Component getTableCellRendererComponent(JTable table, Object value, 44 boolean isSelected, boolean hasFocus, int row, int column) { 45 MetricInfo metricInfo = getMetricInfo(column); 46 String formattedValue = metricInfo.getFormattedValue((SharedFloat)value); 47 Component comp = super.getTableCellRendererComponent(table, formattedValue, isSelected, hasFocus, row, column); 49 if (!isSelected) { 50 Color color = getColorFromValue(value, metricInfo.getName()); 51 comp.setBackground(color); 52 } 53 return comp; 54 } 55 56 61 private MetricInfo getMetricInfo(int column){ 62 ReportMetricSpec reportMetricSpec; 63 int metricIndex = column % getMetricCount(); 64 QueryMetric metric = (QueryMetric) reportResult.getReportQuery().getVisibleMetrics().get(metricIndex); 65 reportMetricSpec = reportResult.getMetricFromName(metric.getName()); 66 return new MetricInfo(metric.getName()); 67 } 68 69 73 private int getMetricCount() { 74 return reportResult.getReportQuery().getVisibleMetrics().size(); 75 } 76 77 83 private Color getColorFromValue(Object value, String metricName) { 84 if (conditionManager != null) { 85 ColorCondition condition = conditionManager.getColor(value, metricName, defaultColor); 87 if (condition != null && condition.getMetricState().getVisible()) { 88 return condition.getColor(); 89 } 90 } 91 return Color.WHITE; 92 } 93 94 98 public void setColorConditionManager(ColorConditionManager colorConditionManager) { 99 this.conditionManager = colorConditionManager; 100 } 101 102 106 public void setReportResult(ReportResult reportResult) { 107 this.reportResult = reportResult; 108 } 109 110 113 private class MetricInfo { 114 115 private String name; 116 117 121 public MetricInfo(String name) { 122 this.name = name; 123 } 124 125 129 public String getName() { 130 return name; 131 } 132 133 137 public void setName(String name) { 138 this.name = name; 139 } 140 141 public String getFormattedValue(SharedFloat value){ 142 144 if ((value==null)||((value.getValue().equals(new Float (Float.NaN))))||((value.getValue().equals(new Float (Float.NEGATIVE_INFINITY))))||((value.getValue().equals(new Float (Float.POSITIVE_INFINITY))))){ 145 return ""; 146 }else{ 147 return new DecimalFormat ("###,###,###,####,##0.00").format(value.floatValue()); 148 149 } 150 } 151 } 152 } 153 | Popular Tags |