001 /* 002 * $Id: StringValues.java 3297 2009-03-11 13:45:10Z 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 */ 021 package org.jdesktop.swingx.renderer; 022 023 import java.io.File; 024 import java.text.DateFormat; 025 import java.text.NumberFormat; 026 027 import javax.swing.filechooser.FileSystemView; 028 029 /** 030 * A collection of common {@code StringValue} implementations. 031 * 032 * @author Karl George Schaefer 033 * @author Jeanette Winzenburg 034 */ 035 public final class StringValues { 036 /** 037 * A {@code StringValue} that always presents an empty string. 038 */ 039 @SuppressWarnings("serial") 040 public final static StringValue EMPTY = new StringValue() { 041 public String getString(Object value) { 042 return ""; 043 } 044 }; 045 046 /** 047 * A {@code StringValue} that presents a {@link Object#toString() toString} 048 * value for the given object. If the value passed is {@code null}, this has 049 * the same effect as {@link StringValues#EMPTY}. 050 */ 051 @SuppressWarnings("serial") 052 public final static StringValue TO_STRING = new StringValue() { 053 public String getString(Object value) { 054 return (value != null) ? value.toString() : StringValues.EMPTY.getString(value); 055 } 056 }; 057 058 /** 059 * A {@code StringValue} that presents the current L&F display name for a 060 * given file. If the value passed to {@code FILE_NAME} is not a 061 * {@link File}, this has the same effect as {@link StringValues#TO_STRING}. 062 */ 063 @SuppressWarnings("serial") 064 public static final StringValue FILE_NAME = new StringValue() { 065 public String getString(Object value) { 066 if (value instanceof File) { 067 FileSystemView fsv = FileSystemView.getFileSystemView(); 068 069 return fsv.getSystemDisplayName((File) value); 070 } 071 072 return StringValues.TO_STRING.getString(value); 073 } 074 }; 075 076 /** 077 * A {@code StringValue} that presents the current L&F type name for a 078 * given file. If the value passed to {@code FILE_TYPE} is not a 079 * {@link File}, this has the same effect as {@link StringValues#TO_STRING}. 080 */ 081 @SuppressWarnings("serial") 082 public static final StringValue FILE_TYPE = new StringValue() { 083 public String getString(Object value) { 084 if (value instanceof File) { 085 FileSystemView fsv = FileSystemView.getFileSystemView(); 086 087 return fsv.getSystemTypeDescription((File) value); 088 } 089 090 return StringValues.TO_STRING.getString(value); 091 } 092 }; 093 094 /** 095 * Default converter for <code>Date</code> types. Uses the default format 096 * as returned from <code>DateFormat</code>. 097 */ 098 @SuppressWarnings("serial") 099 public final static FormatStringValue DATE_TO_STRING = new FormatStringValue() { 100 101 /** 102 * {@inheritDoc} 103 */ 104 @Override 105 public String getString(Object value) { 106 if (format == null) { 107 format = DateFormat.getDateInstance(); 108 } 109 return super.getString(value); 110 } 111 112 }; 113 114 /** 115 * Default converter for <code>Number</code> types. Uses the default format 116 * as returned from <code>NumberFormat</code>. 117 */ 118 @SuppressWarnings("serial") 119 public final static FormatStringValue NUMBER_TO_STRING = new FormatStringValue() { 120 121 /** 122 * {@inheritDoc} 123 */ 124 @Override 125 public String getString(Object value) { 126 if (format == null) { 127 format = NumberFormat.getNumberInstance(); 128 } 129 return super.getString(value); 130 } 131 132 }; 133 134 private StringValues() { 135 // does nothing 136 } 137 }