1 /**********************************************
2 * Copyright (C) 2010-2011 Lukas Laag
3 * This file is part of lib-gwt-svg.
4 *
5 * libgwtsvg is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * libgwtsvg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/
17 **********************************************/
18 /*
19 * Copyright (c) 2004 World Wide Web Consortium,
20 *
21 * (Massachusetts Institute of Technology, European Research Consortium for
22 * Informatics and Mathematics, Keio University). All Rights Reserved. This
23 * work is distributed under the W3C(r) Software License [1] in the hope that
24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
28 */
29
30 package org.vectomatic.dom.svg;
31
32 import org.vectomatic.dom.svg.events.ActivateEvent;
33 import org.vectomatic.dom.svg.events.ActivateHandler;
34 import org.vectomatic.dom.svg.events.FocusInEvent;
35 import org.vectomatic.dom.svg.events.FocusInHandler;
36 import org.vectomatic.dom.svg.events.FocusOutEvent;
37 import org.vectomatic.dom.svg.events.FocusOutHandler;
38 import org.vectomatic.dom.svg.events.HasGraphicalHandlers;
39 import org.vectomatic.dom.svg.impl.SVGElement;
40 import org.vectomatic.dom.svg.impl.SVGImageElement;
41 import org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired;
42 import org.vectomatic.dom.svg.itf.ISVGGraphicsElement;
43 import org.vectomatic.dom.svg.itf.ISVGLangSpace;
44 import org.vectomatic.dom.svg.itf.ISVGStylable;
45 import org.vectomatic.dom.svg.itf.ISVGTests;
46 import org.vectomatic.dom.svg.itf.ISVGTransformable;
47 import org.vectomatic.dom.svg.itf.ISVGURIReference;
48 import org.vectomatic.dom.svg.utils.DOMHelper;
49 import org.vectomatic.dom.svg.utils.SVGConstants;
50
51 import com.google.gwt.core.client.JavaScriptException;
52 import com.google.gwt.dom.client.TagName;
53 import com.google.gwt.event.dom.client.ClickEvent;
54 import com.google.gwt.event.dom.client.ClickHandler;
55 import com.google.gwt.event.dom.client.LoadEvent;
56 import com.google.gwt.event.dom.client.LoadHandler;
57 import com.google.gwt.event.dom.client.MouseDownEvent;
58 import com.google.gwt.event.dom.client.MouseDownHandler;
59 import com.google.gwt.event.dom.client.MouseMoveEvent;
60 import com.google.gwt.event.dom.client.MouseMoveHandler;
61 import com.google.gwt.event.dom.client.MouseOutEvent;
62 import com.google.gwt.event.dom.client.MouseOutHandler;
63 import com.google.gwt.event.dom.client.MouseOverEvent;
64 import com.google.gwt.event.dom.client.MouseOverHandler;
65 import com.google.gwt.event.dom.client.MouseUpEvent;
66 import com.google.gwt.event.dom.client.MouseUpHandler;
67 import com.google.gwt.event.dom.client.TouchCancelEvent;
68 import com.google.gwt.event.dom.client.TouchCancelHandler;
69 import com.google.gwt.event.dom.client.TouchEndEvent;
70 import com.google.gwt.event.dom.client.TouchEndHandler;
71 import com.google.gwt.event.dom.client.TouchMoveEvent;
72 import com.google.gwt.event.dom.client.TouchMoveHandler;
73 import com.google.gwt.event.dom.client.TouchStartEvent;
74 import com.google.gwt.event.dom.client.TouchStartHandler;
75 import com.google.gwt.event.shared.HandlerRegistration;
76
77 /**
78 * The {@link org.vectomatic.dom.svg.OMSVGImageElement} interface corresponds
79 * to the <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement' title='image
80 * element specification'>image</a> element.
81 */
82 @TagName("image")
83 public class OMSVGImageElement extends OMSVGElement implements HasGraphicalHandlers, ISVGURIReference, ISVGTests, ISVGLangSpace, ISVGExternalResourcesRequired, ISVGStylable, ISVGTransformable, ISVGGraphicsElement {
84 public OMSVGImageElement() {
85 this((SVGImageElement)DOMHelper.createElementNS(DOMHelper.getCurrentDocument(), SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_IMAGE_TAG).cast());
86 }
87
88 protected OMSVGImageElement(SVGImageElement ot) {
89 super(ot);
90 }
91
92 // Implementation of the svg::SVGImageElement W3C IDL interface
93 /**
94 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getX()}
95 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement'
96 * title='image element specification'>image</a> element.
97 */
98 public final OMSVGAnimatedLength getX() {
99 return ((SVGImageElement)ot).getX();
100 }
101 /**
102 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getY()}
103 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement'
104 * title='image element specification'>image</a> element.
105 */
106 public final OMSVGAnimatedLength getY() {
107 return ((SVGImageElement)ot).getY();
108 }
109 /**
110 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getWidth()}
111 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement'
112 * title='image element specification'>image</a> element.
113 */
114 public final OMSVGAnimatedLength getWidth() {
115 return ((SVGImageElement)ot).getWidth();
116 }
117 /**
118 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getHeight()}
119 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement'
120 * title='image element specification'>image</a> element.
121 */
122 public final OMSVGAnimatedLength getHeight() {
123 return ((SVGImageElement)ot).getHeight();
124 }
125 /**
126 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getPreserveAspectRatio()}
127 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement'
128 * title='image element specification'>image</a> element.
129 */
130 public final OMSVGAnimatedPreserveAspectRatio getPreserveAspectRatio() {
131 return ((SVGImageElement)ot).getPreserveAspectRatio();
132 }
133
134 // Implementation of the svg::SVGExternalResourcesRequired W3C IDL interface
135 /**
136 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
137 * on the given element. Note that the SVG DOM defines the attribute {@link
138 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
139 * as being of type {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean}, whereas
140 * the SVG language definition says that {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
141 * is not animated. Because the SVG language definition states that {@link
142 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
143 * cannot be animated, the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getAnimVal()}
144 * will always be the same as the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getBaseVal()}.
145 */
146 public final OMSVGAnimatedBoolean getExternalResourcesRequired() {
147 return ((SVGImageElement)ot).getExternalResourcesRequired();
148 }
149
150 // Implementation of the svg::SVGLangSpace W3C IDL interface
151 /**
152 * Corresponds to attribute <code>xml:lang</code> on the given element.
153 */
154 public final String getXmllang() {
155 return ((SVGImageElement)ot).getXmllang();
156 }
157 /**
158 * Corresponds to attribute <code>xml:lang</code> on the given element.
159 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt
160 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only
161 * attribute</a>.
162 */
163 public final void setXmllang(java.lang.String value) throws JavaScriptException {
164 ((SVGImageElement)ot).setXmllang(value);
165 }
166 /**
167 * Corresponds to attribute <code>xml:space</code> on the given element.
168 */
169 public final String getXmlspace() {
170 return ((SVGImageElement)ot).getXmlspace();
171 }
172 /**
173 * Corresponds to attribute <code>xml:space</code> on the given element.
174 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt
175 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only
176 * attribute</a>.
177 */
178 public final void setXmlspace(java.lang.String value) throws JavaScriptException {
179 ((SVGImageElement)ot).setXmlspace(value);
180 }
181
182 // Implementation of the svg::SVGLocatable W3C IDL interface
183 /**
184 * The element which established the current viewport. Often, the nearest
185 * ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' title='svg
186 * element specification'>svg</a> element. Null if the current element is
187 * the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
188 * title='svg element specification'>svg</a> element.
189 */
190 public final OMSVGElement getNearestViewportElement() {
191 return (OMSVGElement)convert(((SVGImageElement)ot).getNearestViewportElement());
192 }
193 /**
194 * The farthest ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
195 * title='svg element specification'>svg</a> element. Null if the current
196 * element is the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
197 * title='svg element specification'>svg</a> element.
198 */
199 public final OMSVGElement getFarthestViewportElement() {
200 return (OMSVGElement)convert(((SVGImageElement)ot).getFarthestViewportElement());
201 }
202 /**
203 * Returns the tight bounding box in current user space (i.e., after application
204 * of the <code>transform</code> attribute, if any) on the geometry of all
205 * contained graphics elements, exclusive of stroking, clipping, masking and
206 * filter effects). Note that getBBox must return the actual bounding box
207 * at the time the method was called, even in case the element has not yet
208 * been rendered.
209 * @return An {@link org.vectomatic.dom.svg.OMSVGRect} object that defines
210 * the bounding box.
211 */
212 public final OMSVGRect getBBox() {
213 return ((SVGImageElement)ot).getBBox();
214 }
215 /**
216 * Returns the transformation matrix from current user units (i.e., after
217 * application of the <code>transform</code> attribute, if any) to the viewport
218 * coordinate system for the {@link org.vectomatic.dom.svg.itf.ISVGLocatable#getNearestViewportElement()}.
219 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines
220 * the CTM.
221 */
222 public final OMSVGMatrix getCTM() {
223 return ((SVGImageElement)ot).getCTM();
224 }
225 /**
226 * Returns the transformation matrix from current user units (i.e., after
227 * application of the <code>transform</code> attribute, if any) to the parent
228 * user agent's notice of a "pixel". For display devices, ideally this represents
229 * a physical screen pixel. For other devices or environments where physical
230 * pixel sizes are not known, then an algorithm similar to the CSS2 definition
231 * of a "pixel" can be used instead. Note that null is returned if this element
232 * is not hooked into the document tree. This method would have been more
233 * aptly named as <code>getClientCTM</code>, but the name <code>getScreenCTM</code>
234 * is kept for historical reasons.
235 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines
236 * the given transformation matrix.
237 */
238 public final OMSVGMatrix getScreenCTM() {
239 return ((SVGImageElement)ot).getScreenCTM();
240 }
241 /**
242 * Returns the transformation matrix from the user coordinate system on the
243 * current element (after application of the <code>transform</code> attribute,
244 * if any) to the user coordinate system on parameter <var>element</var> (after
245 * application of its <code>transform</code> attribute, if any).
246 * @param element The target element.
247 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines
248 * the transformation.
249 * @throws SVGException(SVG_MATRIX_NOT_INVERTABLE) Raised if the currently
250 * defined transformation matrices make it impossible to compute the given
251 * matrix (e.g., because one of the transformations is singular).
252 */
253 public final OMSVGMatrix getTransformToElement(OMSVGElement element) throws JavaScriptException {
254 return ((SVGImageElement)ot).getTransformToElement(((SVGElement)element.ot));
255 }
256
257 // Implementation of the svg::SVGTests W3C IDL interface
258 /**
259 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getRequiredFeatures()}
260 * on the given element.
261 */
262 public final OMSVGStringList getRequiredFeatures() {
263 return ((SVGImageElement)ot).getRequiredFeatures();
264 }
265 /**
266 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getRequiredExtensions()}
267 * on the given element.
268 */
269 public final OMSVGStringList getRequiredExtensions() {
270 return ((SVGImageElement)ot).getRequiredExtensions();
271 }
272 /**
273 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getSystemLanguage()}
274 * on the given element.
275 */
276 public final OMSVGStringList getSystemLanguage() {
277 return ((SVGImageElement)ot).getSystemLanguage();
278 }
279 /**
280 * Returns true if the user agent supports the given extension, specified
281 * by a URI.
282 * @param extension The name of the extension, expressed as a URI.
283 * @return True or false, depending on whether the given extension is supported.
284 */
285 public final boolean hasExtension(String extension) {
286 return ((SVGImageElement)ot).hasExtension(extension);
287 }
288
289 // Implementation of the svg::SVGTransformable W3C IDL interface
290 /**
291 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTransformable#getTransform()}
292 * on the given element.
293 */
294 public final OMSVGAnimatedTransformList getTransform() {
295 return ((SVGImageElement)ot).getTransform();
296 }
297
298 // Implementation of the svg::SVGURIReference W3C IDL interface
299 /**
300 * Corresponds to attribute <span class='attr-name'>'xlink:href'</span> on
301 * the given element.
302 */
303 public final OMSVGAnimatedString getHref() {
304 return ((SVGImageElement)ot).getHref();
305 }
306
307 @Override
308 public final HandlerRegistration addClickHandler(ClickHandler handler) {
309 return addDomHandler(handler, ClickEvent.getType());
310 }
311 @Override
312 public final HandlerRegistration addLoadHandler(LoadHandler handler) {
313 return addDomHandler(handler, LoadEvent.getType());
314 }
315 @Override
316 public final HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
317 return addDomHandler(handler, MouseDownEvent.getType());
318 }
319 @Override
320 public final HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
321 return addDomHandler(handler, MouseMoveEvent.getType());
322 }
323 @Override
324 public final HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
325 return addDomHandler(handler, MouseOutEvent.getType());
326 }
327 @Override
328 public final HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
329 return addDomHandler(handler, MouseOverEvent.getType());
330 }
331 @Override
332 public final HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
333 return addDomHandler(handler, MouseUpEvent.getType());
334 }
335 @Override
336 public final HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
337 return addDomHandler(handler, TouchCancelEvent.getType());
338 }
339 @Override
340 public final HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
341 return addDomHandler(handler, TouchEndEvent.getType());
342 }
343 @Override
344 public final HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
345 return addDomHandler(handler, TouchMoveEvent.getType());
346 }
347 @Override
348 public final HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
349 return addDomHandler(handler, TouchStartEvent.getType());
350 }
351 @Override
352 public final HandlerRegistration addActivateHandler(ActivateHandler handler) {
353 return addDomHandler(handler, ActivateEvent.getType());
354 }
355 @Override
356 public final HandlerRegistration addFocusInHandler(FocusInHandler handler) {
357 return addDomHandler(handler, FocusInEvent.getType());
358 }
359 @Override
360 public final HandlerRegistration addFocusOutHandler(FocusOutHandler handler) {
361 return addDomHandler(handler, FocusOutEvent.getType());
362 }
363 // Helper methods
364 /**
365 * Constructor.
366 * @param x the X coordinate of the image
367 * @param y the Y coordinate of the image
368 * @param width the width of the image
369 * @param height the width of the image
370 * @param href the bitmap image referenced by this image element
371 */
372 public OMSVGImageElement(float x, float y, float width, float height, String href) {
373 this();
374 getX().getBaseVal().setValue(x);
375 getY().getBaseVal().setValue(y);
376 getWidth().getBaseVal().setValue(width);
377 getHeight().getBaseVal().setValue(height);
378 getHref().setBaseVal(href);
379 }
380 }