blob: 714e09f1c23164e29ec13c935d1b14d941129145 (
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
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
|
package com.vaadin;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import com.vaadin.terminal.ApplicationResource;
import com.vaadin.terminal.DownloadStream;
import com.vaadin.terminal.RequestHandler;
import com.vaadin.terminal.WrappedRequest;
import com.vaadin.terminal.WrappedResponse;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.Root;
import com.vaadin.ui.RootLayout;
import com.vaadin.ui.VerticalLayout;
public class RootTestApplication extends Application {
private static class MyRootLayout extends VerticalLayout implements
RootLayout {
private final String rootText;
public MyRootLayout(String rootText) {
this.rootText = rootText;
}
public void init(WrappedRequest request) {
if (rootText != null && rootText.trim().length() != 0) {
addComponent(new Label(rootText));
}
addComponent(new Button("Roots, bloody roots",
new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
event.getButton()
.getRoot()
.executeJavaScript(
"window.alert(\"Here\");");
}
}));
ApplicationResource resource = new ApplicationResource() {
public String getMIMEType() {
return "text/plain";
}
public DownloadStream getStream() {
try {
return new DownloadStream(new ByteArrayInputStream(
"Roots".getBytes("UTF-8")), getMIMEType(),
getFilename());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public String getFilename() {
return "roots.txt";
}
public long getCacheTime() {
return 60 * 60 * 1000;
}
public int getBufferSize() {
return 0;
}
public Application getApplication() {
return MyRootLayout.this.getApplication();
}
};
getApplication().addResource(resource);
addComponent(new Link("Resource", resource));
LoginForm loginForm = new LoginForm();
loginForm.addListener(new LoginForm.LoginListener() {
public void onLogin(LoginEvent event) {
System.out.println("User: "
+ event.getLoginParameter("username")
+ ", Password: "
+ event.getLoginParameter("password"));
}
});
addComponent(loginForm);
}
}
@Override
public void init() {
addRequestHandler(new RequestHandler() {
public boolean handleRequest(Application application,
WrappedRequest request, WrappedResponse response) {
if (request.getParameter("myhandler") != null) {
response.setContentType("text/plain");
try {
PrintWriter writer = response.getWriter();
writer.println("Roots, bloody roots");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
} else {
return false;
}
}
});
}
@Override
protected Root createRoot(WrappedRequest request) {
String rootText = request.getParameter("rootText");
Root root = new Root(new MyRootLayout(rootText));
return root;
}
}
|