DefaultTableCellRenderer.java
Upload User: cddzwx
Upload Date: 2018-11-24
Package Size: 17653k
Code Size: 10k
Category:

JavaScript

Development Platform:

JavaScript

  1. /*
  2.  * @(#)DefaultTableCellRenderer.java 1.38 04/03/05
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5.  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6.  */
  7. package javax.swing.table;
  8. import javax.swing.*;
  9. import javax.swing.table.TableCellRenderer;
  10. import javax.swing.border.*;
  11. import java.awt.Component;
  12. import java.awt.Color;
  13. import java.awt.Rectangle;
  14. import java.io.Serializable;
  15. /**
  16.  * The standard class for rendering (displaying) individual cells
  17.  * in a <code>JTable</code>.
  18.  * <p>
  19.  *
  20.  * <strong><a name="override">Implementation Note:</a></strong>
  21.  * This class inherits from <code>JLabel</code>, a standard component class. 
  22.  * However <code>JTable</code> employs a unique mechanism for rendering
  23.  * its cells and therefore requires some slightly modified behavior
  24.  * from its cell renderer.  
  25.  * The table class defines a single cell renderer and uses it as a 
  26.  * as a rubber-stamp for rendering all cells in the table; 
  27.  * it renders the first cell,
  28.  * changes the contents of that cell renderer, 
  29.  * shifts the origin to the new location, re-draws it, and so on.
  30.  * The standard <code>JLabel</code> component was not
  31.  * designed to be used this way and we want to avoid 
  32.  * triggering a <code>revalidate</code> each time the
  33.  * cell is drawn. This would greatly decrease performance because the
  34.  * <code>revalidate</code> message would be
  35.  * passed up the hierarchy of the container to determine whether any other
  36.  * components would be affected.  
  37.  * As the renderer is only parented for the lifetime of a painting operation
  38.  * we similarly want to avoid the overhead associated with walking the
  39.  * hierarchy for painting operations.
  40.  * So this class
  41.  * overrides the <code>validate</code>, <code>invalidate</code>,
  42.  * <code>revalidate</code>, <code>repaint</code>, and
  43.  * <code>firePropertyChange</code> methods to be 
  44.  * no-ops and override the <code>isOpaque</code> method solely to improve
  45.  * performance.  If you write your own renderer,
  46.  * please keep this performance consideration in mind.
  47.  * <p>
  48.  *
  49.  * <strong>Warning:</strong>
  50.  * Serialized objects of this class will not be compatible with
  51.  * future Swing releases. The current serialization support is
  52.  * appropriate for short term storage or RMI between applications running
  53.  * the same version of Swing.  As of 1.4, support for long term storage
  54.  * of all JavaBeans<sup><font size="-2">TM</font></sup>
  55.  * has been added to the <code>java.beans</code> package.
  56.  * Please see {@link java.beans.XMLEncoder}.
  57.  *
  58.  * @version 1.38 03/05/04
  59.  * @author Philip Milne 
  60.  * @see JTable
  61.  */
  62. public class DefaultTableCellRenderer extends JLabel
  63.     implements TableCellRenderer, Serializable
  64. {
  65.     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 
  66.     
  67.     // We need a place to store the color the JLabel should be returned 
  68.     // to after its foreground and background colors have been set 
  69.     // to the selection background color. 
  70.     // These ivars will be made protected when their names are finalized. 
  71.     private Color unselectedForeground; 
  72.     private Color unselectedBackground; 
  73.     /**
  74.      * Creates a default table cell renderer.
  75.      */
  76.     public DefaultTableCellRenderer() {
  77. super();
  78. setOpaque(true);
  79.         setBorder(noFocusBorder);
  80.     }
  81.     /**
  82.      * Overrides <code>JComponent.setForeground</code> to assign
  83.      * the unselected-foreground color to the specified color.
  84.      * 
  85.      * @param c set the foreground color to this value
  86.      */
  87.     public void setForeground(Color c) {
  88.         super.setForeground(c); 
  89.         unselectedForeground = c; 
  90.     }
  91.     
  92.     /**
  93.      * Overrides <code>JComponent.setBackground</code> to assign
  94.      * the unselected-background color to the specified color.
  95.      *
  96.      * @param c set the background color to this value
  97.      */
  98.     public void setBackground(Color c) {
  99.         super.setBackground(c); 
  100.         unselectedBackground = c; 
  101.     }
  102.     /**
  103.      * Notification from the <code>UIManager</code> that the look and feel
  104.      * [L&F] has changed.
  105.      * Replaces the current UI object with the latest version from the 
  106.      * <code>UIManager</code>.
  107.      *
  108.      * @see JComponent#updateUI
  109.      */
  110.     public void updateUI() {
  111.         super.updateUI(); 
  112. setForeground(null);
  113. setBackground(null);
  114.     }
  115.     
  116.     // implements javax.swing.table.TableCellRenderer
  117.     /**
  118.      *
  119.      * Returns the default table cell renderer.
  120.      *
  121.      * @param table  the <code>JTable</code>
  122.      * @param value  the value to assign to the cell at
  123.      * <code>[row, column]</code>
  124.      * @param isSelected true if cell is selected
  125.      * @param hasFocus true if cell has focus
  126.      * @param row  the row of the cell to render
  127.      * @param column the column of the cell to render
  128.      * @return the default table cell renderer
  129.      */
  130.     public Component getTableCellRendererComponent(JTable table, Object value,
  131.                           boolean isSelected, boolean hasFocus, int row, int column) {
  132. if (isSelected) {
  133.    super.setForeground(table.getSelectionForeground());
  134.    super.setBackground(table.getSelectionBackground());
  135. }
  136. else {
  137.     super.setForeground((unselectedForeground != null) ? unselectedForeground 
  138.                                                        : table.getForeground());
  139.     super.setBackground((unselectedBackground != null) ? unselectedBackground 
  140.                                                        : table.getBackground());
  141. }
  142. setFont(table.getFont());
  143. if (hasFocus) {
  144.     setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
  145.     if (!isSelected && table.isCellEditable(row, column)) {
  146.                 Color col;
  147.                 col = UIManager.getColor("Table.focusCellForeground");
  148.                 if (col != null) {
  149.                     super.setForeground(col);
  150.                 }
  151.                 col = UIManager.getColor("Table.focusCellBackground");
  152.                 if (col != null) {
  153.                     super.setBackground(col);
  154.                 }
  155.     }
  156. } else {
  157.     setBorder(noFocusBorder);
  158. }
  159.         setValue(value); 
  160. return this;
  161.     }
  162.     
  163.     /*
  164.      * The following methods are overridden as a performance measure to 
  165.      * to prune code-paths are often called in the case of renders
  166.      * but which we know are unnecessary.  Great care should be taken
  167.      * when writing your own renderer to weigh the benefits and 
  168.      * drawbacks of overriding methods like these.
  169.      */
  170.     /**
  171.      * Overridden for performance reasons.
  172.      * See the <a href="#override">Implementation Note</a> 
  173.      * for more information.
  174.      */
  175.     public boolean isOpaque() { 
  176. Color back = getBackground();
  177. Component p = getParent(); 
  178. if (p != null) { 
  179.     p = p.getParent(); 
  180. }
  181. // p should now be the JTable. 
  182. boolean colorMatch = (back != null) && (p != null) && 
  183.     back.equals(p.getBackground()) && 
  184. p.isOpaque();
  185. return !colorMatch && super.isOpaque(); 
  186.     }
  187.     /**
  188.      * Overridden for performance reasons.
  189.      * See the <a href="#override">Implementation Note</a> 
  190.      * for more information.
  191.      *
  192.      * @since 1.5
  193.      */
  194.     public void invalidate() {}
  195.     /**
  196.      * Overridden for performance reasons.
  197.      * See the <a href="#override">Implementation Note</a> 
  198.      * for more information.
  199.      */
  200.     public void validate() {}
  201.     /**
  202.      * Overridden for performance reasons.
  203.      * See the <a href="#override">Implementation Note</a> 
  204.      * for more information.
  205.      */
  206.     public void revalidate() {}
  207.     /**
  208.      * Overridden for performance reasons.
  209.      * See the <a href="#override">Implementation Note</a> 
  210.      * for more information.
  211.      */
  212.     public void repaint(long tm, int x, int y, int width, int height) {}
  213.     /**
  214.      * Overridden for performance reasons.
  215.      * See the <a href="#override">Implementation Note</a> 
  216.      * for more information.
  217.      */
  218.     public void repaint(Rectangle r) { }
  219.     /**
  220.      * Overridden for performance reasons.
  221.      * See the <a href="#override">Implementation Note</a> 
  222.      * for more information.
  223.      *
  224.      * @since 1.5
  225.      */
  226.     public void repaint() {
  227.     }
  228.     /**
  229.      * Overridden for performance reasons.
  230.      * See the <a href="#override">Implementation Note</a> 
  231.      * for more information.
  232.      */
  233.     protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
  234. // Strings get interned...
  235. if (propertyName=="text") {
  236.     super.firePropertyChange(propertyName, oldValue, newValue);
  237. }
  238.     }
  239.     /**
  240.      * Overridden for performance reasons.
  241.      * See the <a href="#override">Implementation Note</a> 
  242.      * for more information.
  243.      */
  244.     public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
  245.     /**
  246.      * Sets the <code>String</code> object for the cell being rendered to
  247.      * <code>value</code>.
  248.      * 
  249.      * @param value  the string value for this cell; if value is
  250.      * <code>null</code> it sets the text value to an empty string
  251.      * @see JLabel#setText
  252.      * 
  253.      */
  254.     protected void setValue(Object value) {
  255. setText((value == null) ? "" : value.toString());
  256.     }
  257.     /**
  258.      * A subclass of <code>DefaultTableCellRenderer</code> that
  259.      * implements <code>UIResource</code>.
  260.      * <code>DefaultTableCellRenderer</code> doesn't implement
  261.      * <code>UIResource</code>
  262.      * directly so that applications can safely override the
  263.      * <code>cellRenderer</code> property with
  264.      * <code>DefaultTableCellRenderer</code> subclasses.
  265.      * <p>
  266.      * <strong>Warning:</strong>
  267.      * Serialized objects of this class will not be compatible with
  268.      * future Swing releases. The current serialization support is
  269.      * appropriate for short term storage or RMI between applications running
  270.      * the same version of Swing.  As of 1.4, support for long term storage
  271.      * of all JavaBeans<sup><font size="-2">TM</font></sup>
  272.      * has been added to the <code>java.beans</code> package.
  273.      * Please see {@link java.beans.XMLEncoder}.
  274.      */
  275.     public static class UIResource extends DefaultTableCellRenderer 
  276.         implements javax.swing.plaf.UIResource
  277.     {
  278.     }
  279. }