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