View Javadoc

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