001    /* DefaultListCellRenderer.java --
002       Copyright (C) 2002, 2004 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.swing;
040    
041    import java.awt.Component;
042    import java.awt.Rectangle;
043    import java.io.Serializable;
044    
045    import javax.swing.border.Border;
046    import javax.swing.border.EmptyBorder;
047    
048    /**
049     * The default implementation {@link ListCellRenderer}. It provides a standard
050     * renderer for data objects of all types via {@link Object#toString()}.
051     *
052     * @author Andrew Selkirk
053     */
054    public class DefaultListCellRenderer extends JLabel
055      implements ListCellRenderer, Serializable
056    {
057      private static final long serialVersionUID = 7708947179685189462L;
058    
059      /**
060       * Subclasses <code>DefaultListCellRenderers</code> and implements
061       * {@link javax.swing.plaf.UIResource}. This is used by
062       * {@link javax.swing.plaf.ListUI} subclasses to provide a default for
063       * the <code>List.cellRenderer</code> property. If you want to override
064       * this property, use <code>DefaultListCellRenderer</code> or a subclass.
065       */
066      public static class UIResource extends DefaultListCellRenderer
067        implements javax.swing.plaf.UIResource
068      {
069        public UIResource()
070        {
071          super();
072        }
073      }
074    
075      /**
076       * This border is used whenever renderer doesn't have a focus.
077       */
078      protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
079    
080      /**
081       * getListCellRendererComponent
082       *
083       * @param list JList list for the 'value'
084       * @param value object that should be rendered in the cell
085       * @param index index of the cell
086       * @param isSelected draw cell highlighted if isSelected is true
087       * @param cellHasFocus draw focus rectangle around cell if the cell has
088       *        focus
089       *
090       * @return Component that will be painted to the desired cell.
091       */
092      public Component getListCellRendererComponent(JList list, Object value,
093                                                    int index, boolean isSelected,
094                                                    boolean cellHasFocus)
095      {
096        String s = value != null ? value.toString() : "";
097        setText(s);
098        setOpaque(true);
099        setHorizontalAlignment(LEFT);
100    
101        if (isSelected)
102          {
103            setBackground(list.getSelectionBackground());
104            setForeground(list.getSelectionForeground());
105          }
106        else
107          {
108            setBackground(list.getBackground());
109            setForeground(list.getForeground());
110          }
111    
112        setEnabled(list.isEnabled());
113        setFont(list.getFont());
114    
115        // Use focusCellHighlightBorder when renderer has focus and
116        // noFocusBorder otherwise
117    
118        if (cellHasFocus)
119          setBorder(UIManager.getBorder("List.focusCellHighlightBorder"));
120        else
121          setBorder(noFocusBorder);
122    
123        return this;
124      }
125    
126      public void validate()
127      {
128        // Overridden to do nothing.
129      }
130    
131      public void revalidate()
132      {
133        // Overridden to do nothing.
134      }
135    
136      public void repaint(long tm, int x, int y, int w, int h)
137      {
138        // Overridden to do nothing.
139      }
140    
141      public void repaint(Rectangle rect)
142      {
143        // Overridden to do nothing.
144      }
145    
146      protected void firePropertyChange(String propertyName, Object oldValue,
147                                        Object newValue)
148      {
149        // Overridden to do nothing.
150      }
151    
152      public void firePropertyChange(String propertyName, byte oldValue,
153                                     byte newValue)
154      {
155        // Overridden to do nothing.
156      }
157    
158      public void firePropertyChange(String propertyName, char oldValue,
159                                     char newValue)
160      {
161        // Overridden to do nothing.
162      }
163    
164      public void firePropertyChange(String propertyName, short oldValue,
165                                     short newValue)
166      {
167        // Overridden to do nothing.
168      }
169    
170      public void firePropertyChange(String propertyName, int oldValue,
171                                     int newValue)
172      {
173        // Overridden to do nothing.
174      }
175    
176      public void firePropertyChange(String propertyName, long oldValue,
177                                     long newValue)
178      {
179        // Overridden to do nothing.
180      }
181    
182      public void firePropertyChange(String propertyName, float oldValue,
183                                     float newValue)
184      {
185        // Overridden to do nothing.
186      }
187    
188      public void firePropertyChange(String propertyName, double oldValue,
189                                     double newValue)
190      {
191        // Overridden to do nothing.
192      }
193    
194      public void firePropertyChange(String propertyName, boolean oldValue,
195                                     boolean newValue)
196      {
197        // Overridden to do nothing.
198      }
199    }