Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the Vaadin notification/warning time?

Tags:

vaadin

vaadin7

How to increase the Vaadin notification/warning time? I am using Vaadin Valo theme. Nothing from the Book of Vaadin Notifications page helps.

like image 299
LARKINS CARVALHO Avatar asked Aug 14 '15 00:08

LARKINS CARVALHO


1 Answers

Default Delays

First note that the five types of Notification have different delays by default.

  • Notification.Type.HUMANIZED_MESSAGE
    Displayed until user moves the mouse pointer.
  • Notification.Type.TRAY_NOTIFICATION
    Displayed until 3 seconds after user moves the mouse pointer.
  • Notification.Type.WARNING_MESSAGE
    Displayed until 1.5 seconds after user moves the mouse pointer.
  • Notification.Type.ERROR_MESSAGE
    Displayed until user clicks on the message. (Delay set to -1 milliseconds)
  • Notification.Type.ASSISTIVE_NOTIFICATION
    Does nothing for me in Safari on Mac OS X Mountain Lion.

See below for the source code of an example app demonstrating each type.

Controlling Delay

The Notification class has accessors for the delay: getDelayMsec and setDelayMsec.

For example, to set a delay to seven seconds first we must convert to milliseconds.

notification.setDelayMsec( 7 * 1_000 );

Or better yet, replace "magic" numbers with use of the TimeUnit converter. The converter produces only long values, so we must cast to an int to satisfy the Vaadin setter.

notification.setDelayMsec( ( int ) TimeUnit.SECONDS.toMillis( 7 ) );

Beware of one big change in behavior from the default. If setting a positive delay number on an ERROR_MESSAGE, the user need not click to dismiss; the error message disappears after the delay expires after a move of the mouse pointer.

See this code in action in the following example app. Uncomment the line of code calling the setter.

Example App

Here is some Vaadin 7.5.3 code. This class is a complete Vaadin app. Just create a new Vaadin project and replace with this code.

When disabling/enabling the setter, you may need to restart your Vaadin app. (Or buy a license for JRebel to avoid restarts.)

screenshot of example app with five buttons

package com.example.vaadinnotifs;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import java.util.concurrent.TimeUnit;

/**
 *  By Basil Bourque. 
 *
 * Source code provided under MIT License. 
 * http://opensource.org/licenses/MIT
 */
@Theme ( "mytheme" )
@Widgetset ( "com.example.vaadinnotifs.MyAppWidgetset" )
@SuppressWarnings ( "serial" )
public class MyUI extends UI
{

    @Override
    protected void init ( VaadinRequest vaadinRequest ) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin( true );
        layout.setSpacing( true );
        setContent( layout );

        layout.addComponent( this.makeButton( Notification.Type.HUMANIZED_MESSAGE ) );
        layout.addComponent( this.makeButton( Notification.Type.TRAY_NOTIFICATION ) );
        layout.addComponent( this.makeButton( Notification.Type.WARNING_MESSAGE ) );
        layout.addComponent( this.makeButton( Notification.Type.ERROR_MESSAGE ) );
        layout.addComponent( this.makeButton( Notification.Type.ASSISTIVE_NOTIFICATION ) );
    }

    @WebServlet ( urlPatterns = "/*" , name = "MyUIServlet" , asyncSupported = true )
    @VaadinServletConfiguration ( ui = MyUI.class , productionMode = false )
    public static class MyUIServlet extends VaadinServlet
    {
    }

    private Button makeButton ( Notification.Type typeArg ) {
        // Taking advantage of Java’s nifty Enum capability to generically produce each Button instance.
        Button b = new Button( typeArg.name() );
        b.setData( typeArg );  // "setData" and "getData" are accessors for a little hiding place for any object you wish to carry with a Vaadin widget.
        b.addClickListener( new Button.ClickListener()
        {
            @Override
            public void buttonClick ( ClickEvent event ) {
                Notification.Type t = ( Notification.Type ) event.getButton().getData();  // Cast from Object type used by the widget’s hiding place to the `Notification.Type` type we know we used.
                Notification notification = new Notification( "This is a Notification message." , t );
                //notification.setDelayMsec( ( int ) TimeUnit.SECONDS.toMillis( 7 ) );
                Integer delayMillis = notification.getDelayMsec();
                String description = "Notification.Type: " + t.name() + " has delay of " + delayMillis + " ms.";
                notification.setDescription( description );
                notification.show( Page.getCurrent() );
            }
        } );
        return b;
    }

}
like image 96
Basil Bourque Avatar answered Nov 07 '22 07:11

Basil Bourque