KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > userinterface > DataTableCellRenderer


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 JavaDoc;
8 import javax.swing.*;
9 import java.awt.*;
10 import java.text.NumberFormat JavaDoc;
11 import java.text.DecimalFormat JavaDoc;
12
13 /**
14  * Genera el Render para dibujar la tabla a partir de la informacion obtenida del colorConditionManager
15  */

16 public class DataTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
17   private ColorConditionManager conditionManager;
18   private ReportResult reportResult;
19   private Color defaultColor;
20
21   /**
22    * Crea un Objeto DataTableCellRender
23    * @param conditionManager
24    * @param reportResult
25    * @param background
26    */

27   public DataTableCellRenderer(ColorConditionManager conditionManager, ReportResult reportResult, Color background) {
28     this.conditionManager = conditionManager;
29     this.reportResult = reportResult;
30     this.defaultColor = background;
31   }
32
33   /**
34    * Retorna el componente para la JTable table para una columna fila determinada
35    * @param table
36    * @param value
37    * @param isSelected
38    * @param hasFocus
39    * @param row
40    * @param column
41    * @return
42    */

43   public Component getTableCellRendererComponent(JTable table, Object JavaDoc value,
44                                                  boolean isSelected, boolean hasFocus, int row, int column) {
45     MetricInfo metricInfo = getMetricInfo(column);
46     String JavaDoc formattedValue = metricInfo.getFormattedValue((SharedFloat)value);
47 // String formattedValue = value.toString();
48
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   /**
57    * Retorna un MetricInfo para una columna determinada
58    * @param column
59    * @return
60    */

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   /**
70    * Retorna la cantidad de metricas
71    * @return
72    */

73   private int getMetricCount() {
74     return reportResult.getReportQuery().getVisibleMetrics().size();
75   }
76
77   /**
78    *Retorna el color con que se pintará el componente
79    * @param value
80    * @param metricName
81    * @return
82    */

83   private Color getColorFromValue(Object JavaDoc value, String JavaDoc metricName) {
84     if (conditionManager != null) {
85       //conditionManager.getColor(value, metricName, defaultColor);
86
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   /**
95    * Setea el ColorConditionManager
96    * @param colorConditionManager
97    */

98   public void setColorConditionManager(ColorConditionManager colorConditionManager) {
99     this.conditionManager = colorConditionManager;
100   }
101
102   /**
103    * Setea el ReportResult
104    * @param reportResult
105    */

106   public void setReportResult(ReportResult reportResult) {
107     this.reportResult = reportResult;
108   }
109
110   /**
111    * Clase para identificar las propiedades de las metricas
112    */

113   private class MetricInfo {
114
115     private String JavaDoc name;
116
117     /**
118      * Crea un objeto MetricInfo
119      * @param name
120      */

121     public MetricInfo(String JavaDoc name) {
122       this.name = name;
123     }
124
125     /**
126      * Retorna el nombre de la metrica
127      * @return
128      */

129     public String JavaDoc getName() {
130       return name;
131     }
132
133     /**
134      * Setea el nombre de la metrica
135      * @param name
136      */

137     public void setName(String JavaDoc name) {
138       this.name = name;
139     }
140
141     public String JavaDoc getFormattedValue(SharedFloat value){
142       //return NumberFormat.getNumberInstance(com.calipso.reportgenerator.common.LanguageTraslator.getLocale()).format(value.floatValue());
143

144       if ((value==null)||((value.getValue().equals(new Float JavaDoc(Float.NaN))))||((value.getValue().equals(new Float JavaDoc(Float.NEGATIVE_INFINITY))))||((value.getValue().equals(new Float JavaDoc(Float.POSITIVE_INFINITY))))){
145         return "";
146       }else{
147         return new DecimalFormat JavaDoc("###,###,###,####,##0.00").format(value.floatValue());
148
149       }
150     }
151   }
152 }
153
Popular Tags