Author: rwhitcomb Date: Fri Apr 2 03:57:08 2021 New Revision: 1888284 URL: http://svn.apache.org/viewvc?rev=1888284&view=rev Log: PIVOT-1056,PIVOT-1014: Move the new "xxxFromObject" methods from ComponentSkin into FontUtilities and ColorUtilities so they can be used in other places (decorators, for example).
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java?rev=1888284&r1=1888283&r2=1888284&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java Fri Apr 2 03:57:08 2021 @@ -19,6 +19,7 @@ package org.apache.pivot.wtk; import java.awt.Font; import java.util.Locale; +import org.apache.pivot.collections.Dictionary; import org.apache.pivot.json.JSONSerializer; import org.apache.pivot.serialization.SerializationException; import org.apache.pivot.util.Utils; @@ -259,4 +260,32 @@ public final class FontUtilities { return adjustedSize; } + /** + * Convert any object we support into its corresponding font. + * <p> Uses {@link #decodeFont} or {@link Theme#deriveFont} + * to do the work. + * + * @param fontValue The object to be converted to a font. + * @return The converted font. + * @throws IllegalArgumentException if the value is {@code null} or + * cannot be converted. + */ + public static Font fromObject(final Object fontValue) { + Utils.checkNull(fontValue, "font"); + + if (fontValue instanceof Font) { + return (Font) fontValue; + } else if (fontValue instanceof String) { + return decodeFont((String) fontValue); + } else if (fontValue instanceof Dictionary) { + @SuppressWarnings("unchecked") + Dictionary<String, ?> fontDictionary = (Dictionary<String, ?>) fontValue; + return Theme.deriveFont(fontDictionary); + } else { + throw new IllegalArgumentException("Unable to convert " + + fontValue.getClass().getSimpleName() + " to Font!"); + } + } + + } Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java?rev=1888284&r1=1888283&r2=1888284&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java Fri Apr 2 03:57:08 2021 @@ -19,7 +19,6 @@ package org.apache.pivot.wtk.skin; import java.awt.Color; import java.awt.Font; -import org.apache.pivot.collections.Dictionary; import org.apache.pivot.collections.EnumSet; import org.apache.pivot.util.Utils; import org.apache.pivot.wtk.Bounds; @@ -54,6 +53,7 @@ import org.apache.pivot.wtk.Style; import org.apache.pivot.wtk.TextInputMethodListener; import org.apache.pivot.wtk.Theme; import org.apache.pivot.wtk.Tooltip; +import org.apache.pivot.wtk.util.ColorUtilities; /** * Abstract base class for component skins. @@ -496,22 +496,10 @@ public abstract class ComponentSkin impl * @return The converted font. * @throws IllegalArgumentException if the value is {@code null} or * cannot be converted. + * @see FontUtilities#fromObject */ public Font fontFromObject(final Object fontValue) { - Utils.checkNull(fontValue, "font"); - - if (fontValue instanceof Font) { - return (Font) fontValue; - } else if (fontValue instanceof String) { - return FontUtilities.decodeFont((String) fontValue); - } else if (fontValue instanceof Dictionary) { - @SuppressWarnings("unchecked") - Dictionary<String, ?> fontDictionary = (Dictionary<String, ?>) fontValue; - return Theme.deriveFont(fontDictionary); - } else { - throw new IllegalArgumentException("Unable to convert " - + fontValue.getClass().getSimpleName() + " to Font!"); - } + return FontUtilities.fromObject(fontValue); } /** @@ -595,9 +583,10 @@ public abstract class ComponentSkin impl * color palette. * @return The real {@link Color} value. * @throws IllegalArgumentException if the {@code colorValue} is {@code null} or of a type we don't recognize. + * @see ColorUtilities#fromObject */ public final Color colorFromObject(final Object colorValue) { - return colorFromObject(colorValue, null, false); + return ColorUtilities.fromObject(colorValue, null, false); } /** @@ -610,9 +599,10 @@ public abstract class ComponentSkin impl * @return The real {@link Color} value. * @throws IllegalArgumentException if the {@code colorValue} is {@code null} (unless {@code allowNull} * is {@code true}), or of a type we don't recognize. + * @see ColorUtilities#fromObject */ public final Color colorFromObject(final Object colorValue, final boolean allowNull) { - return colorFromObject(colorValue, null, allowNull); + return ColorUtilities.fromObject(colorValue, null, allowNull); } /** @@ -624,9 +614,10 @@ public abstract class ComponentSkin impl * @param description An optional description for the call to {@link Utils#checkNull} in case of a null input value. * @return The real {@link Color} value. * @throws IllegalArgumentException if the {@code colorValue} is {@code null}, or of a type we don't recognize. + * @see ColorUtilities#fromObject */ public final Color colorFromObject(final Object colorValue, final String description) { - return colorFromObject(colorValue, description, false); + return ColorUtilities.fromObject(colorValue, description, false); } /** @@ -640,34 +631,10 @@ public abstract class ComponentSkin impl * @return The real {@link Color} value. * @throws IllegalArgumentException if the {@code colorValue} is {@code null} (unless {@code allowNull} * is {@code true}), or of a type we don't recognize. + * @see ColorUtilities#fromObject */ public final Color colorFromObject(final Object colorValue, final String description, final boolean allowNull) { - if (!allowNull) { - Utils.checkNull(colorValue, description); - } - - Color color; - - if (allowNull && colorValue == null) { - color = null; - } else if (colorValue instanceof Color) { - color = (Color) colorValue; - } else if (colorValue instanceof String) { - Color decodedColor = GraphicsUtilities.decodeColor((String) colorValue); - if (!allowNull) { - Utils.checkNull(decodedColor, description); - } - color = decodedColor; - } else if (colorValue instanceof CSSColor) { - color = ((CSSColor) colorValue).getColor(); - } else if (colorValue instanceof Number) { - color = getColor(((Number) colorValue).intValue()); - } else { - throw new IllegalArgumentException("Object of type " - + colorValue.getClass().getName() + " cannot be converted to a Color."); - } - - return color; + return ColorUtilities.fromObject(colorValue, description, allowNull); } /** Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java?rev=1888284&r1=1888283&r2=1888284&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java Fri Apr 2 03:57:08 2021 @@ -18,8 +18,10 @@ package org.apache.pivot.wtk.util; import java.awt.Color; +import org.apache.pivot.util.Utils; import org.apache.pivot.wtk.Theme; import org.apache.pivot.wtk.CSSColor; +import org.apache.pivot.wtk.GraphicsUtilities; /** @@ -252,4 +254,46 @@ public final class ColorUtilities { return toStringValue(color.getColor()); } + /** + * Interpret an object as a color value. + * + * @param colorValue One of a {@link String} (interpreted by {@link GraphicsUtilities#decodeColor(String,String)}), + * a straight {@link Color}, one of our {@link CSSColor} values, or an integer index into the theme's + * color palette. + * @param description An optional description for the call to {@link Utils#checkNull} in case of a null input value. + * @param allowNull Whether or not to allow a null color. + * @return The real {@link Color} value. + * @throws IllegalArgumentException if the {@code colorValue} is {@code null} (unless {@code allowNull} + * is {@code true}), or of a type we don't recognize. + */ + public static Color fromObject(final Object colorValue, final String description, final boolean allowNull) { + if (!allowNull) { + Utils.checkNull(colorValue, description); + } + + Color color; + + if (allowNull && colorValue == null) { + color = null; + } else if (colorValue instanceof Color) { + color = (Color) colorValue; + } else if (colorValue instanceof String) { + Color decodedColor = GraphicsUtilities.decodeColor((String) colorValue); + if (!allowNull) { + Utils.checkNull(decodedColor, description); + } + color = decodedColor; + } else if (colorValue instanceof CSSColor) { + color = ((CSSColor) colorValue).getColor(); + } else if (colorValue instanceof Number) { + Theme theme = Theme.getTheme(); + color = theme.getColor(((Number) colorValue).intValue()); + } else { + throw new IllegalArgumentException("Object of type " + + colorValue.getClass().getName() + " cannot be converted to a Color."); + } + + return color; + } + }