001/********************************************** 002 * Copyright (C) 2010-2011 Lukas Laag 003 * This file is part of lib-gwt-svg. 004 * 005 * libgwtsvg is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Lesser General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * libgwtsvg is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/ 017 **********************************************/ 018/* 019 * Copyright (c) 2004 World Wide Web Consortium, 020 * 021 * (Massachusetts Institute of Technology, European Research Consortium for 022 * Informatics and Mathematics, Keio University). All Rights Reserved. This 023 * work is distributed under the W3C(r) Software License [1] in the hope that 024 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 025 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 026 * 027 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231 028 */ 029 030package org.vectomatic.dom.svg; 031 032import org.vectomatic.dom.svg.events.ActivateEvent; 033import org.vectomatic.dom.svg.events.ActivateHandler; 034import org.vectomatic.dom.svg.events.FocusInEvent; 035import org.vectomatic.dom.svg.events.FocusInHandler; 036import org.vectomatic.dom.svg.events.FocusOutEvent; 037import org.vectomatic.dom.svg.events.FocusOutHandler; 038import org.vectomatic.dom.svg.events.HasGraphicalHandlers; 039import org.vectomatic.dom.svg.impl.SVGElement; 040import org.vectomatic.dom.svg.impl.SVGImageElement; 041import org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired; 042import org.vectomatic.dom.svg.itf.ISVGGraphicsElement; 043import org.vectomatic.dom.svg.itf.ISVGLangSpace; 044import org.vectomatic.dom.svg.itf.ISVGStylable; 045import org.vectomatic.dom.svg.itf.ISVGTests; 046import org.vectomatic.dom.svg.itf.ISVGTransformable; 047import org.vectomatic.dom.svg.itf.ISVGURIReference; 048import org.vectomatic.dom.svg.utils.DOMHelper; 049import org.vectomatic.dom.svg.utils.SVGConstants; 050 051import com.google.gwt.core.client.JavaScriptException; 052import com.google.gwt.dom.client.TagName; 053import com.google.gwt.event.dom.client.ClickEvent; 054import com.google.gwt.event.dom.client.ClickHandler; 055import com.google.gwt.event.dom.client.DragEndEvent; 056import com.google.gwt.event.dom.client.DragEndHandler; 057import com.google.gwt.event.dom.client.DragEnterEvent; 058import com.google.gwt.event.dom.client.DragEnterHandler; 059import com.google.gwt.event.dom.client.DragEvent; 060import com.google.gwt.event.dom.client.DragHandler; 061import com.google.gwt.event.dom.client.DragLeaveEvent; 062import com.google.gwt.event.dom.client.DragLeaveHandler; 063import com.google.gwt.event.dom.client.DragOverEvent; 064import com.google.gwt.event.dom.client.DragOverHandler; 065import com.google.gwt.event.dom.client.DragStartEvent; 066import com.google.gwt.event.dom.client.DragStartHandler; 067import com.google.gwt.event.dom.client.DropEvent; 068import com.google.gwt.event.dom.client.DropHandler; 069import com.google.gwt.event.dom.client.LoadEvent; 070import com.google.gwt.event.dom.client.LoadHandler; 071import com.google.gwt.event.dom.client.MouseDownEvent; 072import com.google.gwt.event.dom.client.MouseDownHandler; 073import com.google.gwt.event.dom.client.MouseMoveEvent; 074import com.google.gwt.event.dom.client.MouseMoveHandler; 075import com.google.gwt.event.dom.client.MouseOutEvent; 076import com.google.gwt.event.dom.client.MouseOutHandler; 077import com.google.gwt.event.dom.client.MouseOverEvent; 078import com.google.gwt.event.dom.client.MouseOverHandler; 079import com.google.gwt.event.dom.client.MouseUpEvent; 080import com.google.gwt.event.dom.client.MouseUpHandler; 081import com.google.gwt.event.dom.client.TouchCancelEvent; 082import com.google.gwt.event.dom.client.TouchCancelHandler; 083import com.google.gwt.event.dom.client.TouchEndEvent; 084import com.google.gwt.event.dom.client.TouchEndHandler; 085import com.google.gwt.event.dom.client.TouchMoveEvent; 086import com.google.gwt.event.dom.client.TouchMoveHandler; 087import com.google.gwt.event.dom.client.TouchStartEvent; 088import com.google.gwt.event.dom.client.TouchStartHandler; 089import com.google.gwt.event.shared.HandlerRegistration; 090 091/** 092 * The {@link org.vectomatic.dom.svg.OMSVGImageElement} interface corresponds 093 * to the <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement' title='image 094 * element specification'>image</a> element. 095 */ 096@TagName("image") 097public class OMSVGImageElement extends OMSVGElement implements HasGraphicalHandlers, ISVGURIReference, ISVGTests, ISVGLangSpace, ISVGExternalResourcesRequired, ISVGStylable, ISVGTransformable, ISVGGraphicsElement { 098 public OMSVGImageElement() { 099 this((SVGImageElement)DOMHelper.createElementNS(DOMHelper.getCurrentDocument(), SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_IMAGE_TAG).cast()); 100 } 101 102 protected OMSVGImageElement(SVGImageElement ot) { 103 super(ot); 104 } 105 106 // Implementation of the svg::SVGImageElement W3C IDL interface 107 /** 108 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getX()} 109 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement' 110 * title='image element specification'>image</a> element. 111 */ 112 public final OMSVGAnimatedLength getX() { 113 return ((SVGImageElement)ot).getX(); 114 } 115 /** 116 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getY()} 117 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement' 118 * title='image element specification'>image</a> element. 119 */ 120 public final OMSVGAnimatedLength getY() { 121 return ((SVGImageElement)ot).getY(); 122 } 123 /** 124 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getWidth()} 125 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement' 126 * title='image element specification'>image</a> element. 127 */ 128 public final OMSVGAnimatedLength getWidth() { 129 return ((SVGImageElement)ot).getWidth(); 130 } 131 /** 132 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getHeight()} 133 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement' 134 * title='image element specification'>image</a> element. 135 */ 136 public final OMSVGAnimatedLength getHeight() { 137 return ((SVGImageElement)ot).getHeight(); 138 } 139 /** 140 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGImageElement#getPreserveAspectRatio()} 141 * on the given <a href='http://www.w3.org/TR/SVG11/struct.html#ImageElement' 142 * title='image element specification'>image</a> element. 143 */ 144 public final OMSVGAnimatedPreserveAspectRatio getPreserveAspectRatio() { 145 return ((SVGImageElement)ot).getPreserveAspectRatio(); 146 } 147 148 // Implementation of the svg::SVGExternalResourcesRequired W3C IDL interface 149 /** 150 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()} 151 * on the given element. Note that the SVG DOM defines the attribute {@link 152 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()} 153 * as being of type {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean}, whereas 154 * the SVG language definition says that {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()} 155 * is not animated. Because the SVG language definition states that {@link 156 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()} 157 * cannot be animated, the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getAnimVal()} 158 * will always be the same as the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getBaseVal()}. 159 */ 160 public final OMSVGAnimatedBoolean getExternalResourcesRequired() { 161 return ((SVGImageElement)ot).getExternalResourcesRequired(); 162 } 163 164 // Implementation of the svg::SVGLangSpace W3C IDL interface 165 /** 166 * Corresponds to attribute <code>xml:lang</code> on the given element. 167 */ 168 public final String getXmllang() { 169 return ((SVGImageElement)ot).getXmllang(); 170 } 171 /** 172 * Corresponds to attribute <code>xml:lang</code> on the given element. 173 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 174 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 175 * attribute</a>. 176 */ 177 public final void setXmllang(java.lang.String value) throws JavaScriptException { 178 ((SVGImageElement)ot).setXmllang(value); 179 } 180 /** 181 * Corresponds to attribute <code>xml:space</code> on the given element. 182 */ 183 public final String getXmlspace() { 184 return ((SVGImageElement)ot).getXmlspace(); 185 } 186 /** 187 * Corresponds to attribute <code>xml:space</code> on the given element. 188 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 189 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 190 * attribute</a>. 191 */ 192 public final void setXmlspace(java.lang.String value) throws JavaScriptException { 193 ((SVGImageElement)ot).setXmlspace(value); 194 } 195 196 // Implementation of the svg::SVGLocatable W3C IDL interface 197 /** 198 * The element which established the current viewport. Often, the nearest 199 * ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' title='svg 200 * element specification'>svg</a> element. Null if the current element is 201 * the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' 202 * title='svg element specification'>svg</a> element. 203 */ 204 public final OMSVGElement getNearestViewportElement() { 205 SVGElement elt = ((SVGImageElement)ot).getNearestViewportElement(); 206 return elt != null ? (OMSVGElement)convert(elt) : null; 207 } 208 /** 209 * The farthest ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' 210 * title='svg element specification'>svg</a> element. Null if the current 211 * element is the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' 212 * title='svg element specification'>svg</a> element. 213 */ 214 public final OMSVGElement getFarthestViewportElement() { 215 SVGElement elt = ((SVGImageElement)ot).getFarthestViewportElement(); 216 return elt != null ? (OMSVGElement)convert(elt) : null; 217 } 218 /** 219 * Returns the tight bounding box in current user space (i.e., after application 220 * of the <code>transform</code> attribute, if any) on the geometry of all 221 * contained graphics elements, exclusive of stroking, clipping, masking and 222 * filter effects). Note that getBBox must return the actual bounding box 223 * at the time the method was called, even in case the element has not yet 224 * been rendered. 225 * @return An {@link org.vectomatic.dom.svg.OMSVGRect} object that defines 226 * the bounding box. 227 */ 228 public final OMSVGRect getBBox() { 229 return ((SVGImageElement)ot).getBBox(); 230 } 231 /** 232 * Returns the transformation matrix from current user units (i.e., after 233 * application of the <code>transform</code> attribute, if any) to the viewport 234 * coordinate system for the {@link org.vectomatic.dom.svg.itf.ISVGLocatable#getNearestViewportElement()}. 235 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines 236 * the CTM. 237 */ 238 public final OMSVGMatrix getCTM() { 239 return ((SVGImageElement)ot).getCTM(); 240 } 241 /** 242 * Returns the transformation matrix from current user units (i.e., after 243 * application of the <code>transform</code> attribute, if any) to the parent 244 * user agent's notice of a "pixel". For display devices, ideally this represents 245 * a physical screen pixel. For other devices or environments where physical 246 * pixel sizes are not known, then an algorithm similar to the CSS2 definition 247 * of a "pixel" can be used instead. Note that null is returned if this element 248 * is not hooked into the document tree. This method would have been more 249 * aptly named as <code>getClientCTM</code>, but the name <code>getScreenCTM</code> 250 * is kept for historical reasons. 251 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines 252 * the given transformation matrix. 253 */ 254 public final OMSVGMatrix getScreenCTM() { 255 return ((SVGImageElement)ot).getScreenCTM(); 256 } 257 /** 258 * Returns the transformation matrix from the user coordinate system on the 259 * current element (after application of the <code>transform</code> attribute, 260 * if any) to the user coordinate system on parameter <var>element</var> (after 261 * application of its <code>transform</code> attribute, if any). 262 * @param element The target element. 263 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines 264 * the transformation. 265 * @throws SVGException(SVG_MATRIX_NOT_INVERTABLE) Raised if the currently 266 * defined transformation matrices make it impossible to compute the given 267 * matrix (e.g., because one of the transformations is singular). 268 */ 269 public final OMSVGMatrix getTransformToElement(OMSVGElement element) throws JavaScriptException { 270 return ((SVGImageElement)ot).getTransformToElement(((SVGElement)element.ot)); 271 } 272 273 // Implementation of the svg::SVGTests W3C IDL interface 274 /** 275 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getRequiredFeatures()} 276 * on the given element. 277 */ 278 public final OMSVGStringList getRequiredFeatures() { 279 return ((SVGImageElement)ot).getRequiredFeatures(); 280 } 281 /** 282 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getRequiredExtensions()} 283 * on the given element. 284 */ 285 public final OMSVGStringList getRequiredExtensions() { 286 return ((SVGImageElement)ot).getRequiredExtensions(); 287 } 288 /** 289 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getSystemLanguage()} 290 * on the given element. 291 */ 292 public final OMSVGStringList getSystemLanguage() { 293 return ((SVGImageElement)ot).getSystemLanguage(); 294 } 295 /** 296 * Returns true if the user agent supports the given extension, specified 297 * by a URI. 298 * @param extension The name of the extension, expressed as a URI. 299 * @return True or false, depending on whether the given extension is supported. 300 */ 301 public final boolean hasExtension(String extension) { 302 return ((SVGImageElement)ot).hasExtension(extension); 303 } 304 305 // Implementation of the svg::SVGTransformable W3C IDL interface 306 /** 307 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTransformable#getTransform()} 308 * on the given element. 309 */ 310 public final OMSVGAnimatedTransformList getTransform() { 311 return ((SVGImageElement)ot).getTransform(); 312 } 313 314 // Implementation of the svg::SVGURIReference W3C IDL interface 315 /** 316 * Corresponds to attribute <span class='attr-name'>'xlink:href'</span> on 317 * the given element. 318 */ 319 public final OMSVGAnimatedString getHref() { 320 return ((SVGImageElement)ot).getHref(); 321 } 322 323 @Override 324 public final HandlerRegistration addClickHandler(ClickHandler handler) { 325 return addDomHandler(handler, ClickEvent.getType()); 326 } 327 @Override 328 public final HandlerRegistration addDragEndHandler(DragEndHandler handler) { 329 return addDomHandler(handler, DragEndEvent.getType()); 330 } 331 @Override 332 public final HandlerRegistration addDragEnterHandler(DragEnterHandler handler) { 333 return addDomHandler(handler, DragEnterEvent.getType()); 334 } 335 @Override 336 public final HandlerRegistration addDragHandler(DragHandler handler) { 337 return addDomHandler(handler, DragEvent.getType()); 338 } 339 @Override 340 public final HandlerRegistration addDragLeaveHandler(DragLeaveHandler handler) { 341 return addDomHandler(handler, DragLeaveEvent.getType()); 342 } 343 @Override 344 public final HandlerRegistration addDragOverHandler(DragOverHandler handler) { 345 return addDomHandler(handler, DragOverEvent.getType()); 346 } 347 @Override 348 public final HandlerRegistration addDragStartHandler(DragStartHandler handler) { 349 return addDomHandler(handler, DragStartEvent.getType()); 350 } 351 @Override 352 public final HandlerRegistration addDropHandler(DropHandler handler) { 353 return addDomHandler(handler, DropEvent.getType()); 354 } 355 @Override 356 public final HandlerRegistration addLoadHandler(LoadHandler handler) { 357 return addDomHandler(handler, LoadEvent.getType()); 358 } 359 @Override 360 public final HandlerRegistration addMouseDownHandler(MouseDownHandler handler) { 361 return addDomHandler(handler, MouseDownEvent.getType()); 362 } 363 @Override 364 public final HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) { 365 return addDomHandler(handler, MouseMoveEvent.getType()); 366 } 367 @Override 368 public final HandlerRegistration addMouseOutHandler(MouseOutHandler handler) { 369 return addDomHandler(handler, MouseOutEvent.getType()); 370 } 371 @Override 372 public final HandlerRegistration addMouseOverHandler(MouseOverHandler handler) { 373 return addDomHandler(handler, MouseOverEvent.getType()); 374 } 375 @Override 376 public final HandlerRegistration addMouseUpHandler(MouseUpHandler handler) { 377 return addDomHandler(handler, MouseUpEvent.getType()); 378 } 379 @Override 380 public final HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) { 381 return addDomHandler(handler, TouchCancelEvent.getType()); 382 } 383 @Override 384 public final HandlerRegistration addTouchEndHandler(TouchEndHandler handler) { 385 return addDomHandler(handler, TouchEndEvent.getType()); 386 } 387 @Override 388 public final HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) { 389 return addDomHandler(handler, TouchMoveEvent.getType()); 390 } 391 @Override 392 public final HandlerRegistration addTouchStartHandler(TouchStartHandler handler) { 393 return addDomHandler(handler, TouchStartEvent.getType()); 394 } 395 @Override 396 public final HandlerRegistration addActivateHandler(ActivateHandler handler) { 397 return addDomHandler(handler, ActivateEvent.getType()); 398 } 399 @Override 400 public final HandlerRegistration addFocusInHandler(FocusInHandler handler) { 401 return addDomHandler(handler, FocusInEvent.getType()); 402 } 403 @Override 404 public final HandlerRegistration addFocusOutHandler(FocusOutHandler handler) { 405 return addDomHandler(handler, FocusOutEvent.getType()); 406 } 407 // Helper methods 408 /** 409 * Constructor. 410 * @param x the X coordinate of the image 411 * @param y the Y coordinate of the image 412 * @param width the width of the image 413 * @param height the width of the image 414 * @param href the bitmap image referenced by this image element 415 */ 416 public OMSVGImageElement(float x, float y, float width, float height, String href) { 417 this(); 418 getX().getBaseVal().setValue(x); 419 getY().getBaseVal().setValue(y); 420 getWidth().getBaseVal().setValue(width); 421 getHeight().getBaseVal().setValue(height); 422 getHref().setBaseVal(href); 423 } 424}