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.HasAllMouseHandlers;
34  import com.google.gwt.event.dom.client.LoadHandler;
35  import com.google.gwt.event.dom.client.MouseDownEvent;
36  import com.google.gwt.event.dom.client.MouseDownHandler;
37  import com.google.gwt.event.dom.client.MouseMoveEvent;
38  import com.google.gwt.event.dom.client.MouseMoveHandler;
39  import com.google.gwt.event.dom.client.MouseOutEvent;
40  import com.google.gwt.event.dom.client.MouseOutHandler;
41  import com.google.gwt.event.dom.client.MouseOverEvent;
42  import com.google.gwt.event.dom.client.MouseOverHandler;
43  import com.google.gwt.event.dom.client.MouseUpEvent;
44  import com.google.gwt.event.dom.client.MouseUpHandler;
45  import com.google.gwt.event.dom.client.MouseWheelEvent;
46  import com.google.gwt.event.dom.client.MouseWheelHandler;
47  import com.google.gwt.event.dom.client.ScrollHandler;
48  import com.google.gwt.event.dom.client.TouchCancelEvent;
49  import com.google.gwt.event.dom.client.TouchCancelHandler;
50  import com.google.gwt.event.dom.client.TouchEndEvent;
51  import com.google.gwt.event.dom.client.TouchEndHandler;
52  import com.google.gwt.event.dom.client.TouchMoveEvent;
53  import com.google.gwt.event.dom.client.TouchMoveHandler;
54  import com.google.gwt.event.dom.client.TouchStartEvent;
55  import com.google.gwt.event.dom.client.TouchStartHandler;
56  import com.google.gwt.event.logical.shared.ResizeHandler;
57  import com.google.gwt.event.shared.HandlerRegistration;
58  import com.google.gwt.uibinder.client.ElementParserToUse;
59  
60  /**
61   * A widget which contains a single {@link org.vectomatic.dom.svg.OMSVGSVGElement OMSVGSVGElement}.
62   * The class bridges event subscription methods and forwards
63   * them to the underlying {@link org.vectomatic.dom.svg.OMSVGSVGElement OMSVGSVGElement}.
64   * The class integrates with GWT Widget hierarchy.
65   * <p>You can define an SVGImage using UiBinder templates. 
66   * Depending on your needs, you can either define the SVG inline.
67   * This can be convenient if you want to 
68   * localize the button label, or use styles defined in the template. 
69   * Or you can use an {@link org.vectomatic.dom.svg.ui.SVGResource SVGResource} with the <em>resource</em> attribute,
70   * if your SVG is large or if you want to keep your template more readable.</p>
71   * <p>The following section shows a sample UiBinder template. Notice
72   * that you can bind tags inside the template to java variables. One
73   * of the <em>&lt;g&gt;</em> tag is bound to a java variable
74   * <em>eyes</em> of type {@link org.vectomatic.dom.svg.OMSVGGElement OMSVGGElement}
75   * and one of the <em>&lt;path&gt;</em> tag is bound to a java variable
76   * <em>mouth</em> of type {@link org.vectomatic.dom.svg.OMSVGPathElement OMSVGPathElement}.</p>
77   * <pre>
78   * &lt;svgui:SVGImage&gt;
79   *  &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;
80   *   &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;
81   *   &lt;g ui:field="eyes" transform="translate(10.960155,103.59114)"&gt;
82   *    &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;
83   *    &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;
84   *   &lt;/g&gt;
85   *   &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;
86   *  &lt;/svg&gt;
87   * &lt;/svgui:SVGImage&gt;
88   * </pre>
89   * Note that by default SVGImages are validated against the SVG 1.1 XSD schema.
90   * You can opt out of validation by setting the <code>validated="false"</code>
91   * attribute on the SVGImages.
92   * @author laaglu
93   */
94  @ElementParserToUse(className = "org.vectomatic.dev.svg.impl.gen.SVGImageParser")
95  public class SVGImage extends SVGWidget implements HasGraphicalHandlers, HasAllMouseHandlers, HasDocumentHandlers, ISVGStylable {
96  	protected OMSVGSVGElement svgElement;
97  	/**
98  	 * No-arg constructor.
99  	 * You must call {@link SVGImage#setResource(SVGResource)} or {@link SVGImage#setSvgElement(OMSVGSVGElement)}
100 	 * before using the widget.
101 	 */
102 	public SVGImage() {
103 	}
104 	public SVGImage(SVGResource resource) {
105 		setResource(resource);
106 	}
107 	public SVGImage(OMSVGSVGElement svgElement) {
108 		setSvgElement(svgElement);
109 	}
110 	
111 	public OMSVGSVGElement getSvgElement() {
112 		return svgElement;
113 	}
114 	
115 	public void setResource(SVGResource resource) {
116 		setSvgElement(resource.getSvg());
117 	}
118 	
119 	public void setSvgElement(OMSVGSVGElement svgElement) {
120 		this.svgElement = svgElement;
121 		setElement(this.svgElement.getElement());
122 	}
123  
124 	@Override
125 	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
126 		// NB: standard mouse events will be caught by the container <div> element
127 		// not the the embedded <svg> element
128 		return addDomHandler(handler, MouseDownEvent.getType());
129 	}
130 	@Override
131 	public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
132 		return addDomHandler(handler, MouseUpEvent.getType());
133 	}
134 	@Override
135 	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
136 		return addDomHandler(handler, MouseOutEvent.getType());
137 	}
138 	@Override
139 	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
140 		return addDomHandler(handler, MouseOverEvent.getType());
141 	}
142 	@Override
143 	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
144 		return addDomHandler(handler, MouseMoveEvent.getType());
145 	}
146 	@Override
147 	public final HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
148 		return addDomHandler(handler, TouchCancelEvent.getType());
149 	}
150 	@Override
151 	public final HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
152 		return addDomHandler(handler, TouchEndEvent.getType());
153 	}
154 	@Override
155 	public final HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
156 		return addDomHandler(handler, TouchMoveEvent.getType());
157 	}
158 	@Override
159 	public final HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
160 		return addDomHandler(handler, TouchStartEvent.getType());
161 	}
162 	
163 	@Override
164 	public HandlerRegistration addClickHandler(ClickHandler handler) {
165 		return addDomHandler(handler, ClickEvent.getType());
166 	}
167 	@Override
168 	public HandlerRegistration addLoadHandler(LoadHandler handler) {
169 		return svgElement.addLoadHandler(handler);
170 	}
171 	@Override
172 	public HandlerRegistration addResizeHandler(ResizeHandler handler) {
173 		return svgElement.addResizeHandler(handler);
174 	}
175 	@Override
176 	public HandlerRegistration addScrollHandler(ScrollHandler handler) {
177 		return svgElement.addScrollHandler(handler);
178 	}
179 	@Override
180 	public HandlerRegistration addSVGZoomHandler(SVGZoomHandler handler) {
181 		return svgElement.addSVGZoomHandler(handler);
182 	}
183 	@Override
184 	public HandlerRegistration addFocusInHandler(FocusInHandler handler) {
185 		return svgElement.addFocusInHandler(handler);
186 	}
187 	@Override
188 	public HandlerRegistration addFocusOutHandler(FocusOutHandler handler) {
189 		return svgElement.addFocusOutHandler(handler);
190 	}
191 	@Override
192 	public HandlerRegistration addActivateHandler(ActivateHandler handler) {
193 		return svgElement.addActivateHandler(handler);
194 	}
195 	@Override
196 	public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
197 		return addDomHandler(handler, MouseWheelEvent.getType());
198 	}
199 	
200 	// Implementation of the svg::Stylable W3C IDL interface
201 	public OMSVGStyle getStyle() {
202 		return svgElement.getStyle();
203 	}
204 
205 	public final OMSVGAnimatedString getClassName() {
206 		return svgElement.getClassName();
207 	}
208 
209 	public final void addClassNameBaseVal(String className) {
210 		svgElement.addClassNameBaseVal(className);
211 	}
212 
213 	public final void removeClassNameBaseVal(String className) {
214 		svgElement.removeClassNameBaseVal(className);
215 	}
216 
217 	public final void replaceClassNameBaseVal(String oldClassName, String newClassName) {
218 		svgElement.replaceClassNameBaseVal(oldClassName, newClassName);
219 	}
220 
221 	public final void setClassNameBaseVal(String className) {
222 		svgElement.setClassNameBaseVal(className);
223 	}
224 }