1 32 33 package com.jeantessier.dependencyfinder.gui; 34 35 import java.awt.*; 36 37 import javax.swing.*; 38 import javax.swing.table.*; 39 40 import com.jeantessier.metrics.*; 41 42 public class MeasurementTableCellRenderer extends DefaultTableCellRenderer { 43 private static final Color PRIMARY_NORMAL_BACKGROUND = new Color(247, 247, 247); 44 private static final Color SECONDARY_NORMAL_BACKGROUND = new Color(223, 223, 223); 45 private static final Color NORMAL_FOREGROUND = Color.black; 46 47 private static final Color PRIMARY_HIGHLIGHTED_BACKGROUND = new Color(255, 223, 223); 48 private static final Color SECONDARY_HIGHLIGHTED_BACKGROUND = new Color(255, 207, 207); 49 private static final Color HIGHLIGHTED_FOREGROUND = Color.red; 50 51 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 52 JLabel result = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 53 54 if (column == 0) { 55 result.setHorizontalAlignment(JLabel.LEFT); 56 } else { 57 result.setHorizontalAlignment(JLabel.CENTER); 58 } 59 60 if (value instanceof Measurement) { 61 Measurement measurement = (Measurement) value; 62 if (measurement.isInRange()) { 63 formatAsNormalCell(isSelected, row, result); 64 } else { 65 formatAsHighlightedCell(isSelected, row, result); 66 } 67 68 String text = ""; 69 int dispose = ((OOMetricsTableModel) table.getModel()).getRawColumnDispose(column); 70 71 if (measurement instanceof StatisticalMeasurement) { 72 StatisticalMeasurement stat = (StatisticalMeasurement) measurement; 73 switch (dispose) { 74 case StatisticalMeasurement.DISPOSE_MINIMUM: 75 text = String.valueOf(stat.getMinimum()); 76 break; 77 case StatisticalMeasurement.DISPOSE_MEDIAN: 78 text = String.valueOf(stat.getMedian()); 79 break; 80 case StatisticalMeasurement.DISPOSE_AVERAGE: 81 text = String.valueOf(stat.getAverage()); 82 break; 83 case StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION: 84 text = String.valueOf(stat.getStandardDeviation()); 85 break; 86 case StatisticalMeasurement.DISPOSE_MAXIMUM: 87 text = String.valueOf(stat.getMaximum()); 88 break; 89 case StatisticalMeasurement.DISPOSE_SUM: 90 text = String.valueOf(stat.getSum()); 91 break; 92 case StatisticalMeasurement.DISPOSE_IGNORE: 93 case StatisticalMeasurement.DISPOSE_NB_DATA_POINTS: 94 default: 95 text = "n/a"; 96 break; 97 } 98 } else { 99 text = measurement.getValue().toString(); 100 } 101 102 setCellContent(result, measurement, dispose, text); 103 } else if (value instanceof Metrics) { 104 Metrics metrics = (Metrics) value; 105 106 if (metrics.isInRange()) { 107 formatAsNormalCell(isSelected, row, result); 108 } else { 109 formatAsHighlightedCell(isSelected, row, result); 110 } 111 112 result.setText(metrics.getName()); 113 result.setToolTipText(metrics.getName()); 114 } else { 115 formatAsNormalCell(isSelected, row, result); 116 } 117 118 return result; 119 } 120 121 private void formatAsNormalCell(boolean isSelected, int row, JLabel result) { 122 result.setForeground(NORMAL_FOREGROUND); 123 124 if (!isSelected) { 125 if (((row / 3) % 2) == 0) { 126 result.setBackground(PRIMARY_NORMAL_BACKGROUND); 127 } else { 128 result.setBackground(SECONDARY_NORMAL_BACKGROUND); 129 } 130 } 131 } 132 133 private void formatAsHighlightedCell(boolean isSelected, int row, JLabel result) { 134 result.setForeground(HIGHLIGHTED_FOREGROUND); 135 136 if (!isSelected) { 137 if (((row / 3) % 2) == 0) { 138 result.setBackground(PRIMARY_HIGHLIGHTED_BACKGROUND); 139 } else { 140 result.setBackground(SECONDARY_HIGHLIGHTED_BACKGROUND); 141 } 142 } 143 } 144 145 private void setCellContent(JLabel result, Measurement measurement, int dispose, String text) { 146 StringBuffer tooltip = new StringBuffer (); 147 tooltip.append("<html><body><p>"); 148 tooltip.append("<b>").append(measurement.getContext().getName()).append("</b><br>"); 149 tooltip.append(measurement.getLongName()).append(" (").append(measurement.getShortName()).append(")"); 150 if (measurement instanceof StatisticalMeasurement) { 151 tooltip.append(", ").append(StatisticalMeasurement.getDisposeLabel(dispose)); 152 } 153 tooltip.append("<br>"); 154 tooltip.append("valid range: ").append(measurement.getDescriptor().getRangeAsString()).append("<br>"); 155 tooltip.append("value: ").append(text); 156 if (!measurement.isInRange()) { 157 tooltip.append(" <b>*** out of range ***</b>"); 158 } 159 if (measurement instanceof StatisticalMeasurement) { 160 tooltip.append("<br>").append(measurement); 161 } 162 tooltip.append("</p></body></html>"); 163 164 result.setText(text); 165 result.setToolTipText(tooltip.toString()); 166 } 167 } 168 | Popular Tags |