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.SVGOMCircleElement;
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 <circle> element.
034: *
035: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
036: * @version $Id: SVGCircleElementBridge.java 475477 2006-11-15 22:44:28Z cam $
037: */
038: public class SVGCircleElementBridge extends SVGShapeElementBridge {
039:
040: /**
041: * Constructs a new bridge for the <circle> element.
042: */
043: public SVGCircleElementBridge() {
044: }
045:
046: /**
047: * Returns 'circle'.
048: */
049: public String getLocalName() {
050: return SVG_CIRCLE_TAG;
051: }
052:
053: /**
054: * Returns a new instance of this bridge.
055: */
056: public Bridge getInstance() {
057: return new SVGCircleElementBridge();
058: }
059:
060: /**
061: * Constructs a circle 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: SVGOMCircleElement ce = (SVGOMCircleElement) e;
071:
072: // 'cx' attribute - default is 0
073: float cx = ce.getCx().getAnimVal().getValue();
074:
075: // 'cy' attribute - default is 0
076: float cy = ce.getCy().getAnimVal().getValue();
077:
078: // 'r' attribute - required
079: float r = ce.getR().getAnimVal().getValue();
080:
081: float x = cx - r;
082: float y = cy - r;
083: float w = r * 2;
084: shapeNode.setShape(new Ellipse2D.Float(x, y, w, w));
085: } catch (LiveAttributeException ex) {
086: throw new BridgeException(ctx, ex);
087: }
088: }
089:
090: // BridgeUpdateHandler implementation //////////////////////////////////
091:
092: /**
093: * Invoked when the animated value of an animatable attribute has changed.
094: */
095: public void handleAnimatedAttributeChanged(
096: AnimatedLiveAttributeValue alav) {
097: if (alav.getNamespaceURI() == null) {
098: String ln = alav.getLocalName();
099: if (ln.equals(SVG_CX_ATTRIBUTE)
100: || ln.equals(SVG_CY_ATTRIBUTE)
101: || ln.equals(SVG_R_ATTRIBUTE)) {
102: buildShape(ctx, e, (ShapeNode) node);
103: handleGeometryChanged();
104: return;
105: }
106: }
107: super .handleAnimatedAttributeChanged(alav);
108: }
109:
110: protected ShapePainter createShapePainter(BridgeContext ctx,
111: Element e, ShapeNode shapeNode) {
112: Rectangle2D r2d = shapeNode.getShape().getBounds2D();
113: if ((r2d.getWidth() == 0) || (r2d.getHeight() == 0))
114: return null;
115: return super.createShapePainter(ctx, e, shapeNode);
116: }
117: }
|