Is there a programmatic way to read all the widget a GWT page or Panel contains?
I have several error labels in my form, all suffixed by "ErrorLabel" and I'd like to find them and clear them. I can do a list of them, but I figured out I can actually automate that if I had some...
I am using UiBinder.
Use recursion
method to get all the children of any component.
Steps to follow: (How to read the UiField
names?)
Add below entry in you gwt.xml
<inherits name="com.google.gwt.user.Debug"/>
Use debugId
along with ui:field
as shown below in your ui.xml
<gwt:CheckBox ui:field="myCheckBox" debugId="myCheckBox" />
Now you can get the Id
myCheckBox.getElement().getId();
All the Ids are generated with default prefix gwt-debug-
as shown below. If you want then you can remove it.
gwt-debug-myCheckBox
Here is a utility class:
Note: add more widget and component as per your requirement.
import java.util.Iterator;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
public class WidgetValidator {
private static void validateFlexTable(final FlexTable flextable) {
for (int row = 0; row < flextable.getRowCount(); row++) {
for (int column = 0; column < flextable.getCellCount(row); column++) {
WidgetValidator.validateWidget(flextable.getWidget(row, column));
}
}
}
private static void validatePanel(final Panel panel) {
final Iterator<Widget> iterator = panel.iterator();
while (iterator.hasNext()) {
WidgetValidator.validateWidget(iterator.next());
}
}
public static void validateWidget(final Widget widget) {
if (widget != null) {
if (widget instanceof FlexTable) {
WidgetValidator.validateFlexTable((FlexTable) widget);
} else if (widget instanceof Panel) {
WidgetValidator.validatePanel((Panel) widget);
} else {
System.out.println(widget.getElement().getId().replace("gwt-debug-", ""));
}
}
}
}
The simplest solution is to add a class name to all the labels - for example, "errorLabel", and add the following line to your CSS file:
.hide .errorLabel {display: none}
Then, if you need to hide/show all of these labels, you can do it with a single line of code:
parentPanel.addStyleName("hide");
parentPanel.removeStyleName("hide");
Something like that
private void myFunction(RootPanel panel) {
for (Widget w : panel) {
if(w instanceof Label){
//do something
}
}
}