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 com.kitfox.svg.xml.StyleAttribute;
031    import java.awt.*;
032    import java.awt.geom.*;
033    
034    import com.kitfox.svg.pathcmd.*;
035    import com.kitfox.svg.xml.*;
036    import org.xml.sax.*;
037    
038    //import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
039    
040    /**
041     * @author Mark McKay
042     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
043     */
044    public class Path extends ShapeElement {
045    
046    //    PathCommand[] commands = null;
047    
048        int fillRule = GeneralPath.WIND_NON_ZERO;
049        String d = "";
050    //    ExtendedGeneralPath path;
051        GeneralPath path;
052    
053        /** Creates a new instance of Rect */
054        public Path() {
055        }
056    
057        /*
058        public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
059        {
060                    //Load style string
061            super.loaderStartElement(helper, attrs, parent);
062    
063            StyleAttribute styleAttrib = getStyle("fill-rule");
064            String fillRule = (styleAttrib == null) ? "nonzero" : styleAttrib.getStringValue();
065            
066            String d = attrs.getValue("d");
067            path = buildPath(d, fillRule.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO);
068        }
069        */
070        
071        protected void build() throws SVGException
072        {
073            super.build();
074            
075            StyleAttribute sty = new StyleAttribute();
076            
077        
078            String fillRuleStrn  = (getStyle(sty.setName("fill-rule"))) ? sty.getStringValue() : "nonzero";
079            fillRule = fillRuleStrn.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO;
080            
081    //        String d = "";
082            if (getPres(sty.setName("d"))) d = sty.getStringValue();
083    
084    //System.err.println(d);
085            
086            path = buildPath(d, fillRule);
087            
088    //System.err.println(d);
089        }
090        
091        public void render(Graphics2D g) throws SVGException
092        {
093            beginLayer(g);
094            renderShape(g, path);
095            finishLayer(g);
096        }
097    
098        public Shape getShape()
099        {
100            return shapeToParent(path);
101        }
102    
103        public Rectangle2D getBoundingBox() throws SVGException
104        {
105            return boundsToParent(includeStrokeInBounds(path.getBounds2D()));
106        }
107    
108        /**
109         * Updates all attributes in this diagram associated with a time event.
110         * Ie, all attributes with track information.
111         * @return - true if this node has changed state as a result of the time
112         * update
113         */
114        public boolean updateTime(double curTime) throws SVGException
115        {
116    //        if (trackManager.getNumTracks() == 0) return false;
117            boolean changeState = super.updateTime(curTime);
118    
119            //Get current values for parameters
120            StyleAttribute sty = new StyleAttribute();
121            boolean shapeChange = false;
122            
123            if (getStyle(sty.setName("fill-rule")))
124            {
125                int newVal = sty.getStringValue().equals("evenodd") 
126                    ? GeneralPath.WIND_EVEN_ODD 
127                    : GeneralPath.WIND_NON_ZERO;
128                if (newVal != fillRule)
129                {
130                    fillRule = newVal;
131                    changeState = true;
132                }
133            }
134            
135            if (getPres(sty.setName("d")))
136            {
137                String newVal = sty.getStringValue();
138                if (!newVal.equals(d))
139                {
140                    d = newVal;
141                    shapeChange = true;
142                }
143            }
144            
145            if (shapeChange)
146            {
147                build();
148    //            path = buildPath(d, fillRule);
149    //            return true;
150            }
151            
152            return changeState || shapeChange;
153        }
154    }