I currently have the following relevant code-snippet:
public class MyEntryPoint implements EntryPoint {
private boolean areFieldsEnabled = false;
@Override
public void onModuleLoad(){
addFields();
}
private void addFields(){
Button button = new Button("Show fields");
button.addClickHandler(new ClickHandler(){
@Override
public void onClick(final ClickEvent event){
String entityType = ...;
String entityId = ...;
checkIfFieldsAreEnabled();
MyEntryPoint.this.fields = new FieldsValuesDiv(entityType,
Long.valueOf(entityId), MyEntryPoint.this.areFieldsEnabled);
wrapper.add(MyEntryPoint.this.fields);
}
});
}
private void checkIfFieldsAreEnabled(){
this.fieldsService.areValuesEditable(new AsyncCallback<Boolean>(){
@Override
public void onFailure(final Throwable caught){
Window.alert(caught.getLocalizedMessage());
}
@Override
public void onSuccess(final Boolean result){
MyEntryPoint.this.areFieldsEnabled = result;
}
});
}
}
This results in a button "Show fields". When I click this, the Fields GWT-component is loaded and the fields are shown. Currently all the fields are disabled by default, even though the checkIfFieldsAreEnabled()
is done right before it. When I click on the "Hide fields" button (not shown in the code above), and then the "Show fields" button again, it does work.
The reason? It doesn't wait for the result of the AsyncCallback<Boolean>
, so sometimes it does work and sometimes it doesn't, depending on how fast the Async call is done. I'm coming from a C# background where async
/await
can be used. When I googled for a Java equivalent I came across this SO answer by Jon Skeet, which basically states there is not such a thing in Java. This was from almost three years ago however, so I wonder if anything has changed since then.
If not, does anyone know how I can await the AsyncCallback<Boolean>
which sets the areFieldsEnabled
boolean?