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    @Override
069    public String getTagName()
070    {
071        return TAG_NAME;
072    }
073
074    @Override
075    protected void build() throws SVGException
076    {
077        super.build();
078
079        StyleAttribute sty = new StyleAttribute();
080
081        if (getPres(sty.setName("x")))
082        {
083            x = sty.getFloatValueWithUnits();
084        }
085
086        if (getPres(sty.setName("y")))
087        {
088            y = sty.getFloatValueWithUnits();
089        }
090
091        if (getPres(sty.setName("width")))
092        {
093            width = sty.getFloatValueWithUnits();
094        }
095
096        if (getPres(sty.setName("height")))
097        {
098            height = sty.getFloatValueWithUnits();
099        }
100
101        if (getPres(sty.setName("xlink:href")))
102        {
103            URI src = sty.getURIValue(getXMLBase());
104            href = src;
105//            href = diagram.getUniverse().getElement(src);
106        }
107
108        //Determine use offset/scale
109        refXform = new AffineTransform();
110        refXform.translate(this.x, this.y);
111    }
112
113    @Override
114    public void render(Graphics2D g) throws SVGException
115    {
116        beginLayer(g);
117
118        //AffineTransform oldXform = g.getTransform();
119        AffineTransform oldXform = g.getTransform();
120        g.transform(refXform);
121
122        SVGElement ref = diagram.getUniverse().getElement(href);
123
124        if (ref == null || !(ref instanceof RenderableElement))
125        {
126            return;
127        }
128
129        RenderableElement rendEle = (RenderableElement)ref;
130        rendEle.pushParentContext(this);
131        rendEle.render(g);
132        rendEle.popParentContext();
133
134        g.setTransform(oldXform);
135
136        finishLayer(g);
137    }
138
139    @Override
140    public Shape getShape()
141    {
142        SVGElement ref = diagram.getUniverse().getElement(href);
143        if (ref instanceof ShapeElement)
144        {
145            Shape shape = ((ShapeElement) ref).getShape();
146            shape = refXform.createTransformedShape(shape);
147            shape = shapeToParent(shape);
148            return shape;
149        }
150
151        return null;
152    }
153
154    @Override
155    public Rectangle2D getBoundingBox() throws SVGException
156    {
157        SVGElement ref = diagram.getUniverse().getElement(href);
158        if (ref instanceof ShapeElement)
159        {
160            ShapeElement shapeEle = (ShapeElement) ref;
161            shapeEle.pushParentContext(this);
162            Rectangle2D bounds = shapeEle.getBoundingBox();
163            shapeEle.popParentContext();
164
165            bounds = refXform.createTransformedShape(bounds).getBounds2D();
166            bounds = boundsToParent(bounds);
167
168            return bounds;
169        }
170
171        return null;
172    }
173
174    /**
175     * Updates all attributes in this diagram associated with a time event. Ie,
176     * all attributes with track information.
177     *
178     * @return - true if this node has changed state as a result of the time
179     * update
180     */
181    @Override
182    public boolean updateTime(double curTime) throws SVGException
183    {
184//        if (trackManager.getNumTracks() == 0) return false;
185        boolean changeState = super.updateTime(curTime);
186
187        //Get current values for parameters
188        StyleAttribute sty = new StyleAttribute();
189        boolean shapeChange = false;
190
191        if (getPres(sty.setName("x")))
192        {
193            float newVal = sty.getFloatValueWithUnits();
194            if (newVal != x)
195            {
196                x = newVal;
197                shapeChange = true;
198            }
199        }
200
201        if (getPres(sty.setName("y")))
202        {
203            float newVal = sty.getFloatValueWithUnits();
204            if (newVal != y)
205            {
206                y = newVal;
207                shapeChange = true;
208            }
209        }
210
211        if (getPres(sty.setName("width")))
212        {
213            float newVal = sty.getFloatValueWithUnits();
214            if (newVal != width)
215            {
216                width = newVal;
217                shapeChange = true;
218            }
219        }
220
221        if (getPres(sty.setName("height")))
222        {
223            float newVal = sty.getFloatValueWithUnits();
224            if (newVal != height)
225            {
226                height = newVal;
227                shapeChange = true;
228            }
229        }
230
231        if (getPres(sty.setName("xlink:href")))
232        {
233            URI src = sty.getURIValue(getXMLBase());
234//            SVGElement newVal = diagram.getUniverse().getElement(src);
235            if (!src.equals(href))
236            {
237                href = src;
238                shapeChange = true;
239            }
240        }
241        /*
242         if (getPres(sty.setName("xlink:href")))
243         {
244         URI src = sty.getURIValue(getXMLBase());
245         href = diagram.getUniverse().getElement(src);
246         }
247        
248         //Determine use offset/scale
249         refXform = new AffineTransform();
250         refXform.translate(this.x, this.y);
251         refXform.scale(this.width, this.height);
252         */
253        if (shapeChange)
254        {
255            build();
256            //Determine use offset/scale
257//            refXform.setToTranslation(this.x, this.y);
258//            refXform.scale(this.width, this.height);
259//            return true;
260        }
261
262        return changeState || shapeChange;
263    }
264}