View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 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  package org.vectomatic.dom.svg.ui;
19  
20  import org.vectomatic.dom.svg.OMSVGAnimatedString;
21  import org.vectomatic.dom.svg.OMSVGSVGElement;
22  import org.vectomatic.dom.svg.OMSVGStyle;
23  import org.vectomatic.dom.svg.events.ActivateHandler;
24  import org.vectomatic.dom.svg.events.FocusInHandler;
25  import org.vectomatic.dom.svg.events.FocusOutHandler;
26  import org.vectomatic.dom.svg.events.HasDocumentHandlers;
27  import org.vectomatic.dom.svg.events.HasGraphicalHandlers;
28  import org.vectomatic.dom.svg.events.SVGZoomHandler;
29  import org.vectomatic.dom.svg.itf.ISVGStylable;
30  
31  import com.google.gwt.event.dom.client.ClickEvent;
32  import com.google.gwt.event.dom.client.ClickHandler;
33  import com.google.gwt.event.dom.client.DragEndEvent;
34  import com.google.gwt.event.dom.client.DragEndHandler;
35  import com.google.gwt.event.dom.client.DragEnterEvent;
36  import com.google.gwt.event.dom.client.DragEnterHandler;
37  import com.google.gwt.event.dom.client.DragEvent;
38  import com.google.gwt.event.dom.client.DragHandler;
39  import com.google.gwt.event.dom.client.DragLeaveEvent;
40  import com.google.gwt.event.dom.client.DragLeaveHandler;
41  import com.google.gwt.event.dom.client.DragOverEvent;
42  import com.google.gwt.event.dom.client.DragOverHandler;
43  import com.google.gwt.event.dom.client.DragStartEvent;
44  import com.google.gwt.event.dom.client.DragStartHandler;
45  import com.google.gwt.event.dom.client.DropEvent;
46  import com.google.gwt.event.dom.client.DropHandler;
47  import com.google.gwt.event.dom.client.HasAllMouseHandlers;
48  import com.google.gwt.event.dom.client.LoadHandler;
49  import com.google.gwt.event.dom.client.MouseDownEvent;
50  import com.google.gwt.event.dom.client.MouseDownHandler;
51  import com.google.gwt.event.dom.client.MouseMoveEvent;
52  import com.google.gwt.event.dom.client.MouseMoveHandler;
53  import com.google.gwt.event.dom.client.MouseOutEvent;
54  import com.google.gwt.event.dom.client.MouseOutHandler;
55  import com.google.gwt.event.dom.client.MouseOverEvent;
56  import com.google.gwt.event.dom.client.MouseOverHandler;
57  import com.google.gwt.event.dom.client.MouseUpEvent;
58  import com.google.gwt.event.dom.client.MouseUpHandler;
59  import com.google.gwt.event.dom.client.MouseWheelEvent;
60  import com.google.gwt.event.dom.client.MouseWheelHandler;
61  import com.google.gwt.event.dom.client.ScrollHandler;
62  import com.google.gwt.event.dom.client.TouchCancelEvent;
63  import com.google.gwt.event.dom.client.TouchCancelHandler;
64  import com.google.gwt.event.dom.client.TouchEndEvent;
65  import com.google.gwt.event.dom.client.TouchEndHandler;
66  import com.google.gwt.event.dom.client.TouchMoveEvent;
67  import com.google.gwt.event.dom.client.TouchMoveHandler;
68  import com.google.gwt.event.dom.client.TouchStartEvent;
69  import com.google.gwt.event.dom.client.TouchStartHandler;
70  import com.google.gwt.event.logical.shared.ResizeHandler;
71  import com.google.gwt.event.shared.HandlerRegistration;
72  import com.google.gwt.uibinder.client.ElementParserToUse;
73  
74  /**
75   * A widget which contains a single {@link org.vectomatic.dom.svg.OMSVGSVGElement OMSVGSVGElement}.
76   * The class bridges event subscription methods and forwards
77   * them to the underlying {@link org.vectomatic.dom.svg.OMSVGSVGElement OMSVGSVGElement}.
78   * The class integrates with GWT Widget hierarchy.
79   * <p>You can define an SVGImage using UiBinder templates. 
80   * Depending on your needs, you can either define the SVG inline.
81   * This can be convenient if you want to 
82   * localize the button label, or use styles defined in the template. 
83   * Or you can use an {@link org.vectomatic.dom.svg.ui.SVGResource SVGResource} with the <em>resource</em> attribute,
84   * if your SVG is large or if you want to keep your template more readable.</p>
85   * <p>The following section shows a sample UiBinder template. Notice
86   * that you can bind tags inside the template to java variables. One
87   * of the <em>&lt;g&gt;</em> tag is bound to a java variable
88   * <em>eyes</em> of type {@link org.vectomatic.dom.svg.OMSVGGElement OMSVGGElement}
89   * and one of the <em>&lt;path&gt;</em> tag is bound to a java variable
90   * <em>mouth</em> of type {@link org.vectomatic.dom.svg.OMSVGPathElement OMSVGPathElement}.</p>
91   * <pre>
92   * &lt;svgui:SVGImage&gt;
93   *  &lt;svg width="100" height="100" viewBox="80 190 140 130" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"&gt;
94   *   &lt;path d="m 208,146.86218 a 63,63.5 0 1 1 -126,0 63,63.5 0 1 1 126,0 z" style="fill:#ffff00;stroke:#000000;" transform="translate(3.9895924,108.83705)" /&gt;
95   *   &lt;g ui:field="eyes" transform="translate(10.960155,103.59114)"&gt;
96   *    &lt;path d="m 124,133.36218 c 0,4.97057 -4.02944,9 -9,9 -4.97056,0 -9,-4.02943 -9,-9 0,-4.97056 4.02944,-9 9,-9 4.97056,0 9,4.02944 9,9 z" class="{style.eye}" /&gt;
97   *    &lt;path d="m 124,133.36218 c 0,4.97057 -4.02944,9 -9,9 -4.97056,0 -9,-4.02943 -9,-9 0,-4.97056 4.02944,-9 9,-9 4.97056,0 9,4.02944 9,9 z" class="{style.eye}" transform="translate(44,0)" /&gt;
98   *   &lt;/g&gt;
99   *   &lt;path ui:field="mouth" d="m 118.88908,286.15612 5,10 10,5 30,0 10,-5 5,-10 -5,5 -10,5 -30,0 -10,-5 -5,-5 z" class="{style.mouth}" /&gt;
100  *  &lt;/svg&gt;
101  * &lt;/svgui:SVGImage&gt;
102  * </pre>
103  * Note that by default SVGImages are validated against the SVG 1.1 XSD schema.
104  * You can opt out of validation by setting the <code>validated="false"</code>
105  * attribute on the SVGImages.
106  * @author laaglu
107  */
108 @ElementParserToUse(className = "org.vectomatic.dev.svg.impl.gen.SVGImageParser")
109 public class SVGImage extends SVGWidget implements HasGraphicalHandlers, HasAllMouseHandlers, HasDocumentHandlers, ISVGStylable {
110 	protected OMSVGSVGElement svgElement;
111 	/**
112 	 * No-arg constructor.
113 	 * You must call {@link SVGImage#setResource(SVGResource)} or {@link SVGImage#setSvgElement(OMSVGSVGElement)}
114 	 * before using the widget.
115 	 */
116 	public SVGImage() {
117 	}
118 	public SVGImage(SVGResource resource) {
119 		setResource(resource);
120 	}
121 	public SVGImage(OMSVGSVGElement svgElement) {
122 		setSvgElement(svgElement);
123 	}
124 	
125 	public OMSVGSVGElement getSvgElement() {
126 		return svgElement;
127 	}
128 	
129 	public void setResource(SVGResource resource) {
130 		setSvgElement(resource.getSvg());
131 	}
132 	
133 	public void setSvgElement(OMSVGSVGElement svgElement) {
134 		this.svgElement = svgElement;
135 		setElement(this.svgElement.getElement());
136 	}
137  
138 	@Override
139 	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
140 		// NB: standard mouse events will be caught by the container <div> element
141 		// not the the embedded <svg> element
142 		return addDomHandler(handler, MouseDownEvent.getType());
143 	}
144 	@Override
145 	public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
146 		return addDomHandler(handler, MouseUpEvent.getType());
147 	}
148 	@Override
149 	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
150 		return addDomHandler(handler, MouseOutEvent.getType());
151 	}
152 	@Override
153 	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
154 		return addDomHandler(handler, MouseOverEvent.getType());
155 	}
156 	@Override
157 	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
158 		return addDomHandler(handler, MouseMoveEvent.getType());
159 	}
160 	@Override
161 	public final HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
162 		return addDomHandler(handler, TouchCancelEvent.getType());
163 	}
164 	@Override
165 	public final HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
166 		return addDomHandler(handler, TouchEndEvent.getType());
167 	}
168 	@Override
169 	public final HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
170 		return addDomHandler(handler, TouchMoveEvent.getType());
171 	}
172 	@Override
173 	public final HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
174 		return addDomHandler(handler, TouchStartEvent.getType());
175 	}
176 	@Override
177 	public final HandlerRegistration addDragEndHandler(DragEndHandler handler) {
178 		return addDomHandler(handler, DragEndEvent.getType());
179 	}
180 	@Override
181 	public final HandlerRegistration addDragEnterHandler(DragEnterHandler handler) {
182 		return addDomHandler(handler, DragEnterEvent.getType());
183 	}
184 	@Override
185 	public final HandlerRegistration addDragHandler(DragHandler handler) {
186 		return addDomHandler(handler, DragEvent.getType());
187 	}
188 	@Override
189 	public final HandlerRegistration addDragLeaveHandler(DragLeaveHandler handler) {
190 		return addDomHandler(handler, DragLeaveEvent.getType());
191 	}
192 	@Override
193 	public final HandlerRegistration addDragOverHandler(DragOverHandler handler) {
194 		return addDomHandler(handler, DragOverEvent.getType());
195 	}
196 	@Override
197 	public final HandlerRegistration addDragStartHandler(DragStartHandler handler) {
198 		return addDomHandler(handler, DragStartEvent.getType());
199 	}
200 	@Override
201 	public final HandlerRegistration addDropHandler(DropHandler handler) {
202 		return addDomHandler(handler, DropEvent.getType());
203 	}
204 	@Override
205 	public HandlerRegistration addClickHandler(ClickHandler handler) {
206 		return addDomHandler(handler, ClickEvent.getType());
207 	}
208 	@Override
209 	public HandlerRegistration addLoadHandler(LoadHandler handler) {
210 		return svgElement.addLoadHandler(handler);
211 	}
212 	@Override
213 	public HandlerRegistration addResizeHandler(ResizeHandler handler) {
214 		return svgElement.addResizeHandler(handler);
215 	}
216 	@Override
217 	public HandlerRegistration addScrollHandler(ScrollHandler handler) {
218 		return svgElement.addScrollHandler(handler);
219 	}
220 	@Override
221 	public HandlerRegistration addSVGZoomHandler(SVGZoomHandler handler) {
222 		return svgElement.addSVGZoomHandler(handler);
223 	}
224 	@Override
225 	public HandlerRegistration addFocusInHandler(FocusInHandler handler) {
226 		return svgElement.addFocusInHandler(handler);
227 	}
228 	@Override
229 	public HandlerRegistration addFocusOutHandler(FocusOutHandler handler) {
230 		return svgElement.addFocusOutHandler(handler);
231 	}
232 	@Override
233 	public HandlerRegistration addActivateHandler(ActivateHandler handler) {
234 		return svgElement.addActivateHandler(handler);
235 	}
236 	@Override
237 	public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
238 		return addDomHandler(handler, MouseWheelEvent.getType());
239 	}
240 	
241 	// Implementation of the svg::Stylable W3C IDL interface
242 	public OMSVGStyle getStyle() {
243 		return svgElement.getStyle();
244 	}
245 
246 	public final OMSVGAnimatedString getClassName() {
247 		return svgElement.getClassName();
248 	}
249 
250 	public final void addClassNameBaseVal(String className) {
251 		svgElement.addClassNameBaseVal(className);
252 	}
253 
254 	public final void removeClassNameBaseVal(String className) {
255 		svgElement.removeClassNameBaseVal(className);
256 	}
257 
258 	public final void replaceClassNameBaseVal(String oldClassName, String newClassName) {
259 		svgElement.replaceClassNameBaseVal(oldClassName, newClassName);
260 	}
261 
262 	public final void setClassNameBaseVal(String className) {
263 		svgElement.setClassNameBaseVal(className);
264 	}
265 }