001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.list;
017    
018    import java.awt.Component;
019    
020    import javax.swing.DefaultListCellRenderer;
021    import javax.swing.JList;
022    
023    /**
024     * Abstract base class for ListCellRenderer that convert the cell value into a
025     * String.
026     * <p>
027     * Subclasses need to override <code>getTextValue</code> which is responsible
028     * for the conversion.
029     * 
030     * @author oliverh
031     */
032    public abstract class TextValueListRenderer extends DefaultListCellRenderer {
033    
034        /**
035         * Template method to convert cell value into a String.
036         * 
037         * @param value
038         *            the cell value
039         * @return the representation of value that should be rendered by this
040         *         ListCellRenderer
041         */
042        protected abstract String getTextValue(Object value);
043    
044        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
045                boolean cellHasFocus) {
046            super.getListCellRendererComponent(list, "", index, isSelected, cellHasFocus);
047            setText(getTextValue(value));
048            return this;
049        }
050    }