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.SVGLineElement; 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.utils.DOMHelper; 048import org.vectomatic.dom.svg.utils.SVGConstants; 049 050import com.google.gwt.core.client.JavaScriptException; 051import com.google.gwt.dom.client.TagName; 052import com.google.gwt.event.dom.client.ClickEvent; 053import com.google.gwt.event.dom.client.ClickHandler; 054import com.google.gwt.event.dom.client.DragEndEvent; 055import com.google.gwt.event.dom.client.DragEndHandler; 056import com.google.gwt.event.dom.client.DragEnterEvent; 057import com.google.gwt.event.dom.client.DragEnterHandler; 058import com.google.gwt.event.dom.client.DragEvent; 059import com.google.gwt.event.dom.client.DragHandler; 060import com.google.gwt.event.dom.client.DragLeaveEvent; 061import com.google.gwt.event.dom.client.DragLeaveHandler; 062import com.google.gwt.event.dom.client.DragOverEvent; 063import com.google.gwt.event.dom.client.DragOverHandler; 064import com.google.gwt.event.dom.client.DragStartEvent; 065import com.google.gwt.event.dom.client.DragStartHandler; 066import com.google.gwt.event.dom.client.DropEvent; 067import com.google.gwt.event.dom.client.DropHandler; 068import com.google.gwt.event.dom.client.LoadEvent; 069import com.google.gwt.event.dom.client.LoadHandler; 070import com.google.gwt.event.dom.client.MouseDownEvent; 071import com.google.gwt.event.dom.client.MouseDownHandler; 072import com.google.gwt.event.dom.client.MouseMoveEvent; 073import com.google.gwt.event.dom.client.MouseMoveHandler; 074import com.google.gwt.event.dom.client.MouseOutEvent; 075import com.google.gwt.event.dom.client.MouseOutHandler; 076import com.google.gwt.event.dom.client.MouseOverEvent; 077import com.google.gwt.event.dom.client.MouseOverHandler; 078import com.google.gwt.event.dom.client.MouseUpEvent; 079import com.google.gwt.event.dom.client.MouseUpHandler; 080import com.google.gwt.event.dom.client.TouchCancelEvent; 081import com.google.gwt.event.dom.client.TouchCancelHandler; 082import com.google.gwt.event.dom.client.TouchEndEvent; 083import com.google.gwt.event.dom.client.TouchEndHandler; 084import com.google.gwt.event.dom.client.TouchMoveEvent; 085import com.google.gwt.event.dom.client.TouchMoveHandler; 086import com.google.gwt.event.dom.client.TouchStartEvent; 087import com.google.gwt.event.dom.client.TouchStartHandler; 088import com.google.gwt.event.shared.HandlerRegistration; 089 090/** 091 * The {@link org.vectomatic.dom.svg.OMSVGLineElement} interface corresponds 092 * to the <a href='http://www.w3.org/TR/SVG11/shapes.html#LineElement' title='line 093 * element specification'>line</a> element. 094 */ 095@TagName("line") 096public class OMSVGLineElement extends OMSVGElement implements HasGraphicalHandlers, ISVGTests, ISVGLangSpace, ISVGExternalResourcesRequired, ISVGStylable, ISVGTransformable, ISVGGraphicsElement { 097 public OMSVGLineElement() { 098 this((SVGLineElement)DOMHelper.createElementNS(DOMHelper.getCurrentDocument(), SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_LINE_TAG).cast()); 099 } 100 101 protected OMSVGLineElement(SVGLineElement ot) { 102 super(ot); 103 } 104 105 // Implementation of the svg::SVGLineElement W3C IDL interface 106 /** 107 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGLineElement#getX1()} 108 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#LineElement' 109 * title='line element specification'>line</a> element. 110 */ 111 public final OMSVGAnimatedLength getX1() { 112 return ((SVGLineElement)ot).getX1(); 113 } 114 /** 115 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGLineElement#getY1()} 116 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#LineElement' 117 * title='line element specification'>line</a> element. 118 */ 119 public final OMSVGAnimatedLength getY1() { 120 return ((SVGLineElement)ot).getY1(); 121 } 122 /** 123 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGLineElement#getX2()} 124 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#LineElement' 125 * title='line element specification'>line</a> element. 126 */ 127 public final OMSVGAnimatedLength getX2() { 128 return ((SVGLineElement)ot).getX2(); 129 } 130 /** 131 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGLineElement#getY2()} 132 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#LineElement' 133 * title='line element specification'>line</a> element. 134 */ 135 public final OMSVGAnimatedLength getY2() { 136 return ((SVGLineElement)ot).getY2(); 137 } 138 139 // Implementation of the svg::SVGExternalResourcesRequired W3C IDL interface 140 /** 141 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()} 142 * on the given element. Note that the SVG DOM defines the attribute {@link 143 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()} 144 * as being of type {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean}, whereas 145 * the SVG language definition says that {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()} 146 * is not animated. Because the SVG language definition states that {@link 147 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()} 148 * cannot be animated, the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getAnimVal()} 149 * will always be the same as the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getBaseVal()}. 150 */ 151 public final OMSVGAnimatedBoolean getExternalResourcesRequired() { 152 return ((SVGLineElement)ot).getExternalResourcesRequired(); 153 } 154 155 // Implementation of the svg::SVGLangSpace W3C IDL interface 156 /** 157 * Corresponds to attribute <code>xml:lang</code> on the given element. 158 */ 159 public final String getXmllang() { 160 return ((SVGLineElement)ot).getXmllang(); 161 } 162 /** 163 * Corresponds to attribute <code>xml:lang</code> on the given element. 164 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 165 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 166 * attribute</a>. 167 */ 168 public final void setXmllang(java.lang.String value) throws JavaScriptException { 169 ((SVGLineElement)ot).setXmllang(value); 170 } 171 /** 172 * Corresponds to attribute <code>xml:space</code> on the given element. 173 */ 174 public final String getXmlspace() { 175 return ((SVGLineElement)ot).getXmlspace(); 176 } 177 /** 178 * Corresponds to attribute <code>xml:space</code> on the given element. 179 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 180 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 181 * attribute</a>. 182 */ 183 public final void setXmlspace(java.lang.String value) throws JavaScriptException { 184 ((SVGLineElement)ot).setXmlspace(value); 185 } 186 187 // Implementation of the svg::SVGLocatable W3C IDL interface 188 /** 189 * The element which established the current viewport. Often, the nearest 190 * ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' title='svg 191 * element specification'>svg</a> element. Null if the current element is 192 * the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' 193 * title='svg element specification'>svg</a> element. 194 */ 195 public final OMSVGElement getNearestViewportElement() { 196 SVGElement elt = ((SVGLineElement)ot).getNearestViewportElement(); 197 return elt != null ? (OMSVGElement)convert(elt) : null; 198 } 199 /** 200 * The farthest ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' 201 * title='svg element specification'>svg</a> element. Null if the current 202 * element is the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' 203 * title='svg element specification'>svg</a> element. 204 */ 205 public final OMSVGElement getFarthestViewportElement() { 206 SVGElement elt = ((SVGLineElement)ot).getFarthestViewportElement(); 207 return elt != null ? (OMSVGElement)convert(elt) : null; 208 } 209 /** 210 * Returns the tight bounding box in current user space (i.e., after application 211 * of the <code>transform</code> attribute, if any) on the geometry of all 212 * contained graphics elements, exclusive of stroking, clipping, masking and 213 * filter effects). Note that getBBox must return the actual bounding box 214 * at the time the method was called, even in case the element has not yet 215 * been rendered. 216 * @return An {@link org.vectomatic.dom.svg.OMSVGRect} object that defines 217 * the bounding box. 218 */ 219 public final OMSVGRect getBBox() { 220 return ((SVGLineElement)ot).getBBox(); 221 } 222 /** 223 * Returns the transformation matrix from current user units (i.e., after 224 * application of the <code>transform</code> attribute, if any) to the viewport 225 * coordinate system for the {@link org.vectomatic.dom.svg.itf.ISVGLocatable#getNearestViewportElement()}. 226 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines 227 * the CTM. 228 */ 229 public final OMSVGMatrix getCTM() { 230 return ((SVGLineElement)ot).getCTM(); 231 } 232 /** 233 * Returns the transformation matrix from current user units (i.e., after 234 * application of the <code>transform</code> attribute, if any) to the parent 235 * user agent's notice of a "pixel". For display devices, ideally this represents 236 * a physical screen pixel. For other devices or environments where physical 237 * pixel sizes are not known, then an algorithm similar to the CSS2 definition 238 * of a "pixel" can be used instead. Note that null is returned if this element 239 * is not hooked into the document tree. This method would have been more 240 * aptly named as <code>getClientCTM</code>, but the name <code>getScreenCTM</code> 241 * is kept for historical reasons. 242 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines 243 * the given transformation matrix. 244 */ 245 public final OMSVGMatrix getScreenCTM() { 246 return ((SVGLineElement)ot).getScreenCTM(); 247 } 248 /** 249 * Returns the transformation matrix from the user coordinate system on the 250 * current element (after application of the <code>transform</code> attribute, 251 * if any) to the user coordinate system on parameter <var>element</var> (after 252 * application of its <code>transform</code> attribute, if any). 253 * @param element The target element. 254 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines 255 * the transformation. 256 * @throws SVGException(SVG_MATRIX_NOT_INVERTABLE) Raised if the currently 257 * defined transformation matrices make it impossible to compute the given 258 * matrix (e.g., because one of the transformations is singular). 259 */ 260 public final OMSVGMatrix getTransformToElement(OMSVGElement element) throws JavaScriptException { 261 return ((SVGLineElement)ot).getTransformToElement(((SVGElement)element.ot)); 262 } 263 264 // Implementation of the svg::SVGTests W3C IDL interface 265 /** 266 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getRequiredFeatures()} 267 * on the given element. 268 */ 269 public final OMSVGStringList getRequiredFeatures() { 270 return ((SVGLineElement)ot).getRequiredFeatures(); 271 } 272 /** 273 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getRequiredExtensions()} 274 * on the given element. 275 */ 276 public final OMSVGStringList getRequiredExtensions() { 277 return ((SVGLineElement)ot).getRequiredExtensions(); 278 } 279 /** 280 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getSystemLanguage()} 281 * on the given element. 282 */ 283 public final OMSVGStringList getSystemLanguage() { 284 return ((SVGLineElement)ot).getSystemLanguage(); 285 } 286 /** 287 * Returns true if the user agent supports the given extension, specified 288 * by a URI. 289 * @param extension The name of the extension, expressed as a URI. 290 * @return True or false, depending on whether the given extension is supported. 291 */ 292 public final boolean hasExtension(String extension) { 293 return ((SVGLineElement)ot).hasExtension(extension); 294 } 295 296 // Implementation of the svg::SVGTransformable W3C IDL interface 297 /** 298 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTransformable#getTransform()} 299 * on the given element. 300 */ 301 public final OMSVGAnimatedTransformList getTransform() { 302 return ((SVGLineElement)ot).getTransform(); 303 } 304 305 @Override 306 public final HandlerRegistration addClickHandler(ClickHandler handler) { 307 return addDomHandler(handler, ClickEvent.getType()); 308 } 309 @Override 310 public final HandlerRegistration addDragEndHandler(DragEndHandler handler) { 311 return addDomHandler(handler, DragEndEvent.getType()); 312 } 313 @Override 314 public final HandlerRegistration addDragEnterHandler(DragEnterHandler handler) { 315 return addDomHandler(handler, DragEnterEvent.getType()); 316 } 317 @Override 318 public final HandlerRegistration addDragHandler(DragHandler handler) { 319 return addDomHandler(handler, DragEvent.getType()); 320 } 321 @Override 322 public final HandlerRegistration addDragLeaveHandler(DragLeaveHandler handler) { 323 return addDomHandler(handler, DragLeaveEvent.getType()); 324 } 325 @Override 326 public final HandlerRegistration addDragOverHandler(DragOverHandler handler) { 327 return addDomHandler(handler, DragOverEvent.getType()); 328 } 329 @Override 330 public final HandlerRegistration addDragStartHandler(DragStartHandler handler) { 331 return addDomHandler(handler, DragStartEvent.getType()); 332 } 333 @Override 334 public final HandlerRegistration addDropHandler(DropHandler handler) { 335 return addDomHandler(handler, DropEvent.getType()); 336 } 337 @Override 338 public final HandlerRegistration addLoadHandler(LoadHandler handler) { 339 return addDomHandler(handler, LoadEvent.getType()); 340 } 341 @Override 342 public final HandlerRegistration addMouseDownHandler(MouseDownHandler handler) { 343 return addDomHandler(handler, MouseDownEvent.getType()); 344 } 345 @Override 346 public final HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) { 347 return addDomHandler(handler, MouseMoveEvent.getType()); 348 } 349 @Override 350 public final HandlerRegistration addMouseOutHandler(MouseOutHandler handler) { 351 return addDomHandler(handler, MouseOutEvent.getType()); 352 } 353 @Override 354 public final HandlerRegistration addMouseOverHandler(MouseOverHandler handler) { 355 return addDomHandler(handler, MouseOverEvent.getType()); 356 } 357 @Override 358 public final HandlerRegistration addMouseUpHandler(MouseUpHandler handler) { 359 return addDomHandler(handler, MouseUpEvent.getType()); 360 } 361 @Override 362 public final HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) { 363 return addDomHandler(handler, TouchCancelEvent.getType()); 364 } 365 @Override 366 public final HandlerRegistration addTouchEndHandler(TouchEndHandler handler) { 367 return addDomHandler(handler, TouchEndEvent.getType()); 368 } 369 @Override 370 public final HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) { 371 return addDomHandler(handler, TouchMoveEvent.getType()); 372 } 373 @Override 374 public final HandlerRegistration addTouchStartHandler(TouchStartHandler handler) { 375 return addDomHandler(handler, TouchStartEvent.getType()); 376 } 377 @Override 378 public final HandlerRegistration addActivateHandler(ActivateHandler handler) { 379 return addDomHandler(handler, ActivateEvent.getType()); 380 } 381 @Override 382 public final HandlerRegistration addFocusInHandler(FocusInHandler handler) { 383 return addDomHandler(handler, FocusInEvent.getType()); 384 } 385 @Override 386 public final HandlerRegistration addFocusOutHandler(FocusOutHandler handler) { 387 return addDomHandler(handler, FocusOutEvent.getType()); 388 } 389 // Helper methods 390 /** 391 * Computes the intersection point between this line and the tangent 392 * perpendicular to this line through p. The intersection is 393 * returned in parametric form. If one considers the parametric 394 * equation of this line to be: 395 * <pre>P = P1 + t * (P2 - P1)</pre> 396 * then this method returns the value of t at the intersection point. 397 * @param p The point which defines the tangent. 398 * @return the parametric value of the intersection point. 399 */ 400 public final float parametricIntersection(OMSVGPoint p) { 401 return ((SVGLineElement)ot).parametricIntersection(p); 402 } 403 /** 404 * Computes the intersection point between this line and the tangent 405 * perpendicular to this line through p. 406 * @param p The point which defines the tangent. 407 * @return the intersection point. 408 */ 409 public final OMSVGPoint intersectionPoint(OMSVGPoint p) { 410 return ((SVGLineElement)ot).intersectionPoint(p); 411 } 412 /** 413 * Computes the intersection point between this line and the tangent 414 * perpendicular to this line through p and puts the 415 * result in the specified destination point. 416 * @param p The point which defines the tangent. 417 * @param destination The point where to store the result. 418 * @return the intersection point. 419 */ 420 public final OMSVGPoint intersectionPoint(OMSVGPoint p, OMSVGPoint destination) { 421 return ((SVGLineElement)ot).intersectionPoint(p); 422 } 423 /** 424 * Computes the distance from the specified point to this line. 425 * @param p A point in the plane 426 * @return the distance to this line. 427 */ 428 public final float distanceToLine(OMSVGPoint p) { 429 return ((SVGLineElement)ot).distanceToLine(p); 430 } 431 /** 432 * Constructor 433 * @param x1 the X coordinate of the first endpoint 434 * @param y1 the Y coordinate of the first endpoint 435 * @param x2 the X coordinate of the second endpoint 436 * @param y2 the Y coordinate of the second endpoint 437 */ 438 public OMSVGLineElement(float x1, float y1, float x2, float y2) { 439 this(); 440 getX1().getBaseVal().setValue(x1); 441 getY1().getBaseVal().setValue(y1); 442 getX2().getBaseVal().setValue(x2); 443 getY2().getBaseVal().setValue(y2); 444 } 445 /** 446 * Constructor 447 * @param p1 the first endpoint 448 * @param p2 the the second endpoint 449 */ 450 public OMSVGLineElement(OMSVGPoint p1, OMSVGPoint p2) { 451 this(p1.getX(), p1.getY(), p2.getX(), p2.getY()); 452 } 453}