You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VNativeButton.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.dom.client.Element;
  6. import com.google.gwt.event.dom.client.BlurEvent;
  7. import com.google.gwt.event.dom.client.BlurHandler;
  8. import com.google.gwt.event.dom.client.ClickEvent;
  9. import com.google.gwt.event.dom.client.ClickHandler;
  10. import com.google.gwt.event.dom.client.FocusEvent;
  11. import com.google.gwt.event.dom.client.FocusHandler;
  12. import com.google.gwt.event.shared.HandlerRegistration;
  13. import com.google.gwt.user.client.DOM;
  14. import com.google.gwt.user.client.Event;
  15. import com.google.gwt.user.client.ui.Button;
  16. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  17. import com.vaadin.terminal.gwt.client.BrowserInfo;
  18. import com.vaadin.terminal.gwt.client.EventId;
  19. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  20. import com.vaadin.terminal.gwt.client.Util;
  21. import com.vaadin.terminal.gwt.client.VTooltip;
  22. import com.vaadin.terminal.gwt.client.ui.ButtonConnector.ButtonServerRpc;
  23. public class VNativeButton extends Button implements ClickHandler,
  24. FocusHandler, BlurHandler {
  25. public static final String CLASSNAME = "v-nativebutton";
  26. protected String width = null;
  27. protected String paintableId;
  28. protected ApplicationConnection client;
  29. ButtonServerRpc buttonRpcProxy;
  30. protected Element errorIndicatorElement;
  31. protected final Element captionElement = DOM.createSpan();
  32. protected Icon icon;
  33. /**
  34. * Helper flag to handle special-case where the button is moved from under
  35. * mouse while clicking it. In this case mouse leaves the button without
  36. * moving.
  37. */
  38. private boolean clickPending;
  39. protected HandlerRegistration focusHandlerRegistration;
  40. protected HandlerRegistration blurHandlerRegistration;
  41. protected boolean disableOnClick = false;
  42. public VNativeButton() {
  43. setStyleName(CLASSNAME);
  44. getElement().appendChild(captionElement);
  45. captionElement.setClassName(getStyleName() + "-caption");
  46. addClickHandler(this);
  47. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  48. sinkEvents(Event.ONMOUSEDOWN);
  49. sinkEvents(Event.ONMOUSEUP);
  50. }
  51. @Override
  52. public void setText(String text) {
  53. captionElement.setInnerText(text);
  54. }
  55. @Override
  56. public void onBrowserEvent(Event event) {
  57. super.onBrowserEvent(event);
  58. if (DOM.eventGetType(event) == Event.ONLOAD) {
  59. Util.notifyParentOfSizeChange(this, true);
  60. } else if (DOM.eventGetType(event) == Event.ONMOUSEDOWN
  61. && event.getButton() == Event.BUTTON_LEFT) {
  62. clickPending = true;
  63. } else if (DOM.eventGetType(event) == Event.ONMOUSEMOVE) {
  64. clickPending = false;
  65. } else if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
  66. if (clickPending) {
  67. click();
  68. }
  69. clickPending = false;
  70. }
  71. if (client != null) {
  72. client.handleTooltipEvent(event, this);
  73. }
  74. }
  75. @Override
  76. public void setWidth(String width) {
  77. this.width = width;
  78. super.setWidth(width);
  79. }
  80. /*
  81. * (non-Javadoc)
  82. *
  83. * @see
  84. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
  85. * .dom.client.ClickEvent)
  86. */
  87. public void onClick(ClickEvent event) {
  88. if (paintableId == null || client == null) {
  89. return;
  90. }
  91. if (BrowserInfo.get().isSafari()) {
  92. VNativeButton.this.setFocus(true);
  93. }
  94. if (disableOnClick) {
  95. setEnabled(false);
  96. buttonRpcProxy.disableOnClick();
  97. }
  98. // Add mouse details
  99. MouseEventDetails details = new MouseEventDetails(
  100. event.getNativeEvent(), getElement());
  101. buttonRpcProxy.click(details);
  102. clickPending = false;
  103. }
  104. public void onFocus(FocusEvent arg0) {
  105. client.updateVariable(paintableId, EventId.FOCUS, "", true);
  106. }
  107. public void onBlur(BlurEvent arg0) {
  108. client.updateVariable(paintableId, EventId.BLUR, "", true);
  109. }
  110. @Override
  111. public void setEnabled(boolean enabled) {
  112. if (isEnabled() != enabled) {
  113. super.setEnabled(enabled);
  114. setStyleName(ApplicationConnection.DISABLED_CLASSNAME, !enabled);
  115. }
  116. }
  117. }