1 | /*
|
---|
2 | * SVG Salamander
|
---|
3 | * Copyright (c) 2004, Mark McKay
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or
|
---|
7 | * without modification, are permitted provided that the following
|
---|
8 | * conditions are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above
|
---|
11 | * copyright notice, this list of conditions and the following
|
---|
12 | * disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above
|
---|
14 | * copyright notice, this list of conditions and the following
|
---|
15 | * disclaimer in the documentation and/or other materials
|
---|
16 | * provided with the distribution.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
---|
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
---|
27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
---|
29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | *
|
---|
31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
32 | * projects can be found at http://www.kitfox.com
|
---|
33 | *
|
---|
34 | * Created on January 26, 2004, 1:54 AM
|
---|
35 | */
|
---|
36 | package com.kitfox.svg;
|
---|
37 |
|
---|
38 | import com.kitfox.svg.xml.StyleAttribute;
|
---|
39 | import java.awt.Graphics2D;
|
---|
40 | import java.awt.Shape;
|
---|
41 | import java.awt.geom.AffineTransform;
|
---|
42 | import java.awt.geom.Rectangle2D;
|
---|
43 | import java.net.URI;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * @author Mark McKay
|
---|
47 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
48 | */
|
---|
49 | public class Use extends ShapeElement
|
---|
50 | {
|
---|
51 | public static final String TAG_NAME = "use";
|
---|
52 |
|
---|
53 | float x = 0f;
|
---|
54 | float y = 0f;
|
---|
55 | float width = 1f;
|
---|
56 | float height = 1f;
|
---|
57 | // SVGElement href = null;
|
---|
58 | URI href = null;
|
---|
59 | AffineTransform refXform;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Creates a new instance of LinearGradient
|
---|
63 | */
|
---|
64 | public Use()
|
---|
65 | {
|
---|
66 | }
|
---|
67 |
|
---|
68 | public String getTagName()
|
---|
69 | {
|
---|
70 | return TAG_NAME;
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected void build() throws SVGException
|
---|
74 | {
|
---|
75 | super.build();
|
---|
76 |
|
---|
77 | StyleAttribute sty = new StyleAttribute();
|
---|
78 |
|
---|
79 | if (getPres(sty.setName("x")))
|
---|
80 | {
|
---|
81 | x = sty.getFloatValueWithUnits();
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (getPres(sty.setName("y")))
|
---|
85 | {
|
---|
86 | y = sty.getFloatValueWithUnits();
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (getPres(sty.setName("width")))
|
---|
90 | {
|
---|
91 | width = sty.getFloatValueWithUnits();
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (getPres(sty.setName("height")))
|
---|
95 | {
|
---|
96 | height = sty.getFloatValueWithUnits();
|
---|
97 | }
|
---|
98 |
|
---|
99 | 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 | }
|
---|