2017-01-31 45 views
1

我创建了以下类作为gwtbootstrap3工具提示的扩展。至少有两个原因为什么我想要得到gwtbootstrap3 Tooltip类:GWT/GWTBootstrap3扩展工具提示:C'tor中的addHandler异常

1.)当显示工具提示时添加onWindowClosing处理程序,以便在用户离开页面时隐藏()工具提示(这是 - 据我所知 - 这是Bootstrap中不支持的功能,是吗?)

2.)我想要防止在iPad或iPhone上显示页面时显示工具提示,因为工具提示时它们表现很奇怪(第一个提示显示工具提示,第二个提示执行按钮,这不完全是用户期望的)

请注意,下面给出的类尚未完成...但已经在t他的阶段我添加一个处理程序时得到一个异常。 请注意,无论我添加什么类型的Handler(ShowHandler,ShownHandler等),它都会抛出异常。

任何帮助非常感谢。

package com.mypackage.client.widgets.featureWidgets; 
    import org.gwtbootstrap3.client.shared.event.ShowEvent; 
    import org.gwtbootstrap3.client.shared.event.ShowHandler; 
    import org.gwtbootstrap3.client.ui.constants.Trigger; 

    import com.google.gwt.event.shared.HandlerRegistration; 
    import com.google.gwt.user.client.Window; 
    import com.google.gwt.user.client.Window.ClosingEvent; 

    public class Tooltip extends org.gwtbootstrap3.client.ui.Tooltip { 

     private boolean isMobile; 
     private HandlerRegistration windowClosingHandlerRegistration; 
     private final Tooltip tooltip; 

     public Tooltip() { 
      super(); 
      tooltip = this; 

      this.addShowHandler(new ShowHandler() { 
       @Override 
       public void onShow(final ShowEvent showEvent) { 
        // TODO Auto-generated method stub 
        if (windowClosingHandlerRegistration == null) { 
         windowClosingHandlerRegistration = Window.addWindowClosingHandler(new Window.ClosingHandler() { 
          @Override 
          public void onWindowClosing(final ClosingEvent arg0) { 
           tooltip.hide(); 
          } 
         }); 
        } 

       } 
      }); 
     } 


    } 


    When I create a instance of this tooltip using the following: 

    [...] 
     <b:ButtonToolBar ui:field="itemButtonToolBar" addStyleNames="hiddenPrint"> 
     <b:ButtonGroup> 

      <a:Tooltip title="{msgs.buttomTitleAddItem}" container="body"> 
       <b:Button ui:field="addItemButton" icon="PLUS"/> 
      </a:Tooltip>           
    [...] 

试图添加处理程序时出现以下异常,为什么?

SEVERE: (TypeError) : Cannot read property 'addHandler_11_g$' of undefinedcom.google.gwt.core.client.JavaScriptException: (TypeError) : Cannot read property 'addHandler_11_g$' of undefined 
     at Unknown.addShowHandler_2_g$([email protected]:57195) 
     at Unknown.Tooltip_6_g$([email protected]:57685) 
     at Unknown.build_f_Tooltip2_0_g$([email protected]:31606) 
     at Unknown.get_f_Tooltip2_0_g$([email protected]:31831) 
     at Unknown.build_f_ButtonGroup1_0_g$([email protected]:31524) 
     at Unknown.get_f_ButtonGroup1_0_g$([email protected]:31791) 
     at Unknown.build_itemButtonToolBar_0_g$([email protected]:31696) 
     at Unknown.get_itemButtonToolBar_0_g$([email protected]:31876) 
     at Unknown.createAndBindUi_58_g$([email protected]:31437) 
     at Unknown.createAndBindUi_59_g$([email protected]:31441) 
     at Unknown.ItemButtonGroup_2_g$([email protected]:30733) 
     at Unknown.$init_589_g$([email protected]:37722) 
     at Unknown.SummaryWidget_1_g$([email protected]:37686) 
     at Unknown.loadSummaryWidget_0_g$([email protected]:4991) 
     at Unknown.setSummary_1_g$([email protected]:5028) 
     at Unknown.onSuccess_8_g$([email protected]:3312) 
     at Unknown.onSuccess_9_g$([email protected]:3317) 
     at Unknown.onResponseReceived_0_g$([email protected]:156917) 
     at Unknown.fireOnResponseReceived_0_g$([email protected]:129224) 
     at Unknown.onReadyStateChange_0_g$([email protected]:129532) 
     at Unknown.<anonymous>([email protected]:172082) 
     at Unknown.apply_0_g$([email protected]:104636) 
     at Unknown.entry0_0_g$([email protected]:104692) 
     at Unknown.<anonymous>([email protected]:104672) 

回答

2

免责声明:我用gwtbootstrap3 v0.9.2,我相信这是同一版本使用,因为我得到了同样的错误代码。

工具提示需要一个小工具来操作(在你的情况下,按钮是一个工具提示的小工具)。工具提示使用它的小部件来处理所有事件 - 例如source codeaddShowHandler

现在,你需要了解整个结构是建立:

  • 第一工具提示创建(机智没有widget集)
  • ,则创建按钮
  • 工具提示的setWidget方法被调用来将该按钮设置为小部件

因此,当您在构造函数中使用addShowHandler方法时,实际上调用widget.addHandler而wid get为null。

您可以通过检查Window.alert(tooltip.getWidget() == null ? "null" : tooltip.getWidget().toString());


很少有办法让它工作(越晚越好):

  1. 等待DOM结构要建立由调度延迟命令(如果您确定该小部件将最终设置):

    Scheduler.get().scheduleDeferred(new ScheduledCommand() { 
        @Override 
        public void execute() { 
         // set up events handling 
        } 
    }); 
    
  2. 覆盖setWidget方法(注泰德有两种方法:setWidget(Widget w)setWidget(IsWidget w)):

    @Override 
    public void setWidget(Widget w) { 
        super.setWidget(w); 
        // set up events handling 
    } 
    
  3. 你不需要addWindowClosingHandlershowEvent处理,你可以直接在构造函数中做到这一点:

    public class Tooltip extends org.gwtbootstrap3.client.ui.Tooltip { 
    
        private boolean isMobile; 
        private final Tooltip tooltip; 
    
        public Tooltip() { 
         super(); 
         tooltip = this; 
    
         Window.addWindowClosingHandler(new Window.ClosingHandler() { 
          @Override 
          public void onWindowClosing(final ClosingEvent arg0) { 
           tooltip.hide(); 
          } 
         }); 
        } 
    } 
    
+0

嗨亚当,感谢您的好解释和您的帮助。你是我的英雄! – user668338