UiBinder

From Zanecorpwiki

Jump to: navigation, search

Subclassing UiBinder Components

UiBinder combines nicely with plain old sub-class/superclass semantics to create even more modular and highly re-usable components. The UI-XML is bound to a(n often abstract) parent class, with behavior defined and/or augmented in sub-classes. This looks something like:

UI-XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">

<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.zanecorp.gwt.commoncomponents'>
<g:HTMLPanel>

<div ui:field="messageArea"></div>
<g:Button ui:field="ok" text="OK" />
</g:HTMLPanel>
</ui:UiBinder>

Bound class:

public abstract class TrivialMessageBox extends Composite {
	interface MyUiBinder extends UiBinder<Widget, TrivialMessageBox> {}
	
	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	
	@UiField protected DivElement messageArea;
	@UiField protected Button ok;

	
	public EntryInfoPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		// ui:field and id attributes act as if they're incompatible
		// so we set the HTML-IDs here
		messageArea.setId("messageArea");
		heatSection.setId("heatSection");
	}
	
	@UiHandler("ok")
	protected abstract void handleOkClick(ClickEvent e);
}

You must bind the '@UiHandler's in the base class. UiHandler's in child classes are silently ignored.[notes 1]

Notes

  1. This would seem rather straightforward for the compiler to check, so I hope Google at least provides a warning. I would go so far as to argue for a compile time error as GWT code, in my experience, can generate a number of warnings that can be safely ignored, which leads me to not pay as much attention to the warnings.
Personal tools