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

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