我挖GWT圖像源的liitle一下,發現有「replaceElement(節點)」 - 一個受保護的方法,特別是對於圖像製成,但這種方法是基於
Widget.getElement().replaceChild(newElement, oldElement);
這裏是我的完整SvgWidget類:
public class SvgWidget implements IsWidget {
private String svgUrl;
private ADBTexte strings;
private IsWidget thisW;
private Loading loadingWidget;
@Inject
private SvgWidget(@Assisted final String svgUrl, final ADBTexte strings, final Loading loadingWidget) {
this.svgUrl = svgUrl;
this.strings = strings;
this.loadingWidget = loadingWidget;
}
@Override
public Widget asWidget() {
thisW = new SimplePanel(loadingWidget);
RequestBuilder rB = new RequestBuilder(RequestBuilder.GET, svgUrl);
rB.setCallback(new RequestCallback() {
@Override
public void onResponseReceived(final Request request, final Response response) {
Widget result = new HTML(response.getText());
thisW.asWidget()
.getElement()
.replaceChild(result.getElement(), thisW.asWidget().getElement().getChild(0));
}
@Override
public void onError(final Request request, final Throwable exception) {
Widget result = new Label(strings.chartError());
thisW.asWidget()
.getElement()
.replaceChild(result.getElement(), thisW.asWidget().getElement().getChild(0));
GWT.log("Error on loading chart: ", exception);
}
});
try {
rB.send();
} catch (RequestException e) {
Widget result = new Label(strings.chartError());
thisW.asWidget().getElement().replaceChild(result.getElement(), thisW.asWidget().getElement().getChild(0));
GWT.log("Error on sending request for chart: ", e);
}
return thisW.asWidget();
}
/**
* Use this to get an instance of {@link SvgWidget}.
*
*/
public interface Factory {
/**
*
* @param svgUrl
* url for svg graphic
* @return instance of {@link SvgWidget} displaying the svg accessible via the given url
*/
SvgWidget get(@Assisted String svgUrl);
}
}
相關的東西在asWidget()中。但我不能確定這是否是一個好/最好的解決辦法:
- 是SimplePanel一個很好的容器(我只需要在容器更換的元件)
- 或許我應該實現HasLoadHandlers?
- 小部件顯示加載小部件 - 可能是最好放置
- 其他要考慮的東西?
(對於那些誰不知道依賴注入(杜松子酒):忽略所有未知的註釋和工廠interfce)
你可以發佈關於你如何創建SVGImage的一些代碼,如何添加它到dom,以及你是否設置LoadEvent? –