View Javadoc

1   package baseCode.gui.table;
2   
3   import java.awt.Color;
4   import java.awt.Component;
5   import java.awt.Point;
6   import java.text.DecimalFormat;
7   
8   import javax.swing.JLabel;
9   import javax.swing.JTable;
10  import javax.swing.table.TableCellRenderer;
11  
12  import baseCode.gui.JMatrixDisplay;
13  
14  /***
15   * @author Will Braynen
16   * @version $Id: JMatrixCellRenderer.java,v 1.3 2004/07/27 03:18:58 pavlidis Exp $
17   */
18  public class JMatrixCellRenderer extends JLabel implements TableCellRenderer {
19  
20     JMatrixDisplay m_matrixDisplay;
21  
22     // to format tooltips
23     DecimalFormat m_scientificNotation = new DecimalFormat( "0.##E0" );
24     DecimalFormat m_regular = new DecimalFormat();
25  
26     public JMatrixCellRenderer( JMatrixDisplay matrixDisplay ) {
27  
28        m_matrixDisplay = matrixDisplay;
29        setOpaque( true );
30  
31        // for tooltips
32        m_regular.setMaximumFractionDigits( 3 );
33     }
34  
35     // This method is called each time a cell in a column
36     // using this renderer needs to be rendered.
37     public Component getTableCellRendererComponent( JTable table,
38           Object tableCellValue, boolean isSelected, boolean hasFocus,
39           int displayedRow, int displayedColumn ) {
40        // 'value' is value contained in the cell located at
41        // (rowIndex, vColIndex)
42  
43        if ( isSelected ) {
44           // cell (and perhaps other cells) are selected
45        }
46  
47        if ( hasFocus ) {
48           // this cell is the anchor and the table has the focus
49        }
50  
51        Point coords = ( Point ) tableCellValue;
52        int row = coords.x;
53        int column = coords.y;
54  
55        // Set the color
56        Color matrixColor;
57        try {
58           matrixColor = m_matrixDisplay.getColor( row, column );
59        } catch ( ArrayIndexOutOfBoundsException e ) {
60           matrixColor = m_matrixDisplay.getMissingColor();
61        }
62        setBackground( matrixColor );
63  
64        // The tooltip should always show the actual (non-normalized) value
65        double matrixValue;
66        boolean isStandardized = m_matrixDisplay.getStandardizedEnabled();
67        m_matrixDisplay.setStandardizedEnabled( false );
68        {
69           try {
70              matrixValue = m_matrixDisplay.getValue( row, column );
71           } catch ( ArrayIndexOutOfBoundsException e ) {
72              matrixValue = Double.NaN;
73           }
74        }
75        m_matrixDisplay.setStandardizedEnabled( isStandardized ); // return to
76        // previous
77        // state
78  
79        // Only very small and very large numbers should be displayed in
80        // scientific notation
81        String value;
82        if ( Math.abs( matrixValue ) < 0.01 || Math.abs( matrixValue ) > 100000 ) {
83           value = m_scientificNotation.format( matrixValue );
84        } else {
85           value = m_regular.format( matrixValue );
86        }
87        setToolTipText( value );
88  
89        // Since the renderer is a component, return itself
90        return this;
91     }
92  
93     // The following methods override the defaults for performance reasons
94     public void validate() {
95     }
96  
97     public void revalidate() {
98     }
99  
100    protected void firePropertyChange( String propertyName, Object oldValue,
101          Object newValue ) {
102    }
103 
104    public void firePropertyChange( String propertyName, boolean oldValue,
105          boolean newValue ) {
106    }
107 
108 } // end class MatrixDisplayCellRenderer