001    /*
002     * BoundedElement.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, 9:00 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.AffineTransform;
033    import java.awt.geom.Rectangle2D;
034    
035    
036    /**
037     * Maintains bounding box for this element
038     *
039     * @author Mark McKay
040     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
041     */
042    public class TransformableElement extends SVGElement 
043    {
044    
045        AffineTransform xform = null;
046    //    AffineTransform invXform = null;
047    
048        /** Creates a new instance of BoundedElement */
049        public TransformableElement()
050        {
051        }
052    
053        public TransformableElement(String id, SVGElement parent)
054        {
055            super(id, parent);
056        }
057    
058        /**
059         * Fetches a copy of the cached AffineTransform.  Note that this
060         * value will only be valid after the node has been updated.
061         *
062         * @return
063         */
064        public AffineTransform getXForm()
065        {
066            return new AffineTransform(xform);
067        }
068    /*
069        public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
070        {
071                    //Load style string
072            super.loaderStartElement(helper, attrs, parent);
073    
074            String transform = attrs.getValue("transform");
075            if (transform != null)
076            {
077                xform = parseTransform(transform);
078            }
079        }
080    */
081        
082        protected void build() throws SVGException
083        {
084            super.build();
085            
086            StyleAttribute sty = new StyleAttribute();
087            
088            if (getPres(sty.setName("transform")))
089            {
090                xform = parseTransform(sty.getStringValue());
091            }
092        }
093        
094        protected Shape shapeToParent(Shape shape)
095        {
096            if (xform == null) return shape;
097            return xform.createTransformedShape(shape);
098        }
099    
100        protected Rectangle2D boundsToParent(Rectangle2D rect)
101        {
102            if (xform == null) return rect;
103            return xform.createTransformedShape(rect).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            StyleAttribute sty = new StyleAttribute();
115            
116            if (getPres(sty.setName("transform")))
117            {
118                AffineTransform newXform = parseTransform(sty.getStringValue());
119                if (!newXform.equals(xform))
120                {
121                    xform = newXform;
122                    return true;
123                }
124            }
125            
126            return false;
127        }
128    }