001/*
002 * $Id: LabelProvider.java 3100 2008-10-14 22:33:10Z rah003 $
003 *
004 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.renderer;
022
023import javax.swing.JLabel;
024
025/**
026 * A component provider which uses a <code>JLabel</code> as rendering
027 * component. <p>
028 * 
029 * It configures the Label's text and icon property from the  
030 * StringValue.
031 * 
032 * @author Jeanette Winzenburg
033 * 
034 * @see StringValue
035 * @see FormatStringValue
036 * @see IconValue
037 */
038public class LabelProvider extends ComponentProvider<JLabel> {
039
040    /**
041     * Instantiates a LabelProvider with default to-String converter and LEADING
042     * horizontal alignment .
043     * <p>
044     * 
045     */
046    public LabelProvider() {
047        this(null);
048    }
049    
050    /**
051     * Instantiates a LabelProvider with the given to-String converter and LEADING
052     * horizontal alignment. If the converter is null, the default TO_STRING is
053     * used.
054     * <p>
055     * 
056     * @param converter the converter to use for mapping the cell value to a
057     *        String representation.
058     */
059    public LabelProvider(StringValue converter) {
060        this(converter, JLabel.LEADING);
061    }
062
063    /**
064     * Instantiates a LabelProvider with default to-String converter and given
065     * horizontal alignment. 
066     * 
067     * @param alignment the horizontal alignment.
068     */
069    public LabelProvider(int alignment) {
070        this(null, alignment);
071    }
072
073    /**
074     * Instantiates a LabelProvider with given to-String converter and given
075     * horizontal alignment. If the converter is null, the default TO_STRING is
076     * used.
077     * 
078     * @param converter the converter to use for mapping the cell value to a
079     *        String representation.
080     * @param alignment the horizontal alignment.
081     */
082    public LabelProvider(StringValue converter, int alignment) {
083        super(converter, alignment);
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    protected JLabel createRendererComponent() {
091        return new JRendererLabel();
092    }
093
094    /**
095     * {@inheritDoc}
096     * Here: sets the Label's horizontal alignment to the alignment as configured 
097     * in the controller.
098     */
099    @Override
100    protected void configureState(CellContext context) {
101       rendererComponent.setHorizontalAlignment(getHorizontalAlignment());
102    }
103
104    /**
105     * {@inheritDoc}
106     * Here: sets the labels's text and icon property to the value as 
107     * returned by getValueAsString/Icon, respectively.
108     * 
109     * @param context the cellContext to use
110     * 
111     * @see #getValueAsString(CellContext)
112     * @see #getValueAsIcon(CellContext) 
113     */
114    @Override
115    protected void format(CellContext context) {
116        rendererComponent.setIcon(getValueAsIcon(context));
117        rendererComponent.setText(getValueAsString(context));
118    }
119
120    
121}