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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.checkbox;
  17. import java.util.List;
  18. import com.google.gwt.core.client.JsArrayString;
  19. import com.google.gwt.dom.client.Element;
  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.MouseEventDetailsBuilder;
  25. import com.vaadin.client.VCaption;
  26. import com.vaadin.client.VTooltip;
  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.ComponentStateUtil;
  36. import com.vaadin.shared.ui.Connect;
  37. import com.vaadin.shared.ui.checkbox.CheckBoxServerRpc;
  38. import com.vaadin.shared.ui.checkbox.CheckBoxState;
  39. import com.vaadin.ui.CheckBox;
  40. /**
  41. * The client-side connector for the {@code CheckBoxGroup} component.
  42. *
  43. * @author Vaadin Ltd.
  44. * @since 8.0
  45. */
  46. @Connect(CheckBox.class)
  47. public class CheckBoxConnector extends AbstractFieldConnector
  48. implements ClickHandler {
  49. /**
  50. * The style names from getState().inputStyles which are currently applied
  51. * to the checkbox.
  52. *
  53. * @since 8.7
  54. */
  55. private JsArrayString inputStyleNames = JsArrayString.createArray().cast();
  56. /**
  57. * The style names from getState().labelStyles which are currently applied
  58. * to the checkbox.
  59. *
  60. * @since 8.7
  61. */
  62. private JsArrayString labelStyleNames = JsArrayString.createArray().cast();
  63. @Override
  64. public boolean delegateCaptionHandling() {
  65. return false;
  66. }
  67. @Override
  68. protected void init() {
  69. super.init();
  70. getWidget().addClickHandler(this);
  71. getWidget().client = getConnection();
  72. getWidget().id = getConnectorId();
  73. ConnectorFocusAndBlurHandler.addHandlers(this);
  74. }
  75. @Override
  76. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  77. super.onStateChanged(stateChangeEvent);
  78. getWidget().setAriaInvalid(getState().errorMessage != null);
  79. getWidget().setAriaRequired(isRequiredIndicatorVisible());
  80. if (isReadOnly()) {
  81. getWidget().setEnabled(false);
  82. }
  83. if (getWidget().icon != null) {
  84. getWidget().getElement().removeChild(getWidget().icon.getElement());
  85. getWidget().icon = null;
  86. }
  87. Icon icon = getIcon();
  88. if (icon != null) {
  89. getWidget().icon = icon;
  90. DOM.insertChild(getWidget().getElement(), icon.getElement(), 1);
  91. icon.sinkEvents(VTooltip.TOOLTIP_EVENTS);
  92. icon.sinkEvents(Event.ONCLICK);
  93. }
  94. // Set text
  95. VCaption.setCaptionText(getWidget(), getState());
  96. getWidget().setValue(getState().checked);
  97. // Set styles for input and label
  98. updateStyles(getWidget().getInputElement(), inputStyleNames,
  99. getState().inputStyles);
  100. updateStyles(getWidget().getLabelElement(), labelStyleNames,
  101. getState().labelStyles);
  102. }
  103. @Override
  104. public CheckBoxState getState() {
  105. return (CheckBoxState) super.getState();
  106. }
  107. @Override
  108. public VCheckBox getWidget() {
  109. return (VCheckBox) super.getWidget();
  110. }
  111. @Override
  112. public void onClick(ClickEvent event) {
  113. if (!isEnabled()) {
  114. return;
  115. }
  116. // We get click events also from the label text, which do not alter the
  117. // actual value. The server-side is only interested in real changes to
  118. // the state.
  119. if (getState().checked != getWidget().getValue()) {
  120. getState().checked = getWidget().getValue();
  121. // Add mouse details
  122. MouseEventDetails details = MouseEventDetailsBuilder
  123. .buildMouseEventDetails(event.getNativeEvent(),
  124. getWidget().getElement());
  125. getRpcProxy(CheckBoxServerRpc.class).setChecked(getState().checked,
  126. details);
  127. getConnection().sendPendingVariableChanges();
  128. }
  129. }
  130. private boolean contextEventSunk = false;
  131. @OnStateChange("registeredEventListeners")
  132. void sinkContextClickEvent() {
  133. if (!contextEventSunk && hasEventListener(EventId.CONTEXT_CLICK)) {
  134. // CheckBox.sinkEvents works differently than all other widgets:
  135. // "Unlike other widgets the CheckBox sinks on its inputElement, not
  136. // its wrapper"
  137. DOM.sinkEvents(getWidget().getElement(), Event.ONCONTEXTMENU);
  138. contextEventSunk = true;
  139. }
  140. }
  141. private void updateStyles(Element clientElement,
  142. JsArrayString clientSideStyles, List<String> serverSideStyes) {
  143. // Remove all old stylenames
  144. for (int i = 0; i < clientSideStyles.length(); i++) {
  145. clientElement.removeClassName(clientSideStyles.get(i));
  146. }
  147. clientSideStyles.setLength(0);
  148. if (ComponentStateUtil.hasStyles(serverSideStyes)) {
  149. // add new style names
  150. for (String newStyle : serverSideStyes) {
  151. clientElement.addClassName(newStyle);
  152. clientSideStyles.push(newStyle);
  153. }
  154. }
  155. }
  156. }