Пример #1
0
  @Override
  public HandlerRegistration addClickHandler(ClickHandler clickHandler) {
    /*
     * When we directly subscribe to this widget's ClickEvent, sometimes the
     * click gets ignored (inconsistent repro but it happens enough to be
     * annoying). Doing it this way fixes it.
     */

    hasHandlers_.addHandler(ClickEvent.getType(), clickHandler);

    final HandlerRegistration mouseDown =
        addMouseDownHandler(
            new MouseDownHandler() {
              public void onMouseDown(MouseDownEvent event) {
                event.preventDefault();
                event.stopPropagation();

                addStyleName(styles_.toolbarButtonPushed());
                down_ = true;
              }
            });

    final HandlerRegistration mouseOut =
        addMouseOutHandler(
            new MouseOutHandler() {
              public void onMouseOut(MouseOutEvent event) {
                event.preventDefault();
                event.stopPropagation();

                removeStyleName(styles_.toolbarButtonPushed());
                down_ = false;
              }
            });

    final HandlerRegistration mouseUp =
        addMouseUpHandler(
            new MouseUpHandler() {
              public void onMouseUp(MouseUpEvent event) {
                event.preventDefault();
                event.stopPropagation();

                if (down_) {
                  down_ = false;
                  removeStyleName(styles_.toolbarButtonPushed());

                  NativeEvent clickEvent =
                      Document.get()
                          .createClickEvent(
                              1,
                              event.getScreenX(),
                              event.getScreenY(),
                              event.getClientX(),
                              event.getClientY(),
                              event.getNativeEvent().getCtrlKey(),
                              event.getNativeEvent().getAltKey(),
                              event.getNativeEvent().getShiftKey(),
                              event.getNativeEvent().getMetaKey());
                  DomEvent.fireNativeEvent(clickEvent, hasHandlers_);
                }
              }
            });

    return new HandlerRegistration() {
      public void removeHandler() {
        mouseDown.removeHandler();
        mouseOut.removeHandler();
        mouseUp.removeHandler();
      }
    };
  }
Пример #2
0
 public HandlerRegistration addClickHandler(ClickHandler handler) {
   return addDomHandler(handler, ClickEvent.getType());
 }