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.Shape;
022: import java.awt.geom.Rectangle2D;
023: import java.awt.geom.RoundRectangle2D;
024:
025: import org.apache.batik.dom.svg.AnimatedLiveAttributeValue;
026: import org.apache.batik.dom.svg.LiveAttributeException;
027: import org.apache.batik.dom.svg.SVGOMRectElement;
028: import org.apache.batik.gvt.ShapeNode;
029: import org.apache.batik.gvt.ShapePainter;
030:
031: import org.w3c.dom.Element;
032:
033: /**
034: * Bridge class for the <rect> element.
035: *
036: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
037: * @version $Id: SVGRectElementBridge.java 475477 2006-11-15 22:44:28Z cam $
038: */
039: public class SVGRectElementBridge extends SVGShapeElementBridge {
040:
041: /**
042: * Constructs a new bridge for the <rect> element.
043: */
044: public SVGRectElementBridge() {
045: }
046:
047: /**
048: * Returns 'rect'.
049: */
050: public String getLocalName() {
051: return SVG_RECT_TAG;
052: }
053:
054: /**
055: * Returns a new instance of this bridge.
056: */
057: public Bridge getInstance() {
058: return new SVGRectElementBridge();
059: }
060:
061: /**
062: * Constructs a rectangle according to the specified parameters.
063: *
064: * @param ctx the bridge context to use
065: * @param e the element that describes a rect element
066: * @param shapeNode the shape node to initialize
067: */
068: protected void buildShape(BridgeContext ctx, Element e,
069: ShapeNode shapeNode) {
070:
071: try {
072: SVGOMRectElement re = (SVGOMRectElement) e;
073:
074: // 'x' attribute - default is 0
075: float x = re.getX().getAnimVal().getValue();
076:
077: // 'y' attribute - default is 0
078: float y = re.getY().getAnimVal().getValue();
079:
080: // 'width' attribute - required
081: float w = re.getWidth().getAnimVal().getValue();
082:
083: // 'height' attribute - required
084: float h = re.getHeight().getAnimVal().getValue();
085:
086: // 'rx' attribute - default is 0
087: float rx = re.getRx().getAnimVal().getValue();
088: if (rx > w / 2) {
089: rx = w / 2;
090: }
091:
092: // 'ry' attribute - default is rx
093: float ry = re.getRy().getAnimVal().getValue();
094: if (ry > h / 2) {
095: ry = h / 2;
096: }
097:
098: Shape shape;
099: if (rx == 0 || ry == 0) {
100: shape = new Rectangle2D.Float(x, y, w, h);
101: } else {
102: shape = new RoundRectangle2D.Float(x, y, w, h, rx * 2,
103: ry * 2);
104: }
105: shapeNode.setShape(shape);
106: } catch (LiveAttributeException ex) {
107: throw new BridgeException(ctx, ex);
108: }
109: }
110:
111: // BridgeUpdateHandler implementation //////////////////////////////////
112:
113: /**
114: * Invoked when the animated value of an animatable attribute has changed.
115: */
116: public void handleAnimatedAttributeChanged(
117: AnimatedLiveAttributeValue alav) {
118: if (alav.getNamespaceURI() == null) {
119: String ln = alav.getLocalName();
120: if (ln.equals(SVG_X_ATTRIBUTE)
121: || ln.equals(SVG_Y_ATTRIBUTE)
122: || ln.equals(SVG_WIDTH_ATTRIBUTE)
123: || ln.equals(SVG_HEIGHT_ATTRIBUTE)
124: || ln.equals(SVG_RX_ATTRIBUTE)
125: || ln.equals(SVG_RY_ATTRIBUTE)) {
126: buildShape(ctx, e, (ShapeNode) node);
127: handleGeometryChanged();
128: return;
129: }
130: }
131: super .handleAnimatedAttributeChanged(alav);
132: }
133:
134: protected ShapePainter createShapePainter(BridgeContext ctx,
135: Element e, ShapeNode shapeNode) {
136: Shape shape = shapeNode.getShape();
137: Rectangle2D r2d = shape.getBounds2D();
138: if ((r2d.getWidth() == 0) || (r2d.getHeight() == 0))
139: return null;
140: return super.createShapePainter(ctx, e, shapeNode);
141: }
142: }
|