001 /** 002 * ======================================== 003 * JFreeReport : a free Java report library 004 * ======================================== 005 * 006 * Project Info: http://reporting.pentaho.org/ 007 * 008 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. 009 * 010 * This library is free software; you can redistribute it and/or modify it under the terms 011 * of the GNU Lesser General Public License as published by the Free Software Foundation; 012 * either version 2.1 of the License, or (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 016 * See the GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public License along with this 019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 020 * Boston, MA 02111-1307, USA. 021 * 022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 023 * in the United States and other countries.] 024 * 025 * ------------ 026 * $Id: ColorValueConverter.java 3525 2007-10-16 11:43:48Z tmorgner $ 027 * ------------ 028 * (C) Copyright 2000-2005, by Object Refinery Limited. 029 * (C) Copyright 2005-2007, by Pentaho Corporation. 030 */ 031 package org.jfree.report.util.beans; 032 033 import java.awt.Color; 034 import java.lang.reflect.Field; 035 import java.lang.reflect.Modifier; 036 037 /** 038 * A class that handles the conversion of {@link Integer} attributes to and from their 039 * {@link String} representation. 040 * 041 * @author Thomas Morgner 042 */ 043 public class ColorValueConverter implements ValueConverter 044 { 045 046 /** 047 * Creates a new value converter. 048 */ 049 public ColorValueConverter () 050 { 051 super(); 052 } 053 054 /** 055 * Converts the attribute to a string. 056 * 057 * @param o the attribute ({@link Integer} expected). 058 * @return A string representing the {@link Integer} value. 059 */ 060 public String toAttributeValue (final Object o) 061 { 062 if (!(o instanceof Color)) 063 { 064 throw new ClassCastException("Is no instance of java.awt.Color"); 065 } 066 final Color c = (Color) o; 067 068 try 069 { 070 final Field[] fields = Color.class.getFields(); 071 for (int i = 0; i < fields.length; i++) 072 { 073 final Field f = fields[i]; 074 if (Modifier.isPublic(f.getModifiers()) 075 && Modifier.isFinal(f.getModifiers()) 076 && Modifier.isStatic(f.getModifiers())) 077 { 078 final String name = f.getName(); 079 final Object oColor = f.get(null); 080 if (oColor instanceof Color) 081 { 082 if (c.equals(oColor)) 083 { 084 return name; 085 } 086 } 087 } 088 } 089 } 090 catch (Exception e) 091 { 092 // 093 } 094 095 // no defined constant color, so this must be a user defined color 096 final String color = Integer.toHexString(c.getRGB() & 0x00ffffff); 097 final StringBuffer retval = new StringBuffer(7); 098 retval.append("#"); 099 100 final int fillUp = 6 - color.length(); 101 for (int i = 0; i < fillUp; i++) 102 { 103 retval.append("0"); 104 } 105 106 retval.append(color); 107 return retval.toString(); 108 } 109 110 /** 111 * Converts a string to a {@link Integer}. 112 * 113 * @param value the string. 114 * @return a {@link Integer}. 115 */ 116 public Object toPropertyValue (final String value) 117 { 118 if (value == null) 119 { 120 return Color.black; 121 } 122 try 123 { 124 // get color by hex or octal value 125 return Color.decode(value); 126 } 127 catch (NumberFormatException nfe) 128 { 129 // if we can't decode lets try to get it by name 130 try 131 { 132 // try to get a color by name using reflection 133 final Field f = Color.class.getField(value); 134 135 return f.get(null); 136 } 137 catch (Exception ce) 138 { 139 throw new IllegalArgumentException 140 ("The color string '" + value + "' is not recognized."); 141 } 142 } 143 } 144 }