001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.gui;
034:
035: import java.awt.*;
036:
037: import javax.swing.*;
038: import javax.swing.table.*;
039:
040: import com.jeantessier.metrics.*;
041:
042: public class MeasurementTableCellRenderer extends
043: DefaultTableCellRenderer {
044: private static final Color PRIMARY_NORMAL_BACKGROUND = new Color(
045: 247, 247, 247);
046: private static final Color SECONDARY_NORMAL_BACKGROUND = new Color(
047: 223, 223, 223);
048: private static final Color NORMAL_FOREGROUND = Color.black;
049:
050: private static final Color PRIMARY_HIGHLIGHTED_BACKGROUND = new Color(
051: 255, 223, 223);
052: private static final Color SECONDARY_HIGHLIGHTED_BACKGROUND = new Color(
053: 255, 207, 207);
054: private static final Color HIGHLIGHTED_FOREGROUND = Color.red;
055:
056: public Component getTableCellRendererComponent(JTable table,
057: Object value, boolean isSelected, boolean hasFocus,
058: int row, int column) {
059: JLabel result = (JLabel) super .getTableCellRendererComponent(
060: table, value, isSelected, hasFocus, row, column);
061:
062: if (column == 0) {
063: result.setHorizontalAlignment(JLabel.LEFT);
064: } else {
065: result.setHorizontalAlignment(JLabel.CENTER);
066: }
067:
068: if (value instanceof Measurement) {
069: Measurement measurement = (Measurement) value;
070: if (measurement.isInRange()) {
071: formatAsNormalCell(isSelected, row, result);
072: } else {
073: formatAsHighlightedCell(isSelected, row, result);
074: }
075:
076: String text;
077: int dispose = ((OOMetricsTableModel) table.getModel())
078: .getRawColumnDispose(column);
079:
080: if (measurement instanceof StatisticalMeasurement) {
081: StatisticalMeasurement stat = (StatisticalMeasurement) measurement;
082: switch (dispose) {
083: case StatisticalMeasurement.DISPOSE_MINIMUM:
084: text = String.valueOf(stat.getMinimum());
085: break;
086: case StatisticalMeasurement.DISPOSE_MEDIAN:
087: text = String.valueOf(stat.getMedian());
088: break;
089: case StatisticalMeasurement.DISPOSE_AVERAGE:
090: text = String.valueOf(stat.getAverage());
091: break;
092: case StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION:
093: text = String.valueOf(stat.getStandardDeviation());
094: break;
095: case StatisticalMeasurement.DISPOSE_MAXIMUM:
096: text = String.valueOf(stat.getMaximum());
097: break;
098: case StatisticalMeasurement.DISPOSE_SUM:
099: text = String.valueOf(stat.getSum());
100: break;
101: case StatisticalMeasurement.DISPOSE_IGNORE:
102: case StatisticalMeasurement.DISPOSE_NB_DATA_POINTS:
103: default:
104: text = "n/a";
105: break;
106: }
107: } else {
108: text = measurement.getValue().toString();
109: }
110:
111: setCellContent(result, measurement, dispose, text);
112: } else if (value instanceof Metrics) {
113: Metrics metrics = (Metrics) value;
114:
115: if (metrics.isInRange()) {
116: formatAsNormalCell(isSelected, row, result);
117: } else {
118: formatAsHighlightedCell(isSelected, row, result);
119: }
120:
121: result.setText(metrics.getName());
122: result.setToolTipText(metrics.getName());
123: } else {
124: formatAsNormalCell(isSelected, row, result);
125: }
126:
127: return result;
128: }
129:
130: private void formatAsNormalCell(boolean isSelected, int row,
131: JLabel result) {
132: result.setForeground(NORMAL_FOREGROUND);
133:
134: if (!isSelected) {
135: if (((row / 3) % 2) == 0) {
136: result.setBackground(PRIMARY_NORMAL_BACKGROUND);
137: } else {
138: result.setBackground(SECONDARY_NORMAL_BACKGROUND);
139: }
140: }
141: }
142:
143: private void formatAsHighlightedCell(boolean isSelected, int row,
144: JLabel result) {
145: result.setForeground(HIGHLIGHTED_FOREGROUND);
146:
147: if (!isSelected) {
148: if (((row / 3) % 2) == 0) {
149: result.setBackground(PRIMARY_HIGHLIGHTED_BACKGROUND);
150: } else {
151: result.setBackground(SECONDARY_HIGHLIGHTED_BACKGROUND);
152: }
153: }
154: }
155:
156: private void setCellContent(JLabel result, Measurement measurement,
157: int dispose, String text) {
158: StringBuffer tooltip = new StringBuffer();
159: tooltip.append("<html><body><p>");
160: tooltip.append("<b>")
161: .append(measurement.getContext().getName()).append(
162: "</b><br>");
163: tooltip.append(measurement.getLongName()).append(" (").append(
164: measurement.getShortName()).append(")");
165: if (measurement instanceof StatisticalMeasurement) {
166: tooltip.append(", ").append(
167: StatisticalMeasurement.getDisposeLabel(dispose));
168: }
169: tooltip.append("<br>");
170: tooltip.append("valid range: ").append(
171: measurement.getDescriptor().getRangeAsString()).append(
172: "<br>");
173: tooltip.append("value: ").append(text);
174: if (!measurement.isInRange()) {
175: tooltip.append(" <b>*** out of range ***</b>");
176: }
177: if (measurement instanceof StatisticalMeasurement) {
178: tooltip.append("<br>").append(measurement);
179: }
180: tooltip.append("</p></body></html>");
181:
182: result.setText(text);
183: result.setToolTipText(tooltip.toString());
184: }
185: }
|