KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > SVGEllipseElementBridge


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.bridge;
19
20 import java.awt.geom.Ellipse2D JavaDoc;
21 import java.awt.geom.Rectangle2D JavaDoc;
22
23 import org.apache.batik.gvt.ShapeNode;
24 import org.apache.batik.gvt.ShapePainter;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.events.MutationEvent JavaDoc;
27
28 /**
29  * Bridge class for the <ellipse> element.
30  *
31  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
32  * @version $Id: SVGEllipseElementBridge.java,v 1.15 2004/08/18 07:12:33 vhardy Exp $
33  */

34 public class SVGEllipseElementBridge extends SVGShapeElementBridge {
35
36     /**
37      * Constructs a new bridge for the &lt;ellipse> element.
38      */

39     public SVGEllipseElementBridge() {}
40
41     /**
42      * Returns 'ellipse'.
43      */

44     public String JavaDoc getLocalName() {
45         return SVG_ELLIPSE_TAG;
46     }
47
48     /**
49      * Returns a new instance of this bridge.
50      */

51     public Bridge getInstance() {
52         return new SVGEllipseElementBridge();
53     }
54
55     /**
56      * Constructs an ellipse according to the specified parameters.
57      *
58      * @param ctx the bridge context to use
59      * @param e the element that describes a rect element
60      * @param shapeNode the shape node to initialize
61      */

62     protected void buildShape(BridgeContext ctx,
63                               Element JavaDoc e,
64                               ShapeNode shapeNode) {
65
66         UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
67         String JavaDoc s;
68
69         // 'cx' attribute - default is 0
70
s = e.getAttributeNS(null, SVG_CX_ATTRIBUTE);
71         float cx = 0;
72         if (s.length() != 0) {
73             cx = UnitProcessor.svgHorizontalCoordinateToUserSpace
74                 (s, SVG_CX_ATTRIBUTE, uctx);
75         }
76
77         // 'cy' attribute - default is 0
78
s = e.getAttributeNS(null, SVG_CY_ATTRIBUTE);
79         float cy = 0;
80         if (s.length() != 0) {
81             cy = UnitProcessor.svgVerticalCoordinateToUserSpace
82                 (s, SVG_CY_ATTRIBUTE, uctx);
83         }
84
85         // 'rx' attribute - required
86
s = e.getAttributeNS(null, SVG_RX_ATTRIBUTE);
87         float rx;
88         if (s.length() != 0) {
89             rx = UnitProcessor.svgHorizontalLengthToUserSpace
90                 (s, SVG_RX_ATTRIBUTE, uctx);
91         } else {
92             throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
93                                       new Object JavaDoc[] {SVG_RX_ATTRIBUTE, s});
94         }
95
96         // 'ry' attribute - required
97
s = e.getAttributeNS(null, SVG_RY_ATTRIBUTE);
98         float ry;
99         if (s.length() != 0) {
100             ry = UnitProcessor.svgVerticalLengthToUserSpace
101                 (s, SVG_RY_ATTRIBUTE, uctx);
102         } else {
103             throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
104                                       new Object JavaDoc[] {SVG_RY_ATTRIBUTE, s});
105         }
106
107         shapeNode.setShape(new Ellipse2D.Float JavaDoc(cx-rx, cy-ry, rx*2, ry*2));
108     }
109
110     // BridgeUpdateHandler implementation //////////////////////////////////
111

112     /**
113      * Invoked when an MutationEvent of type 'DOMAttrModified' is fired.
114      */

115     public void handleDOMAttrModifiedEvent(MutationEvent JavaDoc evt) {
116         String JavaDoc attrName = evt.getAttrName();
117         if (attrName.equals(SVG_CX_ATTRIBUTE) ||
118             attrName.equals(SVG_CY_ATTRIBUTE) ||
119             attrName.equals(SVG_RX_ATTRIBUTE) ||
120             attrName.equals(SVG_RY_ATTRIBUTE)) {
121
122             buildShape(ctx, e, (ShapeNode)node);
123             handleGeometryChanged();
124         } else {
125             super.handleDOMAttrModifiedEvent(evt);
126         }
127     }
128
129     protected ShapePainter createShapePainter(BridgeContext ctx,
130                                               Element JavaDoc e,
131                                               ShapeNode shapeNode) {
132         Rectangle2D JavaDoc r2d = shapeNode.getShape().getBounds2D();
133         if ((r2d.getWidth() == 0) || (r2d.getHeight() == 0))
134             return null;
135         return super.createShapePainter(ctx, e, shapeNode);
136     }
137 }
138
Popular Tags