DefaultListCellRenderer.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* DefaultListCellRenderer.java --
  2. Copyright (C) 2002, 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.swing;
  32. import java.awt.Component;
  33. import java.awt.Rectangle;
  34. import java.io.Serializable;
  35. import javax.swing.border.Border;
  36. import javax.swing.border.EmptyBorder;
  37. /**
  38. * The default implementation {@link ListCellRenderer}. It provides a standard
  39. * renderer for data objects of all types via {@link Object#toString()}.
  40. *
  41. * @author Andrew Selkirk
  42. */
  43. public class DefaultListCellRenderer extends JLabel
  44. implements ListCellRenderer, Serializable
  45. {
  46. private static final long serialVersionUID = 7708947179685189462L;
  47. /**
  48. * Subclasses <code>DefaultListCellRenderers</code> and implements
  49. * {@link javax.swing.plaf.UIResource}. This is used by
  50. * {@link javax.swing.plaf.ListUI} subclasses to provide a default for
  51. * the <code>List.cellRenderer</code> property. If you want to override
  52. * this property, use <code>DefaultListCellRenderer</code> or a subclass.
  53. */
  54. public static class UIResource extends DefaultListCellRenderer
  55. implements javax.swing.plaf.UIResource
  56. {
  57. public UIResource()
  58. {
  59. super();
  60. }
  61. }
  62. /**
  63. * This border is used whenever renderer doesn't have a focus.
  64. */
  65. protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
  66. /**
  67. * getListCellRendererComponent
  68. *
  69. * @param list JList list for the 'value'
  70. * @param value object that should be rendered in the cell
  71. * @param index index of the cell
  72. * @param isSelected draw cell highlighted if isSelected is true
  73. * @param cellHasFocus draw focus rectangle around cell if the cell has
  74. * focus
  75. *
  76. * @return Component that will be painted to the desired cell.
  77. */
  78. public Component getListCellRendererComponent(JList list, Object value,
  79. int index, boolean isSelected,
  80. boolean cellHasFocus)
  81. {
  82. String s = value != null ? value.toString() : "";
  83. setText(s);
  84. setOpaque(true);
  85. setHorizontalAlignment(LEFT);
  86. if (isSelected)
  87. {
  88. setBackground(list.getSelectionBackground());
  89. setForeground(list.getSelectionForeground());
  90. }
  91. else
  92. {
  93. setBackground(list.getBackground());
  94. setForeground(list.getForeground());
  95. }
  96. setEnabled(list.isEnabled());
  97. setFont(list.getFont());
  98. // Use focusCellHighlightBorder when renderer has focus and
  99. // noFocusBorder otherwise
  100. if (cellHasFocus)
  101. setBorder(UIManager.getBorder("List.focusCellHighlightBorder"));
  102. else
  103. setBorder(noFocusBorder);
  104. return this;
  105. }
  106. public void validate()
  107. {
  108. // Overridden to do nothing.
  109. }
  110. public void revalidate()
  111. {
  112. // Overridden to do nothing.
  113. }
  114. public void repaint(long tm, int x, int y, int w, int h)
  115. {
  116. // Overridden to do nothing.
  117. }
  118. public void repaint(Rectangle rect)
  119. {
  120. // Overridden to do nothing.
  121. }
  122. protected void firePropertyChange(String propertyName, Object oldValue,
  123. Object newValue)
  124. {
  125. // Overridden to do nothing.
  126. }
  127. public void firePropertyChange(String propertyName, byte oldValue,
  128. byte newValue)
  129. {
  130. // Overridden to do nothing.
  131. }
  132. public void firePropertyChange(String propertyName, char oldValue,
  133. char newValue)
  134. {
  135. // Overridden to do nothing.
  136. }
  137. public void firePropertyChange(String propertyName, short oldValue,
  138. short newValue)
  139. {
  140. // Overridden to do nothing.
  141. }
  142. public void firePropertyChange(String propertyName, int oldValue,
  143. int newValue)
  144. {
  145. // Overridden to do nothing.
  146. }
  147. public void firePropertyChange(String propertyName, long oldValue,
  148. long newValue)
  149. {
  150. // Overridden to do nothing.
  151. }
  152. public void firePropertyChange(String propertyName, float oldValue,
  153. float newValue)
  154. {
  155. // Overridden to do nothing.
  156. }
  157. public void firePropertyChange(String propertyName, double oldValue,
  158. double newValue)
  159. {
  160. // Overridden to do nothing.
  161. }
  162. public void firePropertyChange(String propertyName, boolean oldValue,
  163. boolean newValue)
  164. {
  165. // Overridden to do nothing.
  166. }
  167. }