1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.dom.svg.ui;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.vectomatic.dom.svg.OMSVGAnimatedString;
24 import org.vectomatic.dom.svg.OMSVGSVGElement;
25 import org.vectomatic.dom.svg.OMSVGStyle;
26 import org.vectomatic.dom.svg.itf.ISVGStylable;
27
28 import com.google.gwt.event.dom.client.ClickEvent;
29 import com.google.gwt.event.dom.client.ClickHandler;
30 import com.google.gwt.event.dom.client.HasAllMouseHandlers;
31 import com.google.gwt.event.dom.client.HasClickHandlers;
32 import com.google.gwt.event.dom.client.MouseDownEvent;
33 import com.google.gwt.event.dom.client.MouseDownHandler;
34 import com.google.gwt.event.dom.client.MouseMoveEvent;
35 import com.google.gwt.event.dom.client.MouseMoveHandler;
36 import com.google.gwt.event.dom.client.MouseOutEvent;
37 import com.google.gwt.event.dom.client.MouseOutHandler;
38 import com.google.gwt.event.dom.client.MouseOverEvent;
39 import com.google.gwt.event.dom.client.MouseOverHandler;
40 import com.google.gwt.event.dom.client.MouseUpEvent;
41 import com.google.gwt.event.dom.client.MouseUpHandler;
42 import com.google.gwt.event.dom.client.MouseWheelEvent;
43 import com.google.gwt.event.dom.client.MouseWheelHandler;
44 import com.google.gwt.event.shared.HandlerRegistration;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public abstract class SVGButtonBase extends SVGWidget implements HasClickHandlers, HasAllMouseHandlers, MouseDownHandler, MouseUpHandler, MouseOverHandler, MouseOutHandler, ClickHandler, ISVGStylable {
66
67
68
69
70 public enum SVGFaceName {
71
72
73
74 UP,
75
76
77
78 UP_DISABLED,
79
80
81
82 UP_HOVERING,
83
84
85
86 DOWN,
87
88
89
90 DOWN_DISABLED,
91
92
93
94 DOWN_HOVERING
95 }
96
97
98
99
100
101 public static class SVGFace {
102 private SVGFaceChange[] changes;
103
104
105
106
107
108 public SVGFace(SVGFaceChange[] changes) {
109 this.changes = changes;
110 }
111
112
113
114
115
116
117 public SVGFaceChange[] getChanges() {
118 return changes;
119 }
120 }
121
122
123
124
125
126
127 public static abstract class SVGFaceChange {
128 void install(SVGButtonBase button) {
129 }
130 void uninstall(SVGButtonBase button) {
131 }
132 }
133
134
135
136
137
138
139 public static class SVGStyleChange extends SVGFaceChange {
140 private String[] classNames;
141 public SVGStyleChange(String[] classNames) {
142 this.classNames = classNames;
143 }
144 @Override
145 void uninstall(SVGButtonBase button) {
146 if (button.svgElement != null) {
147 for (String className : classNames) {
148 button.svgElement.removeClassNameBaseVal(className);
149 }
150 }
151 }
152 @Override
153 void install(SVGButtonBase button) {
154 if (button.svgElement != null) {
155 for (String className : classNames) {
156 button.svgElement.addClassNameBaseVal(className);
157 }
158 }
159 }
160 public String[] getClassNames() {
161 return classNames;
162 }
163 public void getClassNames(String[] classNames) {
164 this.classNames = classNames;
165 }
166 }
167
168
169
170
171
172 protected OMSVGSVGElement svgElement;
173
174
175
176 protected SVGFaceName currentFaceName;
177
178
179
180 protected Map<SVGFaceName, SVGFace> faces;
181
182
183
184
185
186
187 protected SVGButtonBase() {
188 faces = new HashMap<SVGFaceName, SVGFace>();
189 }
190
191
192
193
194
195
196
197 protected SVGButtonBase(OMSVGSVGElement svgElement, Map<SVGFaceName, SVGFace> faces) {
198 this();
199 if (faces != null) {
200 this.faces.putAll(faces);
201 }
202 setSvgElement(svgElement);
203 }
204
205
206
207
208
209
210
211 protected SVGButtonBase(SVGResource resource, Map<SVGFaceName, SVGFace> faces) {
212 this(resource.getSvg(), faces);
213 }
214
215
216
217
218
219
220
221
222
223
224
225 public SVGFace addFace(SVGFaceName faceName, SVGFace face) {
226 return faces.put(faceName, face);
227 }
228
229
230
231
232
233
234
235
236 public SVGFace getFace(SVGFaceName faceName) {
237 return faces.get(faceName);
238 }
239
240
241
242
243
244 public void setResource(SVGResource resource) {
245 setSvgElement(resource.getSvg());
246 }
247
248
249
250
251
252 public OMSVGSVGElement getSvgElement() {
253 return svgElement;
254 }
255
256
257
258
259
260 public void setSvgElement(OMSVGSVGElement svgElement) {
261 this.svgElement = svgElement;
262 setElement(this.svgElement.getElement());
263 if (this.svgElement != null) {
264 this.svgElement.addDomHandler(this, MouseOutEvent.getType());
265 this.svgElement.addDomHandler(this, MouseOverEvent.getType());
266 this.svgElement.addDomHandler(this, MouseUpEvent.getType());
267 this.svgElement.addDomHandler(this, MouseDownEvent.getType());
268 this.svgElement.addDomHandler(this, ClickEvent.getType());
269 }
270 }
271
272
273
274
275
276 public SVGFaceName getCurrentFaceName() {
277 return currentFaceName;
278 }
279
280
281
282
283
284 public boolean isEnabled() {
285 return currentFaceName != SVGFaceName.UP_DISABLED && currentFaceName != SVGFaceName.DOWN_DISABLED;
286 }
287
288
289
290
291
292 public void setEnabled(boolean enabled) {
293 if (enabled) {
294 switch(currentFaceName) {
295 case UP_DISABLED:
296 case UP_HOVERING:
297 showFace(SVGFaceName.UP);
298 break;
299 case DOWN_DISABLED:
300 case DOWN_HOVERING:
301 showFace(SVGFaceName.DOWN);
302 break;
303 }
304 } else {
305 switch(currentFaceName) {
306 case UP:
307 case UP_HOVERING:
308 showFace(SVGFaceName.UP_DISABLED);
309 break;
310 case DOWN:
311 case DOWN_HOVERING:
312 showFace(SVGFaceName.DOWN_DISABLED);
313 break;
314 }
315 }
316 }
317
318
319
320
321
322
323 public void showFace(SVGFaceName faceName) {
324
325 SVGFace currentFace = currentFaceName != null ? getFace(currentFaceName) : null;
326 SVGFace face = faceName != null ? getFace(faceName) : null;
327 if (face != currentFace) {
328 if (currentFace != null) {
329 for (SVGFaceChange change : currentFace.getChanges()) {
330 change.uninstall(this);
331 }
332 }
333 if (face != null) {
334 for (SVGFaceChange change : face.getChanges()) {
335 change.install(this);
336 }
337 }
338 }
339 currentFaceName = faceName;
340 }
341
342 @Override
343 public HandlerRegistration addClickHandler(ClickHandler handler) {
344 return svgElement.addDomHandler(handler, ClickEvent.getType());
345 }
346 @Override
347 public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
348 return svgElement.addDomHandler(handler, MouseDownEvent.getType());
349 }
350 @Override
351 public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
352 return svgElement.addDomHandler(handler, MouseUpEvent.getType());
353 }
354 @Override
355 public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
356 return svgElement.addDomHandler(handler, MouseOutEvent.getType());
357 }
358 @Override
359 public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
360 return svgElement.addDomHandler(handler, MouseOverEvent.getType());
361 }
362 @Override
363 public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
364 return svgElement.addDomHandler(handler, MouseMoveEvent.getType());
365 }
366 @Override
367 public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
368 return svgElement.addDomHandler(handler, MouseWheelEvent.getType());
369 }
370
371
372
373 public OMSVGStyle getStyle() {
374 return svgElement.getStyle();
375 }
376
377 public final OMSVGAnimatedString getClassName() {
378 return svgElement.getClassName();
379 }
380
381 public final void addClassNameBaseVal(String className) {
382 svgElement.addClassNameBaseVal(className);
383 }
384
385 public final void removeClassNameBaseVal(String className) {
386 svgElement.removeClassNameBaseVal(className);
387 }
388
389 public final void replaceClassNameBaseVal(String oldClassName, String newClassName) {
390 svgElement.replaceClassNameBaseVal(oldClassName, newClassName);
391 }
392
393 public final void setClassNameBaseVal(String className) {
394 svgElement.setClassNameBaseVal(className);
395 }
396 @Override
397 public void onClick(ClickEvent event) {
398
399
400 event.stopPropagation();
401 event.preventDefault();
402 }
403 }