1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
52
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
98
99
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 }