001/*
002 * $Id: FormatStringValue.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
023import java.text.Format;
024
025/**
026 * Base type for <code>Format</code>-backed <code>StringValue</code>. Has
027 * static defaults for Date and Number which use the locale-dependent default
028 * <code>Format</code>s as returned from xxFormat.getInstance().
029 * <p>
030 * 
031 * This class is intended to ease the handling of formatted cell content.
032 * F.i. to show a list of <code>Date</code>s in the default
033 * <code>Locale</code>'s FULL version and right align the text:
034 * 
035 * <pre><code>
036 *    StringValue stringValue = new FormatStringValue(
037 *        DateFormat.getInstance(DateFormat.FULL));
038 *    list.setCellRenderer(
039 *        new DefaultListRenderer(stringValue, JLabel.RIGHT);  
040 * </code></pre>
041 * 
042 * 
043 * PENDING: need to update on Locale change? How to detect? When?
044 * 
045 * @author Jeanette Winzenburg
046 */
047public class FormatStringValue implements StringValue {
048
049    /** the format used in creating the String representation. */
050    protected Format format;
051
052    /**
053     * Instantiates a formatted converter with null format.
054     *
055     */
056    public FormatStringValue() {
057        this(null);
058    }
059    
060    /**
061     * Instantiates a formatted converter with the given Format.
062     * 
063     * @param format the format to use in creating the String representation.
064     */
065    public FormatStringValue(Format format) {
066       this.format = format; 
067    }
068    
069    /**
070     * 
071     * @return the format used in creating the String representation.
072     */
073    public Format getFormat() {
074        return format;
075    }
076    
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public String getString(Object value) {
082        if (value == null) return "";
083        if (format != null) {
084            try {
085                return format.format(value);
086            } catch (IllegalArgumentException e) {
087                // didn't work, nothing we can do
088            }
089        }
090        return value.toString();
091    }
092
093}