001    /*
002     * Stop.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, 1:56 AM
026     */
027    
028    package com.kitfox.svg;
029    
030    import com.kitfox.svg.xml.StyleAttribute;
031    import java.awt.Shape;
032    import java.awt.geom.Area;
033    import java.util.Iterator;
034    
035    /**
036     * @author Mark McKay
037     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
038     */
039    public class ClipPath extends SVGElement 
040    {
041    
042        public static final int CP_USER_SPACE_ON_USE = 0;
043        public static final int CP_OBJECT_BOUNDING_BOX = 1;
044    
045        int clipPathUnits = CP_USER_SPACE_ON_USE;
046    
047        /** Creates a new instance of Stop */
048        public ClipPath() {
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            String clipPathUnits = attrs.getValue("clipPathUnits");
057    
058            if (clipPathUnits.equals("objectBoundingBox")) this.clipPathUnits = CP_OBJECT_BOUNDING_BOX;
059    
060        }
061    */
062        /**
063         * Called after the start element but before the end element to indicate
064         * each child tag that has been processed
065         */
066        public void loaderAddChild(SVGLoaderHelper helper, SVGElement child) throws SVGElementException
067        {
068                    super.loaderAddChild(helper, child);
069    
070    //        if (child instanceof ShapeElement) members.add(child);
071        }
072    
073        /*
074        public void loaderEndElement(SVGLoaderHelper helper)
075        {
076    //        super.loaderEndElement(helper);
077    
078    //        build();
079        }
080        */
081        
082        protected void build() throws SVGException
083        {
084            super.build();
085            
086            StyleAttribute sty = new StyleAttribute();
087            
088            clipPathUnits = (getPres(sty.setName("clipPathUnits"))
089                && sty.getStringValue().equals("objectBoundingBox")) 
090                ? CP_OBJECT_BOUNDING_BOX 
091                : CP_USER_SPACE_ON_USE;
092        }
093        
094        public int getClipPathUnits()
095        {
096            return clipPathUnits;
097        }
098    
099        public Shape getClipPathShape()
100        {
101            if (children.size() == 0) return null;
102            if (children.size() == 1) return ((ShapeElement)children.get(0)).getShape();
103    
104            Area clipArea = null;
105            for (Iterator it = children.iterator(); it.hasNext();)
106            {
107                ShapeElement se = (ShapeElement)it.next();
108    
109                if (clipArea == null)
110                {
111                    Shape shape = se.getShape();
112                    if (shape != null) clipArea = new Area(se.getShape());
113                    continue;
114                }
115    
116                Shape shape = se.getShape();
117                if (shape != null) clipArea.intersect(new Area(shape));
118            }
119    
120            return clipArea;
121        }
122    
123        /**
124         * Updates all attributes in this diagram associated with a time event.
125         * Ie, all attributes with track information.
126         * @return - true if this node has changed state as a result of the time
127         * update
128         */
129        public boolean updateTime(double curTime) throws SVGException
130        {
131    //        if (trackManager.getNumTracks() == 0) return false;
132    
133            //Get current values for parameters
134            StyleAttribute sty = new StyleAttribute();
135            boolean shapeChange = false;
136    
137            
138            if (getPres(sty.setName("clipPathUnits")))
139            {
140                String newUnitsStrn = sty.getStringValue();
141                int newUnits = newUnitsStrn.equals("objectBoundingBox")
142                    ? CP_OBJECT_BOUNDING_BOX 
143                    : CP_USER_SPACE_ON_USE;
144                    
145                if (newUnits != clipPathUnits)
146                {
147                    clipPathUnits = newUnits;
148                    shapeChange = true;
149                }
150            }
151    
152            if (shapeChange)
153            {
154                build();
155            }
156    
157            return shapeChange;
158        }
159    }