Sun 1 Nov 2009
I was pretty excited to find UiHandler, it makes the organization of code so much cleaner!
I am putting in the full code for both, so you can see the beauty for yourself! Pay attention to the comments where i put the gotchas that got me at first! ![]()
Before:
-
package com.rubygeek.sandbox.client;
-
-
import java.util.ArrayList;
-
-
import com.google.gwt.core.client.GWT;
-
import com.google.gwt.event.dom.client.ClickEvent;
-
import com.google.gwt.event.dom.client.ClickHandler;
-
import com.google.gwt.uibinder.client.UiBinder;
-
import com.google.gwt.uibinder.client.UiField;
-
import com.google.gwt.user.client.Window;
-
import com.google.gwt.user.client.ui.Button;
-
import com.google.gwt.user.client.ui.CheckBox;
-
import com.google.gwt.user.client.ui.Composite;
-
import com.google.gwt.user.client.ui.Widget;
-
-
-
-
interface MyUiBinder extends UiBinder<Widget, LanguageList> {}
-
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
-
-
@UiField CheckBox rubyCheck;
-
@UiField CheckBox pythonCheck;
-
@UiField CheckBox javaCheck;
-
@UiField CheckBox phpCheck;
-
@UiField Button button;
-
-
public LanguageList() {
-
// bind XML file of same name of this class to this class
-
initWidget(uiBinder.createAndBindUi(this));
-
-
final ArrayList<CheckBox> boxes = new ArrayList<CheckBox>();
-
boxes.add(rubyCheck);
-
boxes.add(pythonCheck);
-
boxes.add(javaCheck);
-
boxes.add(phpCheck);
-
-
// add button and handler to alert the values of checkboxes
-
button.addClickHandler(new ClickHandler() {
-
public void onClick(ClickEvent event) {
-
String output = "";
-
for(CheckBox box : boxes) {
-
if (box.getValue()) {
-
output += box.getFormValue() + ", ";
-
}
-
}
-
}
-
});
-
-
}
-
}
After:
-
package com.rubygeek.sandbox.client;
-
-
import java.util.ArrayList;
-
-
import com.google.gwt.core.client.GWT;
-
import com.google.gwt.event.dom.client.ClickEvent;
-
import com.google.gwt.uibinder.client.UiBinder;
-
import com.google.gwt.uibinder.client.UiField;
-
import com.google.gwt.uibinder.client.UiHandler;
-
import com.google.gwt.user.client.Window;
-
import com.google.gwt.user.client.ui.CheckBox;
-
import com.google.gwt.user.client.ui.Composite;
-
import com.google.gwt.user.client.ui.Widget;
-
-
-
interface MyUiBinder extends UiBinder<Widget, LanguageList> {}
-
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
-
-
final ArrayList<CheckBox> boxes = new ArrayList<CheckBox>();
-
-
@UiField CheckBox rubyCheck;
-
@UiField CheckBox pythonCheck;
-
@UiField CheckBox javaCheck;
-
@UiField CheckBox phpCheck;
-
-
// add ClickEvent handler to button with ui:field="languageButton" in the UiBinder code
-
// !!! note no ; at end of this annotation, and the name of function does not matter, its the annotation that ties it to the
-
// body of the method, also this does not go inside the class
(i spend 40 mins figuring that out...) -
@UiHandler("languageButton")
-
void doClick(ClickEvent event) {
-
String output = "";
-
for(CheckBox box : boxes) {
-
if (box.getValue()) {
-
output += box.getFormValue() + ", ";
-
}
-
}
-
}
-
-
public LanguageList() {
-
// bind XML file of same name of this class to this class
-
initWidget(uiBinder.createAndBindUi(this));
-
-
boxes.add(rubyCheck);
-
boxes.add(pythonCheck);
-
boxes.add(javaCheck);
-
boxes.add(phpCheck);
-
}
-
}
See the constructure is further slimmed down!


