blob: 8f1eda68168c27a964c96c5c822fa34b9cd3aa3c (
plain)
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
|
package com.vaadin.tests.minitutorials.v7a3;
import org.json.JSONArray;
import org.json.JSONException;
import com.vaadin.server.ExternalResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.JavaScript;
import com.vaadin.ui.JavaScriptFunction;
import com.vaadin.ui.Link;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.UI;
public class JSAPIUI extends UI {
@Override
public void init(VaadinRequest request) {
JavaScript.getCurrent().addFunction("com.example.api.notify",
new JavaScriptFunction() {
@Override
public void call(JSONArray arguments) throws JSONException {
try {
String caption = arguments.getString(0);
if (arguments.length() == 1) {
// only caption
Notification.show(caption);
} else {
// type should be in [1]
Notification.show(caption,
Type.values()[arguments.getInt(1)]);
}
} catch (JSONException e) {
// We'll log in the console, you might not want to
JavaScript.getCurrent().execute(
"console.error('" + e.getMessage() + "')");
}
}
});
setContent(new Link(
"Send message",
new ExternalResource(
"javascript:(function(){com.example.api.notify(prompt('Message'),2);})();")));
}
}
|