From a24e442731b82235ca18451d8eafe44ce58cda02 Mon Sep 17 00:00:00 2001 From: Jesus Boadas Date: Tue, 18 Jul 2017 19:46:02 -0400 Subject: [PATCH] Vaadin server-push, data binding and validators (#2283) --- libraries/pom.xml | 4 + .../java/com/baeldung/vaadin/BindData.java | 20 ++++ .../java/com/baeldung/vaadin/VaadinUI.java | 102 +++++++++++++++--- 3 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 libraries/src/main/java/com/baeldung/vaadin/BindData.java diff --git a/libraries/pom.xml b/libraries/pom.xml index c23b6ef912..16f70cb171 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -476,6 +476,10 @@ com.vaadin vaadin-themes + + com.vaadin + vaadin-push + org.seleniumhq.selenium selenium-java diff --git a/libraries/src/main/java/com/baeldung/vaadin/BindData.java b/libraries/src/main/java/com/baeldung/vaadin/BindData.java new file mode 100644 index 0000000000..bcdc4eee71 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/vaadin/BindData.java @@ -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; + } + + +} diff --git a/libraries/src/main/java/com/baeldung/vaadin/VaadinUI.java b/libraries/src/main/java/com/baeldung/vaadin/VaadinUI.java index e134b501c8..8343d38f6a 100644 --- a/libraries/src/main/java/com/baeldung/vaadin/VaadinUI.java +++ b/libraries/src/main/java/com/baeldung/vaadin/VaadinUI.java @@ -1,7 +1,18 @@ 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.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.FontAwesome; import com.vaadin.server.VaadinRequest; @@ -29,14 +40,14 @@ import com.vaadin.ui.TwinColSelect; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; -import javax.servlet.annotation.WebServlet; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - +@SuppressWarnings("serial") +@Push @Theme("mytheme") public class VaadinUI extends UI { + private Label currentTime; + + @SuppressWarnings({ "rawtypes", "unchecked" }) @Override protected void init(VaadinRequest vaadinRequest) { final VerticalLayout verticalLayout = new VerticalLayout(); @@ -61,8 +72,7 @@ public class VaadinUI extends UI { label.setCaption("Label"); gridLayout.addComponent(label); - final Link link = new Link("Baeldung", - new ExternalResource("http://www.baeldung.com/")); + final Link link = new Link("Baeldung", new ExternalResource("http://www.baeldung.com/")); link.setId("Link"); link.setTargetName("_blank"); gridLayout.addComponent(link); @@ -118,28 +128,23 @@ public class VaadinUI extends UI { smallButton.addStyleName("small"); buttonLayout.addComponent(smallButton); - Button largeButton = new Button("Large Button"); largeButton.addStyleName("large"); buttonLayout.addComponent(largeButton); - Button hugeButton = new Button("Huge Button"); hugeButton.addStyleName("huge"); buttonLayout.addComponent(hugeButton); - Button disabledButton = new Button("Disabled Button"); disabledButton.setDescription("This button cannot be clicked"); disabledButton.setEnabled(false); buttonLayout.addComponent(disabledButton); - Button dangerButton = new Button("Danger Button"); dangerButton.addStyleName("danger"); buttonLayout.addComponent(dangerButton); - Button friendlyButton = new Button("Friendly Button"); friendlyButton.addStyleName("friendly"); buttonLayout.addComponent(friendlyButton); @@ -171,8 +176,7 @@ public class VaadinUI extends UI { final CheckBox checkbox = new CheckBox("CheckBox"); checkbox.setValue(true); - checkbox.addValueChangeListener(e -> - checkbox.setValue(!checkbox.getValue())); + checkbox.addValueChangeListener(e -> checkbox.setValue(!checkbox.getValue())); formLayout.addComponent(checkbox); List numbers = new ArrayList(); @@ -204,12 +208,62 @@ public class VaadinUI extends UI { panel.setContent(grid); 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(richTextPanel); verticalLayout.addComponent(horizontalLayout); verticalLayout.addComponent(formLayout); verticalLayout.addComponent(twinColSelect); verticalLayout.addComponent(panel); + verticalLayout.addComponent(serverPushPanel); + verticalLayout.addComponent(dataBindingLayout); + verticalLayout.addComponent(validatorLayout); setContent(verticalLayout); } @@ -217,4 +271,24 @@ public class VaadinUI extends UI { @VaadinServletConfiguration(ui = VaadinUI.class, productionMode = false) 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(); + } + } + } } \ No newline at end of file