【发布时间】:2015-08-14 00:09:23
【问题描述】:
如何增加Vaadin 通知/警告时间?我正在使用 Vaadin Valo 主题。 Vaadin 之书Notifications 页面没有任何帮助。
【问题讨论】:
如何增加Vaadin 通知/警告时间?我正在使用 Vaadin Valo 主题。 Vaadin 之书Notifications 页面没有任何帮助。
【问题讨论】:
首先注意the five types 和Notification 默认有不同的延迟。
Notification.Type.HUMANIZED_MESSAGENotification.Type.TRAY_NOTIFICATIONNotification.Type.WARNING_MESSAGENotification.Type.ERROR_MESSAGENotification.Type.ASSISTIVE_NOTIFICATION请参阅下面的示例应用程序的源代码来演示每种类型。
Notification 类具有延迟访问器:getDelayMsec 和 setDelayMsec。
例如,要先将延迟设置为 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;
}
}
【讨论】:
Notification 的类,并在自己的构造函数中添加延迟。然后进行全局搜索和替换。