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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.google.gwt.user.client.ui.Widget;
  17. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  18. import com.vaadin.terminal.gwt.client.BrowserInfo;
  19. import com.vaadin.terminal.gwt.client.EventHelper;
  20. import com.vaadin.terminal.gwt.client.EventId;
  21. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  22. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  23. import com.vaadin.terminal.gwt.client.UIDL;
  24. import com.vaadin.terminal.gwt.client.Util;
  25. import com.vaadin.terminal.gwt.client.VTooltip;
  26. public class VNativeButton extends Button implements VPaintableWidget,
  27. ClickHandler, FocusHandler, BlurHandler {
  28. public static final String CLASSNAME = "v-nativebutton";
  29. protected String width = null;
  30. protected String id;
  31. protected ApplicationConnection client;
  32. protected Element errorIndicatorElement;
  33. protected final Element captionElement = DOM.createSpan();
  34. protected Icon icon;
  35. /**
  36. * Helper flag to handle special-case where the button is moved from under
  37. * mouse while clicking it. In this case mouse leaves the button without
  38. * moving.
  39. */
  40. private boolean clickPending;
  41. private HandlerRegistration focusHandlerRegistration;
  42. private HandlerRegistration blurHandlerRegistration;
  43. private boolean disableOnClick = false;
  44. public VNativeButton() {
  45. setStyleName(CLASSNAME);
  46. getElement().appendChild(captionElement);
  47. captionElement.setClassName(getStyleName() + "-caption");
  48. addClickHandler(this);
  49. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  50. sinkEvents(Event.ONMOUSEDOWN);
  51. sinkEvents(Event.ONMOUSEUP);
  52. }
  53. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  54. // Ensure correct implementation,
  55. // but don't let container manage caption etc.
  56. if (client.updateComponent(this, uidl, false)) {
  57. return;
  58. }
  59. disableOnClick = uidl.hasAttribute(VButton.ATTR_DISABLE_ON_CLICK);
  60. focusHandlerRegistration = EventHelper.updateFocusHandler(this, client,
  61. focusHandlerRegistration);
  62. blurHandlerRegistration = EventHelper.updateBlurHandler(this, client,
  63. blurHandlerRegistration);
  64. // Save details
  65. this.client = client;
  66. id = uidl.getId();
  67. // Set text
  68. setText(uidl.getStringAttribute("caption"));
  69. // handle error
  70. if (uidl.hasAttribute("error")) {
  71. if (errorIndicatorElement == null) {
  72. errorIndicatorElement = DOM.createSpan();
  73. errorIndicatorElement.setClassName("v-errorindicator");
  74. }
  75. getElement().insertBefore(errorIndicatorElement, captionElement);
  76. } else if (errorIndicatorElement != null) {
  77. getElement().removeChild(errorIndicatorElement);
  78. errorIndicatorElement = null;
  79. }
  80. if (uidl.hasAttribute("icon")) {
  81. if (icon == null) {
  82. icon = new Icon(client);
  83. getElement().insertBefore(icon.getElement(), captionElement);
  84. }
  85. icon.setUri(uidl.getStringAttribute("icon"));
  86. } else {
  87. if (icon != null) {
  88. getElement().removeChild(icon.getElement());
  89. icon = null;
  90. }
  91. }
  92. }
  93. @Override
  94. public void setText(String text) {
  95. captionElement.setInnerText(text);
  96. }
  97. @Override
  98. public void onBrowserEvent(Event event) {
  99. super.onBrowserEvent(event);
  100. if (DOM.eventGetType(event) == Event.ONLOAD) {
  101. Util.notifyParentOfSizeChange(this, true);
  102. } else if (DOM.eventGetType(event) == Event.ONMOUSEDOWN
  103. && event.getButton() == Event.BUTTON_LEFT) {
  104. clickPending = true;
  105. } else if (DOM.eventGetType(event) == Event.ONMOUSEMOVE) {
  106. clickPending = false;
  107. } else if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
  108. if (clickPending) {
  109. click();
  110. }
  111. clickPending = false;
  112. }
  113. if (client != null) {
  114. client.handleTooltipEvent(event, this);
  115. }
  116. }
  117. @Override
  118. public void setWidth(String width) {
  119. this.width = width;
  120. super.setWidth(width);
  121. }
  122. /*
  123. * (non-Javadoc)
  124. *
  125. * @see
  126. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
  127. * .dom.client.ClickEvent)
  128. */
  129. public void onClick(ClickEvent event) {
  130. if (id == null || client == null) {
  131. return;
  132. }
  133. if (BrowserInfo.get().isSafari()) {
  134. VNativeButton.this.setFocus(true);
  135. }
  136. if (disableOnClick) {
  137. setEnabled(false);
  138. client.updateVariable(id, "disabledOnClick", true, false);
  139. }
  140. // Add mouse details
  141. MouseEventDetails details = new MouseEventDetails(
  142. event.getNativeEvent(), getElement());
  143. client.updateVariable(id, "mousedetails", details.serialize(), false);
  144. client.updateVariable(id, "state", true, true);
  145. clickPending = false;
  146. }
  147. public void onFocus(FocusEvent arg0) {
  148. client.updateVariable(id, EventId.FOCUS, "", true);
  149. }
  150. public void onBlur(BlurEvent arg0) {
  151. client.updateVariable(id, EventId.BLUR, "", true);
  152. }
  153. @Override
  154. public void setEnabled(boolean enabled) {
  155. if (isEnabled() != enabled) {
  156. super.setEnabled(enabled);
  157. setStyleName(ApplicationConnection.DISABLED_CLASSNAME, !enabled);
  158. }
  159. }
  160. public Widget getWidgetForPaintable() {
  161. return this;
  162. }
  163. }