KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import org.apache.batik.css.engine.CSSEngineEvent;
24 import org.apache.batik.css.engine.SVGCSSEngine;
25 import org.apache.batik.gvt.ShapeNode;
26 import org.apache.batik.parser.AWTPolygonProducer;
27 import org.apache.batik.parser.ParseException;
28 import org.apache.batik.parser.PointsParser;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.events.MutationEvent JavaDoc;
31
32 /**
33  * Bridge class for the <polygon> element.
34  *
35  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
36  * @version $Id: SVGPolygonElementBridge.java,v 1.20 2004/08/18 07:12:35 vhardy Exp $
37  */

38 public class SVGPolygonElementBridge extends SVGDecoratedShapeElementBridge {
39
40     /**
41      * default shape for the update of 'points' when
42      * the value is the empty string.
43      */

44     protected static final Shape JavaDoc DEFAULT_SHAPE = new GeneralPath JavaDoc();
45
46     /**
47      * Constructs a new bridge for the &lt;polygon> element.
48      */

49     public SVGPolygonElementBridge() {}
50
51     /**
52      * Returns 'polygon'.
53      */

54     public String JavaDoc getLocalName() {
55         return SVG_POLYGON_TAG;
56     }
57
58     /**
59      * Returns a new instance of this bridge.
60      */

61     public Bridge getInstance() {
62         return new SVGPolygonElementBridge();
63     }
64
65     /**
66      * Constructs a polygon according to the specified parameters.
67      *
68      * @param ctx the bridge context to use
69      * @param e the element that describes a rect element
70      * @param shapeNode the shape node to initialize
71      */

72     protected void buildShape(BridgeContext ctx,
73                               Element JavaDoc e,
74                               ShapeNode shapeNode) {
75
76         String JavaDoc s = e.getAttributeNS(null, SVG_POINTS_ATTRIBUTE);
77         if (s.length() != 0) {
78             AWTPolygonProducer app = new AWTPolygonProducer();
79             app.setWindingRule(CSSUtilities.convertFillRule(e));
80             try {
81                 PointsParser pp = new PointsParser();
82                 pp.setPointsHandler(app);
83                 pp.parse(s);
84             } catch (ParseException ex) {
85                 BridgeException bex
86                     = new BridgeException(e, ERR_ATTRIBUTE_VALUE_MALFORMED,
87                                           new Object JavaDoc[] {SVG_POINTS_ATTRIBUTE});
88                 bex.setGraphicsNode(shapeNode);
89                 throw bex;
90             } finally {
91                 shapeNode.setShape(app.getShape());
92             }
93         }
94     }
95
96     // BridgeUpdateHandler implementation //////////////////////////////////
97

98     /**
99      * Invoked when an MutationEvent of type 'DOMAttrModified' is fired.
100      */

101     public void handleDOMAttrModifiedEvent(MutationEvent JavaDoc evt) {
102         String JavaDoc attrName = evt.getAttrName();
103         if (attrName.equals(SVG_POINTS_ATTRIBUTE)) {
104             if ( evt.getNewValue().length() == 0 ){
105                 ((ShapeNode)node).setShape(DEFAULT_SHAPE);
106             }
107             else{
108                 buildShape(ctx, e, (ShapeNode)node);
109             }
110             handleGeometryChanged();
111         } else {
112             super.handleDOMAttrModifiedEvent(evt);
113         }
114     }
115
116     protected void handleCSSPropertyChanged(int property) {
117         switch(property) {
118         case SVGCSSEngine.FILL_RULE_INDEX:
119             buildShape(ctx, e, (ShapeNode) node);
120             handleGeometryChanged();
121             break;
122         default:
123             super.handleCSSPropertyChanged(property);
124         }
125     }
126 }
127
Popular Tags