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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.user.client.DOM;
  19. import com.google.gwt.user.client.Event;
  20. import com.vaadin.client.ApplicationConnection;
  21. import com.vaadin.client.BrowserInfo;
  22. import com.vaadin.client.Util;
  23. import com.vaadin.client.VTooltip;
  24. import com.vaadin.client.WidgetUtil.ErrorUtil;
  25. import com.vaadin.client.ui.aria.AriaHelper;
  26. import com.vaadin.client.ui.aria.HandlesAriaInvalid;
  27. import com.vaadin.client.ui.aria.HandlesAriaRequired;
  28. public class VCheckBox extends com.google.gwt.user.client.ui.CheckBox
  29. implements Field, HandlesAriaInvalid, HandlesAriaRequired,
  30. HasErrorIndicatorElement {
  31. public static final String CLASSNAME = "v-checkbox";
  32. /** For internal use only. May be removed or replaced in the future. */
  33. public String id;
  34. /** For internal use only. May be removed or replaced in the future. */
  35. public ApplicationConnection client;
  36. /** For internal use only. May be removed or replaced in the future. */
  37. private Element errorIndicatorElement;
  38. /** For internal use only. May be removed or replaced in the future. */
  39. public Icon icon;
  40. public VCheckBox() {
  41. setStyleName(CLASSNAME);
  42. Element el = DOM.getFirstChild(getElement());
  43. while (el != null) {
  44. DOM.sinkEvents(el, DOM.getEventsSunk(el) | VTooltip.TOOLTIP_EVENTS);
  45. el = DOM.getNextSibling(el);
  46. }
  47. if (BrowserInfo.get().isWebkit() || BrowserInfo.get().isFirefox()) {
  48. // Webkit and Firefox do not focus non-text input elements on click
  49. // (#3944)
  50. addClickHandler(event -> setFocus(true));
  51. }
  52. }
  53. @Override
  54. public void onBrowserEvent(Event event) {
  55. if (icon != null && event.getTypeInt() == Event.ONCLICK
  56. && DOM.eventGetTarget(event) == icon.getElement()) {
  57. // Click on icon should do nothing if widget is disabled
  58. if (isEnabled()) {
  59. setValue(!getValue());
  60. }
  61. }
  62. super.onBrowserEvent(event);
  63. if (event.getTypeInt() == Event.ONLOAD) {
  64. Util.notifyParentOfSizeChange(this, true);
  65. }
  66. }
  67. /**
  68. * Gives access to the input element.
  69. *
  70. * @return Element of the CheckBox itself
  71. */
  72. private Element getCheckBoxElement() {
  73. // FIXME: Would love to use a better way to access the checkbox element
  74. return getElement().getFirstChildElement();
  75. }
  76. @Override
  77. public void setAriaRequired(boolean required) {
  78. AriaHelper.handleInputRequired(getCheckBoxElement(), required);
  79. }
  80. @Override
  81. public void setAriaInvalid(boolean invalid) {
  82. AriaHelper.handleInputInvalid(getCheckBoxElement(), invalid);
  83. }
  84. @Override
  85. public Element getErrorIndicatorElement() {
  86. return errorIndicatorElement;
  87. }
  88. @Override
  89. public void setErrorIndicatorElementVisible(boolean visible) {
  90. if (visible) {
  91. if (errorIndicatorElement == null) {
  92. errorIndicatorElement = ErrorUtil.createErrorIndicatorElement();
  93. getElement().appendChild(errorIndicatorElement);
  94. DOM.sinkEvents(errorIndicatorElement,
  95. VTooltip.TOOLTIP_EVENTS | Event.ONCLICK);
  96. }
  97. } else if (errorIndicatorElement != null) {
  98. getElement().removeChild(errorIndicatorElement);
  99. errorIndicatorElement = null;
  100. }
  101. }
  102. }