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.

CheckBoxConnector.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.checkbox;
  17. import com.google.gwt.dom.client.Style.Display;
  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.MouseEventDetailsBuilder;
  23. import com.vaadin.client.StyleConstants;
  24. import com.vaadin.client.VCaption;
  25. import com.vaadin.client.VTooltip;
  26. import com.vaadin.client.WidgetUtil.ErrorUtil;
  27. import com.vaadin.client.annotations.OnStateChange;
  28. import com.vaadin.client.communication.StateChangeEvent;
  29. import com.vaadin.client.ui.AbstractFieldConnector;
  30. import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
  31. import com.vaadin.client.ui.Icon;
  32. import com.vaadin.client.ui.VCheckBox;
  33. import com.vaadin.shared.EventId;
  34. import com.vaadin.shared.MouseEventDetails;
  35. import com.vaadin.shared.ui.Connect;
  36. import com.vaadin.shared.ui.checkbox.CheckBoxServerRpc;
  37. import com.vaadin.shared.ui.checkbox.CheckBoxState;
  38. import com.vaadin.ui.CheckBox;
  39. /**
  40. * The client-side connector for the {@code CheckBoxGroup} component.
  41. *
  42. * @author Vaadin Ltd.
  43. * @since 8.0
  44. */
  45. @Connect(CheckBox.class)
  46. public class CheckBoxConnector extends AbstractFieldConnector
  47. implements ClickHandler {
  48. @Override
  49. public boolean delegateCaptionHandling() {
  50. return false;
  51. }
  52. @Override
  53. protected void init() {
  54. super.init();
  55. getWidget().addClickHandler(this);
  56. getWidget().client = getConnection();
  57. getWidget().id = getConnectorId();
  58. ConnectorFocusAndBlurHandler.addHandlers(this);
  59. }
  60. @Override
  61. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  62. super.onStateChanged(stateChangeEvent);
  63. getWidget().setAriaInvalid(getState().errorMessage != null);
  64. getWidget().setAriaRequired(isRequiredIndicatorVisible());
  65. if (isReadOnly()) {
  66. getWidget().setEnabled(false);
  67. }
  68. if (getWidget().icon != null) {
  69. getWidget().getElement().removeChild(getWidget().icon.getElement());
  70. getWidget().icon = null;
  71. }
  72. Icon icon = getIcon();
  73. if (icon != null) {
  74. getWidget().icon = icon;
  75. DOM.insertChild(getWidget().getElement(), icon.getElement(), 1);
  76. icon.sinkEvents(VTooltip.TOOLTIP_EVENTS);
  77. icon.sinkEvents(Event.ONCLICK);
  78. }
  79. // Set text
  80. VCaption.setCaptionText(getWidget(), getState());
  81. getWidget().setValue(getState().checked);
  82. }
  83. @Override
  84. public CheckBoxState getState() {
  85. return (CheckBoxState) super.getState();
  86. }
  87. @Override
  88. public VCheckBox getWidget() {
  89. return (VCheckBox) super.getWidget();
  90. }
  91. @Override
  92. public void onClick(ClickEvent event) {
  93. if (!isEnabled()) {
  94. return;
  95. }
  96. // We get click events also from the label text, which do not alter the
  97. // actual value. The server-side is only interested in real changes to
  98. // the state.
  99. if (getState().checked != getWidget().getValue()) {
  100. getState().checked = getWidget().getValue();
  101. // Add mouse details
  102. MouseEventDetails details = MouseEventDetailsBuilder
  103. .buildMouseEventDetails(event.getNativeEvent(),
  104. getWidget().getElement());
  105. getRpcProxy(CheckBoxServerRpc.class).setChecked(getState().checked,
  106. details);
  107. getConnection().sendPendingVariableChanges();
  108. }
  109. }
  110. private boolean contextEventSunk = false;
  111. @OnStateChange("registeredEventListeners")
  112. void sinkContextClickEvent() {
  113. if (!contextEventSunk && hasEventListener(EventId.CONTEXT_CLICK)) {
  114. // CheckBox.sinkEvents works differently than all other widgets:
  115. // "Unlike other widgets the CheckBox sinks on its inputElement, not
  116. // its wrapper"
  117. DOM.sinkEvents(getWidget().getElement(), Event.ONCONTEXTMENU);
  118. contextEventSunk = true;
  119. }
  120. }
  121. }