2010-12-02 2 views

답변

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()); 
관련 문제