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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.event.dom.client.ClickEvent;
  19. import com.google.gwt.event.dom.client.ClickHandler;
  20. import com.google.gwt.user.client.DOM;
  21. import com.google.gwt.user.client.Event;
  22. import com.google.gwt.user.client.ui.Button;
  23. import com.vaadin.client.ApplicationConnection;
  24. import com.vaadin.client.BrowserInfo;
  25. import com.vaadin.client.Util;
  26. import com.vaadin.client.WidgetUtil.ErrorUtil;
  27. import com.vaadin.shared.ui.button.ButtonServerRpc;
  28. public class VNativeButton extends Button
  29. implements ClickHandler, HasErrorIndicatorElement {
  30. public static final String CLASSNAME = "v-nativebutton";
  31. /** For internal use only. May be removed or replaced in the future. */
  32. public String paintableId;
  33. /** For internal use only. May be removed or replaced in the future. */
  34. public ApplicationConnection client;
  35. /** For internal use only. May be removed or replaced in the future. */
  36. public ButtonServerRpc buttonRpcProxy;
  37. /** For internal use only. May be removed or replaced in the future. */
  38. private Element errorIndicatorElement;
  39. /** For internal use only. May be removed or replaced in the future. */
  40. public final Element captionElement = DOM.createSpan();
  41. /** For internal use only. May be removed or replaced in the future. */
  42. public Icon icon;
  43. /**
  44. * Helper flag to handle special-case where the button is moved from under
  45. * mouse while clicking it. In this case mouse leaves the button without
  46. * moving.
  47. */
  48. public boolean clickPending;
  49. private boolean cancelNextClick = false;
  50. public VNativeButton() {
  51. setStyleName(CLASSNAME);
  52. getElement().appendChild(captionElement);
  53. captionElement.setClassName(getStyleName() + "-caption");
  54. addClickHandler(this);
  55. sinkEvents(Event.ONMOUSEDOWN | Event.ONLOAD | Event.ONMOUSEMOVE
  56. | Event.ONFOCUS);
  57. }
  58. @Override
  59. public void setText(String text) {
  60. captionElement.setInnerText(text);
  61. }
  62. @Override
  63. public void setHTML(String html) {
  64. captionElement.setInnerHTML(html);
  65. }
  66. @Override
  67. public void onBrowserEvent(Event event) {
  68. super.onBrowserEvent(event);
  69. if (DOM.eventGetType(event) == Event.ONLOAD) {
  70. Util.notifyParentOfSizeChange(this, true);
  71. } else if (DOM.eventGetType(event) == Event.ONMOUSEDOWN
  72. && event.getButton() == Event.BUTTON_LEFT) {
  73. clickPending = true;
  74. } else if (DOM.eventGetType(event) == Event.ONMOUSEMOVE) {
  75. clickPending = false;
  76. } else if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
  77. if (clickPending) {
  78. click();
  79. }
  80. clickPending = false;
  81. } else if (event.getTypeInt() == Event.ONFOCUS) {
  82. if (BrowserInfo.get().isIE()
  83. && BrowserInfo.get().getBrowserMajorVersion() < 11
  84. && clickPending) {
  85. /*
  86. * The focus event will mess up IE and IE will not trigger the
  87. * mouse up event (which in turn triggers the click event) until
  88. * the mouse is moved. This will result in it appearing as a
  89. * native button not triggering the event. So we manually
  90. * trigger the click here and cancel the next original event
  91. * which will occur on the next mouse move. See ticket #11094
  92. * for details.
  93. */
  94. click();
  95. clickPending = false;
  96. cancelNextClick = true;
  97. }
  98. }
  99. }
  100. /*
  101. * (non-Javadoc)
  102. *
  103. * @see
  104. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
  105. * .dom.client.ClickEvent)
  106. */
  107. @Override
  108. public void onClick(ClickEvent event) {
  109. if (paintableId == null || client == null || cancelNextClick) {
  110. cancelNextClick = false;
  111. return;
  112. }
  113. if (BrowserInfo.get().isWebkit()) {
  114. // Webkit does not focus non-text input elements on click
  115. // (#11854)
  116. setFocus(true);
  117. }
  118. }
  119. @Override
  120. public Element getErrorIndicatorElement() {
  121. return errorIndicatorElement;
  122. }
  123. @Override
  124. public void setErrorIndicatorElementVisible(boolean visible) {
  125. if (visible) {
  126. if (errorIndicatorElement == null) {
  127. errorIndicatorElement = ErrorUtil.createErrorIndicatorElement();
  128. getElement().insertBefore(errorIndicatorElement,
  129. captionElement);
  130. }
  131. } else if (errorIndicatorElement != null) {
  132. getElement().removeChild(errorIndicatorElement);
  133. errorIndicatorElement = null;
  134. }
  135. }
  136. }