Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VNativeButton.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2000-2016 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.MouseEventDetailsBuilder;
  26. import com.vaadin.client.StyleConstants;
  27. import com.vaadin.client.Util;
  28. import com.vaadin.client.WidgetUtil.ErrorUtil;
  29. import com.vaadin.shared.MouseEventDetails;
  30. import com.vaadin.shared.ui.button.ButtonServerRpc;
  31. public class VNativeButton extends Button implements ClickHandler,
  32. HasErrorIndicatorElement {
  33. public static final String CLASSNAME = "v-nativebutton";
  34. /** For internal use only. May be removed or replaced in the future. */
  35. public String paintableId;
  36. /** For internal use only. May be removed or replaced in the future. */
  37. public ApplicationConnection client;
  38. /** For internal use only. May be removed or replaced in the future. */
  39. public ButtonServerRpc buttonRpcProxy;
  40. /** For internal use only. May be removed or replaced in the future. */
  41. private Element errorIndicatorElement;
  42. /** For internal use only. May be removed or replaced in the future. */
  43. public final Element captionElement = DOM.createSpan();
  44. /** For internal use only. May be removed or replaced in the future. */
  45. public Icon icon;
  46. /**
  47. * Helper flag to handle special-case where the button is moved from under
  48. * mouse while clicking it. In this case mouse leaves the button without
  49. * moving.
  50. */
  51. private boolean clickPending;
  52. private boolean cancelNextClick = false;
  53. /** For internal use only. May be removed or replaced in the future. */
  54. public boolean disableOnClick = false;
  55. public VNativeButton() {
  56. setStyleName(CLASSNAME);
  57. getElement().appendChild(captionElement);
  58. captionElement.setClassName(getStyleName() + "-caption");
  59. addClickHandler(this);
  60. sinkEvents(Event.ONMOUSEDOWN | Event.ONLOAD | Event.ONMOUSEMOVE
  61. | Event.ONFOCUS);
  62. }
  63. @Override
  64. public void setText(String text) {
  65. captionElement.setInnerText(text);
  66. }
  67. @Override
  68. public void setHTML(String html) {
  69. captionElement.setInnerHTML(html);
  70. }
  71. @Override
  72. public void onBrowserEvent(Event event) {
  73. super.onBrowserEvent(event);
  74. if (DOM.eventGetType(event) == Event.ONLOAD) {
  75. Util.notifyParentOfSizeChange(this, true);
  76. } else if (DOM.eventGetType(event) == Event.ONMOUSEDOWN
  77. && event.getButton() == Event.BUTTON_LEFT) {
  78. clickPending = true;
  79. } else if (DOM.eventGetType(event) == Event.ONMOUSEMOVE) {
  80. clickPending = false;
  81. } else if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
  82. if (clickPending) {
  83. click();
  84. }
  85. clickPending = false;
  86. } else if (event.getTypeInt() == Event.ONFOCUS) {
  87. if (BrowserInfo.get().isIE()
  88. && BrowserInfo.get().getBrowserMajorVersion() < 11
  89. && clickPending) {
  90. /*
  91. * The focus event will mess up IE and IE will not trigger the
  92. * mouse up event (which in turn triggers the click event) until
  93. * the mouse is moved. This will result in it appearing as a
  94. * native button not triggering the event. So we manually
  95. * trigger the click here and cancel the next original event
  96. * which will occur on the next mouse move. See ticket #11094
  97. * for details.
  98. */
  99. click();
  100. clickPending = false;
  101. cancelNextClick = true;
  102. }
  103. }
  104. }
  105. /*
  106. * (non-Javadoc)
  107. *
  108. * @see
  109. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
  110. * .dom.client.ClickEvent)
  111. */
  112. @Override
  113. public void onClick(ClickEvent event) {
  114. if (paintableId == null || client == null || cancelNextClick) {
  115. cancelNextClick = false;
  116. return;
  117. }
  118. if (BrowserInfo.get().isWebkit()) {
  119. // Webkit does not focus non-text input elements on click
  120. // (#11854)
  121. setFocus(true);
  122. }
  123. if (disableOnClick) {
  124. setEnabled(false);
  125. // FIXME: This should be moved to NativeButtonConnector along with
  126. // buttonRpcProxy
  127. addStyleName(StyleConstants.DISABLED);
  128. buttonRpcProxy.disableOnClick();
  129. }
  130. // Add mouse details
  131. MouseEventDetails details = MouseEventDetailsBuilder
  132. .buildMouseEventDetails(event.getNativeEvent(), getElement());
  133. buttonRpcProxy.click(details);
  134. clickPending = false;
  135. }
  136. @Override
  137. public Element getErrorIndicatorElement() {
  138. return errorIndicatorElement;
  139. }
  140. @Override
  141. public void setErrorIndicatorElementVisible(boolean visible) {
  142. if (visible) {
  143. if (errorIndicatorElement == null) {
  144. errorIndicatorElement = ErrorUtil.createErrorIndicatorElement();
  145. getElement()
  146. .insertBefore(errorIndicatorElement, captionElement);
  147. }
  148. } else if (errorIndicatorElement != null) {
  149. getElement().removeChild(errorIndicatorElement);
  150. errorIndicatorElement = null;
  151. }
  152. }
  153. }