KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > gui > MeasurementTableCellRenderer


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

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 JavaDoc 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 JavaDoc 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 JavaDoc text) {
146         StringBuffer JavaDoc tooltip = new StringBuffer JavaDoc();
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