View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of lib-gwt-svg-samples.
4    * 
5    * libgwtsvg-samples is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU 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-samples 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 General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with libgwtsvg-samples.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.samples.client.smil;
19  
20  import java.util.Iterator;
21  
22  import org.vectomatic.dom.svg.OMSVGAnimateElement;
23  import org.vectomatic.dom.svg.OMSVGCircleElement;
24  import org.vectomatic.dom.svg.OMSVGSVGElement;
25  import org.vectomatic.dom.svg.OMSVGTSpanElement;
26  import org.vectomatic.dom.svg.OMSVGTextElement;
27  import org.vectomatic.dom.svg.OMText;
28  import org.vectomatic.dom.svg.events.BeginEvent;
29  import org.vectomatic.dom.svg.events.BeginHandler;
30  import org.vectomatic.dom.svg.events.EndEvent;
31  import org.vectomatic.dom.svg.events.EndHandler;
32  import org.vectomatic.dom.svg.events.RepeatEvent;
33  import org.vectomatic.dom.svg.events.RepeatHandler;
34  import org.vectomatic.dom.svg.utils.DOMHelper;
35  import org.vectomatic.dom.svg.utils.SVGConstants;
36  import org.vectomatic.dom.svg.utils.SVGPrefixResolver;
37  import org.vectomatic.svg.samples.client.Main;
38  import org.vectomatic.svg.samples.client.Main.MainBundle;
39  import org.vectomatic.svg.samples.client.SampleBase;
40  
41  import com.google.gwt.core.client.GWT;
42  import com.google.gwt.event.dom.client.ClickEvent;
43  import com.google.gwt.uibinder.client.UiBinder;
44  import com.google.gwt.uibinder.client.UiField;
45  import com.google.gwt.uibinder.client.UiHandler;
46  import com.google.gwt.user.client.Timer;
47  import com.google.gwt.user.client.ui.Button;
48  import com.google.gwt.user.client.ui.TabLayoutPanel;
49  
50  /**
51   * Class to demonstrate the use of SVG/SMIL animations
52   * @author laaglu
53   */
54  public class SmilSample extends SampleBase implements RepeatHandler, BeginHandler, EndHandler {
55  	interface SmilSampleBinder extends UiBinder<TabLayoutPanel, SmilSample> {
56  	}
57  	private static SmilSampleBinder binder = GWT.create(SmilSampleBinder.class);
58  
59  	
60  	@UiField(provided=true)
61  	public static MainBundle mainBundle = Main.mainBundle;
62  	@UiField
63  	OMSVGSVGElement svg;
64  	@UiField
65  	OMSVGCircleElement circle;
66  	@UiField
67  	OMSVGTSpanElement countSpan;
68  	@UiField
69  	OMSVGTextElement beginText;
70  	@UiField
71  	OMSVGTextElement endText;
72  	@UiField
73  	Button endButton;
74  	@UiField
75  	Button beginButton;
76  	
77  	Timer beginTimer = new Timer() {
78  		@Override
79  		public void run() {
80  			setTextColor(beginText, SVGConstants.CSS_WHITE_VALUE);
81  		}
82  	};
83  	Timer endTimer = new Timer() {
84  		@Override
85  		public void run() {
86  			setTextColor(endText, SVGConstants.CSS_WHITE_VALUE);
87  		}
88  	};
89  
90  	@Override
91  	public TabLayoutPanel getPanel() {
92  		if (tabPanel == null) {
93  			tabPanel = binder.createAndBindUi(this);
94  			tabPanel.setTabText(0, "Animation");
95  			createCodeTabs("SmilSample");
96  			
97  			// Add the repeat handler manually instead of using the @UiHandler annotation
98  			// Indeed, since many browsers do not yet support the SVG anim tag, the
99  			// anim variable may well be null
100 			OMSVGAnimateElement anim = getAnimation(); 
101 			if (anim != null) {
102 				anim.addRepeatHandler(this);
103 				anim.addBeginHandler(this);
104 				anim.addEndHandler(this);
105 			}
106 		}
107 		return tabPanel;
108 	}
109 	
110 	private OMSVGAnimateElement getAnimation() {
111 		Iterator<OMSVGAnimateElement> iterator = DOMHelper.evaluateXPath(circle, "svg:animate", SVGPrefixResolver.INSTANCE);
112 		if (iterator.hasNext()) {
113 			return iterator.next(); 
114 		}
115 		return null;
116 	}
117 	
118 	@Override
119 	public void onRepeat(RepeatEvent e) {
120 		OMText loopCount = (OMText) countSpan.getFirstChild();
121 		int count = Integer.parseInt(loopCount.getData());
122 		loopCount.setData(Integer.toString(count + 1));
123 	}
124 
125 	@Override
126 	public void onEnd(EndEvent event) {
127 		setTextColor(endText, SVGConstants.CSS_YELLOW_VALUE);
128 		endTimer.schedule(200);
129 	}
130 
131 	@Override
132 	public void onBegin(BeginEvent event) {
133 		setTextColor(beginText, SVGConstants.CSS_YELLOW_VALUE);
134 		beginTimer.schedule(200);
135 	}
136 	
137 	private void setTextColor(OMSVGTextElement text, String color) {
138 		text.getStyle().setSVGProperty(SVGConstants.CSS_FILL_PROPERTY, color);
139 		text.getStyle().setSVGProperty(SVGConstants.CSS_STROKE_PROPERTY, color);
140 	}
141 	
142 	@UiHandler("endButton")
143 	public void end(ClickEvent event) {
144 		OMSVGAnimateElement anim = getAnimation(); 
145 		if (anim != null) {
146 			anim.endElement();
147 		}
148 	}
149 	@UiHandler("beginButton")
150 	public void begin(ClickEvent event) {
151 		OMSVGAnimateElement anim = getAnimation(); 
152 		if (anim != null) {
153 			anim.beginElement();
154 		}
155 	}
156 
157 }