|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| JBarGraphCellRenderer.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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 | 0 |
public JBarGraphCellRenderer() {
|
| 36 | 0 |
super();
|
| 37 | 0 |
setOpaque( false );
|
| 38 | 0 |
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 | 0 |
public Component getTableCellRendererComponent( JTable table, Object value,
|
| 53 |
boolean isSelected, boolean hasFocus, int row, int column ) { |
|
| 54 |
|
|
| 55 | 0 |
m_values = value; |
| 56 |
|
|
| 57 |
// set background
|
|
| 58 | 0 |
m_isSelected = isSelected; |
| 59 | 0 |
if ( isSelected ) {
|
| 60 | 0 |
super.setBackground( m_selectionBackground = table
|
| 61 |
.getSelectionBackground() ); |
|
| 62 |
} else {
|
|
| 63 | 0 |
super.setBackground( table.getBackground() );
|
| 64 |
// or force a white background instead
|
|
| 65 |
} |
|
| 66 |
|
|
| 67 | 0 |
if ( hasFocus ) {
|
| 68 | 0 |
setBorder( UIManager.getBorder( "Table.focusCellHighlightBorder" ) );
|
| 69 |
} else {
|
|
| 70 | 0 |
setBorder( m_noFocusBorder ); |
| 71 |
} |
|
| 72 |
|
|
| 73 | 0 |
m_isBarGraph = false;
|
| 74 | 0 |
if ( value.getClass().equals( ArrayList.class ) ) { |
| 75 |
// bar graph
|
|
| 76 | 0 |
m_isBarGraph = true;
|
| 77 | 0 |
m_values = value; |
| 78 |
|
|
| 79 |
// Double x = new Double( ( ( Double ) ( ( ArrayList ) value ).get( 0 ) )
|
|
| 80 |
// .doubleValue() );
|
|
| 81 |
|
|
| 82 |
//
|
|
| 83 |
// x = new Double( m_regular.format( -Math
|
|
| 84 |
// .log( ( ( Double ) ( ( ArrayList ) value ).get( 0 ) )
|
|
| 85 |
// .doubleValue() )
|
|
| 86 |
// / Math.log( 10 ) ) );
|
|
| 87 |
|
|
| 88 |
// setToolTipText( x.toString() );
|
|
| 89 | 0 |
} else if ( value.getClass().equals( Double.class ) ) { |
| 90 |
// just double value, no bar graph
|
|
| 91 | 0 |
setText( value.toString() ); |
| 92 | 0 |
setFont( table.getFont() ); |
| 93 |
} |
|
| 94 |
|
|
| 95 |
// Since the renderer is a component, return itself
|
|
| 96 | 0 |
return this; |
| 97 |
} |
|
| 98 |
|
|
| 99 | 0 |
protected void paintBackground( Graphics g ) { |
| 100 | 0 |
g.setColor( m_selectionBackground ); |
| 101 | 0 |
g.fillRect( 0, 0, getWidth(), getHeight() ); |
| 102 |
} |
|
| 103 |
|
|
| 104 | 0 |
protected void paintComponent( Graphics g ) { |
| 105 |
|
|
| 106 | 0 |
if ( m_isSelected ) {
|
| 107 | 0 |
paintBackground( g ); |
| 108 |
} |
|
| 109 |
|
|
| 110 | 0 |
super.paintComponent( g );
|
| 111 |
|
|
| 112 | 0 |
if ( !m_isBarGraph ) return; |
| 113 | 0 |
if ( m_values == null ) return; |
| 114 |
|
|
| 115 | 0 |
final int width = getWidth();
|
| 116 | 0 |
final int height = getHeight();
|
| 117 | 0 |
final int y = 0;
|
| 118 |
|
|
| 119 | 0 |
ArrayList values = ( ArrayList ) m_values; |
| 120 |
|
|
| 121 | 0 |
double maxPval = 10.0;
|
| 122 |
|
|
| 123 | 0 |
for ( int i = 0; i < values.size(); i++ ) { |
| 124 |
|
|
| 125 |
// @todo only use log if doLog is requested. probably log should be in genesettablemodel
|
|
| 126 | 0 |
double val = ( ( Double ) values.get( i ) ).doubleValue();
|
| 127 | 0 |
double logval = 0.0;
|
| 128 |
|
|
| 129 | 0 |
if ( val > 0 && val <= 1.0 ) {
|
| 130 | 0 |
logval = Math.min( maxPval, -Math.log( val ) / Math.log( 10 ) ); |
| 131 |
} |
|
| 132 |
|
|
| 133 | 0 |
if ( !Double.isNaN( logval ) ) {
|
| 134 |
// map from [0,1] range to [0,width] range
|
|
| 135 |
// int x = ( int ) ( value * width );
|
|
| 136 | 0 |
int x = ( int ) ( width * logval / maxPval ); |
| 137 |
|
|
| 138 |
// what color to use?
|
|
| 139 | 0 |
if ( i < COLORS.length ) {
|
| 140 | 0 |
g.setColor( COLORS[i] ); |
| 141 |
} else {
|
|
| 142 |
// ran out of colors!
|
|
| 143 | 0 |
g.setColor( Color.LIGHT_GRAY ); |
| 144 |
} |
|
| 145 |
|
|
| 146 |
// draw the vertical bar line
|
|
| 147 | 0 |
if ( x > width ) x = width - LINE_WIDTH;
|
| 148 | 0 |
g.fillRect( x, y, LINE_WIDTH, height ); |
| 149 |
} |
|
| 150 |
} |
|
| 151 |
} // end paintComponent
|
|
| 152 |
|
|
| 153 | 0 |
public void validate() { |
| 154 |
} |
|
| 155 |
|
|
| 156 | 0 |
public void revalidate() { |
| 157 |
} |
|
| 158 |
|
|
| 159 | 0 |
public void repaint( long tm, int x, int y, int width, int height ) { |
| 160 |
} |
|
| 161 |
|
|
| 162 | 0 |
public void repaint( Rectangle r ) { |
| 163 |
} |
|
| 164 |
|
|
| 165 |
} // end class
|
|
| 166 |
|
|
||||||||||