001/*
002 * $Id: StringValues.java 3927 2011-02-22 16:34:11Z kleopatra $
003 *
004 * Copyright 2008 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.io.File;
024import java.text.DateFormat;
025import java.text.NumberFormat;
026import java.util.Locale;
027
028import javax.swing.filechooser.FileSystemView;
029import javax.swing.plaf.UIResource;
030
031import org.jdesktop.swingx.util.Contract;
032
033
034/**
035 * A collection of common {@code StringValue} implementations.
036 * 
037 * @author Karl George Schaefer
038 * @author Jeanette Winzenburg
039 */
040public final class StringValues {
041    /**
042     * A {@code StringValue} that always presents an empty string.
043     */
044    @SuppressWarnings("serial")
045    public final static StringValue EMPTY = new StringValue() {
046        @Override
047        public String getString(Object value) {
048            return "";
049        }
050    };
051    
052    /**
053     * A {@code StringValue} that presents a {@link Object#toString() toString}
054     * value for the given object. If the value passed is {@code null}, this has
055     * the same effect as {@link StringValues#EMPTY}.
056     */
057    @SuppressWarnings("serial")
058    public final static StringValue TO_STRING = new StringValue() {
059        @Override
060        public String getString(Object value) {
061            return (value != null) ? value.toString() : StringValues.EMPTY.getString(value);
062        }
063    };
064
065    /**
066     * A {@code StringValue} that presents the current L&F display name for a
067     * given file. If the value passed to {@code FILE_NAME} is not a
068     * {@link File}, this has the same effect as {@link StringValues#TO_STRING}.
069     */
070    @SuppressWarnings("serial")
071    public static final StringValue FILE_NAME = new StringValue() {
072        @Override
073        public String getString(Object value) {
074            if (value instanceof File) {
075                FileSystemView fsv = FileSystemView.getFileSystemView();
076
077                return fsv.getSystemDisplayName((File) value);
078            }
079
080            return StringValues.TO_STRING.getString(value);
081        }
082    };
083
084    /**
085     * A {@code StringValue} that presents the current L&F type name for a
086     * given file. If the value passed to {@code FILE_TYPE} is not a
087     * {@link File}, this has the same effect as {@link StringValues#TO_STRING}.
088     */
089    @SuppressWarnings("serial")
090    public static final StringValue FILE_TYPE = new StringValue() {
091        @Override
092        public String getString(Object value) {
093            if (value instanceof File) {
094                FileSystemView fsv = FileSystemView.getFileSystemView();
095                
096                return fsv.getSystemTypeDescription((File) value);
097            }
098            
099            return StringValues.TO_STRING.getString(value);
100        }
101    };
102
103    
104    /** keep track of default locale. */
105    private static Locale defaultLocale;
106    
107    /**
108     * Returns a boolean to indicate if the default Locale has changed.
109     * Updates internal state to keep track of the default Locale. 
110     * 
111     * @return true if the default Locale has changed.
112     */
113    private static boolean localeChanged() {
114        boolean changed = !Locale.getDefault().equals(defaultLocale);
115        if (changed) {
116            defaultLocale = Locale.getDefault();
117        }
118        return changed;
119    }
120
121    /**
122     * Default converter for <code>Date</code> types. Uses the default format
123     * as returned from <code>DateFormat</code>.
124     */
125    @SuppressWarnings("serial")
126    public final static FormatStringValue DATE_TO_STRING = new FormatStringValue() {
127        
128        /**
129         * {@inheritDoc}
130         */
131        @Override
132        public String getString(Object value) {
133            if (format == null || localeChanged()) {
134                format = DateFormat.getDateInstance();
135            }
136            return super.getString(value);
137        }
138        
139    };
140    
141    /**
142     * Default converter for <code>Number</code> types. Uses the default format
143     * as returned from <code>NumberFormat</code>.
144     */
145    @SuppressWarnings("serial")
146    public final static FormatStringValue NUMBER_TO_STRING = new FormatStringValue() {
147        
148        /**
149         * {@inheritDoc}
150         */
151        @Override
152        public String getString(Object value) {
153            if (format == null || localeChanged()) {
154                format = NumberFormat.getNumberInstance();
155            }
156            return super.getString(value);
157        }
158        
159    };
160    
161    
162
163    public static final StringValue TO_STRING_UI = new StringValueUIResource(StringValues.TO_STRING);
164    public static final StringValue EMPTY_UI = new StringValueUIResource(StringValues.EMPTY);
165    
166    /**
167     * StringValue wrapper of type UIResource to tag LAF installed converters.
168     * 
169     * @author Jeanette Winzenburg, Berlin
170     */
171    public static class StringValueUIResource implements StringValue, UIResource {
172
173        private StringValue delegate;
174
175        public StringValueUIResource(StringValue toString) {
176            Contract.asNotNull(toString, "delegate StringValue must not be null");
177            this.delegate = toString;
178        }
179
180        @Override
181        public String getString(Object value) {
182            return delegate.getString(value);
183        }
184        
185    }
186    
187    private StringValues() {
188        // does nothing
189    }
190}