Skip to content

Commit a24e442

Browse files
jboadasmaibin
authored andcommitted
Vaadin server-push, data binding and validators (eugenp#2283)
1 parent aba59e8 commit a24e442

3 files changed

Lines changed: 112 additions & 14 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@
476476
<groupId>com.vaadin</groupId>
477477
<artifactId>vaadin-themes</artifactId>
478478
</dependency>
479+
<dependency>
480+
<groupId>com.vaadin</groupId>
481+
<artifactId>vaadin-push</artifactId>
482+
</dependency>
479483
<dependency>
480484
<groupId>org.seleniumhq.selenium</groupId>
481485
<artifactId>selenium-java</artifactId>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.vaadin;
2+
3+
public class BindData {
4+
5+
private String bindName;
6+
7+
public BindData(String bindName){
8+
this.bindName = bindName;
9+
}
10+
11+
public String getBindName() {
12+
return bindName;
13+
}
14+
15+
public void setBindName(String bindName) {
16+
this.bindName = bindName;
17+
}
18+
19+
20+
}

‎libraries/src/main/java/com/baeldung/vaadin/VaadinUI.java‎

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package com.baeldung.vaadin;
22

3+
import java.time.Instant;
4+
import java.util.ArrayList;
5+
import java.util.Date;
6+
import java.util.List;
7+
8+
import javax.servlet.annotation.WebServlet;
9+
10+
import com.vaadin.annotations.Push;
311
import com.vaadin.annotations.Theme;
412
import com.vaadin.annotations.VaadinServletConfiguration;
13+
import com.vaadin.data.Validator.InvalidValueException;
14+
import com.vaadin.data.fieldgroup.BeanFieldGroup;
15+
import com.vaadin.data.validator.StringLengthValidator;
516
import com.vaadin.server.ExternalResource;
617
import com.vaadin.server.FontAwesome;
718
import com.vaadin.server.VaadinRequest;
@@ -29,14 +40,14 @@
2940
import com.vaadin.ui.UI;
3041
import com.vaadin.ui.VerticalLayout;
3142

32-
import javax.servlet.annotation.WebServlet;
33-
import java.util.ArrayList;
34-
import java.util.Date;
35-
import java.util.List;
36-
43+
@SuppressWarnings("serial")
44+
@Push
3745
@Theme("mytheme")
3846
public class VaadinUI extends UI {
3947

48+
private Label currentTime;
49+
50+
@SuppressWarnings({ "rawtypes", "unchecked" })
4051
@Override
4152
protected void init(VaadinRequest vaadinRequest) {
4253
final VerticalLayout verticalLayout = new VerticalLayout();
@@ -61,8 +72,7 @@ protected void init(VaadinRequest vaadinRequest) {
6172
label.setCaption("Label");
6273
gridLayout.addComponent(label);
6374

64-
final Link link = new Link("Baeldung",
65-
new ExternalResource("http://www.baeldung.com/"));
75+
final Link link = new Link("Baeldung", new ExternalResource("http://www.baeldung.com/"));
6676
link.setId("Link");
6777
link.setTargetName("_blank");
6878
gridLayout.addComponent(link);
@@ -118,28 +128,23 @@ protected void init(VaadinRequest vaadinRequest) {
118128
smallButton.addStyleName("small");
119129
buttonLayout.addComponent(smallButton);
120130

121-
122131
Button largeButton = new Button("Large Button");
123132
largeButton.addStyleName("large");
124133
buttonLayout.addComponent(largeButton);
125134

126-
127135
Button hugeButton = new Button("Huge Button");
128136
hugeButton.addStyleName("huge");
129137
buttonLayout.addComponent(hugeButton);
130138

131-
132139
Button disabledButton = new Button("Disabled Button");
133140
disabledButton.setDescription("This button cannot be clicked");
134141
disabledButton.setEnabled(false);
135142
buttonLayout.addComponent(disabledButton);
136143

137-
138144
Button dangerButton = new Button("Danger Button");
139145
dangerButton.addStyleName("danger");
140146
buttonLayout.addComponent(dangerButton);
141147

142-
143148
Button friendlyButton = new Button("Friendly Button");
144149
friendlyButton.addStyleName("friendly");
145150
buttonLayout.addComponent(friendlyButton);
@@ -171,8 +176,7 @@ protected void init(VaadinRequest vaadinRequest) {
171176

172177
final CheckBox checkbox = new CheckBox("CheckBox");
173178
checkbox.setValue(true);
174-
checkbox.addValueChangeListener(e ->
175-
checkbox.setValue(!checkbox.getValue()));
179+
checkbox.addValueChangeListener(e -> checkbox.setValue(!checkbox.getValue()));
176180
formLayout.addComponent(checkbox);
177181

178182
List<String> numbers = new ArrayList<String>();
@@ -204,17 +208,87 @@ protected void init(VaadinRequest vaadinRequest) {
204208
panel.setContent(grid);
205209
panel.setSizeUndefined();
206210

211+
Panel serverPushPanel = new Panel("Server Push");
212+
FormLayout timeLayout = new FormLayout();
213+
timeLayout.setSpacing(true);
214+
timeLayout.setMargin(true);
215+
currentTime = new Label("No TIME...");
216+
timeLayout.addComponent(currentTime);
217+
serverPushPanel.setContent(timeLayout);
218+
serverPushPanel.setSizeUndefined();
219+
new ServerPushThread().start();
220+
221+
FormLayout dataBindingLayout = new FormLayout();
222+
dataBindingLayout.setSpacing(true);
223+
dataBindingLayout.setMargin(true);
224+
225+
BindData bindData = new BindData("BindData");
226+
BeanFieldGroup beanFieldGroup = new BeanFieldGroup(BindData.class);
227+
beanFieldGroup.setItemDataSource(bindData);
228+
TextField bindedTextField = (TextField) beanFieldGroup.buildAndBind("BindName", "bindName");
229+
bindedTextField.setWidth("250px");
230+
dataBindingLayout.addComponent(bindedTextField);
231+
232+
FormLayout validatorLayout = new FormLayout();
233+
validatorLayout.setSpacing(true);
234+
validatorLayout.setMargin(true);
235+
236+
HorizontalLayout textValidatorLayout = new HorizontalLayout();
237+
textValidatorLayout.setSpacing(true);
238+
textValidatorLayout.setMargin(true);
239+
240+
TextField stringValidator = new TextField();
241+
stringValidator.setNullSettingAllowed(true);
242+
stringValidator.setNullRepresentation("");
243+
stringValidator.addValidator(new StringLengthValidator("String must have 2-5 characters lenght", 2, 5, true));
244+
stringValidator.setValidationVisible(false);
245+
textValidatorLayout.addComponent(stringValidator);
246+
Button buttonStringValidator = new Button("Validate String");
247+
buttonStringValidator.addClickListener(e -> {
248+
try {
249+
stringValidator.setValidationVisible(false);
250+
stringValidator.validate();
251+
} catch (InvalidValueException err) {
252+
stringValidator.setValidationVisible(true);
253+
}
254+
});
255+
textValidatorLayout.addComponent(buttonStringValidator);
256+
257+
validatorLayout.addComponent(textValidatorLayout);
207258
verticalLayout.addComponent(gridLayout);
208259
verticalLayout.addComponent(richTextPanel);
209260
verticalLayout.addComponent(horizontalLayout);
210261
verticalLayout.addComponent(formLayout);
211262
verticalLayout.addComponent(twinColSelect);
212263
verticalLayout.addComponent(panel);
264+
verticalLayout.addComponent(serverPushPanel);
265+
verticalLayout.addComponent(dataBindingLayout);
266+
verticalLayout.addComponent(validatorLayout);
213267
setContent(verticalLayout);
214268
}
215269

216270
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
217271
@VaadinServletConfiguration(ui = VaadinUI.class, productionMode = false)
218272
public static class MyUIServlet extends VaadinServlet {
219273
}
274+
275+
class ServerPushThread extends Thread {
276+
@Override
277+
public void run() {
278+
try {
279+
while (true) {
280+
Thread.sleep(1000);
281+
access(new Runnable() {
282+
@Override
283+
public void run() {
284+
currentTime.setValue("Current Time : " + Instant.now());
285+
}
286+
});
287+
}
288+
289+
} catch (InterruptedException e) {
290+
e.printStackTrace();
291+
}
292+
}
293+
}
220294
}

0 commit comments

Comments
 (0)