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