如何增加Vaadin通知/警告时间?

LAR*_*LHO 5 vaadin vaadin7

如何增加Vaadin通知/警告时间?我正在使用Vaadin Valo主题."Vaadin 通知书"页面没有任何帮助.

Bas*_*que 11

默认延迟

首先要注意的五种类型Notification默认情况下具有不同的延迟.

  • Notification.Type.HUMANIZED_MESSAGE
    显示,直到用户移动鼠标指针.
  • Notification.Type.TRAY_NOTIFICATION
    用户移动鼠标指针后显示直到3秒.
  • Notification.Type.WARNING_MESSAGE
    用户移动鼠标指针后显示直到1.5秒.
  • Notification.Type.ERROR_MESSAGE
    显示,直到用户点击该消息.(延迟设置为-1毫秒)
  • Notification.Type.ASSISTIVE_NOTIFICATION
    在Mac OS X Mountain Lion上的Safari中没有任何功能.

请参阅下文,了解演示每种类型的示例应用程序的源代码.

控制延迟

Notification类有延迟存取:getDelayMsecsetDelayMsec.

例如,要首先将延迟设置为7秒,我们必须转换为毫秒.

notification.setDelayMsec( 7 * 1_000 );
Run Code Online (Sandbox Code Playgroud)

或者更好的是,使用TimeUnit转换器替换"魔术"数字.转换器只生成long值,因此我们必须转换int为满足Vaadin setter.

notification.setDelayMsec( ( int ) TimeUnit.SECONDS.toMillis( 7 ) );
Run Code Online (Sandbox Code Playgroud)

谨防默认行为的一个重大变化.如果在a上设置正延迟号ERROR_MESSAGE,则用户无需点击即可解除; 移动鼠标指针后延迟过期后,错误消息消失.

请参阅以下示例应用中的此代码.取消注释调用setter的代码行.

示例应用程序

这是一些Vaadin 7.5.3代码.这个类是一个完整的Vaadin应用程序.只需创建一个新的Vaadin项目并替换为此代码.

禁用/启用setter时,您可能需要重新启动Vaadin应用程序.(或者为JRebel购买许可证以避免重启.)

带有五个按钮的示例应用的屏幕截图

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;
    }

}
Run Code Online (Sandbox Code Playgroud)