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.Map;
21
22 import org.vectomatic.dom.svg.OMSVGSVGElement;
23 import org.vectomatic.dom.svg.ui.SVGButtonBase.SVGFaceName;
24 import org.vectomatic.dom.svg.utils.DOMHelper;
25
26 import com.google.gwt.dom.client.Element;
27 import com.google.gwt.dom.client.EventTarget;
28 import com.google.gwt.dom.client.NativeEvent;
29 import com.google.gwt.event.dom.client.LoseCaptureEvent;
30 import com.google.gwt.event.dom.client.LoseCaptureHandler;
31 import com.google.gwt.event.dom.client.MouseDownEvent;
32 import com.google.gwt.event.dom.client.MouseEvent;
33 import com.google.gwt.event.dom.client.MouseOutEvent;
34 import com.google.gwt.event.dom.client.MouseOverEvent;
35 import com.google.gwt.event.dom.client.MouseUpEvent;
36 import com.google.gwt.uibinder.client.ElementParserToUse;
37 import com.google.gwt.user.client.Timer;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @ElementParserToUse(className = "org.vectomatic.dev.svg.impl.gen.SVGButtonBaseParser")
78 public class SVGPushButton extends SVGButtonBase {
79 private int repeatDelayMillis;
80 private CustomerTimer timer;
81 private class CustomerTimer extends Timer {
82 private EventClone mouseDownEvent;
83 public CustomerTimer(EventClone mouseDownEvent) {
84 this.mouseDownEvent = mouseDownEvent;
85 }
86 @Override
87 public void run() {
88 mouseDownEvent.fireEvent(svgElement.getElement());
89 }
90 }
91 private static class EventClone {
92 private String type;
93 private int canBubbleArg;
94 private int cancelableArg;
95 private int screenX;
96 private int screenY;
97 private int clientX;
98 private int clientY;
99 private boolean ctrlKey;
100 private boolean altKey;
101 private boolean shiftKey;
102 private boolean metaKey;
103 private int button;
104 private EventTarget relatedEventTarget;
105 public EventClone(MouseEvent<?> mouseEvent) {
106 NativeEvent evt = mouseEvent.getNativeEvent();
107 type = evt.getType();
108 screenX = evt.getScreenX();
109 screenY = evt.getScreenY();
110 clientX = evt.getClientX();
111 clientY = evt.getClientY();
112 ctrlKey = evt.getCtrlKey();
113 altKey = evt.getAltKey();
114 shiftKey = evt.getShiftKey();
115 metaKey = evt.getMetaKey();
116 button = evt.getButton();
117 relatedEventTarget = evt.getRelatedEventTarget();
118 }
119
120 public final native void fireEvent(Element element)
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 ;
140 }
141 public SVGPushButton() {
142 }
143 public SVGPushButton(OMSVGSVGElement svgElement, Map<SVGFaceName, SVGFace> faces) {
144 super(svgElement, faces);
145 showFace(SVGFaceName.UP);
146 }
147 public SVGPushButton(SVGResource resource, Map<SVGFaceName, SVGFace> faces) {
148 this(resource.getSvg(), faces);
149 }
150
151 @Override
152 public void setEnabled(boolean enabled) {
153 if (enabled != isEnabled()) {
154 if (enabled) {
155 showFace(SVGFaceName.UP);
156 } else {
157 cancelTimer();
158 showFace(SVGFaceName.UP_DISABLED);
159 }
160 }
161 }
162 public void onMouseDown(MouseDownEvent event) {
163
164 if (isEnabled()) {
165 DOMHelper.setCaptureElement(svgElement, new LoseCaptureHandler() {
166 @Override
167 public void onLoseCapture(LoseCaptureEvent event) {
168 cancelTimer();
169 }
170 });
171 if (repeatDelayMillis > 0 && timer == null) {
172 timer = new CustomerTimer(new EventClone(event));
173 timer.scheduleRepeating(repeatDelayMillis);
174 }
175 showFace(SVGFaceName.DOWN_HOVERING);
176 }
177 event.stopPropagation();
178 event.preventDefault();
179 }
180 public void onMouseUp(MouseUpEvent event) {
181
182 cancelTimer();
183 DOMHelper.releaseCaptureElement();
184 if (isEnabled()) {
185 switch(currentFaceName) {
186 case DOWN:
187 showFace(SVGFaceName.UP);
188 break;
189 case DOWN_HOVERING:
190 showFace(SVGFaceName.UP_HOVERING);
191 break;
192 }
193 }
194 event.stopPropagation();
195 event.preventDefault();
196 }
197 public void onMouseOver(MouseOverEvent event) {
198
199 if (isEnabled()) {
200 switch(currentFaceName) {
201 case UP:
202 showFace(SVGFaceName.UP_HOVERING);
203 break;
204 case DOWN:
205 showFace(SVGFaceName.DOWN_HOVERING);
206 break;
207 }
208 }
209 event.stopPropagation();
210 event.preventDefault();
211 }
212 public void onMouseOut(MouseOutEvent event) {
213
214 if (isEnabled()) {
215 switch(currentFaceName) {
216 case UP_HOVERING:
217 showFace(SVGFaceName.UP);
218 break;
219 case DOWN_HOVERING:
220 showFace(SVGFaceName.DOWN);
221 break;
222 }
223 }
224 event.stopPropagation();
225 event.preventDefault();
226 }
227
228 @Override
229 public SVGFace getFace(SVGFaceName faceName) {
230 if (!faces.containsKey(faceName)) {
231 switch (faceName) {
232 case UP_HOVERING:
233 case UP_DISABLED:
234 case DOWN:
235 faceName = SVGFaceName.UP;
236 break;
237 case DOWN_HOVERING:
238 case DOWN_DISABLED:
239 faceName = SVGFaceName.DOWN;
240 break;
241 }
242 }
243 return super.getFace(faceName);
244 }
245 public void setRepeatDelay(int repeatDelayMillis) {
246 this.repeatDelayMillis = repeatDelayMillis;
247 }
248 public int getRepeatDelay() {
249 return repeatDelayMillis;
250 }
251 private void cancelTimer() {
252 if (timer != null) {
253 timer.cancel();
254 timer = null;
255 }
256 }
257 }