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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2000-2014 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.Util;
  27. import com.vaadin.shared.MouseEventDetails;
  28. import com.vaadin.shared.ui.button.ButtonServerRpc;
  29. public class VNativeButton extends Button implements ClickHandler {
  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. public 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. private boolean clickPending;
  49. private boolean cancelNextClick = false;
  50. /** For internal use only. May be removed or replaced in the future. */
  51. public boolean disableOnClick = false;
  52. public VNativeButton() {
  53. setStyleName(CLASSNAME);
  54. getElement().appendChild(captionElement);
  55. captionElement.setClassName(getStyleName() + "-caption");
  56. addClickHandler(this);
  57. sinkEvents(Event.ONMOUSEDOWN | Event.ONLOAD | Event.ONMOUSEMOVE
  58. | Event.ONFOCUS);
  59. }
  60. @Override
  61. public void setText(String text) {
  62. captionElement.setInnerText(text);
  63. }
  64. @Override
  65. public void setHTML(String html) {
  66. captionElement.setInnerHTML(html);
  67. }
  68. @Override
  69. public void onBrowserEvent(Event event) {
  70. super.onBrowserEvent(event);
  71. if (DOM.eventGetType(event) == Event.ONLOAD) {
  72. Util.notifyParentOfSizeChange(this, true);
  73. } else if (DOM.eventGetType(event) == Event.ONMOUSEDOWN
  74. && event.getButton() == Event.BUTTON_LEFT) {
  75. clickPending = true;
  76. } else if (DOM.eventGetType(event) == Event.ONMOUSEMOVE) {
  77. clickPending = false;
  78. } else if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
  79. if (clickPending) {
  80. click();
  81. }
  82. clickPending = false;
  83. } else if (event.getTypeInt() == Event.ONFOCUS) {
  84. if (BrowserInfo.get().isIE()
  85. && BrowserInfo.get().getBrowserMajorVersion() < 11
  86. && clickPending) {
  87. /*
  88. * The focus event will mess up IE and IE will not trigger the
  89. * mouse up event (which in turn triggers the click event) until
  90. * the mouse is moved. This will result in it appearing as a
  91. * native button not triggering the event. So we manually
  92. * trigger the click here and cancel the next original event
  93. * which will occur on the next mouse move. See ticket #11094
  94. * for details.
  95. */
  96. click();
  97. clickPending = false;
  98. cancelNextClick = true;
  99. }
  100. }
  101. }
  102. /*
  103. * (non-Javadoc)
  104. *
  105. * @see
  106. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
  107. * .dom.client.ClickEvent)
  108. */
  109. @Override
  110. public void onClick(ClickEvent event) {
  111. if (paintableId == null || client == null || cancelNextClick) {
  112. cancelNextClick = false;
  113. return;
  114. }
  115. if (BrowserInfo.get().isWebkit()) {
  116. // Webkit does not focus non-text input elements on click
  117. // (#11854)
  118. setFocus(true);
  119. }
  120. if (disableOnClick) {
  121. setEnabled(false);
  122. // FIXME: This should be moved to NativeButtonConnector along with
  123. // buttonRpcProxy
  124. addStyleName(ApplicationConnection.DISABLED_CLASSNAME);
  125. buttonRpcProxy.disableOnClick();
  126. }
  127. // Add mouse details
  128. MouseEventDetails details = MouseEventDetailsBuilder
  129. .buildMouseEventDetails(event.getNativeEvent(), getElement());
  130. buttonRpcProxy.click(details);
  131. clickPending = false;
  132. }
  133. }