UIBinder与Widget

时间:2013-05-14 08:26:28

标签: gwt widget uibinder

我正在尝试使用UIBinder将一个小部件添加到面板,但是复合只是不加载,这是我的xml:

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
         xmlns:g='urn:import:com.google.gwt.user.client.ui'
         xmlns:my='urn:import:com.abc.client.test.ui'>
 <g:HTMLPanel ui:field="mainLayoutPanel">

  <g:SimplePanel ui:field="menuPanel" >
    <my:MainMenuViewImpl ui:field="mainMenu"/>
  </g:SimplePanel>

  <!-- the widget causing the issue -->
  <my:FinancialsNav ui:field="nav"/>

  <g:SimplePanel ui:field="mainPanel" />

 </g:HTMLPanel>
</ui:UiBinder>

对应的java类:

public class AppLayoutImpl extends Composite implements AppLayout{

@UiField
HTMLPanel mainLayoutPanel;

interface AppLayoutUiBinder extends UiBinder<Widget,AppLayoutImpl>{}

private static AppLayoutUiBinder binder=GWT.create(AppLayoutUiBinder.class);

@UiField
SimplePanel menuPanel;// nav at the top

@UiField(provided=true)
FinancialsNav nav;// nav on the side

@UiField
SimplePanel mainPanel;// center

@UiField(provided=true)
MainMenuViewImpl mainMenu;

public AppLayoutImpl(ClientFactory clientFactory){
    mainMenu=clientFactory.getMainMenuView();
    nav=clientFactory.getNav();
    initWidget(binder.createAndBindUi(this));
}



@Override
public Widget asWidget(){
    return this;
}
}

导致问题的Widget:

public class FinancialsNav extends Composite implements ClickHandler{

private VerticalPanel nav=new VerticalPanel();
// Operations
private DisclosurePanel operationsWrapper=new DisclosurePanel(Proto2.constants.financials_navigation_operations());
private NavButton product=new NavButton(Proto2.constants.financials_navigation_products(),
        SharedUtilities.PRODUCT_TOKEN);


private NavButton generalOptions=new NavButton(Proto2.constants.financials_navigation_generalOptions(),"");

private ArrayList<NavButton[]> buttons;
private NavButton[] operationsButtons={product,vc,fc,emp,others};
//...
private NavButton[] optionsButtons={generalOptions};

private final EventBus bus;

public FinancialsNav(EventBus bus){
    this.bus=bus;
    buildNav();
    initWidget(nav);
}

private void buildNav(){
    buttons=new ArrayList<NavButton[]>();
    buttons.add(operationsButtons);
    //...
    buttons.add(optionsButtons);

    int n=buttons.size();
    int nn;
    for(int i=0;i<n;i++){
        nn=buttons.get(i).length;
        for(int j=0;j<nn;j++){
            (buttons.get(i)[j]).addClickHandler(this);
            (buttons.get(i)[j]).setStylePrimaryName(NAV_BUTTON_STYLE);
            if(i==0){
                operationsWrapper.add(buttons.get(i)[j]);
            //...
            }else if(i==4){
                optionsWrapper.add(buttons.get(i)[j]);
            }
        }
    }

    nav.add(operationsWrapper);
    // ...
    nav.add(optionsWrapper);

}

当不与UIBinder一起使用时,FinancialsNav小部件正常工作,当FinancialsNav不存在时,其他AppLayout按预期工作。

我花了好几个小时来查看各种教程和示例,但却无法找到代码的错误。我还尝试了各种解决方法,例如在UIBinder中声明一个面板而不是FinancialsNav,并将导航添加到面板。

此外,所有内容都在同一个包中,因此不应该是导入问题。

非常感谢任何帮助......

这是clientFactory

public class ClientFactoryImpl implements ClientFactory{
private static final EventBus eventBus=new SimpleEventBus();
private static final PlaceController placeController=new PlaceController(eventBus);

private static final CreatePlanServiceAsync createPlanService=GWT.create(CreatePlanService.class);

private static final FinancialsNav navView=new FinancialsNav(eventBus);
private static final MainMenuViewImpl mainMenuView=new MainMenuViewImpl();

@Override
public EventBus getEventBus(){
    return eventBus;
}

@Override
public PlaceController getPlaceController(){
    return placeController;
}

@Override
public FinancialsNav getNav(){
    return navView;
}

@Override
public MainMenuViewImpl getMainMenuView(){
    return mainMenuView;
}

@Override
public CreatePlanServiceAsync getCreatePlanService(){
    return createPlanService;
} 
}

2 个答案:

答案 0 :(得分:1)

FinancialsNav小部件具有带参数的构造函数。所以不要创建参数构造函数。

条目<my:FinancialsNav ui:field="nav"/>等于new FinancialsNav()

在大多数情况下,这意味着它们必须是默认的可实例化的;也就是说,它们必须提供零参数构造函数。你必须传递参数

 /** Used by MyUiBinder to instantiate FinancialsNav */
  @UiFactory FinancialsNav makeFinancialsNav() { // method name is insignificant. do start with make
    return new FinancialsNav(eventBus);
  }

参考Using a widget that requires constructor args

你能展示clientFactory代码!!

答案 1 :(得分:0)

好的,我发现了错误,实际上与UIBinder没有直接关系。

在实施UIBinder的同时,我试图减少应用中面板使用的表格数量。

正如我注意到披露面板基于表格,我删除了我在FinancialsNav中的中间垂直面板。 因此,拥有DisclosurePanel - &gt;按钮而不是DisclosurePanel - &gt; VerticalPanel - &gt;纽扣。这导致整个区块无法显示。

@adenoyelle和@bumika:谢谢你的帮助