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, final Attributes atts) 102 throws SAXException, XmlReaderException { 103 if ("color1".equals(tagName)) { 104 this.color1Handler = getRootHandler().createHandler(Color.class, tagName, atts); 105 return this.color1Handler; 106 } 107 else if ("color2".equals(tagName)) { 108 this.color2Handler = getRootHandler().createHandler(Color.class, tagName, atts); 109 return this.color2Handler; 110 } 111 else if ("point1".equals(tagName)) { 112 this.point1Handler = getRootHandler().createHandler(Point2D.class, tagName, atts); 113 return this.point1Handler; 114 } 115 else if ("point2".equals(tagName)) { 116 this.point2Handler = getRootHandler().createHandler(Point2D.class, tagName, atts); 117 return this.point2Handler; 118 } 119 return null; 120 } 121 122 /** 123 * At the end of parsing the element, the gradient paint is constructed. 124 * 125 * @throws XmlReaderException if there is a parsing error. 126 */ 127 protected void doneParsing() throws XmlReaderException { 128 if (this.point1Handler == null || this.point2Handler == null 129 || this.color1Handler == null || this.color2Handler == null) { 130 throw new XmlReaderException("Not all required subelements are defined."); 131 } 132 this.gradient = new GradientPaint 133 ((Point2D) this.point1Handler.getObject(), 134 (Color) this.color1Handler.getObject(), 135 (Point2D) this.point2Handler.getObject(), 136 (Color) this.color2Handler.getObject()); 137 } 138 139 }