001/**********************************************
002 * Copyright (C) 2010 Lukas Laag
003 * This file is part of lib-gwt-svg.
004 * 
005 * libgwtsvg is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 * 
010 * libgwtsvg is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with libgwtsvg.  If not, see http://www.gnu.org/licenses/
017 **********************************************/
018package org.vectomatic.dom.svg.ui;
019
020import org.vectomatic.dom.svg.OMSVGAnimatedString;
021import org.vectomatic.dom.svg.OMSVGSVGElement;
022import org.vectomatic.dom.svg.OMSVGStyle;
023import org.vectomatic.dom.svg.events.ActivateHandler;
024import org.vectomatic.dom.svg.events.FocusInHandler;
025import org.vectomatic.dom.svg.events.FocusOutHandler;
026import org.vectomatic.dom.svg.events.HasDocumentHandlers;
027import org.vectomatic.dom.svg.events.HasGraphicalHandlers;
028import org.vectomatic.dom.svg.events.SVGZoomHandler;
029import org.vectomatic.dom.svg.itf.ISVGStylable;
030
031import com.google.gwt.event.dom.client.ClickEvent;
032import com.google.gwt.event.dom.client.ClickHandler;
033import com.google.gwt.event.dom.client.DragEndEvent;
034import com.google.gwt.event.dom.client.DragEndHandler;
035import com.google.gwt.event.dom.client.DragEnterEvent;
036import com.google.gwt.event.dom.client.DragEnterHandler;
037import com.google.gwt.event.dom.client.DragEvent;
038import com.google.gwt.event.dom.client.DragHandler;
039import com.google.gwt.event.dom.client.DragLeaveEvent;
040import com.google.gwt.event.dom.client.DragLeaveHandler;
041import com.google.gwt.event.dom.client.DragOverEvent;
042import com.google.gwt.event.dom.client.DragOverHandler;
043import com.google.gwt.event.dom.client.DragStartEvent;
044import com.google.gwt.event.dom.client.DragStartHandler;
045import com.google.gwt.event.dom.client.DropEvent;
046import com.google.gwt.event.dom.client.DropHandler;
047import com.google.gwt.event.dom.client.HasAllMouseHandlers;
048import com.google.gwt.event.dom.client.LoadHandler;
049import com.google.gwt.event.dom.client.MouseDownEvent;
050import com.google.gwt.event.dom.client.MouseDownHandler;
051import com.google.gwt.event.dom.client.MouseMoveEvent;
052import com.google.gwt.event.dom.client.MouseMoveHandler;
053import com.google.gwt.event.dom.client.MouseOutEvent;
054import com.google.gwt.event.dom.client.MouseOutHandler;
055import com.google.gwt.event.dom.client.MouseOverEvent;
056import com.google.gwt.event.dom.client.MouseOverHandler;
057import com.google.gwt.event.dom.client.MouseUpEvent;
058import com.google.gwt.event.dom.client.MouseUpHandler;
059import com.google.gwt.event.dom.client.MouseWheelEvent;
060import com.google.gwt.event.dom.client.MouseWheelHandler;
061import com.google.gwt.event.dom.client.ScrollHandler;
062import com.google.gwt.event.dom.client.TouchCancelEvent;
063import com.google.gwt.event.dom.client.TouchCancelHandler;
064import com.google.gwt.event.dom.client.TouchEndEvent;
065import com.google.gwt.event.dom.client.TouchEndHandler;
066import com.google.gwt.event.dom.client.TouchMoveEvent;
067import com.google.gwt.event.dom.client.TouchMoveHandler;
068import com.google.gwt.event.dom.client.TouchStartEvent;
069import com.google.gwt.event.dom.client.TouchStartHandler;
070import com.google.gwt.event.logical.shared.ResizeHandler;
071import com.google.gwt.event.shared.HandlerRegistration;
072import com.google.gwt.uibinder.client.ElementParserToUse;
073
074/**
075 * A widget which contains a single {@link org.vectomatic.dom.svg.OMSVGSVGElement OMSVGSVGElement}.
076 * The class bridges event subscription methods and forwards
077 * them to the underlying {@link org.vectomatic.dom.svg.OMSVGSVGElement OMSVGSVGElement}.
078 * The class integrates with GWT Widget hierarchy.
079 * <p>You can define an SVGImage using UiBinder templates. 
080 * Depending on your needs, you can either define the SVG inline.
081 * This can be convenient if you want to 
082 * localize the button label, or use styles defined in the template. 
083 * Or you can use an {@link org.vectomatic.dom.svg.ui.SVGResource SVGResource} with the <em>resource</em> attribute,
084 * if your SVG is large or if you want to keep your template more readable.</p>
085 * <p>The following section shows a sample UiBinder template. Notice
086 * that you can bind tags inside the template to java variables. One
087 * of the <em>&lt;g&gt;</em> tag is bound to a java variable
088 * <em>eyes</em> of type {@link org.vectomatic.dom.svg.OMSVGGElement OMSVGGElement}
089 * and one of the <em>&lt;path&gt;</em> tag is bound to a java variable
090 * <em>mouth</em> of type {@link org.vectomatic.dom.svg.OMSVGPathElement OMSVGPathElement}.</p>
091 * <pre>
092 * &lt;svgui:SVGImage&gt;
093 *  &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;
094 *   &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;
095 *   &lt;g ui:field="eyes" transform="translate(10.960155,103.59114)"&gt;
096 *    &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;
097 *    &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;
098 *   &lt;/g&gt;
099 *   &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")
109public 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}