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