public class Entry implements EntryPoint { @Override public void onModuleLoad() { Button button = new Button(); button.setText("click me"); button.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Window.alert("hello world"); } }); RootPanel.get().add(button); } }Thanks to java's verbosity, the signal to noise ratio is pretty low. It goes wild quite fast when we need to express nested DOM structures, like
public class Entry implements EntryPoint { @Override public void onModuleLoad() { Button button = new Button(); // ... some init code Panel outterContainer = new FlowPanel(); // ... some init codes Panel innerContainer = new FlowPanel(); // ... some init codes outterContainer.add(innerContainer); innerContainer.add(button); RootPanel.get().add(outterContainer); } }Another problem here is, since java code is kind of linear, little aid is provided by the editor to present that nesting structure.
So the xml came to save the world, and we moved to the GWT 2.x era. The basic idea of UiBinder is to express the dom in xml instead of java.
<!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'> <g:FlowPanel> <g:Button>hello world</g:Button> </g:FlowPanel> </ui:UiBinder>The above code will be translated by GWT compiler as
FlowPanel panel = new FlowPanel(); Button button = new Button(); button.setText("hello world"); panel.add(button);To programmatically access the generated component, we need to give an identifier in the xml
<g:Button ui:field='myButton'>hello world</g:Button>Now let's move to the java code, the key here is we are going to declare a seemingly orphan interface, then obtaining an instance of it via the might GWT.create() method.
public class Entry implements EntryPoint { interface UI extends UiBinder<FlowPanel, Entry> {} @Override public void onModuleLoad() { UI uiBinder = GWT.create(UI.class); } }Then using the @UiField annotation to link to the button.
public class Entry implements EntryPoint { interface UI extends UiBinder<FlowPanel, Entry> {} @UiField Button myButton; @Override public void onModuleLoad() { UI uiBinder = GWT.create(UI.class); uiBinder.createAndBindUi(this); Window.alert(myButton.getText()); RootPanel.get().add(myButton); } }Now run the project, you will see a boring alert once the app is loaded.
But wait! UiBinder can do more than that, it can be used to hook events. Things to note here,
- @UiHandler annotation, it's value needs to be the same as the java field name (myButton)
- the annotated method has no return value, void.
- the annotated method takes exactly one argument, which needs to be of some GwtEvent. here ClickEvent is a sub class of GwtEvent.
- the field must have corresponding addXXXHandler()
- the annotated method name as well as the argument name can be anything
@UiHandler("myButton") void onButtonClick(ClickEvent e) { myButton.setText("hello event"); }Now refresh the page and click the button to see its text changed.
Codes are available from https://github.com/verydapeng/gwt-tutorials under the folder UiBinder-Quick-Intro
To dig more out of uibinder, follow the link here http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html
But it’s a fine, low-risk first 3D printer for those getting their feet wet in 3D printing. The Ultimaker S5's twin extruders, good print quality, massive construct quantity, and simple setup and operation make it worthy of our Editors' Choice as a professional-grade filament-based 3D printer. A 3D printing service is sensible as a result of|as a result of} they handle any hassles with print head alignment, clogged injectors, and tweaks to make the printer perform nicely. You may be taught these expertise by way of trial precision machining and error with your own home} 3D printer.
ReplyDelete