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 | @Override
|
---|
69 | public String getTagName()
|
---|
70 | {
|
---|
71 | return TAG_NAME;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | protected void build() throws SVGException
|
---|
76 | {
|
---|
77 | super.build();
|
---|
78 |
|
---|
79 | StyleAttribute sty = new StyleAttribute();
|
---|
80 |
|
---|
81 | if (getPres(sty.setName("x")))
|
---|
82 | {
|
---|
83 | x = sty.getFloatValueWithUnits();
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (getPres(sty.setName("y")))
|
---|
87 | {
|
---|
88 | y = sty.getFloatValueWithUnits();
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (getPres(sty.setName("width")))
|
---|
92 | {
|
---|
93 | width = sty.getFloatValueWithUnits();
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (getPres(sty.setName("height")))
|
---|
97 | {
|
---|
98 | height = sty.getFloatValueWithUnits();
|
---|
99 | }
|
---|
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 | }
|
---|