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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.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.ui.aria.AriaHelper;
  27. import com.vaadin.client.ui.aria.HandlesAriaInvalid;
  28. import com.vaadin.client.ui.aria.HandlesAriaRequired;
  29. public class VCheckBox extends com.google.gwt.user.client.ui.CheckBox
  30. implements Field, HandlesAriaInvalid, HandlesAriaRequired {
  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. public 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()) {
  48. // Webkit does not focus non-text input elements on click
  49. // (#11854)
  50. addClickHandler(new ClickHandler() {
  51. @Override
  52. public void onClick(ClickEvent event) {
  53. setFocus(true);
  54. }
  55. });
  56. }
  57. }
  58. @Override
  59. public void onBrowserEvent(Event event) {
  60. if (icon != null && event.getTypeInt() == Event.ONCLICK
  61. && DOM.eventGetTarget(event) == icon.getElement()) {
  62. // Click on icon should do nothing if widget is disabled
  63. if (isEnabled()) {
  64. setValue(!getValue());
  65. }
  66. }
  67. super.onBrowserEvent(event);
  68. if (event.getTypeInt() == Event.ONLOAD) {
  69. Util.notifyParentOfSizeChange(this, true);
  70. }
  71. }
  72. /**
  73. * Gives access to the input element.
  74. *
  75. * @return Element of the CheckBox itself
  76. */
  77. private Element getCheckBoxElement() {
  78. // FIXME: Would love to use a better way to access the checkbox element
  79. return getElement().getFirstChildElement();
  80. }
  81. @Override
  82. public void setAriaRequired(boolean required) {
  83. AriaHelper.handleInputRequired(getCheckBoxElement(), required);
  84. }
  85. @Override
  86. public void setAriaInvalid(boolean invalid) {
  87. AriaHelper.handleInputInvalid(getCheckBoxElement(), invalid);
  88. }
  89. }