GWT interface的使用例子

1、定義一個接口類git

public interface TicketViewModuleListener {
void fieldsChanged();app

void positionReceived(double latitude, double longitude);
}ide

 

2、定義widget類並實現TicketViewModuleListener接口ui

public class TicketViewWidget extends VOverlay implements OfflineMode,
TicketViewModuleListener {google

private InformationLayout informationLayout;orm

@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();
}rem

private boolean validateFields() {
resetValidations();get

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);
}
}

}

3、定義組件類並調用接口

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);
}
});
}
}

}

4、應用狀況

widget類實現listener接口,並調用組件類,組件類產生事件後調用widget類,達到參數傳遞的目的。

相關文章
相關標籤/搜索