GWT interface的使用例子

一、定义一个接口类

public interface TicketViewModuleListener {
void fieldsChanged();

void positionReceived(double latitude, double longitude);
}

 

二、定义widget类并实现TicketViewModuleListener接口

public class TicketViewWidget extends VOverlay implements OfflineMode,
TicketViewModuleListener {

private InformationLayout informationLayout;

@Override
public void fieldsChanged() {
if (validateFields) {
validateFields();
}
if (listener != null && isApplicationOnline()) {
listener.updateState(getTicket());
}

fieldsChanged = true;
CacheManifestStatusIndicator.setConfirmationRequired(true);
saveTicketButton.setEnabled(true);
}

private boolean isApplicationOnline() {
return OfflineModeEntrypoint.get().getNetworkStatus().isAppOnline();
}

private boolean validateFields() {
resetValidations();

boolean valid = true;
if (!informationLayout.validateFields()) {
valid = false;
}
return valid;
}

private Widget buildSectionWrapper(final Widget content,
final String captionString, final String styleName) {
VCssLayout layout = new VCssLayout();
layout.addStyleName(styleName);

Label caption = new Label(captionString);
caption.addStyleName("sectioncaption");
layout.add(caption);

layout.add(content);

return layout;
}

@Override
public void positionReceived(final double latitude, final double longitude) {
if (listener != null) {
listener.positionReceived(latitude, longitude);
}
}

}

三、定义组件类并调用接口

public class InformationLayout extends VerticalComponentGroupWidget {

 private final TicketViewModuleListener listener;

private void requestUserPosition() {
Geolocation geolocation = Geolocation.getIfSupported();
if (geolocation == null) {
useCurrentLocationSwitch.setValue(false);
} else {
geolocation
.getCurrentPosition(new Callback<com.google.gwt.geolocation.client.Position, PositionError>() {
@Override
public void onSuccess(
final com.google.gwt.geolocation.client.Position result) {
currentPosition = result;
if (listener != null) {
listener.positionReceived(result
.getCoordinates().getLatitude(), result
.getCoordinates().getLongitude());
}
}

@Override
public void onFailure(final PositionError reason) {
useCurrentLocationSwitch.setValue(false, true);
remove(useCurrentLocationSwitch);
}
});
}
}

}

四、应用情况

widget类实现listener接口,并调用组件类,组件类产生事件后调用widget类,达到参数传递的目的。

posted @ 2018-09-07 17:30  瓦砾  阅读(164)  评论(0编辑  收藏  举报