Source for org.jfree.report.util.beans.ColorValueConverter

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: ColorValueConverter.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.util.beans;
  32: 
  33: import java.awt.Color;
  34: import java.lang.reflect.Field;
  35: import java.lang.reflect.Modifier;
  36: 
  37: /**
  38:  * A class that handles the conversion of {@link Integer} attributes to and from their
  39:  * {@link String} representation.
  40:  *
  41:  * @author Thomas Morgner
  42:  */
  43: public class ColorValueConverter implements ValueConverter
  44: {
  45: 
  46:   /**
  47:    * Creates a new value converter.
  48:    */
  49:   public ColorValueConverter ()
  50:   {
  51:     super();
  52:   }
  53: 
  54:   /**
  55:    * Converts the attribute to a string.
  56:    *
  57:    * @param o the attribute ({@link Integer} expected).
  58:    * @return A string representing the {@link Integer} value.
  59:    */
  60:   public String toAttributeValue (final Object o)
  61:   {
  62:     if (!(o instanceof Color))
  63:     {
  64:       throw new ClassCastException("Is no instance of java.awt.Color");
  65:     }
  66:     final Color c = (Color) o;
  67: 
  68:     try
  69:     {
  70:       final Field[] fields = Color.class.getFields();
  71:       for (int i = 0; i < fields.length; i++)
  72:       {
  73:         final Field f = fields[i];
  74:         if (Modifier.isPublic(f.getModifiers())
  75:                 && Modifier.isFinal(f.getModifiers())
  76:                 && Modifier.isStatic(f.getModifiers()))
  77:         {
  78:           final String name = f.getName();
  79:           final Object oColor = f.get(null);
  80:           if (oColor instanceof Color)
  81:           {
  82:             if (c.equals(oColor))
  83:             {
  84:               return name;
  85:             }
  86:           }
  87:         }
  88:       }
  89:     }
  90:     catch (Exception e)
  91:     {
  92:       //
  93:     }
  94: 
  95:     // no defined constant color, so this must be a user defined color
  96:     final String color = Integer.toHexString(c.getRGB() & 0x00ffffff);
  97:     final StringBuffer retval = new StringBuffer(7);
  98:     retval.append("#");
  99: 
 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: }