001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ColorTypeProperty.java 377045 2006-02-11 20:23:47Z jeremias $ */
019:
020: package org.apache.fop.fo.properties;
021:
022: import java.awt.Color;
023:
024: import org.apache.fop.apps.FOUserAgent;
025: import org.apache.fop.fo.FObj;
026: import org.apache.fop.fo.PropertyList;
027: import org.apache.fop.fo.expr.PropertyException;
028: import org.apache.fop.util.ColorUtil;
029:
030: /**
031: * Superclass for properties that wrap Color values
032: */
033: public class ColorProperty extends Property {
034:
035: /**
036: * The color represented by this property.
037: */
038: protected final Color color;
039:
040: /**
041: * Inner class for creating instances of ColorTypeProperty
042: */
043: public static class Maker extends PropertyMaker {
044:
045: /**
046: * @param propId the id of the property for which a Maker should be created
047: */
048: public Maker(int propId) {
049: super (propId);
050: }
051:
052: /**
053: * Return a ColorProperty object based on the passed Property object.
054: * This method is called if the Property object built by the parser
055: * isn't the right type for this property.
056: *
057: * @param p
058: * The Property object return by the expression parser
059: * @param propertyList
060: * The PropertyList object being built for this FO.
061: * @param fo
062: * The parent FO for the FO whose property is being made.
063: * @return A Property of the correct type or null if the parsed value
064: * can't be converted to the correct type.
065: * @throws PropertyException
066: * for invalid or inconsistent FO input
067: * @see org.apache.fop.fo.properties.PropertyMaker#convertProperty(
068: * org.apache.fop.fo.properties.Property,
069: * org.apache.fop.fo.PropertyList, org.apache.fop.fo.FObj)
070: */
071: public Property convertProperty(Property p,
072: PropertyList propertyList, FObj fo)
073: throws PropertyException {
074: if (p instanceof ColorProperty) {
075: return p;
076: }
077: FObj fobj = (fo == null ? propertyList.getFObj() : fo);
078: FOUserAgent ua = (fobj == null ? null : fobj.getUserAgent());
079: Color val = p.getColor(ua);
080: if (val != null) {
081: return new ColorProperty(val);
082: }
083: return convertPropertyDatatype(p, propertyList, fo);
084: }
085:
086: }
087:
088: /**
089: * Set the color given a particular String. For a full List of supported
090: * values please see ColorUtil.
091: *
092: * @param foUserAgent FOP user agent
093: * @param value RGB value as String to be parsed
094: * @throws PropertyException if the value can't be parsed
095: * @see ColorUtil#parseColorString(String)
096: */
097: public ColorProperty(FOUserAgent foUserAgent, String value)
098: throws PropertyException {
099: this .color = ColorUtil.parseColorString(foUserAgent, value);
100: }
101:
102: /**
103: * Create a new ColorProperty with a given color.
104: *
105: * @param value the color to use.
106: */
107: public ColorProperty(Color value) {
108: this .color = value;
109: }
110:
111: /**
112: * Returns an AWT instance of this color
113: * @param foUserAgent FOP user agent
114: * @return float the AWT color represented by this ColorType instance
115: */
116: public Color getColor(FOUserAgent foUserAgent) {
117: return color;
118: }
119:
120: /**
121: * @see java.lang.Object#toString()
122: */
123: public String toString() {
124: return ColorUtil.colorToString(color);
125: }
126:
127: /**
128: * Can't convert to any other types
129: * @return this.colorType
130: */
131: public ColorProperty getColorProperty() {
132: return this ;
133: }
134:
135: /**
136: * @return this.colorType cast as an Object
137: */
138: public Object getObject() {
139: return this;
140: }
141: }
|