Source for org.jfree.xml.parser.coretypes.GradientPaintReadHandler

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ------------------------
  28:  * GradientPaintReadHandler
  29:  * ------------------------
  30:  * (C) Copyright 2003, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: GradientPaintReadHandler.java,v 1.2 2005/10/18 13:33:32 mungady Exp $
  36:  *
  37:  * Changes (from 25-Nov-2003)
  38:  * --------------------------
  39:  * 25-Nov-2003 : Added standard header and Javadocs (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.xml.parser.coretypes;
  44: 
  45: import java.awt.Color;
  46: import java.awt.GradientPaint;
  47: import java.awt.geom.Point2D;
  48: 
  49: import org.jfree.xml.parser.AbstractXmlReadHandler;
  50: import org.jfree.xml.parser.XmlReadHandler;
  51: import org.jfree.xml.parser.XmlReaderException;
  52: import org.xml.sax.Attributes;
  53: import org.xml.sax.SAXException;
  54: 
  55: /**
  56:  * A SAX handler for reading a {@link GradientPaint} from an XML element.
  57:  */
  58: public class GradientPaintReadHandler extends AbstractXmlReadHandler  {
  59: 
  60:     /** The gradient paint under construction. */
  61:     private GradientPaint gradient;
  62: 
  63:     /** The handler for color 1. */
  64:     private XmlReadHandler color1Handler;
  65: 
  66:     /** The handler for color 2. */
  67:     private XmlReadHandler color2Handler;
  68: 
  69:     /** The handler for point 1. */
  70:     private XmlReadHandler point1Handler;
  71: 
  72:     /** The handler for point 2. */
  73:     private XmlReadHandler point2Handler;
  74: 
  75:     /**
  76:      * Creates a new handler.
  77:      */
  78:     public GradientPaintReadHandler() {
  79:         super();
  80:     }
  81: 
  82:     /**
  83:      * Returns the gradient paint under construction.
  84:      * 
  85:      * @return the gradient paint.
  86:      */
  87:     public Object getObject() {
  88:         return this.gradient;
  89:     }
  90: 
  91:     /**
  92:      * Returns the handler for a child element.
  93:      * 
  94:      * @param tagName  the tag name.
  95:      * @param atts  the attributes.
  96:      * 
  97:      * @return the handler.
  98:      * @throws SAXException to indicate a parsing error.
  99:      * @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: }