【问题标题】:How to increase the Vaadin notification/warning time?如何增加 Vaadin 通知/警告时间?
【发布时间】:2015-08-14 00:09:23
【问题描述】:

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

【问题讨论】:

    标签: vaadin vaadin7


    【解决方案1】:

    默认延迟

    首先注意the five typesNotification 默认有不同的延迟。

    • 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 );
    

    或者更好的是,使用TimeUnit 转换器替换“魔术”数字。转换器仅生成 long 值,因此我们必须强制转换为 int 以满足 Vaadin 设置器。

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

    谨防默认行为的一个变化。如果在ERROR_MESSAGE 上设置正延迟数,用户无需点击关闭; 错误消息消失在鼠标指针移动后延迟到期后。

    在以下示例应用程序中查看此代码的运行情况。取消注释调用 setter 的代码行。

    示例应用

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

    禁用/启用设置器时,您可能需要重新启动 Vaadin 应用程序。 (或购买license for 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;
        }
    
    }
    

    【讨论】:

    • 感谢@BasilBourque 的解决方案。我在很多地方都在使用通知。不可能更改每个通知属性。是否有任何 CSS 可以申请通知类或者我们可以重载任何 Vaadin 方法以增加延迟?
    • 我建议你创建一个扩展 Notification 的类,并在自己的构造函数中添加延迟。然后进行全局搜索和替换。
    • 另一个想法:如果您使用通知的次数太多以至于更改它们会惹恼您,那么您可能会因为出现如此多的通知而让您的用户烦恼。您可能需要考虑使用其他用户界面设备向用户提供反馈。
    • 我创建了通知方法,添加了延迟并进行了全局搜索和替换。它工作正常。非常感谢您的热心帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多