Search code examples
gwttagsgwt-rpctag-cloud

How to handle Click event or any other Event in GWT Cloud?


I am using tagcloud_0.4.jar for using Tag Cloud in GWT. Now, when I click on any tag inside the tag cloud, then how can I handle that event or how can I do RPC call when user select any tag from cloud?

Thanks.


Solution

  • Here is an example to catch a ClickEvent on the cloud. But this is a workaround as the library may change. As the developper do not offer to manage events on the cloud neither on tags, this is perhaps for a good reason...

    So use this code at your own risk. Pray for no change of the widget's structure DOM. I suggest you to ask the developper some enhancements on its widgets, like managing such events.

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