1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client.ui;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Button;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.BrowserInfo;
import com.vaadin.terminal.gwt.client.EventHelper;
import com.vaadin.terminal.gwt.client.EventId;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.Util;
import com.vaadin.terminal.gwt.client.VTooltip;
public class VNativeButton extends Button implements Paintable, ClickHandler,
FocusHandler, BlurHandler {
public static final String CLASSNAME = "v-nativebutton";
protected String width = null;
protected String id;
protected ApplicationConnection client;
protected Element errorIndicatorElement;
protected final Element captionElement = DOM.createSpan();
protected Icon icon;
/**
* Helper flag to handle special-case where the button is moved from under
* mouse while clicking it. In this case mouse leaves the button without
* moving.
*/
private boolean clickPending;
private HandlerRegistration focusHandlerRegistration;
private HandlerRegistration blurHandlerRegistration;
public VNativeButton() {
setStyleName(CLASSNAME);
getElement().appendChild(captionElement);
captionElement.setClassName(getStyleName() + "-caption");
addClickHandler(this);
sinkEvents(VTooltip.TOOLTIP_EVENTS);
sinkEvents(Event.ONMOUSEDOWN);
sinkEvents(Event.ONMOUSEUP);
}
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
// Ensure correct implementation,
// but don't let container manage caption etc.
if (client.updateComponent(this, uidl, false)) {
return;
}
focusHandlerRegistration = EventHelper.updateFocusHandler(this, client,
focusHandlerRegistration);
blurHandlerRegistration = EventHelper.updateBlurHandler(this, client,
blurHandlerRegistration);
// Save details
this.client = client;
id = uidl.getId();
// Set text
setText(uidl.getStringAttribute("caption"));
// handle error
if (uidl.hasAttribute("error")) {
if (errorIndicatorElement == null) {
errorIndicatorElement = DOM.createSpan();
errorIndicatorElement.setClassName("v-errorindicator");
}
getElement().insertBefore(errorIndicatorElement, captionElement);
// Fix for IE6, IE7
if (BrowserInfo.get().isIE()) {
errorIndicatorElement.setInnerText(" ");
}
} else if (errorIndicatorElement != null) {
getElement().removeChild(errorIndicatorElement);
errorIndicatorElement = null;
}
if (uidl.hasAttribute("icon")) {
if (icon == null) {
icon = new Icon(client);
getElement().insertBefore(icon.getElement(), captionElement);
}
icon.setUri(uidl.getStringAttribute("icon"));
} else {
if (icon != null) {
getElement().removeChild(icon.getElement());
icon = null;
}
}
if (BrowserInfo.get().isIE7()) {
/*
* Workaround for IE7 size calculation issues. Deferred because of
* issues with a button with an icon using the reindeer theme
*/
if (width.equals("")) {
Scheduler.get().scheduleDeferred(new Command() {
public void execute() {
setWidth("");
setWidth(getOffsetWidth() + "px");
}
});
}
}
}
@Override
public void setText(String text) {
captionElement.setInnerText(text);
}
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
if (DOM.eventGetType(event) == Event.ONLOAD) {
Util.notifyParentOfSizeChange(this, true);
} else if (DOM.eventGetType(event) == Event.ONMOUSEDOWN
&& event.getButton() == Event.BUTTON_LEFT) {
clickPending = true;
} else if (DOM.eventGetType(event) == Event.ONMOUSEMOVE) {
clickPending = false;
} else if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
if (clickPending) {
click();
}
clickPending = false;
}
if (client != null) {
client.handleTooltipEvent(event, this);
}
}
@Override
public void setWidth(String width) {
/* Workaround for IE7 button size part 1 (#2014) */
if (BrowserInfo.get().isIE7() && this.width != null) {
if (this.width.equals(width)) {
return;
}
if (width == null) {
width = "";
}
}
this.width = width;
super.setWidth(width);
/* Workaround for IE7 button size part 2 (#2014) */
if (BrowserInfo.get().isIE7()) {
super.setWidth(width);
}
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
* .dom.client.ClickEvent)
*/
public void onClick(ClickEvent event) {
if (id == null || client == null) {
return;
}
if (BrowserInfo.get().isSafari()) {
VNativeButton.this.setFocus(true);
}
client.updateVariable(id, "state", true, true);
clickPending = false;
}
public void onFocus(FocusEvent arg0) {
client.updateVariable(id, EventId.FOCUS, "", true);
}
public void onBlur(BlurEvent arg0) {
client.updateVariable(id, EventId.BLUR, "", true);
}
}
|