001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------
028: * PaintUtilities.java
029: * -------------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: PaintUtilities.java,v 1.9 2005/11/16 15:58:41 taqua Exp $
036: *
037: * Changes
038: * -------
039: * 13-Nov-2003 : Version 1 (DG);
040: * 04-Oct-2004 : Renamed PaintUtils --> PaintUtilities (DG);
041: * 23-Feb-2005 : Rewrote equal() method with less indenting required (DG);
042: *
043: */
044:
045: package org.jfree.util;
046:
047: import java.awt.GradientPaint;
048: import java.awt.Paint;
049: import java.awt.Color;
050: import java.lang.reflect.Field;
051: import java.lang.reflect.Modifier;
052:
053: /**
054: * Utility code that relates to <code>Paint</code> objects.
055: *
056: * @author David Gilbert
057: */
058: public class PaintUtilities {
059:
060: /**
061: * Private constructor prevents object creation.
062: */
063: private PaintUtilities() {
064: }
065:
066: /**
067: * Returns <code>true</code> if the two <code>Paint</code> objects are equal
068: * OR both <code>null</code>. This method handles
069: * <code>GradientPaint</code> as a special case.
070: *
071: * @param p1 paint 1 (<code>null</code> permitted).
072: * @param p2 paint 2 (<code>null</code> permitted).
073: *
074: * @return A boolean.
075: */
076: public static boolean equal(final Paint p1, final Paint p2) {
077:
078: // handle cases where either or both arguments are null
079: if (p1 == null) {
080: return (p2 == null);
081: }
082: if (p2 == null) {
083: return false;
084: }
085:
086: boolean result = false;
087: // handle GradientPaint as a special case...
088: if (p1 instanceof GradientPaint && p2 instanceof GradientPaint) {
089: final GradientPaint gp1 = (GradientPaint) p1;
090: final GradientPaint gp2 = (GradientPaint) p2;
091: result = gp1.getColor1().equals(gp2.getColor1())
092: && gp1.getColor2().equals(gp2.getColor2())
093: && gp1.getPoint1().equals(gp2.getPoint1())
094: && gp1.getPoint2().equals(gp2.getPoint2())
095: && gp1.isCyclic() == gp2.isCyclic()
096: && gp1.getTransparency() == gp1.getTransparency();
097: } else {
098: result = p1.equals(p2);
099: }
100: return result;
101:
102: }
103:
104: /**
105: * Converts a color into a string. If the color is equal to one of the
106: * defined constant colors, that name is returned instead. Otherwise the
107: * color is returned as hex-string.
108: *
109: * @param c the color.
110: * @return the string for this color.
111: */
112: public static String colorToString(final Color c) {
113: try {
114: final Field[] fields = Color.class.getFields();
115: for (int i = 0; i < fields.length; i++) {
116: final Field f = fields[i];
117: if (Modifier.isPublic(f.getModifiers())
118: && Modifier.isFinal(f.getModifiers())
119: && Modifier.isStatic(f.getModifiers())) {
120: final String name = f.getName();
121: final Object oColor = f.get(null);
122: if (oColor instanceof Color) {
123: if (c.equals(oColor)) {
124: return name;
125: }
126: }
127: }
128: }
129: } catch (Exception e) {
130: //
131: }
132:
133: // no defined constant color, so this must be a user defined color
134: final String color = Integer
135: .toHexString(c.getRGB() & 0x00ffffff);
136: final StringBuffer retval = new StringBuffer(7);
137: retval.append("#");
138:
139: final int fillUp = 6 - color.length();
140: for (int i = 0; i < fillUp; i++) {
141: retval.append("0");
142: }
143:
144: retval.append(color);
145: return retval.toString();
146: }
147:
148: /**
149: * Converts a given string into a color.
150: *
151: * @param value the string, either a name or a hex-string.
152: * @return the color.
153: */
154: public static Color stringToColor(final String value) {
155: if (value == null) {
156: return Color.black;
157: }
158: try {
159: // get color by hex or octal value
160: return Color.decode(value);
161: } catch (NumberFormatException nfe) {
162: // if we can't decode lets try to get it by name
163: try {
164: // try to get a color by name using reflection
165: final Field f = Color.class.getField(value);
166:
167: return (Color) f.get(null);
168: } catch (Exception ce) {
169: Log.info("No such Color : " + value);
170: // if we can't get any color return black
171: return Color.black;
172: }
173: }
174: }
175: }
|