1. /*
  2. * @(#)DefaultTableCellRenderer.java 1.33 03/01/23
  3. *
  4. * Copyright 2003 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. So this class
  37. * overrides the <code>validate</code>, <code>revalidate</code>,
  38. * <code>repaint</code>, and <code>firePropertyChange</code> methods to be
  39. * no-ops. If you write your own renderer,
  40. * please keep this performance consideration in mind.
  41. * <p>
  42. *
  43. * <strong>Warning:</strong>
  44. * Serialized objects of this class will not be compatible with
  45. * future Swing releases. The current serialization support is
  46. * appropriate for short term storage or RMI between applications running
  47. * the same version of Swing. As of 1.4, support for long term storage
  48. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  49. * has been added to the <code>java.beans</code> package.
  50. * Please see {@link java.beans.XMLEncoder}.
  51. *
  52. * @version 1.33 01/23/03
  53. * @author Philip Milne
  54. * @see JTable
  55. */
  56. public class DefaultTableCellRenderer extends JLabel
  57. implements TableCellRenderer, Serializable
  58. {
  59. protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
  60. // We need a place to store the color the JLabel should be returned
  61. // to after its foreground and background colors have been set
  62. // to the selection background color.
  63. // These ivars will be made protected when their names are finalized.
  64. private Color unselectedForeground;
  65. private Color unselectedBackground;
  66. /**
  67. * Creates a default table cell renderer.
  68. */
  69. public DefaultTableCellRenderer() {
  70. super();
  71. setOpaque(true);
  72. setBorder(noFocusBorder);
  73. }
  74. /**
  75. * Overrides <code>JComponent.setForeground</code> to assign
  76. * the unselected-foreground color to the specified color.
  77. *
  78. * @param c set the foreground color to this value
  79. */
  80. public void setForeground(Color c) {
  81. super.setForeground(c);
  82. unselectedForeground = c;
  83. }
  84. /**
  85. * Overrides <code>JComponent.setBackground</code> to assign
  86. * the unselected-background color to the specified color.
  87. *
  88. * @param c set the background color to this value
  89. */
  90. public void setBackground(Color c) {
  91. super.setBackground(c);
  92. unselectedBackground = c;
  93. }
  94. /**
  95. * Notification from the <code>UIManager</code> that the look and feel
  96. * [L&F] has changed.
  97. * Replaces the current UI object with the latest version from the
  98. * <code>UIManager</code>.
  99. *
  100. * @see JComponent#updateUI
  101. */
  102. public void updateUI() {
  103. super.updateUI();
  104. setForeground(null);
  105. setBackground(null);
  106. }
  107. // implements javax.swing.table.TableCellRenderer
  108. /**
  109. *
  110. * Returns the default table cell renderer.
  111. *
  112. * @param table the <code>JTable</code>
  113. * @param value the value to assign to the cell at
  114. * <code>[row, column]</code>
  115. * @param isSelected true if cell is selected
  116. * @param hasFocus true if cell has focus
  117. * @param row the row of the cell to render
  118. * @param column the column of the cell to render
  119. * @return the default table cell renderer
  120. */
  121. public Component getTableCellRendererComponent(JTable table, Object value,
  122. boolean isSelected, boolean hasFocus, int row, int column) {
  123. if (isSelected) {
  124. super.setForeground(table.getSelectionForeground());
  125. super.setBackground(table.getSelectionBackground());
  126. }
  127. else {
  128. super.setForeground((unselectedForeground != null) ? unselectedForeground
  129. : table.getForeground());
  130. super.setBackground((unselectedBackground != null) ? unselectedBackground
  131. : table.getBackground());
  132. }
  133. setFont(table.getFont());
  134. if (hasFocus) {
  135. setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
  136. if (table.isCellEditable(row, column)) {
  137. super.setForeground( UIManager.getColor("Table.focusCellForeground") );
  138. super.setBackground( UIManager.getColor("Table.focusCellBackground") );
  139. }
  140. } else {
  141. setBorder(noFocusBorder);
  142. }
  143. setValue(value);
  144. return this;
  145. }
  146. /*
  147. * The following methods are overridden as a performance measure to
  148. * to prune code-paths are often called in the case of renders
  149. * but which we know are unnecessary. Great care should be taken
  150. * when writing your own renderer to weigh the benefits and
  151. * drawbacks of overriding methods like these.
  152. */
  153. /**
  154. * Overridden for performance reasons.
  155. * See the <a href="#override">Implementation Note</a>
  156. * for more information.
  157. */
  158. public boolean isOpaque() {
  159. Color back = getBackground();
  160. Component p = getParent();
  161. if (p != null) {
  162. p = p.getParent();
  163. }
  164. // p should now be the JTable.
  165. boolean colorMatch = (back != null) && (p != null) &&
  166. back.equals(p.getBackground()) &&
  167. p.isOpaque();
  168. return !colorMatch && super.isOpaque();
  169. }
  170. /**
  171. * Overridden for performance reasons.
  172. * See the <a href="#override">Implementation Note</a>
  173. * for more information.
  174. */
  175. public void validate() {}
  176. /**
  177. * Overridden for performance reasons.
  178. * See the <a href="#override">Implementation Note</a>
  179. * for more information.
  180. */
  181. public void revalidate() {}
  182. /**
  183. * Overridden for performance reasons.
  184. * See the <a href="#override">Implementation Note</a>
  185. * for more information.
  186. */
  187. public void repaint(long tm, int x, int y, int width, int height) {}
  188. /**
  189. * Overridden for performance reasons.
  190. * See the <a href="#override">Implementation Note</a>
  191. * for more information.
  192. */
  193. public void repaint(Rectangle r) { }
  194. /**
  195. * Overridden for performance reasons.
  196. * See the <a href="#override">Implementation Note</a>
  197. * for more information.
  198. */
  199. protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
  200. // Strings get interned...
  201. if (propertyName=="text") {
  202. super.firePropertyChange(propertyName, oldValue, newValue);
  203. }
  204. }
  205. /**
  206. * Overridden for performance reasons.
  207. * See the <a href="#override">Implementation Note</a>
  208. * for more information.
  209. */
  210. public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
  211. /**
  212. * Sets the <code>String</code> object for the cell being rendered to
  213. * <code>value</code>.
  214. *
  215. * @param value the string value for this cell; if value is
  216. * <code>null</code> it sets the text value to an empty string
  217. * @see JLabel#setText
  218. *
  219. */
  220. protected void setValue(Object value) {
  221. setText((value == null) ? "" : value.toString());
  222. }
  223. /**
  224. * A subclass of <code>DefaultTableCellRenderer</code> that
  225. * implements <code>UIResource</code>.
  226. * <code>DefaultTableCellRenderer</code> doesn't implement
  227. * <code>UIResource</code>
  228. * directly so that applications can safely override the
  229. * <code>cellRenderer</code> property with
  230. * <code>DefaultTableCellRenderer</code> subclasses.
  231. * <p>
  232. * <strong>Warning:</strong>
  233. * Serialized objects of this class will not be compatible with
  234. * future Swing releases. The current serialization support is
  235. * appropriate for short term storage or RMI between applications running
  236. * the same version of Swing. As of 1.4, support for long term storage
  237. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  238. * has been added to the <code>java.beans</code> package.
  239. * Please see {@link java.beans.XMLEncoder}.
  240. */
  241. public static class UIResource extends DefaultTableCellRenderer
  242. implements javax.swing.plaf.UIResource
  243. {
  244. }
  245. }