Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CheckBoxConnector.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 com.google.gwt.core.client.JsArrayString;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.event.dom.client.ClickEvent;
  20. import com.google.gwt.event.dom.client.ClickHandler;
  21. import com.google.gwt.user.client.DOM;
  22. import com.google.gwt.user.client.Event;
  23. import com.vaadin.client.MouseEventDetailsBuilder;
  24. import com.vaadin.client.VCaption;
  25. import com.vaadin.client.VTooltip;
  26. import com.vaadin.client.annotations.OnStateChange;
  27. import com.vaadin.client.communication.StateChangeEvent;
  28. import com.vaadin.client.ui.AbstractFieldConnector;
  29. import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
  30. import com.vaadin.client.ui.Icon;
  31. import com.vaadin.client.ui.VCheckBox;
  32. import com.vaadin.shared.EventId;
  33. import com.vaadin.shared.MouseEventDetails;
  34. import com.vaadin.shared.ui.ComponentStateUtil;
  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. import java.util.List;
  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
  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
  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, getState().inputStyles);
  99. updateStyles(getWidget().getLabelElement(), labelStyleNames, getState().labelStyles);
  100. }
  101. @Override
  102. public CheckBoxState getState() {
  103. return (CheckBoxState) super.getState();
  104. }
  105. @Override
  106. public VCheckBox getWidget() {
  107. return (VCheckBox) super.getWidget();
  108. }
  109. @Override
  110. public void onClick(ClickEvent event) {
  111. if (!isEnabled()) {
  112. return;
  113. }
  114. // We get click events also from the label text, which do not alter the
  115. // actual value. The server-side is only interested in real changes to
  116. // the state.
  117. if (getState().checked != getWidget().getValue()) {
  118. getState().checked = getWidget().getValue();
  119. // Add mouse details
  120. MouseEventDetails details = MouseEventDetailsBuilder
  121. .buildMouseEventDetails(event.getNativeEvent(),
  122. getWidget().getElement());
  123. getRpcProxy(CheckBoxServerRpc.class).setChecked(getState().checked,
  124. details);
  125. getConnection().sendPendingVariableChanges();
  126. }
  127. }
  128. private boolean contextEventSunk = false;
  129. @OnStateChange("registeredEventListeners")
  130. void sinkContextClickEvent() {
  131. if (!contextEventSunk && hasEventListener(EventId.CONTEXT_CLICK)) {
  132. // CheckBox.sinkEvents works differently than all other widgets:
  133. // "Unlike other widgets the CheckBox sinks on its inputElement, not
  134. // its wrapper"
  135. DOM.sinkEvents(getWidget().getElement(), Event.ONCONTEXTMENU);
  136. contextEventSunk = true;
  137. }
  138. }
  139. private void updateStyles(Element clientElement, JsArrayString clientSideStyles, List<String> serverSideStyes) {
  140. // Remove all old stylenames
  141. for (int i = 0; i < clientSideStyles.length(); i++) {
  142. clientElement.removeClassName(clientSideStyles.get(i));
  143. }
  144. clientSideStyles.setLength(0);
  145. if (ComponentStateUtil.hasStyles(serverSideStyes)) {
  146. // add new style names
  147. for (String newStyle : serverSideStyes) {
  148. clientElement.addClassName(newStyle);
  149. clientSideStyles.push(newStyle);
  150. }
  151. }
  152. }
  153. }