如何在GWT Cloud中处理Click事件或任何其他事件?

时间:2010-12-02 10:27:56

标签: gwt tags gwt-rpc tag-cloud

我正在使用tagcloud_0.4.jar在GWT中使用Tag Cloud。现在,当我点击标签云中的任何标签时,我该如何处理该事件,或者当用户从云中选择任何标签时如何进行RPC调用?

感谢。

1 个答案:

答案 0 :(得分:0)

以下是在云上捕获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());