001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import java.awt.geom.Ellipse2D;
022: import java.awt.geom.Rectangle2D;
023:
024: import org.apache.batik.dom.svg.AnimatedLiveAttributeValue;
025: import org.apache.batik.dom.svg.LiveAttributeException;
026: import org.apache.batik.dom.svg.SVGOMEllipseElement;
027: import org.apache.batik.gvt.ShapeNode;
028: import org.apache.batik.gvt.ShapePainter;
029:
030: import org.w3c.dom.Element;
031:
032: /**
033: * Bridge class for the <ellipse> element.
034: *
035: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
036: * @version $Id: SVGEllipseElementBridge.java 475477 2006-11-15 22:44:28Z cam $
037: */
038: public class SVGEllipseElementBridge extends SVGShapeElementBridge {
039:
040: /**
041: * Constructs a new bridge for the <ellipse> element.
042: */
043: public SVGEllipseElementBridge() {
044: }
045:
046: /**
047: * Returns 'ellipse'.
048: */
049: public String getLocalName() {
050: return SVG_ELLIPSE_TAG;
051: }
052:
053: /**
054: * Returns a new instance of this bridge.
055: */
056: public Bridge getInstance() {
057: return new SVGEllipseElementBridge();
058: }
059:
060: /**
061: * Constructs an ellipse according to the specified parameters.
062: *
063: * @param ctx the bridge context to use
064: * @param e the element that describes a rect element
065: * @param shapeNode the shape node to initialize
066: */
067: protected void buildShape(BridgeContext ctx, Element e,
068: ShapeNode shapeNode) {
069: try {
070: SVGOMEllipseElement ee = (SVGOMEllipseElement) e;
071:
072: // 'cx' attribute - default is 0
073: float cx = ee.getCx().getAnimVal().getValue();
074:
075: // 'cy' attribute - default is 0
076: float cy = ee.getCy().getAnimVal().getValue();
077:
078: // 'rx' attribute - required
079: float rx = ee.getRx().getAnimVal().getValue();
080:
081: // 'ry' attribute - required
082: float ry = ee.getRy().getAnimVal().getValue();
083:
084: shapeNode.setShape(new Ellipse2D.Float(cx - rx, cy - ry,
085: rx * 2, ry * 2));
086: } catch (LiveAttributeException ex) {
087: throw new BridgeException(ctx, ex);
088: }
089: }
090:
091: // BridgeUpdateHandler implementation //////////////////////////////////
092:
093: /**
094: * Invoked when the animated value of an animatable attribute has changed.
095: */
096: public void handleAnimatedAttributeChanged(
097: AnimatedLiveAttributeValue alav) {
098: if (alav.getNamespaceURI() == null) {
099: String ln = alav.getLocalName();
100: if (ln.equals(SVG_CX_ATTRIBUTE)
101: || ln.equals(SVG_CY_ATTRIBUTE)
102: || ln.equals(SVG_RX_ATTRIBUTE)
103: || ln.equals(SVG_RY_ATTRIBUTE)) {
104: buildShape(ctx, e, (ShapeNode) node);
105: handleGeometryChanged();
106: return;
107: }
108: }
109: super .handleAnimatedAttributeChanged(alav);
110: }
111:
112: protected ShapePainter createShapePainter(BridgeContext ctx,
113: Element e, ShapeNode shapeNode) {
114: Rectangle2D r2d = shapeNode.getShape().getBounds2D();
115: if ((r2d.getWidth() == 0) || (r2d.getHeight() == 0))
116: return null;
117: return super.createShapePainter(ctx, e, shapeNode);
118: }
119: }
|