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
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client;
import java.util.ArrayList;
import java.util.List;
/**
*
* class for event handlers used by ComponentEventHandler
*
* @author davengo GmbH (Germany/Berlin, www.davengo.com)
*
*/
public class ComponentEventHandler {
public static final String HANDLER_LISTEN_ATTRIBUTE = "listenEvents";
public static final String HANDLER_TRIGGER_VARIABLE = "fireEvent";
private List<String> eventRegistrations;
private ComponentDetail detail;
private ApplicationConnection client;
public ComponentEventHandler(ComponentDetail detail,
ApplicationConnection client) {
this.detail = detail;
this.client = client;
this.eventRegistrations = null;
}
/**
* Fires a event which is transmitted to the server and passed on the the
* components handleEvent method provided listeners have been registered on
* the server side.
*
* @param eventIdentifier
* the unique identifier for the event
* @param parameters
* the parameters for the event (can be null)
*/
public void fireEvent(String eventIdentifier, String... parameters) {
fireEvent(eventIdentifier, false, parameters);
}
/**
* Fires a component event which is transmitted to the server and passed on
* the the components handleEvent method. The event is sent to the server
* even though there are no explicit listeners registered on the server
* side.
*
* @param eventIdentifier
* the unique identifier for the event
* @param parameters
* the parameters for the event (can be null)
*/
public void fireComponentEvent(String eventIdentifier, String... parameters) {
fireEvent(eventIdentifier, true, parameters);
}
private void fireEvent(String eventIdentifier, boolean forceTransmission,
String... parameters) {
String[] event;
// filter events which are not listened on the server-side right here
boolean transmit = forceTransmission
|| ((!(eventRegistrations == null)) && eventRegistrations
.contains(eventIdentifier));
if (transmit) {
if (parameters != null) {
event = new String[parameters.length + 1];
event[0] = eventIdentifier;
for (int i = 0; i < parameters.length; i++) {
event[i + 1] = parameters[i];
}
} else {
event = new String[] { eventIdentifier };
}
// transmit the event to the server-side
client.updateVariable(detail.getPid(), HANDLER_TRIGGER_VARIABLE,
event, true);
}
}
void registerEventsFromUIDL(UIDL componentUIDL) {
// read out the request event handlers
if (componentUIDL.hasAttribute(HANDLER_LISTEN_ATTRIBUTE)) {
String[] requestedEvents = componentUIDL
.getStringArrayAttribute(HANDLER_LISTEN_ATTRIBUTE);
// create the eventRegistrations list if necessary
if ((requestedEvents.length > 0) && (eventRegistrations == null)) {
eventRegistrations = new ArrayList<String>();
}
// parse the requested event handlers
for (String reqEvent : requestedEvents) {
if (!eventRegistrations.contains(reqEvent)) {
eventRegistrations.add(reqEvent);
}
}
}
}
}
|