如何正确地为svg图像创建gwt小部件?
我知道如何将svg图像加载到gwt中: 有了这个,我只想创建一个SvgImage类。它应该像gwt的imageurl一样工作: 我可以添加独立于加载状态的图像,一旦加载完成,图像就会变得可见。 如何实现这一点?我试图读取图像的源代码,但这很糟糕-既找不到加载url的位置,也找不到引发LoadEvent的位置如何正确地为svg图像创建gwt小部件?,gwt,Gwt,我知道如何将svg图像加载到gwt中: 有了这个,我只想创建一个SvgImage类。它应该像gwt的imageurl一样工作: 我可以添加独立于加载状态的图像,一旦加载完成,图像就会变得可见。 如何实现这一点?我试图读取图像的源代码,但这很糟糕-既找不到加载url的位置,也找不到引发LoadEvent的位置 或者一般来说,创建通过给定url加载其内容的小部件的好方法是什么?我在gwt image source中挖掘了一个liitle位,发现有一个replaceElementNode—一个专门为i
或者一般来说,创建通过给定url加载其内容的小部件的好方法是什么?我在gwt image source中挖掘了一个liitle位,发现有一个replaceElementNode—一个专门为image设计的受保护方法,但该方法基于
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是一个好的容器吗?我只需要这个容器来替换一个元素
也许我应该实现HasLoadHandler?!
小部件显示一个正在加载的小部件-可能它的位置更好
还有什么需要考虑的吗?
对于那些不知道gin依赖注入的人:忽略所有未知的注释和工厂干涉你能发布一些关于如何创建SvgImage、如何将其添加到dom以及是否设置LoadEvent的代码吗?