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

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