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.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.OMSVGRectElement} interface corresponds
78 * to the <a href='http://www.w3.org/TR/SVG11/shapes.html#RectElement' title='rect
79 * element specification'>rect</a> element.
80 */
81 @TagName("rect")
82 public class OMSVGRectElement extends OMSVGElement implements HasGraphicalHandlers, ISVGTests, ISVGLangSpace, ISVGExternalResourcesRequired, ISVGStylable, ISVGTransformable, ISVGGraphicsElement {
83 public OMSVGRectElement() {
84 this((SVGRectElement)DOMHelper.createElementNS(DOMHelper.getCurrentDocument(), SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_RECT_TAG).cast());
85 }
86
87 protected OMSVGRectElement(SVGRectElement ot) {
88 super(ot);
89 }
90
91 // Implementation of the svg::SVGRectElement W3C IDL interface
92 /**
93 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGRectElement#getX()}
94 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#RectElement'
95 * title='rect element specification'>rect</a> element.
96 */
97 public final OMSVGAnimatedLength getX() {
98 return ((SVGRectElement)ot).getX();
99 }
100 /**
101 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGRectElement#getY()}
102 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#RectElement'
103 * title='rect element specification'>rect</a> element.
104 */
105 public final OMSVGAnimatedLength getY() {
106 return ((SVGRectElement)ot).getY();
107 }
108 /**
109 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGRectElement#getWidth()}
110 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#RectElement'
111 * title='rect element specification'>rect</a> element.
112 */
113 public final OMSVGAnimatedLength getWidth() {
114 return ((SVGRectElement)ot).getWidth();
115 }
116 /**
117 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGRectElement#getHeight()}
118 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#RectElement'
119 * title='rect element specification'>rect</a> element.
120 */
121 public final OMSVGAnimatedLength getHeight() {
122 return ((SVGRectElement)ot).getHeight();
123 }
124 /**
125 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGRectElement#getRx()}
126 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#RectElement'
127 * title='rect element specification'>rect</a> element.
128 */
129 public final OMSVGAnimatedLength getRx() {
130 return ((SVGRectElement)ot).getRx();
131 }
132 /**
133 * Corresponds to attribute {@link org.vectomatic.dom.svg.OMSVGRectElement#getRy()}
134 * on the given <a href='http://www.w3.org/TR/SVG11/shapes.html#RectElement'
135 * title='rect element specification'>rect</a> element.
136 */
137 public final OMSVGAnimatedLength getRy() {
138 return ((SVGRectElement)ot).getRy();
139 }
140
141 // Implementation of the svg::SVGExternalResourcesRequired W3C IDL interface
142 /**
143 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
144 * on the given element. Note that the SVG DOM defines the attribute {@link
145 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
146 * as being of type {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean}, whereas
147 * the SVG language definition says that {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
148 * is not animated. Because the SVG language definition states that {@link
149 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
150 * cannot be animated, the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getAnimVal()}
151 * will always be the same as the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getBaseVal()}.
152 */
153 public final OMSVGAnimatedBoolean getExternalResourcesRequired() {
154 return ((SVGRectElement)ot).getExternalResourcesRequired();
155 }
156
157 // Implementation of the svg::SVGLangSpace W3C IDL interface
158 /**
159 * Corresponds to attribute <code>xml:lang</code> on the given element.
160 */
161 public final String getXmllang() {
162 return ((SVGRectElement)ot).getXmllang();
163 }
164 /**
165 * Corresponds to attribute <code>xml:lang</code> on the given element.
166 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt
167 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only
168 * attribute</a>.
169 */
170 public final void setXmllang(java.lang.String value) throws JavaScriptException {
171 ((SVGRectElement)ot).setXmllang(value);
172 }
173 /**
174 * Corresponds to attribute <code>xml:space</code> on the given element.
175 */
176 public final String getXmlspace() {
177 return ((SVGRectElement)ot).getXmlspace();
178 }
179 /**
180 * Corresponds to attribute <code>xml:space</code> on the given element.
181 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt
182 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only
183 * attribute</a>.
184 */
185 public final void setXmlspace(java.lang.String value) throws JavaScriptException {
186 ((SVGRectElement)ot).setXmlspace(value);
187 }
188
189 // Implementation of the svg::SVGLocatable W3C IDL interface
190 /**
191 * The element which established the current viewport. Often, the nearest
192 * ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' title='svg
193 * element specification'>svg</a> element. Null if the current element is
194 * the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
195 * title='svg element specification'>svg</a> element.
196 */
197 public final OMSVGElement getNearestViewportElement() {
198 return (OMSVGElement)convert(((SVGRectElement)ot).getNearestViewportElement());
199 }
200 /**
201 * The farthest ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
202 * title='svg element specification'>svg</a> element. Null if the current
203 * element is the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
204 * title='svg element specification'>svg</a> element.
205 */
206 public final OMSVGElement getFarthestViewportElement() {
207 return (OMSVGElement)convert(((SVGRectElement)ot).getFarthestViewportElement());
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 ((SVGRectElement)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 ((SVGRectElement)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 ((SVGRectElement)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 ((SVGRectElement)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 ((SVGRectElement)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 ((SVGRectElement)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 ((SVGRectElement)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 ((SVGRectElement)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 ((SVGRectElement)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 addLoadHandler(LoadHandler handler) {
311 return addDomHandler(handler, LoadEvent.getType());
312 }
313 @Override
314 public final HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
315 return addDomHandler(handler, MouseDownEvent.getType());
316 }
317 @Override
318 public final HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
319 return addDomHandler(handler, MouseMoveEvent.getType());
320 }
321 @Override
322 public final HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
323 return addDomHandler(handler, MouseOutEvent.getType());
324 }
325 @Override
326 public final HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
327 return addDomHandler(handler, MouseOverEvent.getType());
328 }
329 @Override
330 public final HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
331 return addDomHandler(handler, MouseUpEvent.getType());
332 }
333 @Override
334 public final HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
335 return addDomHandler(handler, TouchCancelEvent.getType());
336 }
337 @Override
338 public final HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
339 return addDomHandler(handler, TouchEndEvent.getType());
340 }
341 @Override
342 public final HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
343 return addDomHandler(handler, TouchMoveEvent.getType());
344 }
345 @Override
346 public final HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
347 return addDomHandler(handler, TouchStartEvent.getType());
348 }
349 @Override
350 public final HandlerRegistration addActivateHandler(ActivateHandler handler) {
351 return addDomHandler(handler, ActivateEvent.getType());
352 }
353 @Override
354 public final HandlerRegistration addFocusInHandler(FocusInHandler handler) {
355 return addDomHandler(handler, FocusInEvent.getType());
356 }
357 @Override
358 public final HandlerRegistration addFocusOutHandler(FocusOutHandler handler) {
359 return addDomHandler(handler, FocusOutEvent.getType());
360 }
361 // Helper methods
362 /**
363 * Constructor
364 * @param x the X coordinate of the upper left corner
365 * @param y the Y coordinate of the upper left corner
366 * @param width the width of the rectangle
367 * @param height the width of the rectangle
368 * @param rx the X-axis radius of the rectangle corner
369 * @param ry the Y-axis radius of the rectangle corner
370 */
371 public OMSVGRectElement(float x, float y, float width, float height, float rx, float ry) {
372 this();
373 getX().getBaseVal().setValue(x);
374 getY().getBaseVal().setValue(y);
375 getWidth().getBaseVal().setValue(width);
376 getHeight().getBaseVal().setValue(height);
377 if (rx != 0f) {
378 getRx().getBaseVal().setValue(rx);
379 }
380 if (ry != 0f) {
381 getRy().getBaseVal().setValue(ry);
382 }
383 }
384 /**
385 * Constructor
386 * @param rect The SVG rect to use to initialize this rect.
387 */
388 public OMSVGRectElement(OMSVGRect rect) {
389 this(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), 0, 0);
390 }
391 }