001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import java.awt.Shape;
022: import java.awt.geom.GeneralPath;
023:
024: import org.apache.batik.css.engine.SVGCSSEngine;
025: import org.apache.batik.dom.svg.AnimatedLiveAttributeValue;
026: import org.apache.batik.dom.svg.LiveAttributeException;
027: import org.apache.batik.dom.svg.SVGOMPolygonElement;
028: import org.apache.batik.gvt.ShapeNode;
029: import org.apache.batik.parser.AWTPolygonProducer;
030:
031: import org.w3c.dom.Element;
032: import org.w3c.dom.svg.SVGPoint;
033: import org.w3c.dom.svg.SVGPointList;
034:
035: /**
036: * Bridge class for the <polygon> element.
037: *
038: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
039: * @version $Id: SVGPolygonElementBridge.java 475477 2006-11-15 22:44:28Z cam $
040: */
041: public class SVGPolygonElementBridge extends
042: SVGDecoratedShapeElementBridge {
043:
044: /**
045: * default shape for the update of 'points' when
046: * the value is the empty string.
047: */
048: protected static final Shape DEFAULT_SHAPE = new GeneralPath();
049:
050: /**
051: * Constructs a new bridge for the <polygon> element.
052: */
053: public SVGPolygonElementBridge() {
054: }
055:
056: /**
057: * Returns 'polygon'.
058: */
059: public String getLocalName() {
060: return SVG_POLYGON_TAG;
061: }
062:
063: /**
064: * Returns a new instance of this bridge.
065: */
066: public Bridge getInstance() {
067: return new SVGPolygonElementBridge();
068: }
069:
070: /**
071: * Constructs a polygon according to the specified parameters.
072: *
073: * @param ctx the bridge context to use
074: * @param e the element that describes a rect element
075: * @param shapeNode the shape node to initialize
076: */
077: protected void buildShape(BridgeContext ctx, Element e,
078: ShapeNode shapeNode) {
079:
080: try {
081: SVGOMPolygonElement pe = (SVGOMPolygonElement) e;
082: SVGPointList pl = pe.getAnimatedPoints();
083: int size = pl.getNumberOfItems();
084: if (size == 0) {
085: shapeNode.setShape(DEFAULT_SHAPE);
086: } else {
087: AWTPolygonProducer app = new AWTPolygonProducer();
088: app.setWindingRule(CSSUtilities.convertFillRule(e));
089: app.startPoints();
090: for (int i = 0; i < size; i++) {
091: SVGPoint p = pl.getItem(i);
092: app.point(p.getX(), p.getY());
093: }
094: app.endPoints();
095: shapeNode.setShape(app.getShape());
096: }
097: } catch (LiveAttributeException ex) {
098: throw new BridgeException(ctx, ex);
099: }
100: }
101:
102: // BridgeUpdateHandler implementation //////////////////////////////////
103:
104: /**
105: * Invoked when the animated value of an animatable attribute has changed.
106: */
107: public void handleAnimatedAttributeChanged(
108: AnimatedLiveAttributeValue alav) {
109: if (alav.getNamespaceURI() == null) {
110: String ln = alav.getLocalName();
111: if (ln.equals(SVG_POINTS_ATTRIBUTE)) {
112: buildShape(ctx, e, (ShapeNode) node);
113: handleGeometryChanged();
114: return;
115: }
116: }
117: super .handleAnimatedAttributeChanged(alav);
118: }
119:
120: protected void handleCSSPropertyChanged(int property) {
121: switch (property) {
122: case SVGCSSEngine.FILL_RULE_INDEX:
123: buildShape(ctx, e, (ShapeNode) node);
124: handleGeometryChanged();
125: break;
126: default:
127: super.handleCSSPropertyChanged(property);
128: }
129: }
130: }
|