001    /*
002     * Rect.java
003     *
004     *
005     *  The Salamander Project - 2D and 3D graphics libraries in Java
006     *  Copyright (C) 2004 Mark McKay
007     *
008     *  This library is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU Lesser General Public
010     *  License as published by the Free Software Foundation; either
011     *  version 2.1 of the License, or (at your option) any later version.
012     *
013     *  This library is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016     *  Lesser General Public License for more details.
017     *
018     *  You should have received a copy of the GNU Lesser General Public
019     *  License along with this library; if not, write to the Free Software
020     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     *
022     *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
023     *  projects can be found at http://www.kitfox.com
024     *
025     * Created on January 26, 2004, 5:25 PM
026     */
027    
028    package com.kitfox.svg;
029    
030    import java.awt.geom.*;
031    import java.awt.*;
032    
033    import com.kitfox.svg.xml.*;
034    
035    /**
036     * @author Mark McKay
037     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
038     */
039    public class Polyline extends ShapeElement {
040    
041        int fillRule = GeneralPath.WIND_NON_ZERO;
042        String pointsStrn = "";
043    //    float[] points = null;
044        GeneralPath path;
045    
046        /** Creates a new instance of Rect */
047        public Polyline() {
048        }
049    
050        /*
051        public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
052        {
053                    //Load style string
054            super.loaderStartElement(helper, attrs, parent);
055    
056    
057            points = XMLParseUtil.parseFloatList(attrs.getValue("points"));
058    
059            build();
060        }
061        */
062    
063        public void build() throws SVGException
064        {
065            super.build();
066            
067            StyleAttribute sty = new StyleAttribute();
068            
069            if (getPres(sty.setName("points"))) pointsStrn = sty.getStringValue();
070            
071            String fillRuleStrn = getStyle(sty.setName("fill-rule")) ? sty.getStringValue() : "nonzero";
072            fillRule = fillRuleStrn.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO;
073    
074            buildPath();
075        }
076        
077        protected void buildPath()
078        {
079            float[] points = XMLParseUtil.parseFloatList(pointsStrn);
080            path = new GeneralPath(fillRule, points.length / 2);
081    
082            path.moveTo(points[0], points[1]);
083            for (int i = 2; i < points.length; i += 2)
084            {
085                path.lineTo(points[i], points[i + 1]);
086            }
087        }
088    
089        public void render(Graphics2D g) throws SVGException
090        {
091            beginLayer(g);
092            renderShape(g, path);
093            finishLayer(g);
094        }
095    
096        public Shape getShape()
097        {
098            return shapeToParent(path);
099        }
100    
101        public Rectangle2D getBoundingBox() throws SVGException
102        {
103            return boundsToParent(includeStrokeInBounds(path.getBounds2D()));
104        }
105    
106        /**
107         * Updates all attributes in this diagram associated with a time event.
108         * Ie, all attributes with track information.
109         * @return - true if this node has changed state as a result of the time
110         * update
111         */
112        public boolean updateTime(double curTime) throws SVGException
113        {
114    //        if (trackManager.getNumTracks() == 0) return false;
115            boolean changeState = super.updateTime(curTime);
116    
117            //Get current values for parameters
118            StyleAttribute sty = new StyleAttribute();
119            boolean shapeChange = false;
120            
121            if (getStyle(sty.setName("fill-rule")))
122            {
123                int newVal = sty.getStringValue().equals("evenodd") 
124                    ? GeneralPath.WIND_EVEN_ODD 
125                    : GeneralPath.WIND_NON_ZERO;
126                if (newVal != fillRule)
127                {
128                    fillRule = newVal;
129                    shapeChange = true;
130                }
131            }
132            
133            if (getPres(sty.setName("points")))
134            {
135                String newVal = sty.getStringValue();
136                if (!newVal.equals(pointsStrn))
137                {
138                    pointsStrn = newVal;
139                    shapeChange = true;
140                }
141            }
142            
143            
144            if (shapeChange)
145            {
146                build();
147    //            buildPath();
148    //            return true;
149            }
150            
151            return changeState || shapeChange;
152        }
153    }