KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2001-2004 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.GeneralPath JavaDoc;
22 import java.awt.geom.Point2D JavaDoc;
23
24 import org.apache.batik.css.engine.CSSEngineEvent;
25 import org.apache.batik.css.engine.SVGCSSEngine;
26 import org.apache.batik.dom.svg.SVGPathContext;
27 import org.apache.batik.ext.awt.geom.PathLength;
28 import org.apache.batik.gvt.ShapeNode;
29 import org.apache.batik.parser.AWTPathProducer;
30 import org.apache.batik.parser.ParseException;
31 import org.apache.batik.parser.PathParser;
32
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.events.MutationEvent JavaDoc;
35
36 /**
37  * Bridge class for the <path> element.
38  *
39  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
40  * @version $Id: SVGPathElementBridge.java,v 1.20 2005/02/27 02:08:51 deweese Exp $
41  */

42 public class SVGPathElementBridge extends SVGDecoratedShapeElementBridge
43        implements SVGPathContext {
44
45     /**
46      * default shape for the update of 'd' when
47      * the value is the empty string.
48      */

49     protected static final Shape JavaDoc DEFAULT_SHAPE = new GeneralPath JavaDoc();
50
51     /**
52      * Constructs a new bridge for the &lt;path> element.
53      */

54     public SVGPathElementBridge() {}
55
56     /**
57      * Returns 'path'.
58      */

59     public String JavaDoc getLocalName() {
60         return SVG_PATH_TAG;
61     }
62
63     /**
64      * Returns a new instance of this bridge.
65      */

66     public Bridge getInstance() {
67         return new SVGPathElementBridge();
68     }
69
70     /**
71      * Constructs a path according to the specified parameters.
72      *
73      * @param ctx the bridge context to use
74      * @param e the element that describes a rect element
75      * @param shapeNode the shape node to initialize
76      */

77     protected void buildShape(BridgeContext ctx,
78                               Element JavaDoc e,
79                               ShapeNode shapeNode) {
80
81
82         String JavaDoc s = e.getAttributeNS(null, SVG_D_ATTRIBUTE);
83         if (s.length() != 0) {
84             AWTPathProducer app = new AWTPathProducer();
85             app.setWindingRule(CSSUtilities.convertFillRule(e));
86             try {
87                 PathParser pathParser = new PathParser();
88                 pathParser.setPathHandler(app);
89                 pathParser.parse(s);
90             } catch (ParseException ex) {
91                 BridgeException bex
92                     = new BridgeException(e, ERR_ATTRIBUTE_VALUE_MALFORMED,
93                                           new Object JavaDoc[] {SVG_D_ATTRIBUTE});
94                 bex.setGraphicsNode(shapeNode);
95                 throw bex;
96             } finally {
97                 shapeNode.setShape(app.getShape());
98             }
99         }
100     }
101
102     // BridgeUpdateHandler implementation //////////////////////////////////
103

104     /**
105      * Invoked when an MutationEvent of type 'DOMAttrModified' is fired.
106      */

107     public void handleDOMAttrModifiedEvent(MutationEvent JavaDoc evt) {
108         String JavaDoc attrName = evt.getAttrName();
109         if (attrName.equals(SVG_D_ATTRIBUTE)) {
110             if ( evt.getNewValue().length() == 0 ){
111                 ((ShapeNode)node).setShape(DEFAULT_SHAPE);
112             }
113             else{
114                 buildShape(ctx, e, (ShapeNode)node);
115             }
116             handleGeometryChanged();
117         } else {
118             super.handleDOMAttrModifiedEvent(evt);
119         }
120     }
121
122     protected void handleCSSPropertyChanged(int property) {
123         switch(property) {
124         case SVGCSSEngine.FILL_RULE_INDEX:
125             buildShape(ctx, e, (ShapeNode) node);
126             handleGeometryChanged();
127             break;
128         default:
129             super.handleCSSPropertyChanged(property);
130         }
131     }
132
133     Shape JavaDoc pathLengthShape = null;
134     PathLength pathLength = null;
135
136     PathLength getPathLengthObj() {
137         Shape JavaDoc s = ((ShapeNode)node).getShape();
138         if (pathLengthShape != s) {
139             pathLength = new PathLength(s);
140             pathLengthShape = s;
141         }
142         return pathLength;
143     }
144
145     // SVGPathContext interface
146
public float getTotalLength() {
147         PathLength pl = getPathLengthObj();
148         return pl.lengthOfPath();
149     }
150
151     public Point2D JavaDoc getPointAtLength(float distance) {
152         PathLength pl = getPathLengthObj();
153         return pl.pointAtLength(distance);
154     }
155 }
156
Popular Tags