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.

VCheckBox.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.core.client.Scheduler;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.dom.client.Style.Visibility;
  20. import com.google.gwt.user.client.DOM;
  21. import com.google.gwt.user.client.Event;
  22. import com.vaadin.client.ApplicationConnection;
  23. import com.vaadin.client.BrowserInfo;
  24. import com.vaadin.client.Util;
  25. import com.vaadin.client.VTooltip;
  26. import com.vaadin.client.WidgetUtil.ErrorUtil;
  27. import com.vaadin.client.ui.aria.AriaHelper;
  28. import com.vaadin.client.ui.aria.HandlesAriaInvalid;
  29. import com.vaadin.client.ui.aria.HandlesAriaRequired;
  30. public class VCheckBox extends com.google.gwt.user.client.ui.CheckBox
  31. implements Field, HandlesAriaInvalid, HandlesAriaRequired,
  32. HasErrorIndicatorElement {
  33. public static final String CLASSNAME = "v-checkbox";
  34. /** For internal use only. May be removed or replaced in the future. */
  35. public String id;
  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. private Element errorIndicatorElement;
  40. /** For internal use only. May be removed or replaced in the future. */
  41. public Icon icon;
  42. public VCheckBox() {
  43. setStyleName(CLASSNAME);
  44. Element el = DOM.getFirstChild(getElement());
  45. while (el != null) {
  46. DOM.sinkEvents(el, DOM.getEventsSunk(el) | VTooltip.TOOLTIP_EVENTS);
  47. el = DOM.getNextSibling(el);
  48. }
  49. if (BrowserInfo.get().isWebkit() || BrowserInfo.get().isFirefox()) {
  50. // Webkit and Firefox do not focus non-text input elements on click
  51. // (#3944)
  52. addClickHandler(event -> setFocus(true));
  53. }
  54. }
  55. @Override
  56. public void onBrowserEvent(Event event) {
  57. if (icon != null && event.getTypeInt() == Event.ONCLICK
  58. && DOM.eventGetTarget(event) == icon.getElement()) {
  59. // Click on icon should do nothing if widget is disabled
  60. if (isEnabled()) {
  61. setValue(!getValue());
  62. }
  63. }
  64. super.onBrowserEvent(event);
  65. if (event.getTypeInt() == Event.ONLOAD) {
  66. Util.notifyParentOfSizeChange(this, true);
  67. }
  68. }
  69. /**
  70. * Gives access to the input element.
  71. *
  72. * @return Element of the CheckBox itself
  73. * @since 8.7
  74. */
  75. public Element getInputElement() {
  76. // public to allow CheckBoxState to access it.
  77. // FIXME: Would love to use a better way to access the checkbox element
  78. return getElement().getFirstChildElement();
  79. }
  80. /**
  81. * Gives access to the label element.
  82. *
  83. * @return Element of the Label itself
  84. * @since 8.7
  85. */
  86. public Element getLabelElement() {
  87. // public to allow CheckBoxState to access it.
  88. // FIXME: Would love to use a better way to access the label element
  89. return getInputElement().getNextSiblingElement();
  90. }
  91. @Override
  92. public void setAriaRequired(boolean required) {
  93. AriaHelper.handleInputRequired(getInputElement(), required);
  94. }
  95. @Override
  96. public void setAriaInvalid(boolean invalid) {
  97. AriaHelper.handleInputInvalid(getInputElement(), invalid);
  98. }
  99. @Override
  100. public Element getErrorIndicatorElement() {
  101. return errorIndicatorElement;
  102. }
  103. @Override
  104. public void setErrorIndicatorElementVisible(boolean visible) {
  105. if (visible) {
  106. if (errorIndicatorElement == null) {
  107. errorIndicatorElement = ErrorUtil.createErrorIndicatorElement();
  108. getElement().appendChild(errorIndicatorElement);
  109. DOM.sinkEvents(errorIndicatorElement,
  110. VTooltip.TOOLTIP_EVENTS | Event.ONCLICK);
  111. }
  112. } else if (errorIndicatorElement != null) {
  113. getElement().removeChild(errorIndicatorElement);
  114. errorIndicatorElement = null;
  115. }
  116. }
  117. @Override
  118. protected void onAttach() {
  119. super.onAttach();
  120. if (BrowserInfo.get().isSafari()) {
  121. /*
  122. * Sometimes Safari does not render checkbox correctly when
  123. * attaching. Setting the visibility to hidden and a bit later
  124. * restoring will make everything just fine.
  125. */
  126. getElement().getStyle().setVisibility(Visibility.HIDDEN);
  127. Scheduler.get().scheduleFinally(() -> {
  128. getElement().getStyle().setVisibility(Visibility.VISIBLE);
  129. });
  130. }
  131. }
  132. }