001/*
002 * $Id: DefaultTableRenderer.java 3927 2011-02-22 16:34:11Z kleopatra $
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
023
024import java.awt.Component;
025
026import javax.swing.JTable;
027import javax.swing.table.TableCellRenderer;
028
029
030/**
031 * Adapter to glue SwingX renderer support to core api. It has convenience
032 * constructors to create a LabelProvider, optionally configured with a
033 * StringValue and horizontal alignment. Typically, client code does not
034 * interact with this class except at instantiation time.
035 * <p>
036 * 
037 * <code>JXTable</code> uses instances of this as per-class default renderers.
038 * 
039 * <pre><code>
040 * setDefaultRenderer(Object.class, new DefaultTableRenderer());
041 * setDefaultRenderer(Number.class, new DefaultTableRenderer(
042 *         FormatStringValues.NUMBER_TO_STRING, JLabel.RIGHT));
043 * setDefaultRenderer(Date.class, new DefaultTableRenderer(
044 *         FormatStringValues.DATE_TO_STRING));
045 * // use the same center aligned default for Image/Icon
046 * TableCellRenderer renderer = new DefaultTableRenderer(new MappedValue(
047 *         StringValues.EMPTY, IconValues.ICON), JLabel.CENTER);
048 * setDefaultRenderer(Icon.class, renderer);
049 * setDefaultRenderer(ImageIcon.class, renderer);
050 * // use a CheckBoxProvider for booleans
051 * setDefaultRenderer(Boolean.class,
052 *         new DefaultTableRenderer(new CheckBoxProvider()));
053 * </code></pre>
054 * 
055 * 
056 * 
057 * @author Jeanette Winzenburg
058 * 
059 * @see ComponentProvider
060 * @see LabelProvider
061 * @see StringValue
062 * @see IconValue
063 * @see MappedValue
064 * @see CellContext
065 * 
066 */
067public class DefaultTableRenderer extends AbstractRenderer
068        implements TableCellRenderer {
069
070    private TableCellContext cellContext;
071    
072    
073    /**
074     * Instantiates a default table renderer with the default component
075     * provider. 
076     * 
077     * @see #DefaultTableRenderer(ComponentProvider)
078     */
079    public DefaultTableRenderer() {
080        this((ComponentProvider<?>) null);
081    }
082
083    /**
084     * Instantiates a default table renderer with the given component provider.
085     * If the controller is null, creates and uses a default. The default
086     * provider is of type <code>LabelProvider</code>.
087     * 
088     * @param componentProvider the provider of the configured component to
089     *        use for cell rendering
090     */
091    public DefaultTableRenderer(ComponentProvider<?> componentProvider) {
092        super(componentProvider);
093        this.cellContext = new TableCellContext();
094    }
095
096    /**
097     * Instantiates a default table renderer with a default component
098     * provider using the given converter. 
099     * 
100     * @param converter the converter to use for mapping the
101     *   content value to a String representation.
102     *   
103     * @see #DefaultTableRenderer(ComponentProvider)  
104     */
105    public DefaultTableRenderer(StringValue converter) {
106        this(new LabelProvider(converter));
107    }
108
109    /**
110     * Instantiates a default table renderer with a default component
111     * provider using the given converter and horizontal 
112     * alignment. 
113     * 
114     * @param converter the converter to use for mapping the
115     *   content value to a String representation.
116     *   
117     * @see #DefaultTableRenderer(ComponentProvider)  
118     */
119    public DefaultTableRenderer(StringValue converter, int alignment) {
120        this(new LabelProvider(converter, alignment));
121    }
122
123    /**
124     * Intantiates a default table renderer with default component provider
125     * using both converters.
126     * 
127     * @param stringValue the converter to use for the string representation
128     * @param iconValue the converter to use for the icon representation
129     */
130    public DefaultTableRenderer(StringValue stringValue, IconValue iconValue) {
131        this(new MappedValue(stringValue, iconValue));
132    }
133
134    /**
135     * Intantiates a default table renderer with default component provider
136     * using both converters and the given alignment.
137     * 
138     * @param stringValue the converter to use for the string representation
139     * @param iconValue the converter to use for the icon representation
140     * @param alignment the rendering component's horizontal alignment
141     */
142    public DefaultTableRenderer(StringValue stringValue, IconValue iconValue,
143            int alignment) {
144        this(new MappedValue(stringValue, iconValue), alignment);
145    }
146
147    // -------------- implements javax.swing.table.TableCellRenderer
148    /**
149     * 
150     * Returns a configured component, appropriate to render the given
151     * list cell. <p> 
152     * 
153     * Note: The component's name is set to "Table.cellRenderer" for the sake
154     * of Synth-based LAFs.
155     * 
156     * @param table the <code>JTable</code>
157     * @param value the value to assign to the cell at
158     *        <code>[row, column]</code>
159     * @param isSelected true if cell is selected
160     * @param hasFocus true if cell has focus
161     * @param row the row of the cell to render
162     * @param column the column of the cell to render
163     * @return the default table cell renderer
164     */
165    @Override
166    public Component getTableCellRendererComponent(JTable table, Object value,
167            boolean isSelected, boolean hasFocus, int row, int column) {
168        cellContext.installContext(table, value, row, column, isSelected, hasFocus,
169                true, true);
170        Component comp = componentController.getRendererComponent(cellContext);
171        // fix issue #1040-swingx: memory leak if value not released
172        cellContext.replaceValue(null);
173        return comp;
174    }
175
176    /**
177     * {@inheritDoc}
178     */ 
179    @Override
180    protected ComponentProvider<?> createDefaultComponentProvider() {
181        return new LabelProvider();
182    }
183
184
185}
186
187