public Object convert()

in jelly-tags/swing/src/main/java/org/apache/commons/jelly/tags/swing/converters/ColorConverter.java [41:86]


    public Object convert(Class type, Object value) {
        if (value != null) {
            String s = value.toString();
            if (s.length() <= 1) {
                throw new IllegalArgumentException(usageText);
            }
            if (s.charAt(0) == '#') {
                if (s.length() != 7) {
                    throw new IllegalArgumentException(usageText);
                }
                int colorValue = 0;
                try {
                    colorValue = Integer.parseInt(s.substring(1), 16);
                    return new Color(colorValue);
                }
                catch (NumberFormatException ex) {
                    throw new IllegalArgumentException(
                        "Can't parse \""
                            + s
                            + "\" as an hexadecimal number: "
                            + ex);
                }
            }
            else {
                // a color name
                try {
                    // could it be this is already somewhere: get the value of  a static final by string
                    Field f = SystemColor.class.getField(s);
                    if (f == null
                        || !Modifier.isStatic(f.getModifiers())
                        || !Modifier.isFinal(f.getModifiers())
                        || !Modifier.isPublic(f.getModifiers())
                        || !Color.class.isAssignableFrom(f.getType())) {

                        throw new IllegalArgumentException(usageText);
                    }
                    return (Color) f.get(SystemColor.class);
                }
                catch (Exception ex) {
                    throw new IllegalArgumentException(
                        "Can't parse \"" + s + "\" as a color-name: " + ex);
                }
            }
        }
        return null;
    }