如何处理GWT Cloud中的点击事件或任何其他事件?
题
我正在使用tagcloud_0.4.jar在GWT中使用标签云。现在,当我单击标签云中的任何标签时,如何处理该事件,或者当用户从云中选择任何标签时如何进行RPC调用?
谢谢。
解决方案
这是一个示例,可以在云上捕捉clickevent。但这是一个解决方法,因为图书馆可能会改变。由于开发器没有提供在标签上都无法管理云上的事件,因此这可能是有充分理由的...
因此,请自行使用此代码。祈祷没有改变小部件的结构dom。我建议您向开发器询问其小部件的一些增强功能,例如管理此类事件。
final TagCloud cloud = new TagCloud();
cloud.addWord(new WordTag("AAA"));
cloud.addWord(new WordTag("AAA"));
cloud.addWord(new WordTag("BBB"));
cloud.addWord(new WordTag("CCC"));
cloud.addWord(new WordTag("CCC"));
cloud.addWord(new WordTag("CCC"));
cloud.addDomHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
// Prevent the click from the tag to be executed
event.preventDefault();
Element e;
// For each tag present in the cloud, look for the one that is triggered
for (int i = 0; i < cloud.getTags().size(); ++i) {
// Gets the element in the cloud, this really is the unsafe part of the code
// if the developer change the dom of its widget
e = DOM.getChild(DOM.getChild(cloud.getElement(), 0), i);
// Is the current element targeted by the event?
if (event.getClientX() >= e.getOffsetLeft() && event.getClientY() >= e.getOffsetTop()
&& event.getClientX() <= e.getOffsetLeft() + e.getOffsetWidth()
&& event.getClientY() <= e.getOffsetTop() + e.getOffsetHeight()) {
// Gets the abstract tag
Tag t = cloud.getTags().get(i);
// Gets tag descendant
if (t instanceof WordTag) {
// Do what you want with your WordTag, maybe an RPC call
} else if (t instanceof ImageTag) {
// Do what you want with your ImageTag, maybe an RPC call
}
break;
}
}
}
}, ClickEvent.getType());
不隶属于 StackOverflow