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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.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.ConnectorFocusAndBlurHandler;
  30. import com.vaadin.client.ui.Icon;
  31. import com.vaadin.shared.EventId;
  32. import com.vaadin.shared.MouseEventDetails;
  33. import com.vaadin.shared.ui.Connect;
  34. import com.vaadin.v7.client.ui.AbstractFieldConnector;
  35. import com.vaadin.v7.client.ui.VCheckBox;
  36. import com.vaadin.v7.shared.ui.checkbox.CheckBoxServerRpc;
  37. import com.vaadin.v7.shared.ui.checkbox.CheckBoxState;
  38. import com.vaadin.v7.ui.CheckBox;
  39. @Connect(CheckBox.class)
  40. public class CheckBoxConnector extends AbstractFieldConnector
  41. implements ClickHandler {
  42. @Override
  43. public boolean delegateCaptionHandling() {
  44. return false;
  45. }
  46. @Override
  47. protected void init() {
  48. super.init();
  49. getWidget().addClickHandler(this);
  50. getWidget().client = getConnection();
  51. getWidget().id = getConnectorId();
  52. ConnectorFocusAndBlurHandler.addHandlers(this);
  53. }
  54. @Override
  55. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  56. super.onStateChanged(stateChangeEvent);
  57. if (null != getState().errorMessage) {
  58. getWidget().setAriaInvalid(true);
  59. if (getWidget().errorIndicatorElement == null) {
  60. getWidget().errorIndicatorElement = DOM.createSpan();
  61. getWidget().errorIndicatorElement.setInnerHTML(" ");
  62. DOM.setElementProperty(getWidget().errorIndicatorElement,
  63. "className", StyleConstants.STYLE_NAME_ERROR_INDICATOR);
  64. DOM.appendChild(getWidget().getElement(),
  65. getWidget().errorIndicatorElement);
  66. DOM.sinkEvents(getWidget().errorIndicatorElement,
  67. VTooltip.TOOLTIP_EVENTS | Event.ONCLICK);
  68. } else {
  69. getWidget().errorIndicatorElement.getStyle().clearDisplay();
  70. }
  71. ErrorUtil.setErrorLevelStyle(getWidget().errorIndicatorElement,
  72. StyleConstants.STYLE_NAME_ERROR_INDICATOR,
  73. getState().errorLevel);
  74. } else if (getWidget().errorIndicatorElement != null) {
  75. getWidget().errorIndicatorElement.getStyle()
  76. .setDisplay(Display.NONE);
  77. getWidget().setAriaInvalid(false);
  78. }
  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. getWidget().immediate = getState().immediate;
  98. }
  99. @Override
  100. public CheckBoxState getState() {
  101. return (CheckBoxState) super.getState();
  102. }
  103. @Override
  104. public VCheckBox getWidget() {
  105. return (VCheckBox) super.getWidget();
  106. }
  107. @Override
  108. public void onClick(ClickEvent event) {
  109. if (!isEnabled()) {
  110. return;
  111. }
  112. // We get click events also from the label text, which do not alter the
  113. // actual value. The server-side is only interested in real changes to
  114. // the state.
  115. if (getState().checked != getWidget().getValue()) {
  116. getState().checked = getWidget().getValue();
  117. // Add mouse details
  118. MouseEventDetails details = MouseEventDetailsBuilder
  119. .buildMouseEventDetails(event.getNativeEvent(),
  120. getWidget().getElement());
  121. getRpcProxy(CheckBoxServerRpc.class).setChecked(getState().checked,
  122. details);
  123. if (getState().immediate) {
  124. getConnection().sendPendingVariableChanges();
  125. }
  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. }