1. /*
  2. * @(#)DefaultListCellRenderer.java 1.25 03/12/19
  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;
  8. import javax.swing.*;
  9. import javax.swing.event.*;
  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. * Renders an item in a list.
  17. * <p>
  18. * <strong><a name="override">Implementation Note:</a></strong>
  19. * This class overrides
  20. * <code>invalidate</code>,
  21. * <code>validate</code>,
  22. * <code>revalidate</code>,
  23. * <code>repaint</code>,
  24. * <code>isOpaque</code>,
  25. * and
  26. * <code>firePropertyChange</code>
  27. * solely to improve performance.
  28. * If not overridden, these frequently called methods would execute code paths
  29. * that are unnecessary for the default list cell renderer.
  30. * If you write your own renderer,
  31. * take care to weigh the benefits and
  32. * drawbacks of overriding these methods.
  33. *
  34. * <p>
  35. *
  36. * <strong>Warning:</strong>
  37. * Serialized objects of this class will not be compatible with
  38. * future Swing releases. The current serialization support is
  39. * appropriate for short term storage or RMI between applications running
  40. * the same version of Swing. As of 1.4, support for long term storage
  41. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  42. * has been added to the <code>java.beans</code> package.
  43. * Please see {@link java.beans.XMLEncoder}.
  44. *
  45. * @version 1.25 12/19/03
  46. * @author Philip Milne
  47. * @author Hans Muller
  48. */
  49. public class DefaultListCellRenderer extends JLabel
  50. implements ListCellRenderer, Serializable
  51. {
  52. protected static Border noFocusBorder;
  53. /**
  54. * Constructs a default renderer object for an item
  55. * in a list.
  56. */
  57. public DefaultListCellRenderer() {
  58. super();
  59. if (noFocusBorder == null) {
  60. noFocusBorder = new EmptyBorder(1, 1, 1, 1);
  61. }
  62. setOpaque(true);
  63. setBorder(noFocusBorder);
  64. }
  65. public Component getListCellRendererComponent(
  66. JList list,
  67. Object value,
  68. int index,
  69. boolean isSelected,
  70. boolean cellHasFocus)
  71. {
  72. setComponentOrientation(list.getComponentOrientation());
  73. if (isSelected) {
  74. setBackground(list.getSelectionBackground());
  75. setForeground(list.getSelectionForeground());
  76. }
  77. else {
  78. setBackground(list.getBackground());
  79. setForeground(list.getForeground());
  80. }
  81. if (value instanceof Icon) {
  82. setIcon((Icon)value);
  83. setText("");
  84. }
  85. else {
  86. setIcon(null);
  87. setText((value == null) ? "" : value.toString());
  88. }
  89. setEnabled(list.isEnabled());
  90. setFont(list.getFont());
  91. setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
  92. return this;
  93. }
  94. /**
  95. * Overridden for performance reasons.
  96. * See the <a href="#override">Implementation Note</a>
  97. * for more information.
  98. *
  99. * @since 1.5
  100. * @return <code>true</code> if the background is completely opaque
  101. * and differs from the JList's background;
  102. * <code>false</code> otherwise
  103. */
  104. public boolean isOpaque() {
  105. Color back = getBackground();
  106. Component p = getParent();
  107. if (p != null) {
  108. p = p.getParent();
  109. }
  110. // p should now be the JList.
  111. boolean colorMatch = (back != null) && (p != null) &&
  112. back.equals(p.getBackground()) &&
  113. p.isOpaque();
  114. return !colorMatch && super.isOpaque();
  115. }
  116. /**
  117. * Overridden for performance reasons.
  118. * See the <a href="#override">Implementation Note</a>
  119. * for more information.
  120. */
  121. public void validate() {}
  122. /**
  123. * Overridden for performance reasons.
  124. * See the <a href="#override">Implementation Note</a>
  125. * for more information.
  126. *
  127. * @since 1.5
  128. */
  129. public void invalidate() {}
  130. /**
  131. * Overridden for performance reasons.
  132. * See the <a href="#override">Implementation Note</a>
  133. * for more information.
  134. *
  135. * @since 1.5
  136. */
  137. public void repaint() {}
  138. /**
  139. * Overridden for performance reasons.
  140. * See the <a href="#override">Implementation Note</a>
  141. * for more information.
  142. */
  143. public void revalidate() {}
  144. /**
  145. * Overridden for performance reasons.
  146. * See the <a href="#override">Implementation Note</a>
  147. * for more information.
  148. */
  149. public void repaint(long tm, int x, int y, int width, int height) {}
  150. /**
  151. * Overridden for performance reasons.
  152. * See the <a href="#override">Implementation Note</a>
  153. * for more information.
  154. */
  155. public void repaint(Rectangle r) {}
  156. /**
  157. * Overridden for performance reasons.
  158. * See the <a href="#override">Implementation Note</a>
  159. * for more information.
  160. */
  161. protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
  162. // Strings get interned...
  163. if (propertyName=="text")
  164. super.firePropertyChange(propertyName, oldValue, newValue);
  165. }
  166. /**
  167. * Overridden for performance reasons.
  168. * See the <a href="#override">Implementation Note</a>
  169. * for more information.
  170. */
  171. public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {}
  172. /**
  173. * Overridden for performance reasons.
  174. * See the <a href="#override">Implementation Note</a>
  175. * for more information.
  176. */
  177. public void firePropertyChange(String propertyName, char oldValue, char newValue) {}
  178. /**
  179. * Overridden for performance reasons.
  180. * See the <a href="#override">Implementation Note</a>
  181. * for more information.
  182. */
  183. public void firePropertyChange(String propertyName, short oldValue, short newValue) {}
  184. /**
  185. * Overridden for performance reasons.
  186. * See the <a href="#override">Implementation Note</a>
  187. * for more information.
  188. */
  189. public void firePropertyChange(String propertyName, int oldValue, int newValue) {}
  190. /**
  191. * Overridden for performance reasons.
  192. * See the <a href="#override">Implementation Note</a>
  193. * for more information.
  194. */
  195. public void firePropertyChange(String propertyName, long oldValue, long newValue) {}
  196. /**
  197. * Overridden for performance reasons.
  198. * See the <a href="#override">Implementation Note</a>
  199. * for more information.
  200. */
  201. public void firePropertyChange(String propertyName, float oldValue, float newValue) {}
  202. /**
  203. * Overridden for performance reasons.
  204. * See the <a href="#override">Implementation Note</a>
  205. * for more information.
  206. */
  207. public void firePropertyChange(String propertyName, double oldValue, double newValue) {}
  208. /**
  209. * Overridden for performance reasons.
  210. * See the <a href="#override">Implementation Note</a>
  211. * for more information.
  212. */
  213. public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
  214. /**
  215. * A subclass of DefaultListCellRenderer that implements UIResource.
  216. * DefaultListCellRenderer doesn't implement UIResource
  217. * directly so that applications can safely override the
  218. * cellRenderer property with DefaultListCellRenderer 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
  223. * appropriate for short term storage or RMI between applications running
  224. * the same version of Swing. As of 1.4, support for long term storage
  225. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  226. * has been added to the <code>java.beans</code> package.
  227. * Please see {@link java.beans.XMLEncoder}.
  228. */
  229. public static class UIResource extends DefaultListCellRenderer
  230. implements javax.swing.plaf.UIResource
  231. {
  232. }
  233. }