KickJava   Java API By Example, From Geeks To Geeks.

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


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.Shape JavaDoc;
21 import java.awt.geom.Rectangle2D JavaDoc;
22 import java.awt.geom.RoundRectangle2D JavaDoc;
23
24 import org.apache.batik.gvt.ShapeNode;
25 import org.apache.batik.gvt.ShapePainter;
26
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.events.MutationEvent JavaDoc;
29
30 /**
31  * Bridge class for the <rect> element.
32  *
33  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
34  * @version $Id: SVGRectElementBridge.java,v 1.16 2004/08/18 07:12:35 vhardy Exp $
35  */

36 public class SVGRectElementBridge extends SVGShapeElementBridge {
37
38     /**
39      * Constructs a new bridge for the &lt;rect> element.
40      */

41     public SVGRectElementBridge() {}
42
43     /**
44      * Returns 'rect'.
45      */

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

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

64     protected void buildShape(BridgeContext ctx,
65                               Element JavaDoc e,
66                               ShapeNode shapeNode) {
67
68         UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
69         String JavaDoc s;
70
71         // 'x' attribute - default is 0
72
s = e.getAttributeNS(null, SVG_X_ATTRIBUTE);
73         float x = 0;
74         if (s.length() != 0) {
75             x = UnitProcessor.svgHorizontalCoordinateToUserSpace
76                 (s, SVG_X_ATTRIBUTE, uctx);
77         }
78
79         // 'y' attribute - default is 0
80
s = e.getAttributeNS(null, SVG_Y_ATTRIBUTE);
81         float y = 0;
82         if (s.length() != 0) {
83             y = UnitProcessor.svgVerticalCoordinateToUserSpace
84                 (s, SVG_Y_ATTRIBUTE, uctx);
85         }
86
87         // 'width' attribute - required
88
s = e.getAttributeNS(null, SVG_WIDTH_ATTRIBUTE);
89         float w;
90         if (s.length() != 0) {
91             w = UnitProcessor.svgHorizontalLengthToUserSpace
92                 (s, SVG_WIDTH_ATTRIBUTE, uctx);
93         } else {
94             throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
95                                       new Object JavaDoc[] {SVG_WIDTH_ATTRIBUTE, s});
96         }
97
98         // 'height' attribute - required
99
s = e.getAttributeNS(null, SVG_HEIGHT_ATTRIBUTE);
100         float h;
101         if (s.length() != 0) {
102             h = UnitProcessor.svgVerticalLengthToUserSpace
103                 (s, SVG_HEIGHT_ATTRIBUTE, uctx);
104         } else {
105             throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
106                                       new Object JavaDoc[] {SVG_HEIGHT_ATTRIBUTE, s});
107         }
108
109         // 'rx' attribute - default is 0
110
s = e.getAttributeNS(null, SVG_RX_ATTRIBUTE);
111         boolean rxs = (s.length() != 0);
112         float rx = 0;
113         if (rxs) {
114             rx = UnitProcessor.svgHorizontalLengthToUserSpace
115                 (s, SVG_RX_ATTRIBUTE, uctx);
116         }
117         rx = (rx > w / 2) ? w / 2 : rx;
118
119         // 'ry' attribute - default is 0
120
s = e.getAttributeNS(null, SVG_RY_ATTRIBUTE);
121         boolean rys = (s.length() != 0);
122         float ry = 0;
123         if (rys) {
124             ry = UnitProcessor.svgVerticalLengthToUserSpace
125                 (s, SVG_RY_ATTRIBUTE, uctx);
126         }
127         ry = (ry > h / 2) ? h / 2 : ry;
128
129         Shape JavaDoc shape = null;
130         if (rxs && rys) {
131             if (rx == 0 || ry == 0) {
132                 shape = new Rectangle2D.Float JavaDoc(x, y, w, h);
133             } else {
134                 shape = new RoundRectangle2D.Float JavaDoc(x, y, w, h, rx*2, ry*2);
135             }
136         } else if (rxs) {
137             if (rx == 0) {
138                 shape = new Rectangle2D.Float JavaDoc(x, y, w, h);
139             } else {
140                 shape = new RoundRectangle2D.Float JavaDoc(x, y, w, h, rx*2, rx*2);
141             }
142         } else if (rys) {
143             if (ry == 0) {
144                 shape = new Rectangle2D.Float JavaDoc(x, y, w, h);
145             } else {
146                 shape = new RoundRectangle2D.Float JavaDoc(x, y, w, h, ry*2, ry*2);
147             }
148         } else {
149             shape = new Rectangle2D.Float JavaDoc(x, y, w, h);
150         }
151         shapeNode.setShape(shape);
152     }
153
154     // BridgeUpdateHandler implementation //////////////////////////////////
155

156     /**
157      * Invoked when an MutationEvent of type 'DOMAttrModified' is fired.
158      */

159     public void handleDOMAttrModifiedEvent(MutationEvent JavaDoc evt) {
160         String JavaDoc attrName = evt.getAttrName();
161         if (attrName.equals(SVG_X_ATTRIBUTE) ||
162             attrName.equals(SVG_Y_ATTRIBUTE) ||
163             attrName.equals(SVG_WIDTH_ATTRIBUTE) ||
164             attrName.equals(SVG_HEIGHT_ATTRIBUTE) ||
165             attrName.equals(SVG_RX_ATTRIBUTE) ||
166             attrName.equals(SVG_RY_ATTRIBUTE)) {
167
168             buildShape(ctx, e, (ShapeNode)node);
169             handleGeometryChanged();
170         } else {
171             super.handleDOMAttrModifiedEvent(evt);
172         }
173     }
174
175
176     protected ShapePainter createShapePainter(BridgeContext ctx,
177                                               Element JavaDoc e,
178                                               ShapeNode shapeNode) {
179         Shape JavaDoc shape = shapeNode.getShape();
180         Rectangle2D JavaDoc r2d = shape.getBounds2D();
181         if ((r2d.getWidth() == 0) || (r2d.getHeight() == 0))
182             return null;
183         return super.createShapePainter(ctx, e, shapeNode);
184     }
185 }
186
Popular Tags