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: * GradientPaintReadHandler
029: * ------------------------
030: * (C) Copyright 2003, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: GradientPaintReadHandler.java,v 1.2 2005/10/18 13:33:32 mungady Exp $
036: *
037: * Changes (from 25-Nov-2003)
038: * --------------------------
039: * 25-Nov-2003 : Added standard header and Javadocs (DG);
040: *
041: */
042:
043: package org.jfree.xml.parser.coretypes;
044:
045: import java.awt.Color;
046: import java.awt.GradientPaint;
047: import java.awt.geom.Point2D;
048:
049: import org.jfree.xml.parser.AbstractXmlReadHandler;
050: import org.jfree.xml.parser.XmlReadHandler;
051: import org.jfree.xml.parser.XmlReaderException;
052: import org.xml.sax.Attributes;
053: import org.xml.sax.SAXException;
054:
055: /**
056: * A SAX handler for reading a {@link GradientPaint} from an XML element.
057: */
058: public class GradientPaintReadHandler extends AbstractXmlReadHandler {
059:
060: /** The gradient paint under construction. */
061: private GradientPaint gradient;
062:
063: /** The handler for color 1. */
064: private XmlReadHandler color1Handler;
065:
066: /** The handler for color 2. */
067: private XmlReadHandler color2Handler;
068:
069: /** The handler for point 1. */
070: private XmlReadHandler point1Handler;
071:
072: /** The handler for point 2. */
073: private XmlReadHandler point2Handler;
074:
075: /**
076: * Creates a new handler.
077: */
078: public GradientPaintReadHandler() {
079: super ();
080: }
081:
082: /**
083: * Returns the gradient paint under construction.
084: *
085: * @return the gradient paint.
086: */
087: public Object getObject() {
088: return this .gradient;
089: }
090:
091: /**
092: * Returns the handler for a child element.
093: *
094: * @param tagName the tag name.
095: * @param atts the attributes.
096: *
097: * @return the handler.
098: * @throws SAXException to indicate a parsing error.
099: * @throws XmlReaderException if there is a reader error.
100: */
101: protected XmlReadHandler getHandlerForChild(final String tagName,
102: final Attributes atts) throws SAXException,
103: XmlReaderException {
104: if ("color1".equals(tagName)) {
105: this .color1Handler = getRootHandler().createHandler(
106: Color.class, tagName, atts);
107: return this .color1Handler;
108: } else if ("color2".equals(tagName)) {
109: this .color2Handler = getRootHandler().createHandler(
110: Color.class, tagName, atts);
111: return this .color2Handler;
112: } else if ("point1".equals(tagName)) {
113: this .point1Handler = getRootHandler().createHandler(
114: Point2D.class, tagName, atts);
115: return this .point1Handler;
116: } else if ("point2".equals(tagName)) {
117: this .point2Handler = getRootHandler().createHandler(
118: Point2D.class, tagName, atts);
119: return this .point2Handler;
120: }
121: return null;
122: }
123:
124: /**
125: * At the end of parsing the element, the gradient paint is constructed.
126: *
127: * @throws XmlReaderException if there is a parsing error.
128: */
129: protected void doneParsing() throws XmlReaderException {
130: if (this .point1Handler == null || this .point2Handler == null
131: || this .color1Handler == null
132: || this .color2Handler == null) {
133: throw new XmlReaderException(
134: "Not all required subelements are defined.");
135: }
136: this .gradient = new GradientPaint((Point2D) this .point1Handler
137: .getObject(), (Color) this .color1Handler.getObject(),
138: (Point2D) this .point2Handler.getObject(),
139: (Color) this.color2Handler.getObject());
140: }
141:
142: }
|