Back to index

wims  3.65+svn20090927
SharpCellRenderer.java
Go to the documentation of this file.
00001 /*
00002  * SharpCellRenderer.java
00003  *
00004  * $Id: SharpCellRenderer.java,v 1.9 2001/06/03 22:21:28 huaz Exp $
00005  *
00006  * Created on October 26, 2000, 9:23 PM
00007 */
00008 package SharpTools;
00009 import javax.swing.*;
00010 import javax.swing.*;
00011 import javax.swing.table.DefaultTableCellRenderer;
00012 import javax.swing.border.*;
00013 import java.awt.Component;
00014 import java.awt.Color;
00015 import java.awt.Rectangle;
00016 
00029 public class SharpCellRenderer extends DefaultTableCellRenderer {
00030 
00034     public SharpCellRenderer() {
00035        super();
00036     }
00037 
00038     // implements javax.swing.table.TableCellRenderer
00050     public Component getTableCellRendererComponent(JTable table, Object value,
00051                    boolean isSelected, boolean hasFocus, int row, int column) {
00052 
00053         /* the following is the similar to DefaultTableCellRenderer */
00054        if (isSelected && !(isHeaderCell(row, column))) {
00055           super.setForeground(table.getSelectionForeground());
00056           super.setBackground(table.getSelectionBackground());
00057        }else {
00058            /* set header cells background to grey and rest to default */
00059            if (isHeaderCell(row, column)) {
00060               super.setBackground(Color.lightGray);
00061               super.setForeground(table.getForeground());
00062            }else {
00063               super.setForeground(table.getForeground());
00064               super.setBackground(table.getBackground());
00065            }
00066        }
00067        
00068        setFont(table.getFont());
00069 
00070        if (hasFocus) {
00071            setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
00072            if (table.isCellEditable(row, column)) {
00073                super.setForeground( UIManager.getColor(
00074                                        "Table.focusCellForeground") );
00075                super.setBackground( UIManager.getColor(
00076                                         "Table.focusCellBackground") );
00077            }
00078        } else {
00079            setBorder(noFocusBorder);
00080        }
00081 
00082         /* this method has been changed for formula feature */
00083         setValue(value, isSelected, hasFocus, row, column); 
00084 
00085         //DefaulTableCellRenderer code
00086        // begin optimization to avoid painting background
00087        Color back = getBackground();
00088        boolean colorMatch = (back != null) && 
00089                     ( back.equals(table.getBackground()) ) && table.isOpaque();
00090         setOpaque(!colorMatch);
00091        // end optimization to aviod painting background
00092 
00093        return this;
00094     }
00095     
00105     protected void setValue(Object value, boolean hasFocus,
00106                                      boolean isSelected, int row, int column) {
00107         if (value instanceof Cell) {
00108             //we only care about the value, not the formula string
00109             Cell temp = (Cell)value;
00110            Object data = temp.getValue();
00111             
00112            if (isHeaderCell(row, column)) {
00113                 //label cells are center aligned
00114               setHorizontalAlignment(JTextField.CENTER);
00115            }else {
00116                 if (data instanceof Number) {
00117                     //numbers are right justified
00118                   setHorizontalAlignment(JTextField.RIGHT);
00119                 }else {
00120                     //everything else is left justified
00121                   setHorizontalAlignment(JTextField.LEFT);
00122                }
00123             }
00124             
00125             //value to display in table
00126            setText((data == null) ? "" : data.toString());
00127         }
00128         else {
00129             //not cell object so render with toString of that object
00130             setText((value == null) ? "" : value.toString());
00131         }
00132     }
00133 
00141     protected boolean isHeaderCell(int row, int column) {
00142        return (/*row == 0 || */column == SharpTools.baseCol-1);
00143     }
00144 }