Vaadin server-push, data binding and validators (#2283)
This commit is contained in:
parent
aba59e88d9
commit
a24e442731
@ -476,6 +476,10 @@
|
|||||||
<groupId>com.vaadin</groupId>
|
<groupId>com.vaadin</groupId>
|
||||||
<artifactId>vaadin-themes</artifactId>
|
<artifactId>vaadin-themes</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.vaadin</groupId>
|
||||||
|
<artifactId>vaadin-push</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.seleniumhq.selenium</groupId>
|
<groupId>org.seleniumhq.selenium</groupId>
|
||||||
<artifactId>selenium-java</artifactId>
|
<artifactId>selenium-java</artifactId>
|
||||||
|
20
libraries/src/main/java/com/baeldung/vaadin/BindData.java
Normal file
20
libraries/src/main/java/com/baeldung/vaadin/BindData.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.vaadin;
|
||||||
|
|
||||||
|
public class BindData {
|
||||||
|
|
||||||
|
private String bindName;
|
||||||
|
|
||||||
|
public BindData(String bindName){
|
||||||
|
this.bindName = bindName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBindName() {
|
||||||
|
return bindName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBindName(String bindName) {
|
||||||
|
this.bindName = bindName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,7 +1,18 @@
|
|||||||
package com.baeldung.vaadin;
|
package com.baeldung.vaadin;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
|
||||||
|
import com.vaadin.annotations.Push;
|
||||||
import com.vaadin.annotations.Theme;
|
import com.vaadin.annotations.Theme;
|
||||||
import com.vaadin.annotations.VaadinServletConfiguration;
|
import com.vaadin.annotations.VaadinServletConfiguration;
|
||||||
|
import com.vaadin.data.Validator.InvalidValueException;
|
||||||
|
import com.vaadin.data.fieldgroup.BeanFieldGroup;
|
||||||
|
import com.vaadin.data.validator.StringLengthValidator;
|
||||||
import com.vaadin.server.ExternalResource;
|
import com.vaadin.server.ExternalResource;
|
||||||
import com.vaadin.server.FontAwesome;
|
import com.vaadin.server.FontAwesome;
|
||||||
import com.vaadin.server.VaadinRequest;
|
import com.vaadin.server.VaadinRequest;
|
||||||
@ -29,14 +40,14 @@ import com.vaadin.ui.TwinColSelect;
|
|||||||
import com.vaadin.ui.UI;
|
import com.vaadin.ui.UI;
|
||||||
import com.vaadin.ui.VerticalLayout;
|
import com.vaadin.ui.VerticalLayout;
|
||||||
|
|
||||||
import javax.servlet.annotation.WebServlet;
|
@SuppressWarnings("serial")
|
||||||
import java.util.ArrayList;
|
@Push
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Theme("mytheme")
|
@Theme("mytheme")
|
||||||
public class VaadinUI extends UI {
|
public class VaadinUI extends UI {
|
||||||
|
|
||||||
|
private Label currentTime;
|
||||||
|
|
||||||
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
@Override
|
@Override
|
||||||
protected void init(VaadinRequest vaadinRequest) {
|
protected void init(VaadinRequest vaadinRequest) {
|
||||||
final VerticalLayout verticalLayout = new VerticalLayout();
|
final VerticalLayout verticalLayout = new VerticalLayout();
|
||||||
@ -61,8 +72,7 @@ public class VaadinUI extends UI {
|
|||||||
label.setCaption("Label");
|
label.setCaption("Label");
|
||||||
gridLayout.addComponent(label);
|
gridLayout.addComponent(label);
|
||||||
|
|
||||||
final Link link = new Link("Baeldung",
|
final Link link = new Link("Baeldung", new ExternalResource("http://www.baeldung.com/"));
|
||||||
new ExternalResource("http://www.baeldung.com/"));
|
|
||||||
link.setId("Link");
|
link.setId("Link");
|
||||||
link.setTargetName("_blank");
|
link.setTargetName("_blank");
|
||||||
gridLayout.addComponent(link);
|
gridLayout.addComponent(link);
|
||||||
@ -118,28 +128,23 @@ public class VaadinUI extends UI {
|
|||||||
smallButton.addStyleName("small");
|
smallButton.addStyleName("small");
|
||||||
buttonLayout.addComponent(smallButton);
|
buttonLayout.addComponent(smallButton);
|
||||||
|
|
||||||
|
|
||||||
Button largeButton = new Button("Large Button");
|
Button largeButton = new Button("Large Button");
|
||||||
largeButton.addStyleName("large");
|
largeButton.addStyleName("large");
|
||||||
buttonLayout.addComponent(largeButton);
|
buttonLayout.addComponent(largeButton);
|
||||||
|
|
||||||
|
|
||||||
Button hugeButton = new Button("Huge Button");
|
Button hugeButton = new Button("Huge Button");
|
||||||
hugeButton.addStyleName("huge");
|
hugeButton.addStyleName("huge");
|
||||||
buttonLayout.addComponent(hugeButton);
|
buttonLayout.addComponent(hugeButton);
|
||||||
|
|
||||||
|
|
||||||
Button disabledButton = new Button("Disabled Button");
|
Button disabledButton = new Button("Disabled Button");
|
||||||
disabledButton.setDescription("This button cannot be clicked");
|
disabledButton.setDescription("This button cannot be clicked");
|
||||||
disabledButton.setEnabled(false);
|
disabledButton.setEnabled(false);
|
||||||
buttonLayout.addComponent(disabledButton);
|
buttonLayout.addComponent(disabledButton);
|
||||||
|
|
||||||
|
|
||||||
Button dangerButton = new Button("Danger Button");
|
Button dangerButton = new Button("Danger Button");
|
||||||
dangerButton.addStyleName("danger");
|
dangerButton.addStyleName("danger");
|
||||||
buttonLayout.addComponent(dangerButton);
|
buttonLayout.addComponent(dangerButton);
|
||||||
|
|
||||||
|
|
||||||
Button friendlyButton = new Button("Friendly Button");
|
Button friendlyButton = new Button("Friendly Button");
|
||||||
friendlyButton.addStyleName("friendly");
|
friendlyButton.addStyleName("friendly");
|
||||||
buttonLayout.addComponent(friendlyButton);
|
buttonLayout.addComponent(friendlyButton);
|
||||||
@ -171,8 +176,7 @@ public class VaadinUI extends UI {
|
|||||||
|
|
||||||
final CheckBox checkbox = new CheckBox("CheckBox");
|
final CheckBox checkbox = new CheckBox("CheckBox");
|
||||||
checkbox.setValue(true);
|
checkbox.setValue(true);
|
||||||
checkbox.addValueChangeListener(e ->
|
checkbox.addValueChangeListener(e -> checkbox.setValue(!checkbox.getValue()));
|
||||||
checkbox.setValue(!checkbox.getValue()));
|
|
||||||
formLayout.addComponent(checkbox);
|
formLayout.addComponent(checkbox);
|
||||||
|
|
||||||
List<String> numbers = new ArrayList<String>();
|
List<String> numbers = new ArrayList<String>();
|
||||||
@ -204,12 +208,62 @@ public class VaadinUI extends UI {
|
|||||||
panel.setContent(grid);
|
panel.setContent(grid);
|
||||||
panel.setSizeUndefined();
|
panel.setSizeUndefined();
|
||||||
|
|
||||||
|
Panel serverPushPanel = new Panel("Server Push");
|
||||||
|
FormLayout timeLayout = new FormLayout();
|
||||||
|
timeLayout.setSpacing(true);
|
||||||
|
timeLayout.setMargin(true);
|
||||||
|
currentTime = new Label("No TIME...");
|
||||||
|
timeLayout.addComponent(currentTime);
|
||||||
|
serverPushPanel.setContent(timeLayout);
|
||||||
|
serverPushPanel.setSizeUndefined();
|
||||||
|
new ServerPushThread().start();
|
||||||
|
|
||||||
|
FormLayout dataBindingLayout = new FormLayout();
|
||||||
|
dataBindingLayout.setSpacing(true);
|
||||||
|
dataBindingLayout.setMargin(true);
|
||||||
|
|
||||||
|
BindData bindData = new BindData("BindData");
|
||||||
|
BeanFieldGroup beanFieldGroup = new BeanFieldGroup(BindData.class);
|
||||||
|
beanFieldGroup.setItemDataSource(bindData);
|
||||||
|
TextField bindedTextField = (TextField) beanFieldGroup.buildAndBind("BindName", "bindName");
|
||||||
|
bindedTextField.setWidth("250px");
|
||||||
|
dataBindingLayout.addComponent(bindedTextField);
|
||||||
|
|
||||||
|
FormLayout validatorLayout = new FormLayout();
|
||||||
|
validatorLayout.setSpacing(true);
|
||||||
|
validatorLayout.setMargin(true);
|
||||||
|
|
||||||
|
HorizontalLayout textValidatorLayout = new HorizontalLayout();
|
||||||
|
textValidatorLayout.setSpacing(true);
|
||||||
|
textValidatorLayout.setMargin(true);
|
||||||
|
|
||||||
|
TextField stringValidator = new TextField();
|
||||||
|
stringValidator.setNullSettingAllowed(true);
|
||||||
|
stringValidator.setNullRepresentation("");
|
||||||
|
stringValidator.addValidator(new StringLengthValidator("String must have 2-5 characters lenght", 2, 5, true));
|
||||||
|
stringValidator.setValidationVisible(false);
|
||||||
|
textValidatorLayout.addComponent(stringValidator);
|
||||||
|
Button buttonStringValidator = new Button("Validate String");
|
||||||
|
buttonStringValidator.addClickListener(e -> {
|
||||||
|
try {
|
||||||
|
stringValidator.setValidationVisible(false);
|
||||||
|
stringValidator.validate();
|
||||||
|
} catch (InvalidValueException err) {
|
||||||
|
stringValidator.setValidationVisible(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
textValidatorLayout.addComponent(buttonStringValidator);
|
||||||
|
|
||||||
|
validatorLayout.addComponent(textValidatorLayout);
|
||||||
verticalLayout.addComponent(gridLayout);
|
verticalLayout.addComponent(gridLayout);
|
||||||
verticalLayout.addComponent(richTextPanel);
|
verticalLayout.addComponent(richTextPanel);
|
||||||
verticalLayout.addComponent(horizontalLayout);
|
verticalLayout.addComponent(horizontalLayout);
|
||||||
verticalLayout.addComponent(formLayout);
|
verticalLayout.addComponent(formLayout);
|
||||||
verticalLayout.addComponent(twinColSelect);
|
verticalLayout.addComponent(twinColSelect);
|
||||||
verticalLayout.addComponent(panel);
|
verticalLayout.addComponent(panel);
|
||||||
|
verticalLayout.addComponent(serverPushPanel);
|
||||||
|
verticalLayout.addComponent(dataBindingLayout);
|
||||||
|
verticalLayout.addComponent(validatorLayout);
|
||||||
setContent(verticalLayout);
|
setContent(verticalLayout);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,4 +271,24 @@ public class VaadinUI extends UI {
|
|||||||
@VaadinServletConfiguration(ui = VaadinUI.class, productionMode = false)
|
@VaadinServletConfiguration(ui = VaadinUI.class, productionMode = false)
|
||||||
public static class MyUIServlet extends VaadinServlet {
|
public static class MyUIServlet extends VaadinServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ServerPushThread extends Thread {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
access(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
currentTime.setValue("Current Time : " + Instant.now());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user