blob: be1408e5a2b94ce4ee899ea23d061e272e57b819 [file] [log] [blame]
/*
Copyright 2001-2003,2006 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.batik.bridge;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import org.apache.batik.dom.svg.AnimatedLiveAttributeValue;
import org.apache.batik.dom.svg.LiveAttributeException;
import org.apache.batik.dom.svg.SVGOMCircleElement;
import org.apache.batik.gvt.ShapeNode;
import org.apache.batik.gvt.ShapePainter;
import org.w3c.dom.Element;
/**
* Bridge class for the <circle> element.
*
* @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
* @version $Id$
*/
public class SVGCircleElementBridge extends SVGShapeElementBridge {
/**
* Constructs a new bridge for the &lt;circle> element.
*/
public SVGCircleElementBridge() {}
/**
* Returns 'circle'.
*/
public String getLocalName() {
return SVG_CIRCLE_TAG;
}
/**
* Returns a new instance of this bridge.
*/
public Bridge getInstance() {
return new SVGCircleElementBridge();
}
/**
* Constructs a circle according to the specified parameters.
*
* @param ctx the bridge context to use
* @param e the element that describes a rect element
* @param shapeNode the shape node to initialize
*/
protected void buildShape(BridgeContext ctx,
Element e,
ShapeNode shapeNode) {
try {
SVGOMCircleElement ce = (SVGOMCircleElement) e;
// 'cx' attribute - default is 0
float cx = ce.getCx().getAnimVal().getValue();
// 'cy' attribute - default is 0
float cy = ce.getCy().getAnimVal().getValue();
// 'r' attribute - required
float r = ce.getR().getAnimVal().getValue();
float x = cx - r;
float y = cy - r;
float w = r * 2;
shapeNode.setShape(new Ellipse2D.Float(x, y, w, w));
} catch (LiveAttributeException ex) {
throw new BridgeException
(ctx, ex.getElement(),
ex.isMissing() ? ERR_ATTRIBUTE_MISSING
: ERR_ATTRIBUTE_VALUE_MALFORMED,
new Object[] { ex.getAttributeName(), ex.getValue() });
}
}
// BridgeUpdateHandler implementation //////////////////////////////////
/**
* Invoked when the animated value of an animatable attribute has changed.
*/
public void handleAnimatedAttributeChanged
(AnimatedLiveAttributeValue alav) {
if (alav.getNamespaceURI() == null) {
String ln = alav.getLocalName();
if (ln.equals(SVG_CX_ATTRIBUTE)
|| ln.equals(SVG_CY_ATTRIBUTE)
|| ln.equals(SVG_R_ATTRIBUTE)) {
buildShape(ctx, e, (ShapeNode)node);
handleGeometryChanged();
return;
}
}
super.handleAnimatedAttributeChanged(alav);
}
protected ShapePainter createShapePainter(BridgeContext ctx,
Element e,
ShapeNode shapeNode) {
Rectangle2D r2d = shapeNode.getShape().getBounds2D();
if ((r2d.getWidth() == 0) || (r2d.getHeight() == 0))
return null;
return super.createShapePainter(ctx, e, shapeNode);
}
}