views:

651

answers:

1

Hello,

how can I use the same UI template (*.ui.xml file) with multiple Java objects extending from Composite?

I need to build several pages that should display basically the same information with the same layout, but on one page some fields will be editable, and on a different page other fields will be editable. I would like to specify layout only once in ui.xml, and create different behaviors in different *.java classes.

Eclipse is giving me a syntax error "FirstAppUI.ui.xml is missing" on

@UiTemplate("Template.ui.xml")
public class FirstAppUI extends Composite {
  interface FirstAppUIUiBinder extends
          UiBinder<Widget, FirstAppUI> {
  }
}

thanks! jane prusakova

+1  A: 

I think you need to place the @UiTemplate annotation on the Binder, not on the Composite class

This code works for me:

public class TestUiBinder extends Composite {

  @UiTemplate("SomeTemplate.ui.xml")
  interface TestUiBinderUiBinder extends UiBinder<Widget, TestUiBinder> {}
  private static TestUiBinderUiBinder uiBinder = GWT.create(TestUiBinderUiBinder.class);

  public TestUiBinder() {
    initWidget(uiBinder.createAndBindUi(this));
  }
}

public class AnotherTestUiBinder extends Composite {

  @UiTemplate("SomeTemplate.ui.xml")
  interface TestUiBinderUiBinder extends UiBinder<Widget, AnotherTestUiBinder> {}
  private static TestUiBinderUiBinder uiBinder = GWT.create(TestUiBinderUiBinder.class);

  public AnotherTestUiBinder() {
    initWidget(uiBinder.createAndBindUi(this));
  }
}

This seems somewhat similar to the solution for applying different templates to the same widget.

Jason Hall
Jason is correct, the @UiTemplate annotation goes on the UiBinder-derived interface, not the containing Composite class.Among other things, this makes it possible to have more than one template, and declare a different UiBinder for each template, as in http://code.google.com/webtoolkit/doc/latest/DevGuideUiBinder.html#Apply_different_xml
aem
initWidget method insists on parameter of type Template, otherwise gives a syntax error: public FirstAppUI(final UiBinder<Widget, FirstAppUI> binder) { initWidget(uiBinder.createAndBindUi(this)); }Thanks for the link, it looks relevant, but unfortunately does not work.
jprusakova
Note that there is an error in the GWT page linked above, the body of the `FirstAppUI` constructor should be `initWidget(binder.createAndBindUi(this))`.
Philippe Beaudoin
Edited my answer with code that I verified compiles.
Jason Hall
Jason, that does work, thanks a lot!
jprusakova