1 package baseCode.gui.table;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Graphics;
6 import java.awt.Rectangle;
7 import java.text.DecimalFormat;
8 import java.util.ArrayList;
9
10 import javax.swing.JLabel;
11 import javax.swing.JTable;
12 import javax.swing.UIManager;
13 import javax.swing.border.Border;
14 import javax.swing.border.EmptyBorder;
15 import javax.swing.table.TableCellRenderer;
16
17 /***
18 * @author Will Braynen
19 * @version $Id: JBarGraphCellRenderer.java,v 1.10 2005/03/21 18:01:04 pavlidis Exp $
20 */
21 public class JBarGraphCellRenderer extends JLabel implements TableCellRenderer {
22
23 protected Object m_values = null;
24 protected final static int LINE_WIDTH = 2;
25 protected final static Color[] COLORS = {
26 Color.BLUE, Color.GRAY, Color.RED, Color.GREEN, Color.CYAN,
27 Color.MAGENTA, Color.ORANGE
28 };
29 protected static Border m_noFocusBorder = new EmptyBorder( 1, 1, 1, 1 );
30 protected static Color m_selectionBackground;
31 protected boolean m_isSelected = false;
32 protected boolean m_isBarGraph = false;
33 DecimalFormat m_regular = new DecimalFormat();
34
35 public JBarGraphCellRenderer() {
36 super();
37 setOpaque( false );
38 setBorder( m_noFocusBorder );
39 }
40
41 /***
42 * This method is called each time a cell in a column using this renderer needs to be rendered.
43 *
44 * @param table the <code>JTable</code>
45 * @param value the value to assign to the cell at <code>[row, column]</code>
46 * @param isSelected true if cell is selected
47 * @param hasFocus true if cell has focus
48 * @param row the row of the cell to render
49 * @param column the column of the cell to render
50 * @return the default table cell renderer
51 */
52 public Component getTableCellRendererComponent( JTable table, Object value,
53 boolean isSelected, boolean hasFocus, int row, int column ) {
54
55 m_values = value;
56
57
58 m_isSelected = isSelected;
59 if ( isSelected ) {
60 super.setBackground( m_selectionBackground = table
61 .getSelectionBackground() );
62 } else {
63 super.setBackground( table.getBackground() );
64
65 }
66
67 if ( hasFocus ) {
68 setBorder( UIManager.getBorder( "Table.focusCellHighlightBorder" ) );
69 } else {
70 setBorder( m_noFocusBorder );
71 }
72
73 m_isBarGraph = false;
74 if ( value.getClass().equals( ArrayList.class ) ) {
75
76 m_isBarGraph = true;
77 m_values = value;
78
79
80
81
82
83
84
85
86
87
88
89 } else if ( value.getClass().equals( Double.class ) ) {
90
91 setText( value.toString() );
92 setFont( table.getFont() );
93 }
94
95
96 return this;
97 }
98
99 protected void paintBackground( Graphics g ) {
100 g.setColor( m_selectionBackground );
101 g.fillRect( 0, 0, getWidth(), getHeight() );
102 }
103
104 protected void paintComponent( Graphics g ) {
105
106 if ( m_isSelected ) {
107 paintBackground( g );
108 }
109
110 super.paintComponent( g );
111
112 if ( !m_isBarGraph ) return;
113 if ( m_values == null ) return;
114
115 final int width = getWidth();
116 final int height = getHeight();
117 final int y = 0;
118
119 ArrayList values = ( ArrayList ) m_values;
120
121 double maxPval = 10.0;
122
123 for ( int i = 0; i < values.size(); i++ ) {
124
125
126 double val = ( ( Double ) values.get( i ) ).doubleValue();
127 double logval = 0.0;
128
129 if ( val > 0 && val <= 1.0 ) {
130 logval = Math.min( maxPval, -Math.log( val ) / Math.log( 10 ) );
131 }
132
133 if ( !Double.isNaN( logval ) ) {
134
135
136 int x = ( int ) ( width * logval / maxPval );
137
138
139 if ( i < COLORS.length ) {
140 g.setColor( COLORS[i] );
141 } else {
142
143 g.setColor( Color.LIGHT_GRAY );
144 }
145
146
147 if ( x > width ) x = width - LINE_WIDTH;
148 g.fillRect( x, y, LINE_WIDTH, height );
149 }
150 }
151 }
152
153 public void validate() {
154 }
155
156 public void revalidate() {
157 }
158
159 public void repaint( long tm, int x, int y, int width, int height ) {
160 }
161
162 public void repaint( Rectangle r ) {
163 }
164
165 }