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