0
# Framework Integration
1
2
GWT integration utilities and callback framework for building interactive Errai applications with text selection controls and standardized callback interfaces.
3
4
## Capabilities
5
6
### Errai Common Entry Point
7
8
Main GWT entry point providing DOM utilities and framework initialization.
9
10
```java { .api }
11
/**
12
* Main GWT entry point and utility class for text selection handling
13
*/
14
public class ErraiCommon implements EntryPoint {
15
/**
16
* GWT entry point method called when module loads
17
*/
18
public void onModuleLoad();
19
20
/**
21
* Enable or disable text selection on DOM elements
22
* @param e - DOM element to modify
23
* @param disable - true to disable text selection, false to enable
24
*/
25
public static void disableTextSelection(Element e, boolean disable);
26
}
27
```
28
29
**GWT Module Configuration:**
30
31
```xml
32
<!-- In your .gwt.xml file -->
33
<module>
34
<inherits name='com.google.gwt.user.User'/>
35
<inherits name="com.google.gwt.json.JSON"/>
36
<entry-point class='org.jboss.errai.common.client.ErraiCommon'/>
37
</module>
38
```
39
40
**Usage Examples:**
41
42
```java
43
import org.jboss.errai.common.client.ErraiCommon;
44
import com.google.gwt.dom.client.Element;
45
import com.google.gwt.user.client.DOM;
46
47
// Disable text selection on a specific element
48
Element element = DOM.getElementById("myElement");
49
ErraiCommon.disableTextSelection(element, true);
50
51
// Re-enable text selection
52
ErraiCommon.disableTextSelection(element, false);
53
```
54
55
**JavaScript Implementation Details:**
56
57
The `disableTextSelection` method uses native JavaScript to control text selection:
58
59
- When `disable = true`: Sets `ondrag = function() { return false; }` and `onselectstart = function() { return false; }`
60
- When `disable = false`: Sets `ondrag = null` and `onselectstart = null`
61
62
### Callback Framework
63
64
Standardized callback interface for asynchronous operations and user interactions.
65
66
```java { .api }
67
/**
68
* Interface for classes that can accept callback methods
69
*/
70
public interface AcceptsCallback {
71
/** Standard OK message constant */
72
public static final String MESSAGE_OK = "OK";
73
74
/** Standard CANCEL message constant */
75
public static final String MESSAGE_CANCEL = "CANCEL";
76
77
/**
78
* Callback method invoked by callers
79
* @param message - Message being returned (often MESSAGE_OK or MESSAGE_CANCEL)
80
* @param data - Additional data (optional, can be null)
81
*/
82
public void callback(Object message, Object data);
83
}
84
```
85
86
**Usage Examples:**
87
88
```java
89
import org.jboss.errai.common.client.framework.AcceptsCallback;
90
91
// Implement callback interface
92
public class MyDialogHandler implements AcceptsCallback {
93
public void callback(Object message, Object data) {
94
if (AcceptsCallback.MESSAGE_OK.equals(message)) {
95
// Handle OK action
96
System.out.println("User clicked OK");
97
if (data != null) {
98
processData(data);
99
}
100
} else if (AcceptsCallback.MESSAGE_CANCEL.equals(message)) {
101
// Handle cancel action
102
System.out.println("User cancelled");
103
} else {
104
// Handle custom message
105
System.out.println("Received message: " + message);
106
}
107
}
108
109
private void processData(Object data) {
110
// Process callback data
111
}
112
}
113
114
// Use callback with async operation
115
public class AsyncService {
116
public void performOperation(String input, AcceptsCallback callback) {
117
// Simulate async operation
118
Timer timer = new Timer() {
119
@Override
120
public void run() {
121
try {
122
// Perform operation
123
String result = processInput(input);
124
callback.callback(AcceptsCallback.MESSAGE_OK, result);
125
} catch (Exception e) {
126
callback.callback("ERROR", e.getMessage());
127
}
128
}
129
};
130
timer.schedule(1000); // Execute after 1 second
131
}
132
133
private String processInput(String input) {
134
return "Processed: " + input;
135
}
136
}
137
138
// Usage
139
AsyncService service = new AsyncService();
140
MyDialogHandler handler = new MyDialogHandler();
141
service.performOperation("test data", handler);
142
```
143
144
### Dialog and UI Patterns
145
146
The callback framework is commonly used for dialog boxes and user confirmations:
147
148
```java
149
import org.jboss.errai.common.client.framework.AcceptsCallback;
150
151
public class ConfirmDialog {
152
public static void show(String message, AcceptsCallback callback) {
153
// Create dialog UI
154
DialogBox dialog = new DialogBox();
155
dialog.setText("Confirm");
156
157
VerticalPanel panel = new VerticalPanel();
158
panel.add(new Label(message));
159
160
HorizontalPanel buttonPanel = new HorizontalPanel();
161
162
Button okButton = new Button("OK", new ClickHandler() {
163
public void onClick(ClickEvent event) {
164
dialog.hide();
165
callback.callback(AcceptsCallback.MESSAGE_OK, null);
166
}
167
});
168
169
Button cancelButton = new Button("Cancel", new ClickHandler() {
170
public void onClick(ClickEvent event) {
171
dialog.hide();
172
callback.callback(AcceptsCallback.MESSAGE_CANCEL, null);
173
}
174
});
175
176
buttonPanel.add(okButton);
177
buttonPanel.add(cancelButton);
178
panel.add(buttonPanel);
179
180
dialog.setWidget(panel);
181
dialog.center();
182
dialog.show();
183
}
184
}
185
186
// Usage
187
ConfirmDialog.show("Are you sure you want to delete this item?", new AcceptsCallback() {
188
public void callback(Object message, Object data) {
189
if (AcceptsCallback.MESSAGE_OK.equals(message)) {
190
deleteItem();
191
}
192
// Cancel requires no action
193
}
194
});
195
```
196
197
### Form Validation Patterns
198
199
Callback interface can be used for form validation workflows:
200
201
```java
202
import org.jboss.errai.common.client.framework.AcceptsCallback;
203
204
public class FormValidator {
205
public void validateForm(Map<String, String> formData, AcceptsCallback callback) {
206
List<String> errors = new ArrayList<>();
207
208
// Validate required fields
209
if (formData.get("name") == null || formData.get("name").trim().isEmpty()) {
210
errors.add("Name is required");
211
}
212
213
if (formData.get("email") == null || !isValidEmail(formData.get("email"))) {
214
errors.add("Valid email is required");
215
}
216
217
// Return results via callback
218
if (errors.isEmpty()) {
219
callback.callback(AcceptsCallback.MESSAGE_OK, formData);
220
} else {
221
callback.callback("VALIDATION_ERROR", errors);
222
}
223
}
224
225
private boolean isValidEmail(String email) {
226
return email != null && email.contains("@");
227
}
228
}
229
230
// Usage
231
FormValidator validator = new FormValidator();
232
Map<String, String> formData = new HashMap<>();
233
formData.put("name", "John Doe");
234
formData.put("email", "john@example.com");
235
236
validator.validateForm(formData, new AcceptsCallback() {
237
public void callback(Object message, Object data) {
238
if (AcceptsCallback.MESSAGE_OK.equals(message)) {
239
// Form is valid, submit data
240
submitForm((Map<String, String>) data);
241
} else if ("VALIDATION_ERROR".equals(message)) {
242
// Show validation errors
243
List<String> errors = (List<String>) data;
244
showErrors(errors);
245
}
246
}
247
});
248
```
249
250
## GWT Integration Requirements
251
252
### Dependencies
253
254
```xml
255
<dependencies>
256
<dependency>
257
<groupId>com.google.gwt</groupId>
258
<artifactId>gwt-user</artifactId>
259
<scope>provided</scope>
260
</dependency>
261
</dependencies>
262
```
263
264
### Module Inheritance
265
266
```xml
267
<!-- In your .gwt.xml file -->
268
<module>
269
<inherits name='com.google.gwt.user.User'/>
270
<inherits name="com.google.gwt.json.JSON"/>
271
<inherits name="org.jboss.errai.common.ErraiCommon"/>
272
</module>
273
```
274
275
### Required Imports
276
277
```java
278
// GWT core imports
279
import com.google.gwt.core.client.EntryPoint;
280
import com.google.gwt.dom.client.Element;
281
import com.google.gwt.json.client.JSONObject;
282
283
// Errai Common imports
284
import org.jboss.errai.common.client.ErraiCommon;
285
import org.jboss.errai.common.client.framework.AcceptsCallback;
286
```
287
288
## Best Practices
289
290
### Callback Error Handling
291
292
```java
293
public class RobustCallbackHandler implements AcceptsCallback {
294
public void callback(Object message, Object data) {
295
try {
296
if (AcceptsCallback.MESSAGE_OK.equals(message)) {
297
handleSuccess(data);
298
} else if (AcceptsCallback.MESSAGE_CANCEL.equals(message)) {
299
handleCancel();
300
} else if (message instanceof String) {
301
handleError((String) message, data);
302
} else {
303
handleUnknownMessage(message, data);
304
}
305
} catch (Exception e) {
306
// Log error and provide fallback
307
GWT.log("Callback error: " + e.getMessage(), e);
308
handleError("CALLBACK_ERROR", e);
309
}
310
}
311
312
private void handleSuccess(Object data) { /* ... */ }
313
private void handleCancel() { /* ... */ }
314
private void handleError(String error, Object data) { /* ... */ }
315
private void handleUnknownMessage(Object message, Object data) { /* ... */ }
316
}
317
```
318
319
### Text Selection Management
320
321
```java
322
public class TextSelectionManager {
323
private Set<Element> disabledElements = new HashSet<>();
324
325
public void disableTextSelection(Element element) {
326
if (!disabledElements.contains(element)) {
327
ErraiCommon.disableTextSelection(element, true);
328
disabledElements.add(element);
329
}
330
}
331
332
public void enableTextSelection(Element element) {
333
if (disabledElements.contains(element)) {
334
ErraiCommon.disableTextSelection(element, false);
335
disabledElements.remove(element);
336
}
337
}
338
339
public void enableAllTextSelection() {
340
for (Element element : new HashSet<>(disabledElements)) {
341
enableTextSelection(element);
342
}
343
}
344
}
345
```