001/*
002 * SVG Salamander
003 * Copyright (c) 2004, Mark McKay
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or 
007 * without modification, are permitted provided that the following
008 * conditions are met:
009 *
010 *   - Redistributions of source code must retain the above 
011 *     copyright notice, this list of conditions and the following
012 *     disclaimer.
013 *   - Redistributions in binary form must reproduce the above
014 *     copyright notice, this list of conditions and the following
015 *     disclaimer in the documentation and/or other materials 
016 *     provided with the distribution.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
029 * OF THE POSSIBILITY OF SUCH DAMAGE. 
030 * 
031 * Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
032 * projects can be found at http://www.kitfox.com
033 *
034 * Created on January 26, 2004, 1:54 AM
035 */
036package com.kitfox.svg;
037
038import com.kitfox.svg.xml.StyleAttribute;
039import java.awt.Graphics2D;
040import java.awt.Shape;
041import java.awt.geom.AffineTransform;
042import java.awt.geom.Rectangle2D;
043import java.net.URI;
044
045/**
046 * @author Mark McKay
047 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
048 */
049public class Use extends ShapeElement
050{
051    public static final String TAG_NAME = "use";
052    
053    float x = 0f;
054    float y = 0f;
055    float width = 1f;
056    float height = 1f;
057//    SVGElement href = null;
058    URI href = null;
059    AffineTransform refXform;
060
061    /**
062     * Creates a new instance of LinearGradient
063     */
064    public Use()
065    {
066    }
067
068    public String getTagName()
069    {
070        return TAG_NAME;
071    }
072
073    protected void build() throws SVGException
074    {
075        super.build();
076
077        StyleAttribute sty = new StyleAttribute();
078
079        if (getPres(sty.setName("x")))
080        {
081            x = sty.getFloatValueWithUnits();
082        }
083
084        if (getPres(sty.setName("y")))
085        {
086            y = sty.getFloatValueWithUnits();
087        }
088
089        if (getPres(sty.setName("width")))
090        {
091            width = sty.getFloatValueWithUnits();
092        }
093
094        if (getPres(sty.setName("height")))
095        {
096            height = sty.getFloatValueWithUnits();
097        }
098
099        if (getPres(sty.setName("xlink:href")))
100        {
101            URI src = sty.getURIValue(getXMLBase());
102            href = src;
103//            href = diagram.getUniverse().getElement(src);
104        }
105
106        //Determine use offset/scale
107        refXform = new AffineTransform();
108        refXform.translate(this.x, this.y);
109    }
110
111    public void render(Graphics2D g) throws SVGException
112    {
113        beginLayer(g);
114
115        //AffineTransform oldXform = g.getTransform();
116        AffineTransform oldXform = g.getTransform();
117        g.transform(refXform);
118
119        SVGElement ref = diagram.getUniverse().getElement(href);
120
121        if (ref == null || !(ref instanceof RenderableElement))
122        {
123            return;
124        }
125
126        RenderableElement rendEle = (RenderableElement)ref;
127        rendEle.pushParentContext(this);
128        rendEle.render(g);
129        rendEle.popParentContext();
130
131        g.setTransform(oldXform);
132
133        finishLayer(g);
134    }
135
136    public Shape getShape()
137    {
138        SVGElement ref = diagram.getUniverse().getElement(href);
139        if (ref instanceof ShapeElement)
140        {
141            Shape shape = ((ShapeElement) ref).getShape();
142            shape = refXform.createTransformedShape(shape);
143            shape = shapeToParent(shape);
144            return shape;
145        }
146
147        return null;
148    }
149
150    public Rectangle2D getBoundingBox() throws SVGException
151    {
152        SVGElement ref = diagram.getUniverse().getElement(href);
153        if (ref instanceof ShapeElement)
154        {
155            ShapeElement shapeEle = (ShapeElement) ref;
156            shapeEle.pushParentContext(this);
157            Rectangle2D bounds = shapeEle.getBoundingBox();
158            shapeEle.popParentContext();
159
160            bounds = refXform.createTransformedShape(bounds).getBounds2D();
161            bounds = boundsToParent(bounds);
162
163            return bounds;
164        }
165
166        return null;
167    }
168
169    /**
170     * Updates all attributes in this diagram associated with a time event. Ie,
171     * all attributes with track information.
172     *
173     * @return - true if this node has changed state as a result of the time
174     * update
175     */
176    public boolean updateTime(double curTime) throws SVGException
177    {
178//        if (trackManager.getNumTracks() == 0) return false;
179        boolean changeState = super.updateTime(curTime);
180
181        //Get current values for parameters
182        StyleAttribute sty = new StyleAttribute();
183        boolean shapeChange = false;
184
185        if (getPres(sty.setName("x")))
186        {
187            float newVal = sty.getFloatValueWithUnits();
188            if (newVal != x)
189            {
190                x = newVal;
191                shapeChange = true;
192            }
193        }
194
195        if (getPres(sty.setName("y")))
196        {
197            float newVal = sty.getFloatValueWithUnits();
198            if (newVal != y)
199            {
200                y = newVal;
201                shapeChange = true;
202            }
203        }
204
205        if (getPres(sty.setName("width")))
206        {
207            float newVal = sty.getFloatValueWithUnits();
208            if (newVal != width)
209            {
210                width = newVal;
211                shapeChange = true;
212            }
213        }
214
215        if (getPres(sty.setName("height")))
216        {
217            float newVal = sty.getFloatValueWithUnits();
218            if (newVal != height)
219            {
220                height = newVal;
221                shapeChange = true;
222            }
223        }
224
225        if (getPres(sty.setName("xlink:href")))
226        {
227            URI src = sty.getURIValue(getXMLBase());
228//            SVGElement newVal = diagram.getUniverse().getElement(src);
229            if (!src.equals(href))
230            {
231                href = src;
232                shapeChange = true;
233            }
234        }
235        /*
236         if (getPres(sty.setName("xlink:href")))
237         {
238         URI src = sty.getURIValue(getXMLBase());
239         href = diagram.getUniverse().getElement(src);
240         }
241        
242         //Determine use offset/scale
243         refXform = new AffineTransform();
244         refXform.translate(this.x, this.y);
245         refXform.scale(this.width, this.height);
246         */
247        if (shapeChange)
248        {
249            build();
250            //Determine use offset/scale
251//            refXform.setToTranslation(this.x, this.y);
252//            refXform.scale(this.width, this.height);
253//            return true;
254        }
255
256        return changeState || shapeChange;
257    }
258}