KickJava   Java API By Example, From Geeks To Geeks.

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


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 <circle> element.
30  *
31  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
32  * @version $Id: SVGCircleElementBridge.java,v 1.15 2004/08/18 07:12:33 vhardy Exp $
33  */

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

39     public SVGCircleElementBridge() {}
40
41     /**
42      * Returns 'circle'.
43      */

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

51     public Bridge getInstance() {
52         return new SVGCircleElementBridge();
53     }
54
55     /**
56      * Constructs a circle 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         UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
66         String JavaDoc s;
67
68         // 'cx' attribute - default is 0
69
s = e.getAttributeNS(null, SVG_CX_ATTRIBUTE);
70         float cx = 0;
71         if (s.length() != 0) {
72             cx = UnitProcessor.svgHorizontalCoordinateToUserSpace
73                 (s, SVG_CX_ATTRIBUTE, uctx);
74         }
75
76         // 'cy' attribute - default is 0
77
s = e.getAttributeNS(null, SVG_CY_ATTRIBUTE);
78         float cy = 0;
79         if (s.length() != 0) {
80             cy = UnitProcessor.svgVerticalCoordinateToUserSpace
81                 (s, SVG_CY_ATTRIBUTE, uctx);
82         }
83
84         // 'r' attribute - required
85
s = e.getAttributeNS(null, SVG_R_ATTRIBUTE);
86         float r;
87         if (s.length() != 0) {
88             r = UnitProcessor.svgOtherLengthToUserSpace
89                 (s, SVG_R_ATTRIBUTE, uctx);
90         } else {
91             throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
92                                       new Object JavaDoc[] {SVG_R_ATTRIBUTE, s});
93         }
94         float x = cx - r;
95         float y = cy - r;
96         float w = r * 2;
97         shapeNode.setShape(new Ellipse2D.Float JavaDoc(x, y, w, w));
98     }
99
100     // BridgeUpdateHandler implementation //////////////////////////////////
101

102     /**
103      * Invoked when an MutationEvent of type 'DOMAttrModified' is fired.
104      */

105     public void handleDOMAttrModifiedEvent(MutationEvent JavaDoc evt) {
106         String JavaDoc attrName = evt.getAttrName();
107         if (attrName.equals(SVG_CX_ATTRIBUTE) ||
108             attrName.equals(SVG_CY_ATTRIBUTE) ||
109             attrName.equals(SVG_R_ATTRIBUTE)) {
110
111             buildShape(ctx, e, (ShapeNode)node);
112             handleGeometryChanged();
113         } else {
114             super.handleDOMAttrModifiedEvent(evt);
115         }
116     }
117
118     protected ShapePainter createShapePainter(BridgeContext ctx,
119                                               Element JavaDoc e,
120                                               ShapeNode shapeNode) {
121         Rectangle2D JavaDoc r2d = shapeNode.getShape().getBounds2D();
122         if ((r2d.getWidth() == 0) || (r2d.getHeight() == 0))
123             return null;
124         return super.createShapePainter(ctx, e, shapeNode);
125     }
126 }
127
Popular Tags