11package 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 ;
311import com .vaadin .annotations .Theme ;
412import 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 ;
516import com .vaadin .server .ExternalResource ;
617import com .vaadin .server .FontAwesome ;
718import com .vaadin .server .VaadinRequest ;
2940import com .vaadin .ui .UI ;
3041import 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" )
3846public 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