KickJava   Java API By Example, From Geeks To Geeks.

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


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.RenderingHints JavaDoc;
21
22 import org.apache.batik.css.engine.CSSEngineEvent;
23 import org.apache.batik.css.engine.SVGCSSEngine;
24 import org.apache.batik.gvt.GraphicsNode;
25 import org.apache.batik.gvt.ShapeNode;
26 import org.apache.batik.gvt.ShapePainter;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * The base bridge class for shapes. Subclasses bridge <tt>ShapeNode</tt>.
31  *
32  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
33  * @version $Id: SVGShapeElementBridge.java,v 1.28 2004/08/20 19:29:46 deweese Exp $
34  */

35 public abstract class SVGShapeElementBridge extends AbstractGraphicsNodeBridge {
36
37     /**
38      * Constructs a new bridge for SVG shapes.
39      */

40     protected SVGShapeElementBridge() {}
41
42     /**
43      * Creates a graphics node using the specified BridgeContext and
44      * for the specified element.
45      *
46      * @param ctx the bridge context to use
47      * @param e the element that describes the graphics node to build
48      * @return a graphics node that represents the specified element
49      */

50     public GraphicsNode createGraphicsNode(BridgeContext ctx, Element JavaDoc e) {
51         ShapeNode shapeNode = (ShapeNode)super.createGraphicsNode(ctx, e);
52     if (shapeNode == null) {
53         return null;
54     }
55         // delegates to subclasses the shape construction
56
buildShape(ctx, e, shapeNode);
57
58         // 'shape-rendering' and 'color-rendering'
59
RenderingHints JavaDoc hints = null;
60         hints = CSSUtilities.convertColorRendering(e, hints);
61         hints = CSSUtilities.convertShapeRendering(e, hints);
62         if (hints != null)
63             shapeNode.setRenderingHints(hints);
64
65         return shapeNode;
66     }
67
68     /**
69      * Creates a <tt>ShapeNode</tt>.
70      */

71     protected GraphicsNode instantiateGraphicsNode() {
72         return new ShapeNode();
73     }
74
75     /**
76      * Builds using the specified BridgeContext and element, the
77      * specified graphics node.
78      *
79      * @param ctx the bridge context to use
80      * @param e the element that describes the graphics node to build
81      * @param node the graphics node to build
82      */

83     public void buildGraphicsNode(BridgeContext ctx,
84                                   Element JavaDoc e,
85                                   GraphicsNode node) {
86         ShapeNode shapeNode = (ShapeNode)node;
87         shapeNode.setShapePainter(createShapePainter(ctx, e, shapeNode));
88         super.buildGraphicsNode(ctx, e, node);
89     }
90
91     /**
92      * Creates the shape painter associated to the specified element.
93      * This implementation creates a shape painter considering the
94      * various fill and stroke properties.
95      *
96      * @param ctx the bridge context to use
97      * @param e the element that describes the shape painter to use
98      * @param shapeNode the shape node that is interested in its shape painter
99      */

100     protected ShapePainter createShapePainter(BridgeContext ctx,
101                                               Element JavaDoc e,
102                                               ShapeNode shapeNode) {
103         // 'fill'
104
// 'fill-opacity'
105
// 'stroke'
106
// 'stroke-opacity',
107
// 'stroke-width'
108
// 'stroke-linecap'
109
// 'stroke-linejoin'
110
// 'stroke-miterlimit'
111
// 'stroke-dasharray'
112
// 'stroke-dashoffset'
113
return PaintServer.convertFillAndStroke(e, shapeNode, ctx);
114     }
115
116     /**
117      * Initializes the specified ShapeNode's shape defined by the
118      * specified Element and using the specified bridge context.
119      *
120      * @param ctx the bridge context to use
121      * @param e the element that describes the shape node to build
122      * @param node the shape node to initialize
123      */

124     protected abstract void buildShape(BridgeContext ctx,
125                                        Element JavaDoc e,
126                                        ShapeNode node);
127
128     /**
129      * Returns false as shapes are not a container.
130      */

131     public boolean isComposite() {
132         return false;
133     }
134
135     // BridgeUpdateHandler implementation //////////////////////////////////
136

137     /**
138      * Invoked when the geometry of an graphical element has changed.
139      */

140     protected void handleGeometryChanged() {
141         super.handleGeometryChanged();
142         ShapeNode shapeNode = (ShapeNode)node;
143         shapeNode.setShapePainter(createShapePainter(ctx, e, shapeNode));
144     }
145
146     /**
147      * This flag bit indicates if a new shape painter has already been created.
148      * Avoid creating one ShapePainter per CSS property change
149      */

150     protected boolean hasNewShapePainter;
151
152     /**
153      * Invoked when CSS properties have changed on an element.
154      *
155      * @param evt the CSSEngine event that describes the update
156      */

157     public void handleCSSEngineEvent(CSSEngineEvent evt) {
158         hasNewShapePainter = false;
159         super.handleCSSEngineEvent(evt);
160     }
161
162     /**
163      * Invoked for each CSS property that has changed.
164      */

165     protected void handleCSSPropertyChanged(int property) {
166         switch(property) {
167         case SVGCSSEngine.FILL_INDEX:
168         case SVGCSSEngine.FILL_OPACITY_INDEX:
169         case SVGCSSEngine.STROKE_INDEX:
170         case SVGCSSEngine.STROKE_OPACITY_INDEX:
171             // Opportunity to just 'update' the existing shape painters...
172
case SVGCSSEngine.STROKE_WIDTH_INDEX:
173         case SVGCSSEngine.STROKE_LINECAP_INDEX:
174         case SVGCSSEngine.STROKE_LINEJOIN_INDEX:
175         case SVGCSSEngine.STROKE_MITERLIMIT_INDEX:
176         case SVGCSSEngine.STROKE_DASHARRAY_INDEX:
177         case SVGCSSEngine.STROKE_DASHOFFSET_INDEX: {
178             if (!hasNewShapePainter) {
179                 hasNewShapePainter = true;
180                 ShapeNode shapeNode = (ShapeNode)node;
181                 shapeNode.setShapePainter(createShapePainter(ctx, e, shapeNode));
182             }
183             break;
184         }
185         case SVGCSSEngine.SHAPE_RENDERING_INDEX: {
186             RenderingHints JavaDoc hints = node.getRenderingHints();
187             hints = CSSUtilities.convertShapeRendering(e, hints);
188             if (hints != null) {
189                 node.setRenderingHints(hints);
190             }
191             break;
192           }
193         case SVGCSSEngine.COLOR_RENDERING_INDEX: {
194             RenderingHints JavaDoc hints = node.getRenderingHints();
195             hints = CSSUtilities.convertColorRendering(e, hints);
196             if (hints != null) {
197                 node.setRenderingHints(hints);
198             }
199             break;
200         }
201         default:
202             super.handleCSSPropertyChanged(property);
203         }
204     }
205 }
206
Popular Tags