1 18 package org.apache.batik.bridge; 19 20 import java.awt.Shape ; 21 import java.awt.geom.GeneralPath ; 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 ; 30 import org.w3c.dom.events.MutationEvent ; 31 32 38 public class SVGPolygonElementBridge extends SVGDecoratedShapeElementBridge { 39 40 44 protected static final Shape DEFAULT_SHAPE = new GeneralPath (); 45 46 49 public SVGPolygonElementBridge() {} 50 51 54 public String getLocalName() { 55 return SVG_POLYGON_TAG; 56 } 57 58 61 public Bridge getInstance() { 62 return new SVGPolygonElementBridge(); 63 } 64 65 72 protected void buildShape(BridgeContext ctx, 73 Element e, 74 ShapeNode shapeNode) { 75 76 String 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 [] {SVG_POINTS_ATTRIBUTE}); 88 bex.setGraphicsNode(shapeNode); 89 throw bex; 90 } finally { 91 shapeNode.setShape(app.getShape()); 92 } 93 } 94 } 95 96 98 101 public void handleDOMAttrModifiedEvent(MutationEvent evt) { 102 String 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 |