Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gwt uibinder in an abstract parent class

Tags:

gwt

gwt2

uibinder

i'm wondering if there's a way to build the gwt uibinder logic into an abstract parent class so that i don't have to repeat the code in every class i want to bind.

for example, i'd like to be able to do something like this:

public abstract class BasePanel<Panel extends BasePanel> extends Composite {
    interface Binder<BinderPanel extends BasePanel> extends UiBinder<Widget, BinderPanel> { }
    private static final Binder binder = GWT.create(Binder<Panel>.class);

    public BasePanel() {
        initWidget(binder.createAndBindUi(this));
        init();
    }
}

basically this would allow any child classes to do something like this:

public MyPanel extends BasePanel<MyPanel> {
    //my code here
}

the default constructor would take care of all the code to bind MyPanel to MyPanel.ui.xml.

basically i want to be lazy and only have to build the interface and the binder once so that it's done in a common way. thoughts?

thanks in advance.

like image 691
jctierney Avatar asked Jan 01 '26 05:01

jctierney


1 Answers

Proper way to do abstract UI binder classes is to define a super class that will contain logic that is common to all subclass widgets. This class can have fields marked as @UiField, event handlers and anything else that goes into the UI binder class. And child classes actually have UI binder instantiation logic. So something like this:

public abstract BaseWidget extends Composite {
  @UiField TextBox textBoxCommon1;
  @UiField TextBox textBoxCommon2;

  @UiHandler("textBoxCommon1")
  void onTextBoxCommon1Changed( ValueChangeEvent<String> event ) {
    //...
  }

  @UiHandler("textBoxCommon2")
  void onTextBoxCommon2Changed( ValueChangeEvent<String> event ) {
    //...
  }
}

public class SomeWidget extends BaseWidget {
  interface SomeWidgetUiBinder extends UiBinder<Widget,SomeWidget> {}

  private static SomeWidgetUiBinder uiBinder = GWT.create(SomeWidgetUiBinder.class);

  @UiField Button someWidgetButton;

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

  @UiHandler("someWidgetButton")
  void onButtonClicked(ClickEvent e) {
    Window.alert(textBoxCommon1.getValue());
  }
}
like image 53
Strelok Avatar answered Jan 06 '26 19:01

Strelok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!